llvm.org GIT mirror llvm / fdedd53
Fix some serious logical errors in CStrInCStrNoCase pointed out by Bill. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50826 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 11 years ago
1 changed file(s) with 8 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
143143
144144 /// CStrInCStrNoCase - Portable version of strcasestr. Locates the first
145145 /// occurance of c-string 's1' in string 's2', ignoring case. Returns
146 /// NULL if 's1' cannot be found.
146 /// NULL if 's1' cannot be found. NOTE: the arguments are provided
147 /// in a different order than strcasestr.
147148 static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
148149
149150 // Are either strings NULL or empty?
150151 if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
151152 return 0;
152153
154 if (s1 == s2)
155 return s1;
156
153157 const char *I1=s1, *I2=s2;
154158
155159 while (*I1 != '\0' || *I2 != '\0' )
156160 if (tolower(*I1) != tolower(*I2)) { // No match. Start over.
157 ++s1; I1 = s1; I2 = s2;
161 ++s2; I1 = s1; I2 = s2;
158162 }
159163 else { // Character match. Advance to the next character.
160164 ++I1; ++I2;
161165 }
162166
163 // If we exhausted all of the characters in 's2', then 's1' does not occur
164 // in it.
165 return *I2 == '\0' ? 0 : I1;
167 // If we exhausted all of the characters in 's1', then 's1' appears in 's2'.
168 return *I1 == '\0' ? s2 : 0;
166169 }
167170
168171 /// getToken - This function extracts one token from source, ignoring any