Add StrInStrNoCase, a StringRef version of CStrInCStrNoCase.
authorBenjamin Kramer <benny.kra@googlemail.com>
Mon, 11 Jan 2010 19:45:18 +0000 (19:45 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Mon, 11 Jan 2010 19:45:18 +0000 (19:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93174 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringExtras.h
lib/Support/StringExtras.cpp

index 2f5c39cad8a9e037e649da22bf68b43c5496f9d7..55a7cfb983b418fe5bb46a607dc806707f492626 100644 (file)
@@ -203,6 +203,11 @@ static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
   return *I2 == '\0' ? s1 : 0;
 }
 
+/// StrInStrNoCase - Portable version of strcasestr.  Locates the first
+/// occurrence of string 's1' in string 's2', ignoring case.  Returns
+/// the offset of s2 in s1 or npos if s2 cannot be found.
+StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
+
 /// getToken - This function extracts one token from source, ignoring any
 /// leading characters that appear in the Delimiters string, and ending the
 /// token at any of the characters that appear in the Delimiters string.  If
index 2363ad60d1789ff6c63fde3e4f41f71d86882770..7fd840b0aed90159564886a6004b340fbf5c9473 100644 (file)
 #include "llvm/ADT/StringExtras.h"
 using namespace llvm;
 
+/// StrInStrNoCase - Portable version of strcasestr.  Locates the first
+/// occurrence of string 's1' in string 's2', ignoring case.  Returns
+/// the offset of s2 in s1 or npos if s2 cannot be found.
+StringRef::size_type llvm::StrInStrNoCase(StringRef s1, StringRef s2) {
+  size_t N = s2.size(), M = s1.size();
+  if (N > M)
+    return StringRef::npos;
+  for (size_t i = 0, e = M - N + 1; i != e; ++i)
+    if (s1.substr(i, N).equals_lower(s2))
+      return i;
+  return StringRef::npos;
+}
+
 /// getToken - This function extracts one token from source, ignoring any
 /// leading characters that appear in the Delimiters string, and ending the
 /// token at any of the characters that appear in the Delimiters string.  If