xref: /aosp_15_r20/external/llvm/lib/Support/StringExtras.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- StringExtras.cpp - Implement the StringExtras header --------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the StringExtras.h header
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
16*9880d681SAndroid Build Coastguard Worker using namespace llvm;
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker /// StrInStrNoCase - Portable version of strcasestr.  Locates the first
19*9880d681SAndroid Build Coastguard Worker /// occurrence of string 's1' in string 's2', ignoring case.  Returns
20*9880d681SAndroid Build Coastguard Worker /// the offset of s2 in s1 or npos if s2 cannot be found.
StrInStrNoCase(StringRef s1,StringRef s2)21*9880d681SAndroid Build Coastguard Worker StringRef::size_type llvm::StrInStrNoCase(StringRef s1, StringRef s2) {
22*9880d681SAndroid Build Coastguard Worker   size_t N = s2.size(), M = s1.size();
23*9880d681SAndroid Build Coastguard Worker   if (N > M)
24*9880d681SAndroid Build Coastguard Worker     return StringRef::npos;
25*9880d681SAndroid Build Coastguard Worker   for (size_t i = 0, e = M - N + 1; i != e; ++i)
26*9880d681SAndroid Build Coastguard Worker     if (s1.substr(i, N).equals_lower(s2))
27*9880d681SAndroid Build Coastguard Worker       return i;
28*9880d681SAndroid Build Coastguard Worker   return StringRef::npos;
29*9880d681SAndroid Build Coastguard Worker }
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker /// getToken - This function extracts one token from source, ignoring any
32*9880d681SAndroid Build Coastguard Worker /// leading characters that appear in the Delimiters string, and ending the
33*9880d681SAndroid Build Coastguard Worker /// token at any of the characters that appear in the Delimiters string.  If
34*9880d681SAndroid Build Coastguard Worker /// there are no tokens in the source string, an empty string is returned.
35*9880d681SAndroid Build Coastguard Worker /// The function returns a pair containing the extracted token and the
36*9880d681SAndroid Build Coastguard Worker /// remaining tail string.
getToken(StringRef Source,StringRef Delimiters)37*9880d681SAndroid Build Coastguard Worker std::pair<StringRef, StringRef> llvm::getToken(StringRef Source,
38*9880d681SAndroid Build Coastguard Worker                                                StringRef Delimiters) {
39*9880d681SAndroid Build Coastguard Worker   // Figure out where the token starts.
40*9880d681SAndroid Build Coastguard Worker   StringRef::size_type Start = Source.find_first_not_of(Delimiters);
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker   // Find the next occurrence of the delimiter.
43*9880d681SAndroid Build Coastguard Worker   StringRef::size_type End = Source.find_first_of(Delimiters, Start);
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   return std::make_pair(Source.slice(Start, End), Source.substr(End));
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker /// SplitString - Split up the specified string according to the specified
49*9880d681SAndroid Build Coastguard Worker /// delimiters, appending the result fragments to the output list.
SplitString(StringRef Source,SmallVectorImpl<StringRef> & OutFragments,StringRef Delimiters)50*9880d681SAndroid Build Coastguard Worker void llvm::SplitString(StringRef Source,
51*9880d681SAndroid Build Coastguard Worker                        SmallVectorImpl<StringRef> &OutFragments,
52*9880d681SAndroid Build Coastguard Worker                        StringRef Delimiters) {
53*9880d681SAndroid Build Coastguard Worker   std::pair<StringRef, StringRef> S = getToken(Source, Delimiters);
54*9880d681SAndroid Build Coastguard Worker   while (!S.first.empty()) {
55*9880d681SAndroid Build Coastguard Worker     OutFragments.push_back(S.first);
56*9880d681SAndroid Build Coastguard Worker     S = getToken(S.second, Delimiters);
57*9880d681SAndroid Build Coastguard Worker   }
58*9880d681SAndroid Build Coastguard Worker }
59