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