view th_strglob.c @ 722:4ca6a3b30fe8

Bump copyright years.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Jan 2021 11:35:54 +0200
parents b87395754c8d
children 29e44a58bc73
line wrap: on
line source

/*
 * String glob match implementation
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2021 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */

BOOL TH_STRGLOB_FUNC (const char *haystack, const char *pattern)
{
    BOOL matched = TRUE, any = FALSE, end = FALSE;
    const char *tmp = NULL;

    // Check given pattern and string
    if (haystack == NULL || pattern == NULL)
        return FALSE;

    // Start comparision
    while (matched && !end)
    switch (*pattern)
    {
    case '?':
        // Any single character matches
        if (*haystack)
        {
            pattern++;
            haystack++;
        }
        else
            matched = FALSE;
        break;

    case '*':
        pattern++;
        if (!*pattern || *pattern == '?')
            end = TRUE;
        any = TRUE;
        tmp = pattern;
        break;

    case 0:
        if (any)
        {
            if (*haystack)
                haystack++;
            else
                end = TRUE;
        }
        else
        if (*haystack)
        {
            if (tmp)
            {
                any = TRUE;
                pattern = tmp;
            }
            else
                matched = FALSE;
        }
        else
            end = TRUE;
        break;

    default:
        {
            BOOL equals = TH_STRGLOB_COLLATE(*pattern) == TH_STRGLOB_COLLATE(*haystack);
            if (any)
            {
                if (equals)
                {
                    any = FALSE;
                }
                else
                if (*haystack)
                    haystack++;
                else
                    matched = FALSE;
            }
            else
            {
                if (equals)
                {
                    if (*pattern)
                        pattern++;
                    if (*haystack)
                        haystack++;
                }
                else
                if (tmp)
                {
                    any = TRUE;
                    pattern = tmp;
                }
                else
                    matched = FALSE;
            }

            if (!*haystack && !*pattern)
                end = TRUE;
        }
        break;
    }

    return matched;
}


#undef TH_STRGLOB_FUNC
#undef TH_STRGLOB_COLLATE