LLVM API Documentation
00001 //===-- StringExtras.cpp - Implement the StringExtras header --------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the StringExtras.h header 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/ADT/SmallVector.h" 00015 #include "llvm/ADT/STLExtras.h" 00016 #include "llvm/ADT/StringExtras.h" 00017 using namespace llvm; 00018 00019 /// StrInStrNoCase - Portable version of strcasestr. Locates the first 00020 /// occurrence of string 's1' in string 's2', ignoring case. Returns 00021 /// the offset of s2 in s1 or npos if s2 cannot be found. 00022 StringRef::size_type llvm::StrInStrNoCase(StringRef s1, StringRef s2) { 00023 size_t N = s2.size(), M = s1.size(); 00024 if (N > M) 00025 return StringRef::npos; 00026 for (size_t i = 0, e = M - N + 1; i != e; ++i) 00027 if (s1.substr(i, N).equals_lower(s2)) 00028 return i; 00029 return StringRef::npos; 00030 } 00031 00032 /// getToken - This function extracts one token from source, ignoring any 00033 /// leading characters that appear in the Delimiters string, and ending the 00034 /// token at any of the characters that appear in the Delimiters string. If 00035 /// there are no tokens in the source string, an empty string is returned. 00036 /// The function returns a pair containing the extracted token and the 00037 /// remaining tail string. 00038 std::pair<StringRef, StringRef> llvm::getToken(StringRef Source, 00039 StringRef Delimiters) { 00040 // Figure out where the token starts. 00041 StringRef::size_type Start = Source.find_first_not_of(Delimiters); 00042 00043 // Find the next occurrence of the delimiter. 00044 StringRef::size_type End = Source.find_first_of(Delimiters, Start); 00045 00046 return std::make_pair(Source.slice(Start, End), Source.substr(End)); 00047 } 00048 00049 /// SplitString - Split up the specified string according to the specified 00050 /// delimiters, appending the result fragments to the output list. 00051 void llvm::SplitString(StringRef Source, 00052 SmallVectorImpl<StringRef> &OutFragments, 00053 StringRef Delimiters) { 00054 std::pair<StringRef, StringRef> S = getToken(Source, Delimiters); 00055 while (!S.first.empty()) { 00056 OutFragments.push_back(S.first); 00057 S = getToken(S.second, Delimiters); 00058 } 00059 }