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