Tcl Source Code

Changes On Branch aspect-string-match
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch aspect-string-match Excluding Merge-Ins

This is equivalent to a diff from 2158eea530 to c84d53f778

2017-05-22
11:07
fix advancement of snext: could not cook up a test for this, but the error is clear Leaf check-in: c84d53f778 user: aspect tags: aspect-string-match
10:49
retab Tcl_StringCaseMatch. Oops check-in: 34aca29ae4 user: aspect tags: aspect-string-match
2017-05-06
00:36
streamline StringCaseMatch with switch and comments. BEHAVIOUR CHANGE: incomplete bracket groups no... check-in: 5efe8a956f user: aspect tags: aspect-string-match
2017-05-01
20:23
[f9fe90d0fa] [file join] normalization. See filesystem-1.52* check-in: 650c12161a user: dgp tags: core-8-5-branch
2017-04-30
05:34
glob for hidden files on unix should not require stat() Leaf check-in: e83297c2af user: aspect tags: aspect-bug-391bc0fd2c
2017-04-28
17:52
[f34cf83dd0] An optimization was being taken in a case where it produced the wrong result, failing t... check-in: e015bfe72f user: dgp tags: core-8-6-branch
17:49
[f34cf83dd0] An optimization was being taken in a case where it produced the wrong result, failing t... check-in: 2158eea530 user: dgp tags: core-8-5-branch
17:43
Test for [f34cf83dd0]. check-in: f49a421a0d user: dgp tags: core-8-5-branch

Changes to generic/tclUtil.c.

1914
1915
1916
1917
1918
1919
1920
1921
1922
1923

1924


1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935


1936
1937
1938
1939







1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960


1961


1962




1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993

1994
1995



1996
1997
1998

1999
2000
2001
2002
2003
2004



2005



2006

2007

2008

2009
2010









2011


2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030

2031
2032
2033

2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047

2048
2049

2050
2051
2052
2053
2054
2055
2056

2057
2058
2059
2060

2061
2062
2063
2064



2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078

2079
2080

2081
2082


2083

2084
2085
2086




2087
2088
2089
2090
2091
2092
2093
2094
2095

2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107

2108
2109

2110
2111
2112
2113
2114
2115
2116
2117
2118
2119

2120
2121
2122

2123

2124













