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"
11 #include "llvm/ADT/SmallVector.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 // Compute the edit distance between the two given strings.
40 unsigned StringRef::edit_distance(llvm::StringRef Other,
41 bool AllowReplacements) {
42 // The algorithm implemented below is the "classic"
43 // dynamic-programming algorithm for computing the Levenshtein
44 // distance, which is described here:
46 // http://en.wikipedia.org/wiki/Levenshtein_distance
48 // Although the algorithm is typically described using an m x n
49 // array, only two rows are used at a time, so this implemenation
50 // just keeps two separate vectors for those two rows.
52 size_type n = Other.size();
54 SmallVector<unsigned, 32> previous(n+1, 0);
55 for (SmallVector<unsigned, 32>::size_type i = 0; i <= n; ++i)
58 SmallVector<unsigned, 32> current(n+1, 0);
59 for (size_type y = 1; y <= m; ++y) {
60 current.assign(n+1, 0);
62 for (size_type x = 1; x <= n; ++x) {
63 if (AllowReplacements) {
64 current[x] = min(previous[x-1] + ((*this)[y-1] == Other[x-1]? 0u:1u),
65 min(current[x-1], previous[x])+1);
68 if ((*this)[y-1] == Other[x-1]) current[x] = previous[x-1];
69 else current[x] = min(current[x-1], previous[x]) + 1;
72 current.swap(previous);
78 //===----------------------------------------------------------------------===//
80 //===----------------------------------------------------------------------===//
83 /// find - Search for the first string \arg Str in the string.
85 /// \return - The index of the first occurence of \arg Str, or npos if not
87 size_t StringRef::find(StringRef Str, size_t From) const {
88 size_t N = Str.size();
91 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
92 if (substr(i, N).equals(Str))
97 /// rfind - Search for the last string \arg Str in the string.
99 /// \return - The index of the last occurence of \arg Str, or npos if not
101 size_t StringRef::rfind(StringRef Str) const {
102 size_t N = Str.size();
105 for (size_t i = Length - N + 1, e = 0; i != e;) {
107 if (substr(i, N).equals(Str))
113 /// find_first_of - Find the first character in the string that is in \arg
114 /// Chars, or npos if not found.
116 /// Note: O(size() * Chars.size())
117 StringRef::size_type StringRef::find_first_of(StringRef Chars,
119 for (size_type i = min(From, Length), e = Length; i != e; ++i)
120 if (Chars.find(Data[i]) != npos)
125 /// find_first_not_of - Find the first character in the string that is not
126 /// \arg C or npos if not found.
127 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
128 for (size_type i = min(From, Length), e = Length; i != e; ++i)
134 /// find_first_not_of - Find the first character in the string that is not
135 /// in the string \arg Chars, or npos if not found.
137 /// Note: O(size() * Chars.size())
138 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
140 for (size_type i = min(From, Length), e = Length; i != e; ++i)
141 if (Chars.find(Data[i]) == npos)
147 //===----------------------------------------------------------------------===//
148 // Helpful Algorithms
149 //===----------------------------------------------------------------------===//
151 /// count - Return the number of non-overlapped occurrences of \arg Str in
153 size_t StringRef::count(StringRef Str) const {
155 size_t N = Str.size();
158 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
159 if (substr(i, N).equals(Str))
164 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
165 /// sequence of radix up to 36 to an unsigned long long value.
166 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
167 unsigned long long &Result) {
168 // Autosense radix if not specified.
170 if (Str.startswith("0x")) {
173 } else if (Str.startswith("0b")) {
176 } else if (Str.startswith("0"))
182 // Empty strings (after the radix autosense) are invalid.
183 if (Str.empty()) return true;
185 // Parse all the bytes of the string given this radix. Watch for overflow.
187 while (!Str.empty()) {
189 if (Str[0] >= '0' && Str[0] <= '9')
190 CharVal = Str[0]-'0';
191 else if (Str[0] >= 'a' && Str[0] <= 'z')
192 CharVal = Str[0]-'a'+10;
193 else if (Str[0] >= 'A' && Str[0] <= 'Z')
194 CharVal = Str[0]-'A'+10;
198 // If the parsed value is larger than the integer radix, the string is
200 if (CharVal >= Radix)
203 // Add in this character.
204 unsigned long long PrevResult = Result;
205 Result = Result*Radix+CharVal;
207 // Check for overflow.
208 if (Result < PrevResult)
217 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
218 return GetAsUnsignedInteger(*this, Radix, Result);
222 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
223 unsigned long long ULLVal;
225 // Handle positive strings first.
226 if (empty() || front() != '-') {
227 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
228 // Check for value so large it overflows a signed value.
229 (long long)ULLVal < 0)
235 // Get the positive part of the value.
236 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
237 // Reject values so large they'd overflow as negative signed, but allow
238 // "-0". This negates the unsigned so that the negative isn't undefined
239 // on signed overflow.
240 (long long)-ULLVal > 0)
247 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
249 if (getAsInteger(Radix, Val) ||
256 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
257 unsigned long long Val;
258 if (getAsInteger(Radix, Val) ||
259 (unsigned)Val != Val)