Daniel Dunbar | e655128 | 2009-09-16 22:38:48 +0000 | [diff] [blame] | 1 | //===-- StringRef.cpp - Lightweight String References ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/ADT/StringRef.h" |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/APInt.h" |
Ted Kremenek | 13302ec | 2010-11-07 06:09:02 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/OwningPtr.h" |
Kaelyn Uhrain | 01d53ec | 2012-02-15 22:13:07 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/edit_distance.h" |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 14 | #include <bitset> |
Douglas Gregor | ad6b6da | 2010-01-07 00:51:54 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | e655128 | 2009-09-16 22:38:48 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Daniel Dunbar | 77696be | 2009-09-22 03:34:40 +0000 | [diff] [blame] | 18 | // MSVC emits references to this into the translation units which reference it. |
| 19 | #ifndef _MSC_VER |
Daniel Dunbar | e655128 | 2009-09-16 22:38:48 +0000 | [diff] [blame] | 20 | const size_t StringRef::npos; |
Daniel Dunbar | 77696be | 2009-09-22 03:34:40 +0000 | [diff] [blame] | 21 | #endif |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 22 | |
Benjamin Kramer | 05872ea | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 23 | static char ascii_tolower(char x) { |
| 24 | if (x >= 'A' && x <= 'Z') |
| 25 | return x - 'A' + 'a'; |
| 26 | return x; |
| 27 | } |
| 28 | |
Daniel Dunbar | 589fbb1 | 2011-11-06 18:04:43 +0000 | [diff] [blame] | 29 | static char ascii_toupper(char x) { |
| 30 | if (x >= 'a' && x <= 'z') |
| 31 | return x - 'a' + 'A'; |
| 32 | return x; |
| 33 | } |
| 34 | |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 35 | static bool ascii_isdigit(char x) { |
| 36 | return x >= '0' && x <= '9'; |
| 37 | } |
| 38 | |
Benjamin Kramer | 05872ea | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 39 | /// compare_lower - Compare strings, ignoring case. |
| 40 | int StringRef::compare_lower(StringRef RHS) const { |
Daniel Dunbar | 58ce7ac | 2009-11-19 18:53:18 +0000 | [diff] [blame] | 41 | for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) { |
Benjamin Kramer | 0043e35 | 2010-08-26 14:21:08 +0000 | [diff] [blame] | 42 | unsigned char LHC = ascii_tolower(Data[I]); |
| 43 | unsigned char RHC = ascii_tolower(RHS.Data[I]); |
Benjamin Kramer | 05872ea | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 44 | if (LHC != RHC) |
| 45 | return LHC < RHC ? -1 : 1; |
| 46 | } |
| 47 | |
| 48 | if (Length == RHS.Length) |
Benjamin Kramer | 0043e35 | 2010-08-26 14:21:08 +0000 | [diff] [blame] | 49 | return 0; |
Benjamin Kramer | 05872ea | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 50 | return Length < RHS.Length ? -1 : 1; |
| 51 | } |
| 52 | |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 53 | /// compare_numeric - Compare strings, handle embedded numbers. |
| 54 | int StringRef::compare_numeric(StringRef RHS) const { |
| 55 | for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) { |
Jakob Stoklund Olesen | 7850dd0 | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 56 | // Check for sequences of digits. |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 57 | if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) { |
Jakob Stoklund Olesen | 7850dd0 | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 58 | // The longer sequence of numbers is considered larger. |
| 59 | // This doesn't really handle prefixed zeros well. |
| 60 | size_t J; |
| 61 | for (J = I + 1; J != E + 1; ++J) { |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 62 | bool ld = J < Length && ascii_isdigit(Data[J]); |
| 63 | bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]); |
| 64 | if (ld != rd) |
| 65 | return rd ? -1 : 1; |
| 66 | if (!rd) |
| 67 | break; |
| 68 | } |
Jakob Stoklund Olesen | 7850dd0 | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 69 | // The two number sequences have the same length (J-I), just memcmp them. |
| 70 | if (int Res = compareMemory(Data + I, RHS.Data + I, J - I)) |
| 71 | return Res < 0 ? -1 : 1; |
| 72 | // Identical number sequences, continue search after the numbers. |
| 73 | I = J - 1; |
| 74 | continue; |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 75 | } |
Jakob Stoklund Olesen | 7850dd0 | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 76 | if (Data[I] != RHS.Data[I]) |
| 77 | return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? -1 : 1; |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 78 | } |
| 79 | if (Length == RHS.Length) |
Benjamin Kramer | 0043e35 | 2010-08-26 14:21:08 +0000 | [diff] [blame] | 80 | return 0; |
Jakob Stoklund Olesen | 160a3bf | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 81 | return Length < RHS.Length ? -1 : 1; |
| 82 | } |
| 83 | |
Douglas Gregor | 7e54d5b | 2009-12-31 04:24:34 +0000 | [diff] [blame] | 84 | // Compute the edit distance between the two given strings. |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 85 | unsigned StringRef::edit_distance(llvm::StringRef Other, |
Douglas Gregor | 5ee568a | 2010-10-19 22:13:48 +0000 | [diff] [blame] | 86 | bool AllowReplacements, |
| 87 | unsigned MaxEditDistance) { |
Kaelyn Uhrain | 01d53ec | 2012-02-15 22:13:07 +0000 | [diff] [blame] | 88 | return llvm::ComputeEditDistance( |
| 89 | llvm::ArrayRef<char>(data(), size()), |
| 90 | llvm::ArrayRef<char>(Other.data(), Other.size()), |
| 91 | AllowReplacements, MaxEditDistance); |
Douglas Gregor | 441c8b4 | 2009-12-30 17:23:44 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 94 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 589fbb1 | 2011-11-06 18:04:43 +0000 | [diff] [blame] | 95 | // String Operations |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | std::string StringRef::lower() const { |
| 99 | std::string Result(size(), char()); |
| 100 | for (size_type i = 0, e = size(); i != e; ++i) { |
| 101 | Result[i] = ascii_tolower(Data[i]); |
| 102 | } |
| 103 | return Result; |
| 104 | } |
| 105 | |
| 106 | std::string StringRef::upper() const { |
| 107 | std::string Result(size(), char()); |
| 108 | for (size_type i = 0, e = size(); i != e; ++i) { |
Benjamin Kramer | a7b966f | 2011-11-06 20:36:50 +0000 | [diff] [blame] | 109 | Result[i] = ascii_toupper(Data[i]); |
Daniel Dunbar | 589fbb1 | 2011-11-06 18:04:43 +0000 | [diff] [blame] | 110 | } |
| 111 | return Result; |
| 112 | } |
| 113 | |
| 114 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 115 | // String Searching |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | |
| 119 | /// find - Search for the first string \arg Str in the string. |
| 120 | /// |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 121 | /// \return - The index of the first occurrence of \arg Str, or npos if not |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 122 | /// found. |
Daniel Dunbar | 64066bd | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 123 | size_t StringRef::find(StringRef Str, size_t From) const { |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 124 | size_t N = Str.size(); |
| 125 | if (N > Length) |
| 126 | return npos; |
Benjamin Kramer | 6e6a558 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 127 | |
| 128 | // For short haystacks or unsupported needles fall back to the naive algorithm |
| 129 | if (Length < 16 || N > 255 || N == 0) { |
| 130 | for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i) |
| 131 | if (substr(i, N).equals(Str)) |
| 132 | return i; |
| 133 | return npos; |
| 134 | } |
| 135 | |
Benjamin Kramer | e7a0719 | 2011-10-17 20:49:40 +0000 | [diff] [blame] | 136 | if (From >= Length) |
| 137 | return npos; |
| 138 | |
Benjamin Kramer | 6e6a558 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 139 | // Build the bad char heuristic table, with uint8_t to reduce cache thrashing. |
| 140 | uint8_t BadCharSkip[256]; |
| 141 | std::memset(BadCharSkip, N, 256); |
| 142 | for (unsigned i = 0; i != N-1; ++i) |
| 143 | BadCharSkip[(uint8_t)Str[i]] = N-1-i; |
| 144 | |
Benjamin Kramer | e7a0719 | 2011-10-17 20:49:40 +0000 | [diff] [blame] | 145 | unsigned Len = Length-From, Pos = From; |
Benjamin Kramer | 6e6a558 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 146 | while (Len >= N) { |
| 147 | if (substr(Pos, N).equals(Str)) // See if this is the correct substring. |
| 148 | return Pos; |
| 149 | |
| 150 | // Otherwise skip the appropriate number of bytes. |
Benjamin Kramer | e7a0719 | 2011-10-17 20:49:40 +0000 | [diff] [blame] | 151 | uint8_t Skip = BadCharSkip[(uint8_t)(*this)[Pos+N-1]]; |
Benjamin Kramer | 6e6a558 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 152 | Len -= Skip; |
| 153 | Pos += Skip; |
| 154 | } |
| 155 | |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 156 | return npos; |
| 157 | } |
| 158 | |
| 159 | /// rfind - Search for the last string \arg Str in the string. |
| 160 | /// |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 161 | /// \return - The index of the last occurrence of \arg Str, or npos if not |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 162 | /// found. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 163 | size_t StringRef::rfind(StringRef Str) const { |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 164 | size_t N = Str.size(); |
| 165 | if (N > Length) |
| 166 | return npos; |
| 167 | for (size_t i = Length - N + 1, e = 0; i != e;) { |
| 168 | --i; |
| 169 | if (substr(i, N).equals(Str)) |
| 170 | return i; |
| 171 | } |
| 172 | return npos; |
| 173 | } |
| 174 | |
Daniel Dunbar | 64066bd | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 175 | /// find_first_of - Find the first character in the string that is in \arg |
| 176 | /// Chars, or npos if not found. |
| 177 | /// |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 178 | /// Note: O(size() + Chars.size()) |
Daniel Dunbar | 64066bd | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 179 | StringRef::size_type StringRef::find_first_of(StringRef Chars, |
| 180 | size_t From) const { |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 181 | std::bitset<1 << CHAR_BIT> CharBits; |
| 182 | for (size_type i = 0; i != Chars.size(); ++i) |
| 183 | CharBits.set((unsigned char)Chars[i]); |
| 184 | |
Daniel Dunbar | 58ce7ac | 2009-11-19 18:53:18 +0000 | [diff] [blame] | 185 | for (size_type i = min(From, Length), e = Length; i != e; ++i) |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 186 | if (CharBits.test((unsigned char)Data[i])) |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 187 | return i; |
| 188 | return npos; |
| 189 | } |
| 190 | |
| 191 | /// find_first_not_of - Find the first character in the string that is not |
Daniel Dunbar | 64066bd | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 192 | /// \arg C or npos if not found. |
| 193 | StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const { |
Daniel Dunbar | 58ce7ac | 2009-11-19 18:53:18 +0000 | [diff] [blame] | 194 | for (size_type i = min(From, Length), e = Length; i != e; ++i) |
Daniel Dunbar | 64066bd | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 195 | if (Data[i] != C) |
| 196 | return i; |
| 197 | return npos; |
| 198 | } |
| 199 | |
| 200 | /// find_first_not_of - Find the first character in the string that is not |
| 201 | /// in the string \arg Chars, or npos if not found. |
| 202 | /// |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 203 | /// Note: O(size() + Chars.size()) |
Daniel Dunbar | 64066bd | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 204 | StringRef::size_type StringRef::find_first_not_of(StringRef Chars, |
| 205 | size_t From) const { |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 206 | std::bitset<1 << CHAR_BIT> CharBits; |
| 207 | for (size_type i = 0; i != Chars.size(); ++i) |
| 208 | CharBits.set((unsigned char)Chars[i]); |
| 209 | |
Daniel Dunbar | 58ce7ac | 2009-11-19 18:53:18 +0000 | [diff] [blame] | 210 | for (size_type i = min(From, Length), e = Length; i != e; ++i) |
Benjamin Kramer | 250eb00 | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 211 | if (!CharBits.test((unsigned char)Data[i])) |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 212 | return i; |
| 213 | return npos; |
| 214 | } |
| 215 | |
Michael J. Spencer | 63c133b | 2010-11-30 23:27:35 +0000 | [diff] [blame] | 216 | /// find_last_of - Find the last character in the string that is in \arg C, |
| 217 | /// or npos if not found. |
| 218 | /// |
| 219 | /// Note: O(size() + Chars.size()) |
| 220 | StringRef::size_type StringRef::find_last_of(StringRef Chars, |
| 221 | size_t From) const { |
| 222 | std::bitset<1 << CHAR_BIT> CharBits; |
| 223 | for (size_type i = 0; i != Chars.size(); ++i) |
| 224 | CharBits.set((unsigned char)Chars[i]); |
| 225 | |
| 226 | for (size_type i = min(From, Length) - 1, e = -1; i != e; --i) |
| 227 | if (CharBits.test((unsigned char)Data[i])) |
| 228 | return i; |
| 229 | return npos; |
| 230 | } |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 231 | |
Duncan Sands | bf8653f | 2012-02-21 12:00:25 +0000 | [diff] [blame^] | 232 | void StringRef::split(SmallVectorImpl<StringRef> &A, |
| 233 | StringRef Separators, int MaxSplit, |
| 234 | bool KeepEmpty) const { |
| 235 | StringRef rest = *this; |
| 236 | |
| 237 | // rest.data() is used to distinguish cases like "a," that splits into |
| 238 | // "a" + "" and "a" that splits into "a" + 0. |
| 239 | for (int splits = 0; |
| 240 | rest.data() != NULL && (MaxSplit < 0 || splits < MaxSplit); |
| 241 | ++splits) { |
| 242 | std::pair<StringRef, StringRef> p = rest.split(Separators); |
| 243 | |
| 244 | if (p.first.size() != 0 || KeepEmpty) |
| 245 | A.push_back(p.first); |
| 246 | rest = p.second; |
| 247 | } |
| 248 | // If we have a tail left, add it. |
| 249 | if (rest.data() != NULL && (rest.size() != 0 || KeepEmpty)) |
| 250 | A.push_back(rest); |
| 251 | } |
| 252 | |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
| 254 | // Helpful Algorithms |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | |
| 257 | /// count - Return the number of non-overlapped occurrences of \arg Str in |
| 258 | /// the string. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 259 | size_t StringRef::count(StringRef Str) const { |
Chris Lattner | 05a32c8 | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 260 | size_t Count = 0; |
| 261 | size_t N = Str.size(); |
| 262 | if (N > Length) |
| 263 | return 0; |
| 264 | for (size_t i = 0, e = Length - N + 1; i != e; ++i) |
| 265 | if (substr(i, N).equals(Str)) |
| 266 | ++Count; |
| 267 | return Count; |
| 268 | } |
| 269 | |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 270 | static unsigned GetAutoSenseRadix(StringRef &Str) { |
| 271 | if (Str.startswith("0x")) { |
| 272 | Str = Str.substr(2); |
| 273 | return 16; |
| 274 | } else if (Str.startswith("0b")) { |
| 275 | Str = Str.substr(2); |
| 276 | return 2; |
| 277 | } else if (Str.startswith("0")) { |
| 278 | return 8; |
| 279 | } else { |
| 280 | return 10; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
Chris Lattner | 63c6b7d | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 285 | /// GetAsUnsignedInteger - Workhorse method that converts a integer character |
| 286 | /// sequence of radix up to 36 to an unsigned long long value. |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 287 | static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix, |
| 288 | unsigned long long &Result) { |
| 289 | // Autosense radix if not specified. |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 290 | if (Radix == 0) |
| 291 | Radix = GetAutoSenseRadix(Str); |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 292 | |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 293 | // Empty strings (after the radix autosense) are invalid. |
| 294 | if (Str.empty()) return true; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 295 | |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 296 | // Parse all the bytes of the string given this radix. Watch for overflow. |
| 297 | Result = 0; |
| 298 | while (!Str.empty()) { |
| 299 | unsigned CharVal; |
| 300 | if (Str[0] >= '0' && Str[0] <= '9') |
| 301 | CharVal = Str[0]-'0'; |
| 302 | else if (Str[0] >= 'a' && Str[0] <= 'z') |
| 303 | CharVal = Str[0]-'a'+10; |
| 304 | else if (Str[0] >= 'A' && Str[0] <= 'Z') |
| 305 | CharVal = Str[0]-'A'+10; |
| 306 | else |
| 307 | return true; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 308 | |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 309 | // If the parsed value is larger than the integer radix, the string is |
| 310 | // invalid. |
| 311 | if (CharVal >= Radix) |
| 312 | return true; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 313 | |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 314 | // Add in this character. |
| 315 | unsigned long long PrevResult = Result; |
| 316 | Result = Result*Radix+CharVal; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 317 | |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 318 | // Check for overflow. |
| 319 | if (Result < PrevResult) |
| 320 | return true; |
| 321 | |
| 322 | Str = Str.substr(1); |
| 323 | } |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 324 | |
Chris Lattner | cea1438 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | |
| 328 | bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const { |
| 329 | return GetAsUnsignedInteger(*this, Radix, Result); |
| 330 | } |
| 331 | |
Chris Lattner | 63c6b7d | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 332 | |
| 333 | bool StringRef::getAsInteger(unsigned Radix, long long &Result) const { |
| 334 | unsigned long long ULLVal; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 63c6b7d | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 336 | // Handle positive strings first. |
| 337 | if (empty() || front() != '-') { |
| 338 | if (GetAsUnsignedInteger(*this, Radix, ULLVal) || |
| 339 | // Check for value so large it overflows a signed value. |
| 340 | (long long)ULLVal < 0) |
| 341 | return true; |
| 342 | Result = ULLVal; |
| 343 | return false; |
| 344 | } |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 63c6b7d | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 346 | // Get the positive part of the value. |
| 347 | if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) || |
| 348 | // Reject values so large they'd overflow as negative signed, but allow |
| 349 | // "-0". This negates the unsigned so that the negative isn't undefined |
| 350 | // on signed overflow. |
| 351 | (long long)-ULLVal > 0) |
| 352 | return true; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 63c6b7d | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 354 | Result = -ULLVal; |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | bool StringRef::getAsInteger(unsigned Radix, int &Result) const { |
| 359 | long long Val; |
| 360 | if (getAsInteger(Radix, Val) || |
| 361 | (int)Val != Val) |
| 362 | return true; |
| 363 | Result = Val; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const { |
| 368 | unsigned long long Val; |
| 369 | if (getAsInteger(Radix, Val) || |
| 370 | (unsigned)Val != Val) |
| 371 | return true; |
| 372 | Result = Val; |
| 373 | return false; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 374 | } |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 375 | |
| 376 | bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { |
| 377 | StringRef Str = *this; |
| 378 | |
| 379 | // Autosense radix if not specified. |
| 380 | if (Radix == 0) |
| 381 | Radix = GetAutoSenseRadix(Str); |
| 382 | |
| 383 | assert(Radix > 1 && Radix <= 36); |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 384 | |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 385 | // Empty strings (after the radix autosense) are invalid. |
| 386 | if (Str.empty()) return true; |
| 387 | |
| 388 | // Skip leading zeroes. This can be a significant improvement if |
| 389 | // it means we don't need > 64 bits. |
| 390 | while (!Str.empty() && Str.front() == '0') |
| 391 | Str = Str.substr(1); |
| 392 | |
| 393 | // If it was nothing but zeroes.... |
| 394 | if (Str.empty()) { |
| 395 | Result = APInt(64, 0); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | // (Over-)estimate the required number of bits. |
| 400 | unsigned Log2Radix = 0; |
| 401 | while ((1U << Log2Radix) < Radix) Log2Radix++; |
| 402 | bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix); |
| 403 | |
| 404 | unsigned BitWidth = Log2Radix * Str.size(); |
| 405 | if (BitWidth < Result.getBitWidth()) |
| 406 | BitWidth = Result.getBitWidth(); // don't shrink the result |
| 407 | else |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 408 | Result = Result.zext(BitWidth); |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 409 | |
| 410 | APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix |
| 411 | if (!IsPowerOf2Radix) { |
| 412 | // These must have the same bit-width as Result. |
| 413 | RadixAP = APInt(BitWidth, Radix); |
| 414 | CharAP = APInt(BitWidth, 0); |
| 415 | } |
| 416 | |
| 417 | // Parse all the bytes of the string given this radix. |
| 418 | Result = 0; |
| 419 | while (!Str.empty()) { |
| 420 | unsigned CharVal; |
| 421 | if (Str[0] >= '0' && Str[0] <= '9') |
| 422 | CharVal = Str[0]-'0'; |
| 423 | else if (Str[0] >= 'a' && Str[0] <= 'z') |
| 424 | CharVal = Str[0]-'a'+10; |
| 425 | else if (Str[0] >= 'A' && Str[0] <= 'Z') |
| 426 | CharVal = Str[0]-'A'+10; |
| 427 | else |
| 428 | return true; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 429 | |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 430 | // If the parsed value is larger than the integer radix, the string is |
| 431 | // invalid. |
| 432 | if (CharVal >= Radix) |
| 433 | return true; |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 434 | |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 435 | // Add in this character. |
| 436 | if (IsPowerOf2Radix) { |
| 437 | Result <<= Log2Radix; |
| 438 | Result |= CharVal; |
| 439 | } else { |
| 440 | Result *= RadixAP; |
| 441 | CharAP = CharVal; |
| 442 | Result += CharAP; |
| 443 | } |
| 444 | |
| 445 | Str = Str.substr(1); |
| 446 | } |
Michael J. Spencer | 326990f | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 447 | |
John McCall | 1e7ad39 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 448 | return false; |
| 449 | } |