2125
2126
2127
2128
2129
2130
2131
int
Tcl_StringCaseMatch(
    CONST char *str,		/* String. */
    CONST char *pattern,	/* Pattern, which may contain special
				 * characters. */
    int nocase)			/* 0 for case sensitive, 1 for insensitive */
{
    int p, charLen;
    CONST char *pstart = pattern;
    Tcl_UniChar ch1, ch2;




    while (1) {
	p = *pattern;

	/*
	 * See if we're at the end of both the pattern and the string. If so,
	 * we succeeded. If we're at the end of the pattern but not at the end
	 * of the string, we failed.
	 */

	if (p == '\0') {
	    return (*str == '\0');


	}
	if ((*str == '\0') && (p != '*')) {
	    return 0;
	}








	/*
	 * Check for a "*" as the next pattern character. It matches any
	 * substring. We handle this by calling ourselves recursively for each
	 * postfix of string, until either we match or we reach the end of the
	 * string.
	 */

	if (p == '*') {
	    /*
	     * Skip all successive *'s in the pattern
	     */

	    while (*(++pattern) == '*') {}
	    p = *pattern;
	    if (p == '\0') {
		return 1;
	    }

	    /*
	     * This is a special case optimization for single-byte utf.


	     */







	    if (UCHAR(*pattern) < 0x80) {
		ch2 = (Tcl_UniChar)
			(nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
	    } else {
		Tcl_UtfToUniChar(pattern, &ch2);
		if (nocase) {
		    ch2 = Tcl_UniCharToLower(ch2);
		}
	    }

	    while (1) {
		/*
		 * Optimization for matching - cruise through the string
		 * quickly if the next char in the pattern isn't a special
		 * character
		 */

		if ((p != '[') && (p != '?') && (p != '\\')) {
		    if (nocase) {
			while (*str) {
			    charLen = TclUtfToUniChar(str, &ch1);
			    if (ch2==ch1 || ch2==Tcl_UniCharToLower(ch1)) {
				break;
			    }
			    str += charLen;
			}
		    } else {
			/*
			 * There's no point in trying to make this code
			 * shorter, as the number of bytes you want to compare
			 * each time is non-constant.

			 */




			while (*str) {
			    charLen = TclUtfToUniChar(str, &ch1);
			    if (ch2 == ch1) {

				break;
			    }
			    str += charLen;
			}
		    }
		}



		if (Tcl_StringCaseMatch(str, pattern, nocase)) {



		    return 1;

		}

		if (*str == '\0') {

		    return 0;
		}









		str += TclUtfToUniChar(str, &ch1);


	    }
	}

	/*
	 * Check for a "?" as the next pattern character. It matches any
	 * single character.
	 */

	if (p == '?') {
	    pattern++;
	    str += TclUtfToUniChar(str, &ch1);
	    continue;
	}

	/*
	 * Check for a "[" as the next pattern character. It is followed by a
	 * list of characters that are acceptable, or by a range (two
	 * characters separated by "-").
	 */


	if (p == '[') {
	    Tcl_UniChar startChar, endChar;


	    pattern++;
	    if (UCHAR(*str) < 0x80) {
		ch1 = (Tcl_UniChar)
			(nocase ? tolower(UCHAR(*str)) : UCHAR(*str));
		str++;
	    } else {
		str += Tcl_UtfToUniChar(str, &ch1);
		if (nocase) {
		    ch1 = Tcl_UniCharToLower(ch1);
		}
	    }
	    while (1) {
		if ((*pattern == ']') || (*pattern == '\0')) {

		    return 0;
		}

		if (UCHAR(*pattern) < 0x80) {
		    startChar = (Tcl_UniChar) (nocase
			    ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
		    pattern++;
		} else {
		    pattern += Tcl_UtfToUniChar(pattern, &startChar);
		    if (nocase) {

			startChar = Tcl_UniCharToLower(startChar);
		    }
		}
		if (*pattern == '-') {

		    pattern++;
		    if (*pattern == '\0') {
			return 0;
		    }



		    if (UCHAR(*pattern) < 0x80) {
			endChar = (Tcl_UniChar) (nocase
				? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
			pattern++;
		    } else {
			pattern += Tcl_UtfToUniChar(pattern, &endChar);
			if (nocase) {
			    endChar = Tcl_UniCharToLower(endChar);
			}
		    }
		    if (((startChar <= ch1) && (ch1 <= endChar))
			    || ((endChar <= ch1) && (ch1 <= startChar))) {
			/*
			 * Matches ranges of form [a-z] or [z-a].

			 */


			break;
		    }


		} else if (startChar == ch1) {

		    break;
		}
	    }




	    while (*pattern != ']') {
		if (*pattern == '\0') {
		    pattern = Tcl_UtfPrev(pattern, pstart);
		    break;
		}
		pattern++;
	    }
	    pattern++;
	    continue;

	}

	/*
	 * If the next pattern character is '\', just strip off the '\' so we
	 * do exact matching on the character that follows.
	 */

	if (p == '\\') {
	    pattern++;
	    if (*pattern == '\0') {
		return 0;
	    }

	}


	/*
	 * There's no special character. Just make sure that the next bytes of
	 * each string match.
	 */

	str += TclUtfToUniChar(str, &ch1);
	pattern += TclUtfToUniChar(pattern, &ch2);
	if (nocase) {
	    if (Tcl_UniCharToLower(ch1) != Tcl_UniCharToLower(ch2)) {
		return 0;

	    }
	} else if (ch1 != ch2) {
	    return 0;

	}

    }













}

/*
 *----------------------------------------------------------------------
 *
 * TclByteArrayMatch --
 *







|

|
>

>
>

|
|
<
<
<
<
<
|
|
|
>
>
|
<
<
|
>
>
>
>
>
>
>
|
<
<
<
<
<
<

|
|
|
|
<
|
<
<
<
<
<
<
|
>
>
|
>
>
|
>
>
>
>
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
|
<
|
<
|
<
<
<
>
|
|
>
>
>
|
|
|
>
|
|
|
|
|
|
>
>
>
|
>
>
>
|
>
|
>
|
>
|
|
>
>
>
>
>
>
>
>
>
|
>
>
|
|

|
<
|
|
|
<
|
<
<
<
<
|
<
|
<
|
>
|
|
<
>
|
<
|
|
|
|
|
|
|
|
|
|
<
|
>
|
<
>
|
<
<
|
|
<
<
>
|
<
<
<
>
|
|
|
|
>
>
>
|
|
|
|
|
|
|
|
|
|
<
|
|
|
>
|
|
>
|
|
>
>
|
>
|
|
|
>
>
>
>
|
|
|
<
|
|
|
|
<
>
|
<
<
<
<
<
<
|
|
|
|
|
>
|
|
>
|
<
|
|
<
|
|
|
|
<
>
|
|
<
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>







1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930





1931
1932
1933
1934
1935
1936


1937
1938
1939
1940
1941
1942
1943
1944
1945






1946
1947
1948
1949
1950

1951






1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969









1970






1971

1972

1973



1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020

2021
2022
2023

2024




2025

2026

2027
2028
2029
2030

2031
2032

2033
2034
2035
2036
2037
2038
2039
2040
2041
2042

2043
2044
2045

2046
2047


2048
2049


2050
2051



2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069

2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092

2093
2094
2095
2096

2097
2098






2099
2100
2101
2102
2103
2104
2105
2106
2107
2108

2109
2110

2111
2112
2113
2114

2115
2116
2117

2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
int
Tcl_StringCaseMatch(
    CONST char *str,		/* String. */
    CONST char *pattern,	/* Pattern, which may contain special
				 * characters. */
    int nocase)			/* 0 for case sensitive, 1 for insensitive */
{
    int charLen;
    CONST char *pstart = pattern;
    Tcl_UniChar pch, sch;
    Tcl_UniChar startChar, endChar;

    CONST char *pnext = 0, *snext;

    while (1) {
matchLoop:
        switch (*pattern) {





            case '\0':
                if (*str == '\0') {
                    return 1;
                } else {
                    goto matchFail;
                }



            case '?':
                if (*str == '\0') {
                    goto matchFail;
                } else {
                    str += TclUtfToUniChar(str, &sch);
                    ++pattern;
                    goto matchLoop;
                }







            case '*':
                /*
                 * Skip past the '*', and any following '*'s
                 */

                while (*(++pattern) == '*') {}







                /*
                 * If the pattern ends after '*', we have a match
                 */
                if (*pattern == '\0') {
                    return 1;
                }

                /*
                 * Peek at the next pattern char
                 */
                if (UCHAR(*pattern) < 0x80) {
                    pch = (Tcl_UniChar)
                        (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
                } else {
                    TclUtfToUniChar(pattern, &pch);
                    if (nocase) {
                        pch = Tcl_UniCharToLower(pch);









                    }






                }



                /*



                 * If the next char in pattern is a literal, zoom through str to the next match
                 */
                switch (*pattern) {
                    case '[': case '?': case '\\':
                        break;
                    default:
                        while (*str) {
                            charLen = TclUtfToUniChar(str, &sch);
                            if ( (pch == sch)
                                    || (nocase && (pch == Tcl_UniCharToLower(sch)))) {
                                break;
                            }
                            str += charLen;
                        }
                }

                /*
                 * Avoid recursion, using technique described at https://research.swtch.com/glob
                 *
                 * `pnext` stores the first pattern char following a (sequence of) '*'.
                 * `snext` stores the current match position - on resume at matchFail it
                 *   will need to be incremented.
                 */
                pnext = pattern - 1;
                snext = str;

                goto matchLoop;

            case '[':
                ++pattern;

                /*
                 * Take the next char from input string to match
                 * against '[]' group
                 */
                if(UCHAR(*str) < 0x80) {
                    sch = (Tcl_UniChar)
                        (nocase ? tolower(UCHAR(*str)) : UCHAR(*str));
                    ++str;
                } else {
                    str += TclUtfToUniChar(str, &sch);
                    if (nocase) {
                        sch = Tcl_UniCharToLower(sch);
                    }
                }

                /*

                 * '[]' group loop: process single chars and a-z ranges
                 */
                while (1) {

                    if (*pattern == ']') {




                        /*

                         * End of [] range with no match - fail!

                         */
                        goto matchFail;
                    }
                    if (*pattern == '\0') {

                        return 0;       /* Illegal pattern! (incomplete [..) */
                    }

                    if (UCHAR(*pattern) < 0x80) {
                        pch = (Tcl_UniChar)
                            (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
                        ++pattern;
                    } else {
                        pattern += TclUtfToUniChar(pattern, &pch);
                        if (nocase) {
                            pch = Tcl_UniCharToLower(pch);
                        }
                    }

                    if (*pattern != '-') {
                        /* 
                         * Try to match a single char in '[]' group

                         */
                        if (pch == sch) break;


                        continue;
                    } else {


                        /*
                         * Start of a 'a-z' style range.



                         */
                        ++pattern;
                        if (*pattern == '\0') {
                            return 0;   /* Illegal pattern! (incomplete [..) */
                        }
                        // if (*pattern == ']') {
                        //      return 0;       /* Illegal pattern! (incomplete [a-]) */
                        // }
                        if (UCHAR(*pattern) < 0x80) {
                            pch = (Tcl_UniChar)
                                (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
                            ++pattern;
                        } else {
                            pattern += TclUtfToUniChar(pattern, &pch);
                            if (nocase) {
                                pch = Tcl_UniCharToLower(pch);
                            }
                        }

                        endChar = pch;
                        /*
                         * We have a range ('a-z' inside '[]') with
                         * startChar, endChar as the endpoints.
                         */
                        if (((startChar <= sch) && (sch <= endChar))
                                || ((endChar <= sch) && (sch <= startChar))) {
                            break;
                        }
                        /*
                         * No match with this character or range.  Carry
                         * on with the next part of '[]'
                         */
                        continue;
                    }
                }
                /*
                 * If we get here, a [] group has successfully matched. *pattern
                 * is still inside the brackets, so advance past next ']'
                 */
                while (*pattern != ']') {
                    if (*pattern == '\0') {
                        return 0;       /* Illegal pattern! (incomplete [..) */

                    }
                    pattern++;
                }
                pattern++;

                goto matchLoop;







            case '\\':
                ++pattern;
                if (*pattern == '\0') {
                    return 0;           /* Illegal pattern! (lone \ at end) */
                }
                goto matchLiteral;

            default:
matchLiteral:
                /*

                 * Try to match a literal character in pattern
                 */

                str += TclUtfToUniChar(str, &sch);
                pattern += TclUtfToUniChar(pattern, &pch);
                if (nocase) {
                    if (Tcl_UniCharToLower(sch) != Tcl_UniCharToLower(pch)) {

                        goto matchFail;
                    }
                } else if (sch != pch) {

                    goto matchFail;
                }
                goto matchLoop;
        }
    }
matchFail:
    if (pnext != 0) {
        /*
         * Backtrack to position following last '*' in `pattern`.
         * `snext` points to where we last tried to match, so advance
         * it first.
         */
        pattern = pnext;
        str = snext + TclUtfToUniChar(snext, &sch);
        if (*str) goto matchLoop;
    }
    return 0;
}

/*
 *----------------------------------------------------------------------
 *
 * TclByteArrayMatch --
 *