1 //===-- StringRef.cpp - Lightweight String References ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
14 // MSVC emits references to this into the translation units which reference it.
16 const size_t StringRef::npos;
19 static char ascii_tolower(char x) {
20 if (x >= 'A' && x <= 'Z')
25 /// compare_lower - Compare strings, ignoring case.
26 int StringRef::compare_lower(StringRef RHS) const {
27 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
28 char LHC = ascii_tolower(Data[I]);
29 char RHC = ascii_tolower(RHS.Data[I]);
31 return LHC < RHC ? -1 : 1;
34 if (Length == RHS.Length)
36 return Length < RHS.Length ? -1 : 1;
39 /// \brief Compute the edit distance between the two given strings.
40 unsigned StringRef::edit_distance(llvm::StringRef Other,
41 bool AllowReplacements) {
43 size_type n = Other.size();
45 std::vector<unsigned> previous(n+1, 0);
46 for (std::vector<unsigned>::size_type i = 0; i <= n; ++i)
49 std::vector<unsigned> current(n+1, 0);
50 for (size_type y = 1; y <= m; ++y) {
51 current.assign(n+1, 0);
53 for (size_type x = 1; x <= n; ++x) {
54 if (AllowReplacements) {
55 current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
56 min(current[x-1], previous[x])+1);
59 if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
60 else current[x] = min(current[x-1], previous[x]) + 1;
63 current.swap(previous);
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
74 /// find - Search for the first string \arg Str in the string.
76 /// \return - The index of the first occurence of \arg Str, or npos if not
78 size_t StringRef::find(StringRef Str, size_t From) const {
79 size_t N = Str.size();
82 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
83 if (substr(i, N).equals(Str))
88 /// rfind - Search for the last string \arg Str in the string.
90 /// \return - The index of the last occurence of \arg Str, or npos if not
92 size_t StringRef::rfind(StringRef Str) const {
93 size_t N = Str.size();
96 for (size_t i = Length - N + 1, e = 0; i != e;) {
98 if (substr(i, N).equals(Str))
104 /// find_first_of - Find the first character in the string that is in \arg
105 /// Chars, or npos if not found.
107 /// Note: O(size() * Chars.size())
108 StringRef::size_type StringRef::find_first_of(StringRef Chars,
110 for (size_type i = min(From, Length), e = Length; i != e; ++i)
111 if (Chars.find(Data[i]) != npos)
116 /// find_first_not_of - Find the first character in the string that is not
117 /// \arg C or npos if not found.
118 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
119 for (size_type i = min(From, Length), e = Length; i != e; ++i)
125 /// find_first_not_of - Find the first character in the string that is not
126 /// in the string \arg Chars, or npos if not found.
128 /// Note: O(size() * Chars.size())
129 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
131 for (size_type i = min(From, Length), e = Length; i != e; ++i)
132 if (Chars.find(Data[i]) == npos)
138 //===----------------------------------------------------------------------===//
139 // Helpful Algorithms
140 //===----------------------------------------------------------------------===//
142 /// count - Return the number of non-overlapped occurrences of \arg Str in
144 size_t StringRef::count(StringRef Str) const {
146 size_t N = Str.size();
149 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
150 if (substr(i, N).equals(Str))
155 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
156 /// sequence of radix up to 36 to an unsigned long long value.
157 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
158 unsigned long long &Result) {
159 // Autosense radix if not specified.
161 if (Str.startswith("0x")) {
164 } else if (Str.startswith("0b")) {
167 } else if (Str.startswith("0"))
173 // Empty strings (after the radix autosense) are invalid.
174 if (Str.empty()) return true;
176 // Parse all the bytes of the string given this radix. Watch for overflow.
178 while (!Str.empty()) {
180 if (Str[0] >= '0' && Str[0] <= '9')
181 CharVal = Str[0]-'0';
182 else if (Str[0] >= 'a' && Str[0] <= 'z')
183 CharVal = Str[0]-'a'+10;
184 else if (Str[0] >= 'A' && Str[0] <= 'Z')
185 CharVal = Str[0]-'A'+10;
189 // If the parsed value is larger than the integer radix, the string is
191 if (CharVal >= Radix)
194 // Add in this character.
195 unsigned long long PrevResult = Result;
196 Result = Result*Radix+CharVal;
198 // Check for overflow.
199 if (Result < PrevResult)
208 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
209 return GetAsUnsignedInteger(*this, Radix, Result);
213 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
214 unsigned long long ULLVal;
216 // Handle positive strings first.
217 if (empty() || front() != '-') {
218 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
219 // Check for value so large it overflows a signed value.
220 (long long)ULLVal < 0)
226 // Get the positive part of the value.
227 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
228 // Reject values so large they'd overflow as negative signed, but allow
229 // "-0". This negates the unsigned so that the negative isn't undefined
230 // on signed overflow.
231 (long long)-ULLVal > 0)
238 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
240 if (getAsInteger(Radix, Val) ||
247 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
248 unsigned long long Val;
249 if (getAsInteger(Radix, Val) ||
250 (unsigned)Val != Val)