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
|
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 p, charLen;
int charLen;
CONST char *pstart = pattern;
Tcl_UniChar ch1, ch2;
Tcl_UniChar pch, sch;
Tcl_UniChar startChar, endChar;
CONST char *pnext = 0, *snext;
while (1) {
p = *pattern;
matchLoop:
switch (*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');
}
case '\0':
if (*str == '\0') {
return 1;
} else {
goto matchFail;
}
if ((*str == '\0') && (p != '*')) {
return 0;
}
case '?':
if (*str == '\0') {
goto matchFail;
} else {
str += TclUtfToUniChar(str, &sch);
++pattern;
goto matchLoop;
}
/*
* 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
*/
case '*':
/*
* Skip past the '*', and any following '*'s
*/
while (*(++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);
/*
* 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);
}
}
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);
}
}
* 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);
}
}
/*
/*
* Check for a "?" as the next pattern character. It matches any
* single character.
*/
* '[]' group loop: process single chars and a-z ranges
*/
while (1) {
if (p == '?') {
pattern++;
if (*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
* End of [] range with no match - fail!
* characters separated by "-").
*/
if (p == '[') {
*/
goto matchFail;
}
if (*pattern == '\0') {
Tcl_UniChar startChar, endChar;
return 0; /* Illegal pattern! (incomplete [..) */
}
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);
}
}
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);
}
}
while (1) {
if ((*pattern == ']') || (*pattern == '\0')) {
return 0;
if (*pattern != '-') {
/*
* Try to match a single char in '[]' group
}
if (UCHAR(*pattern) < 0x80) {
*/
if (pch == sch) break;
startChar = (Tcl_UniChar) (nocase
? tolower(UCHAR(*pattern)) : UCHAR(*pattern));
pattern++;
} else {
continue;
} else {
pattern += Tcl_UtfToUniChar(pattern, &startChar);
if (nocase) {
startChar = Tcl_UniCharToLower(startChar);
/*
* Start of a 'a-z' style range.
}
}
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);
}
}
*/
++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);
}
}
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);
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 [..) */
break;
}
pattern++;
}
pattern++;
}
pattern++;
}
pattern++;
continue;
}
goto matchLoop;
/*
* 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;
}
}
/*
case '\\':
++pattern;
if (*pattern == '\0') {
return 0; /* Illegal pattern! (lone \ at end) */
}
goto matchLiteral;
default:
matchLiteral:
/*
* There's no special character. Just make sure that the next bytes of
* each string match.
*/
* Try to match a literal character in pattern
*/
str += TclUtfToUniChar(str, &ch1);
pattern += TclUtfToUniChar(pattern, &ch2);
if (nocase) {
if (Tcl_UniCharToLower(ch1) != Tcl_UniCharToLower(ch2)) {
str += TclUtfToUniChar(str, &sch);
pattern += TclUtfToUniChar(pattern, &pch);
if (nocase) {
if (Tcl_UniCharToLower(sch) != Tcl_UniCharToLower(pch)) {
return 0;
}
} else if (ch1 != ch2) {
goto matchFail;
}
} else if (sch != pch) {
return 0;
}
}
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 --
*
|