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" |
Zachary Turner | 8bd42a1 | 2017-02-14 19:06:37 +0000 | [diff] [blame^] | 11 | #include "llvm/ADT/APFloat.h" |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/APInt.h" |
Chandler Carruth | ca99ad3 | 2012-03-04 10:55:27 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Hashing.h" |
Kaelyn Uhrain | 7a9ccf4 | 2012-02-15 22:13:07 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/edit_distance.h" |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 15 | #include <bitset> |
Douglas Gregor | 09470e6 | 2010-01-07 00:51:54 +0000 | [diff] [blame] | 16 | |
Daniel Dunbar | 4498168 | 2009-09-16 22:38:48 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Daniel Dunbar | c827d9e | 2009-09-22 03:34:40 +0000 | [diff] [blame] | 19 | // MSVC emits references to this into the translation units which reference it. |
| 20 | #ifndef _MSC_VER |
Daniel Dunbar | 4498168 | 2009-09-16 22:38:48 +0000 | [diff] [blame] | 21 | const size_t StringRef::npos; |
Daniel Dunbar | c827d9e | 2009-09-22 03:34:40 +0000 | [diff] [blame] | 22 | #endif |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 23 | |
Benjamin Kramer | 68e4945 | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 24 | static char ascii_tolower(char x) { |
| 25 | if (x >= 'A' && x <= 'Z') |
| 26 | return x - 'A' + 'a'; |
| 27 | return x; |
| 28 | } |
| 29 | |
Daniel Dunbar | 3fa528d | 2011-11-06 18:04:43 +0000 | [diff] [blame] | 30 | static char ascii_toupper(char x) { |
| 31 | if (x >= 'a' && x <= 'z') |
| 32 | return x - 'a' + 'A'; |
| 33 | return x; |
| 34 | } |
| 35 | |
Jakob Stoklund Olesen | d1d7ed6 | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 36 | static bool ascii_isdigit(char x) { |
| 37 | return x >= '0' && x <= '9'; |
| 38 | } |
| 39 | |
Rui Ueyama | 00e24e4 | 2013-10-30 18:32:26 +0000 | [diff] [blame] | 40 | // strncasecmp() is not available on non-POSIX systems, so define an |
| 41 | // alternative function here. |
| 42 | static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) { |
| 43 | for (size_t I = 0; I < Length; ++I) { |
| 44 | unsigned char LHC = ascii_tolower(LHS[I]); |
| 45 | unsigned char RHC = ascii_tolower(RHS[I]); |
Benjamin Kramer | 68e4945 | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 46 | if (LHC != RHC) |
| 47 | return LHC < RHC ? -1 : 1; |
| 48 | } |
Rui Ueyama | 00e24e4 | 2013-10-30 18:32:26 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | } |
Benjamin Kramer | 68e4945 | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 51 | |
Rui Ueyama | 00e24e4 | 2013-10-30 18:32:26 +0000 | [diff] [blame] | 52 | /// compare_lower - Compare strings, ignoring case. |
| 53 | int StringRef::compare_lower(StringRef RHS) const { |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 54 | if (int Res = ascii_strncasecmp(Data, RHS.Data, std::min(Length, RHS.Length))) |
Rui Ueyama | 00e24e4 | 2013-10-30 18:32:26 +0000 | [diff] [blame] | 55 | return Res; |
Benjamin Kramer | 68e4945 | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 56 | if (Length == RHS.Length) |
Benjamin Kramer | b04d4af | 2010-08-26 14:21:08 +0000 | [diff] [blame] | 57 | return 0; |
Benjamin Kramer | 68e4945 | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 58 | return Length < RHS.Length ? -1 : 1; |
| 59 | } |
| 60 | |
Rui Ueyama | 00e24e4 | 2013-10-30 18:32:26 +0000 | [diff] [blame] | 61 | /// Check if this string starts with the given \p Prefix, ignoring case. |
| 62 | bool StringRef::startswith_lower(StringRef Prefix) const { |
| 63 | return Length >= Prefix.Length && |
| 64 | ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0; |
| 65 | } |
| 66 | |
| 67 | /// Check if this string ends with the given \p Suffix, ignoring case. |
| 68 | bool StringRef::endswith_lower(StringRef Suffix) const { |
| 69 | return Length >= Suffix.Length && |
| 70 | ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0; |
| 71 | } |
| 72 | |
Zachary Turner | 17412b0 | 2016-11-12 17:17:12 +0000 | [diff] [blame] | 73 | size_t StringRef::find_lower(char C, size_t From) const { |
| 74 | char L = ascii_tolower(C); |
| 75 | return find_if([L](char D) { return ascii_tolower(D) == L; }, From); |
| 76 | } |
| 77 | |
Jakob Stoklund Olesen | d1d7ed6 | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 78 | /// compare_numeric - Compare strings, handle embedded numbers. |
| 79 | int StringRef::compare_numeric(StringRef RHS) const { |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 80 | for (size_t I = 0, E = std::min(Length, RHS.Length); I != E; ++I) { |
Jakob Stoklund Olesen | c874e2d | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 81 | // Check for sequences of digits. |
Jakob Stoklund Olesen | d1d7ed6 | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 82 | if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) { |
Jakob Stoklund Olesen | c874e2d | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 83 | // The longer sequence of numbers is considered larger. |
| 84 | // This doesn't really handle prefixed zeros well. |
| 85 | size_t J; |
| 86 | for (J = I + 1; J != E + 1; ++J) { |
Jakob Stoklund Olesen | d1d7ed6 | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 87 | bool ld = J < Length && ascii_isdigit(Data[J]); |
| 88 | bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]); |
| 89 | if (ld != rd) |
| 90 | return rd ? -1 : 1; |
| 91 | if (!rd) |
| 92 | break; |
| 93 | } |
Jakob Stoklund Olesen | c874e2d | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 94 | // The two number sequences have the same length (J-I), just memcmp them. |
| 95 | if (int Res = compareMemory(Data + I, RHS.Data + I, J - I)) |
| 96 | return Res < 0 ? -1 : 1; |
| 97 | // Identical number sequences, continue search after the numbers. |
| 98 | I = J - 1; |
| 99 | continue; |
Jakob Stoklund Olesen | d1d7ed6 | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 100 | } |
Jakob Stoklund Olesen | c874e2d | 2011-09-30 17:03:55 +0000 | [diff] [blame] | 101 | if (Data[I] != RHS.Data[I]) |
| 102 | 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] | 103 | } |
| 104 | if (Length == RHS.Length) |
Benjamin Kramer | b04d4af | 2010-08-26 14:21:08 +0000 | [diff] [blame] | 105 | return 0; |
Jakob Stoklund Olesen | d1d7ed6 | 2010-05-26 21:47:28 +0000 | [diff] [blame] | 106 | return Length < RHS.Length ? -1 : 1; |
| 107 | } |
| 108 | |
Douglas Gregor | 5639af4 | 2009-12-31 04:24:34 +0000 | [diff] [blame] | 109 | // Compute the edit distance between the two given strings. |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 110 | unsigned StringRef::edit_distance(llvm::StringRef Other, |
Douglas Gregor | 21afc3b | 2010-10-19 22:13:48 +0000 | [diff] [blame] | 111 | bool AllowReplacements, |
Dmitri Gribenko | 292c920 | 2013-08-24 01:50:41 +0000 | [diff] [blame] | 112 | unsigned MaxEditDistance) const { |
Kaelyn Uhrain | 7a9ccf4 | 2012-02-15 22:13:07 +0000 | [diff] [blame] | 113 | return llvm::ComputeEditDistance( |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 114 | makeArrayRef(data(), size()), |
| 115 | makeArrayRef(Other.data(), Other.size()), |
Kaelyn Uhrain | 7a9ccf4 | 2012-02-15 22:13:07 +0000 | [diff] [blame] | 116 | AllowReplacements, MaxEditDistance); |
Douglas Gregor | 165882c | 2009-12-30 17:23:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 119 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 3fa528d | 2011-11-06 18:04:43 +0000 | [diff] [blame] | 120 | // String Operations |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
| 123 | std::string StringRef::lower() const { |
| 124 | std::string Result(size(), char()); |
| 125 | for (size_type i = 0, e = size(); i != e; ++i) { |
| 126 | Result[i] = ascii_tolower(Data[i]); |
| 127 | } |
| 128 | return Result; |
| 129 | } |
| 130 | |
| 131 | std::string StringRef::upper() const { |
| 132 | std::string Result(size(), char()); |
| 133 | for (size_type i = 0, e = size(); i != e; ++i) { |
Benjamin Kramer | e3b94d1 | 2011-11-06 20:36:50 +0000 | [diff] [blame] | 134 | Result[i] = ascii_toupper(Data[i]); |
Daniel Dunbar | 3fa528d | 2011-11-06 18:04:43 +0000 | [diff] [blame] | 135 | } |
| 136 | return Result; |
| 137 | } |
| 138 | |
| 139 | //===----------------------------------------------------------------------===// |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 140 | // String Searching |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
| 143 | |
| 144 | /// find - Search for the first string \arg Str in the string. |
| 145 | /// |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 146 | /// \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] | 147 | /// found. |
Daniel Dunbar | 9806e4a | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 148 | size_t StringRef::find(StringRef Str, size_t From) const { |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 149 | if (From > Length) |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 150 | return npos; |
Benjamin Kramer | 4d681d7 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | ecbe619 | 2016-12-11 07:46:21 +0000 | [diff] [blame] | 152 | const char *Start = Data + From; |
| 153 | size_t Size = Length - From; |
| 154 | |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 155 | const char *Needle = Str.data(); |
| 156 | size_t N = Str.size(); |
| 157 | if (N == 0) |
| 158 | return From; |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 159 | if (Size < N) |
| 160 | return npos; |
Chandler Carruth | ecbe619 | 2016-12-11 07:46:21 +0000 | [diff] [blame] | 161 | if (N == 1) { |
| 162 | const char *Ptr = (const char *)::memchr(Start, Needle[0], Size); |
| 163 | return Ptr == nullptr ? npos : Ptr - Data; |
| 164 | } |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 165 | |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 166 | const char *Stop = Start + (Size - N + 1); |
| 167 | |
Benjamin Kramer | 4d681d7 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 168 | // For short haystacks or unsupported needles fall back to the naive algorithm |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 169 | if (Size < 16 || N > 255) { |
| 170 | do { |
| 171 | if (std::memcmp(Start, Needle, N) == 0) |
| 172 | return Start - Data; |
| 173 | ++Start; |
| 174 | } while (Start < Stop); |
Benjamin Kramer | 4d681d7 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 175 | return npos; |
| 176 | } |
| 177 | |
| 178 | // Build the bad char heuristic table, with uint8_t to reduce cache thrashing. |
| 179 | uint8_t BadCharSkip[256]; |
| 180 | std::memset(BadCharSkip, N, 256); |
| 181 | for (unsigned i = 0; i != N-1; ++i) |
| 182 | BadCharSkip[(uint8_t)Str[i]] = N-1-i; |
| 183 | |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 184 | do { |
Chandler Carruth | ecbe619 | 2016-12-11 07:46:21 +0000 | [diff] [blame] | 185 | uint8_t Last = Start[N - 1]; |
| 186 | if (LLVM_UNLIKELY(Last == (uint8_t)Needle[N - 1])) |
| 187 | if (std::memcmp(Start, Needle, N - 1) == 0) |
| 188 | return Start - Data; |
Benjamin Kramer | 4d681d7 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 189 | |
| 190 | // Otherwise skip the appropriate number of bytes. |
Chandler Carruth | ecbe619 | 2016-12-11 07:46:21 +0000 | [diff] [blame] | 191 | Start += BadCharSkip[Last]; |
Chandler Carruth | 233edd2 | 2015-09-10 11:17:49 +0000 | [diff] [blame] | 192 | } while (Start < Stop); |
Benjamin Kramer | 4d681d7 | 2011-10-15 10:08:31 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 194 | return npos; |
| 195 | } |
| 196 | |
Zachary Turner | 17412b0 | 2016-11-12 17:17:12 +0000 | [diff] [blame] | 197 | size_t StringRef::find_lower(StringRef Str, size_t From) const { |
| 198 | StringRef This = substr(From); |
| 199 | while (This.size() >= Str.size()) { |
| 200 | if (This.startswith_lower(Str)) |
| 201 | return From; |
| 202 | This = This.drop_front(); |
| 203 | ++From; |
| 204 | } |
| 205 | return npos; |
| 206 | } |
| 207 | |
| 208 | size_t StringRef::rfind_lower(char C, size_t From) const { |
| 209 | From = std::min(From, Length); |
| 210 | size_t i = From; |
| 211 | while (i != 0) { |
| 212 | --i; |
| 213 | if (ascii_tolower(Data[i]) == ascii_tolower(C)) |
| 214 | return i; |
| 215 | } |
| 216 | return npos; |
| 217 | } |
| 218 | |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 219 | /// rfind - Search for the last string \arg Str in the string. |
| 220 | /// |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 221 | /// \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] | 222 | /// found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 223 | size_t StringRef::rfind(StringRef Str) const { |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 224 | size_t N = Str.size(); |
| 225 | if (N > Length) |
| 226 | return npos; |
| 227 | for (size_t i = Length - N + 1, e = 0; i != e;) { |
| 228 | --i; |
| 229 | if (substr(i, N).equals(Str)) |
| 230 | return i; |
| 231 | } |
| 232 | return npos; |
| 233 | } |
| 234 | |
Zachary Turner | 17412b0 | 2016-11-12 17:17:12 +0000 | [diff] [blame] | 235 | size_t StringRef::rfind_lower(StringRef Str) const { |
| 236 | size_t N = Str.size(); |
| 237 | if (N > Length) |
| 238 | return npos; |
| 239 | for (size_t i = Length - N + 1, e = 0; i != e;) { |
| 240 | --i; |
| 241 | if (substr(i, N).equals_lower(Str)) |
| 242 | return i; |
| 243 | } |
| 244 | return npos; |
| 245 | } |
| 246 | |
Daniel Dunbar | 9806e4a | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 247 | /// find_first_of - Find the first character in the string that is in \arg |
| 248 | /// Chars, or npos if not found. |
| 249 | /// |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 250 | /// Note: O(size() + Chars.size()) |
Daniel Dunbar | 9806e4a | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 251 | StringRef::size_type StringRef::find_first_of(StringRef Chars, |
| 252 | size_t From) const { |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 253 | std::bitset<1 << CHAR_BIT> CharBits; |
| 254 | for (size_type i = 0; i != Chars.size(); ++i) |
| 255 | CharBits.set((unsigned char)Chars[i]); |
| 256 | |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 257 | for (size_type i = std::min(From, Length), e = Length; i != e; ++i) |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 258 | if (CharBits.test((unsigned char)Data[i])) |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 259 | return i; |
| 260 | return npos; |
| 261 | } |
| 262 | |
| 263 | /// 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] | 264 | /// \arg C or npos if not found. |
| 265 | StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const { |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 266 | for (size_type i = std::min(From, Length), e = Length; i != e; ++i) |
Daniel Dunbar | 9806e4a | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 267 | if (Data[i] != C) |
| 268 | return i; |
| 269 | return npos; |
| 270 | } |
| 271 | |
| 272 | /// find_first_not_of - Find the first character in the string that is not |
| 273 | /// in the string \arg Chars, or npos if not found. |
| 274 | /// |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 275 | /// Note: O(size() + Chars.size()) |
Daniel Dunbar | 9806e4a | 2009-11-11 00:28:53 +0000 | [diff] [blame] | 276 | StringRef::size_type StringRef::find_first_not_of(StringRef Chars, |
| 277 | size_t From) const { |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 278 | std::bitset<1 << CHAR_BIT> CharBits; |
| 279 | for (size_type i = 0; i != Chars.size(); ++i) |
| 280 | CharBits.set((unsigned char)Chars[i]); |
| 281 | |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 282 | for (size_type i = std::min(From, Length), e = Length; i != e; ++i) |
Benjamin Kramer | 08fd2cf | 2010-08-23 18:16:08 +0000 | [diff] [blame] | 283 | if (!CharBits.test((unsigned char)Data[i])) |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 284 | return i; |
| 285 | return npos; |
| 286 | } |
| 287 | |
Michael J. Spencer | e1d3603d | 2010-11-30 23:27:35 +0000 | [diff] [blame] | 288 | /// find_last_of - Find the last character in the string that is in \arg C, |
| 289 | /// or npos if not found. |
| 290 | /// |
| 291 | /// Note: O(size() + Chars.size()) |
| 292 | StringRef::size_type StringRef::find_last_of(StringRef Chars, |
| 293 | size_t From) const { |
| 294 | std::bitset<1 << CHAR_BIT> CharBits; |
| 295 | for (size_type i = 0; i != Chars.size(); ++i) |
| 296 | CharBits.set((unsigned char)Chars[i]); |
| 297 | |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 298 | for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i) |
Michael J. Spencer | e1d3603d | 2010-11-30 23:27:35 +0000 | [diff] [blame] | 299 | if (CharBits.test((unsigned char)Data[i])) |
| 300 | return i; |
| 301 | return npos; |
| 302 | } |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 303 | |
Michael J. Spencer | 9330381 | 2012-05-11 22:08:50 +0000 | [diff] [blame] | 304 | /// find_last_not_of - Find the last character in the string that is not |
| 305 | /// \arg C, or npos if not found. |
| 306 | StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const { |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 307 | for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i) |
Michael J. Spencer | 9330381 | 2012-05-11 22:08:50 +0000 | [diff] [blame] | 308 | if (Data[i] != C) |
| 309 | return i; |
| 310 | return npos; |
| 311 | } |
| 312 | |
| 313 | /// find_last_not_of - Find the last character in the string that is not in |
| 314 | /// \arg Chars, or npos if not found. |
| 315 | /// |
| 316 | /// Note: O(size() + Chars.size()) |
| 317 | StringRef::size_type StringRef::find_last_not_of(StringRef Chars, |
| 318 | size_t From) const { |
| 319 | std::bitset<1 << CHAR_BIT> CharBits; |
| 320 | for (size_type i = 0, e = Chars.size(); i != e; ++i) |
| 321 | CharBits.set((unsigned char)Chars[i]); |
| 322 | |
Craig Topper | 3ced27c | 2014-08-21 04:31:10 +0000 | [diff] [blame] | 323 | for (size_type i = std::min(From, Length) - 1, e = -1; i != e; --i) |
Michael J. Spencer | 9330381 | 2012-05-11 22:08:50 +0000 | [diff] [blame] | 324 | if (!CharBits.test((unsigned char)Data[i])) |
| 325 | return i; |
| 326 | return npos; |
| 327 | } |
| 328 | |
Duncan Sands | 8570b29 | 2012-02-21 12:00:25 +0000 | [diff] [blame] | 329 | void StringRef::split(SmallVectorImpl<StringRef> &A, |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 330 | StringRef Separator, int MaxSplit, |
Duncan Sands | 8570b29 | 2012-02-21 12:00:25 +0000 | [diff] [blame] | 331 | bool KeepEmpty) const { |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 332 | StringRef S = *this; |
Duncan Sands | 8570b29 | 2012-02-21 12:00:25 +0000 | [diff] [blame] | 333 | |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 334 | // Count down from MaxSplit. When MaxSplit is -1, this will just split |
| 335 | // "forever". This doesn't support splitting more than 2^31 times |
| 336 | // intentionally; if we ever want that we can make MaxSplit a 64-bit integer |
| 337 | // but that seems unlikely to be useful. |
| 338 | while (MaxSplit-- != 0) { |
| 339 | size_t Idx = S.find(Separator); |
| 340 | if (Idx == npos) |
| 341 | break; |
Duncan Sands | 8570b29 | 2012-02-21 12:00:25 +0000 | [diff] [blame] | 342 | |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 343 | // Push this split. |
| 344 | if (KeepEmpty || Idx > 0) |
| 345 | A.push_back(S.slice(0, Idx)); |
| 346 | |
| 347 | // Jump forward. |
| 348 | S = S.slice(Idx + Separator.size(), npos); |
Duncan Sands | 8570b29 | 2012-02-21 12:00:25 +0000 | [diff] [blame] | 349 | } |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 350 | |
| 351 | // Push the tail. |
| 352 | if (KeepEmpty || !S.empty()) |
| 353 | A.push_back(S); |
Duncan Sands | 8570b29 | 2012-02-21 12:00:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Chandler Carruth | 4771217 | 2015-09-10 06:07:03 +0000 | [diff] [blame] | 356 | void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator, |
| 357 | int MaxSplit, bool KeepEmpty) const { |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 358 | StringRef S = *this; |
Chandler Carruth | 4771217 | 2015-09-10 06:07:03 +0000 | [diff] [blame] | 359 | |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 360 | // Count down from MaxSplit. When MaxSplit is -1, this will just split |
| 361 | // "forever". This doesn't support splitting more than 2^31 times |
| 362 | // intentionally; if we ever want that we can make MaxSplit a 64-bit integer |
| 363 | // but that seems unlikely to be useful. |
| 364 | while (MaxSplit-- != 0) { |
| 365 | size_t Idx = S.find(Separator); |
| 366 | if (Idx == npos) |
| 367 | break; |
Chandler Carruth | 4771217 | 2015-09-10 06:07:03 +0000 | [diff] [blame] | 368 | |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 369 | // Push this split. |
| 370 | if (KeepEmpty || Idx > 0) |
| 371 | A.push_back(S.slice(0, Idx)); |
| 372 | |
| 373 | // Jump forward. |
| 374 | S = S.slice(Idx + 1, npos); |
Chandler Carruth | 4771217 | 2015-09-10 06:07:03 +0000 | [diff] [blame] | 375 | } |
Chandler Carruth | 4425c91 | 2015-09-10 07:51:37 +0000 | [diff] [blame] | 376 | |
| 377 | // Push the tail. |
| 378 | if (KeepEmpty || !S.empty()) |
| 379 | A.push_back(S); |
Chandler Carruth | 4771217 | 2015-09-10 06:07:03 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 382 | //===----------------------------------------------------------------------===// |
| 383 | // Helpful Algorithms |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | |
| 386 | /// count - Return the number of non-overlapped occurrences of \arg Str in |
| 387 | /// the string. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 388 | size_t StringRef::count(StringRef Str) const { |
Chris Lattner | 372a8ae | 2009-09-20 01:22:16 +0000 | [diff] [blame] | 389 | size_t Count = 0; |
| 390 | size_t N = Str.size(); |
| 391 | if (N > Length) |
| 392 | return 0; |
| 393 | for (size_t i = 0, e = Length - N + 1; i != e; ++i) |
| 394 | if (substr(i, N).equals(Str)) |
| 395 | ++Count; |
| 396 | return Count; |
| 397 | } |
| 398 | |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 399 | static unsigned GetAutoSenseRadix(StringRef &Str) { |
Zachary Turner | d5d5763 | 2016-09-22 15:55:05 +0000 | [diff] [blame] | 400 | if (Str.empty()) |
| 401 | return 10; |
| 402 | |
Colin LeMahieu | 0143146 | 2016-03-18 18:22:07 +0000 | [diff] [blame] | 403 | if (Str.startswith("0x") || Str.startswith("0X")) { |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 404 | Str = Str.substr(2); |
| 405 | return 16; |
Chris Lattner | 0a1bafe | 2012-04-21 22:03:05 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Colin LeMahieu | 0143146 | 2016-03-18 18:22:07 +0000 | [diff] [blame] | 408 | if (Str.startswith("0b") || Str.startswith("0B")) { |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 409 | Str = Str.substr(2); |
| 410 | return 2; |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | 0a1bafe | 2012-04-21 22:03:05 +0000 | [diff] [blame] | 412 | |
| 413 | if (Str.startswith("0o")) { |
| 414 | Str = Str.substr(2); |
| 415 | return 8; |
| 416 | } |
| 417 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 418 | if (Str[0] == '0' && Str.size() > 1 && ascii_isdigit(Str[1])) { |
| 419 | Str = Str.substr(1); |
Chris Lattner | 0a1bafe | 2012-04-21 22:03:05 +0000 | [diff] [blame] | 420 | return 8; |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Chris Lattner | 0a1bafe | 2012-04-21 22:03:05 +0000 | [diff] [blame] | 423 | return 10; |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 426 | bool llvm::consumeUnsignedInteger(StringRef &Str, unsigned Radix, |
| 427 | unsigned long long &Result) { |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 428 | // Autosense radix if not specified. |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 429 | if (Radix == 0) |
| 430 | Radix = GetAutoSenseRadix(Str); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 432 | // Empty strings (after the radix autosense) are invalid. |
| 433 | if (Str.empty()) return true; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 435 | // Parse all the bytes of the string given this radix. Watch for overflow. |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 436 | StringRef Str2 = Str; |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 437 | Result = 0; |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 438 | while (!Str2.empty()) { |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 439 | unsigned CharVal; |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 440 | if (Str2[0] >= '0' && Str2[0] <= '9') |
| 441 | CharVal = Str2[0] - '0'; |
| 442 | else if (Str2[0] >= 'a' && Str2[0] <= 'z') |
| 443 | CharVal = Str2[0] - 'a' + 10; |
| 444 | else if (Str2[0] >= 'A' && Str2[0] <= 'Z') |
| 445 | CharVal = Str2[0] - 'A' + 10; |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 446 | else |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 447 | break; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 448 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 449 | // If the parsed value is larger than the integer radix, we cannot |
| 450 | // consume any more characters. |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 451 | if (CharVal >= Radix) |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 452 | break; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 454 | // Add in this character. |
| 455 | unsigned long long PrevResult = Result; |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 456 | Result = Result * Radix + CharVal; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 457 | |
Nick Kledzik | 35c79da | 2012-10-02 20:01:48 +0000 | [diff] [blame] | 458 | // Check for overflow by shifting back and seeing if bits were lost. |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 459 | if (Result / Radix < PrevResult) |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 460 | return true; |
| 461 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 462 | Str2 = Str2.substr(1); |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 463 | } |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 464 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 465 | // We consider the operation a failure if no characters were consumed |
| 466 | // successfully. |
| 467 | if (Str.size() == Str2.size()) |
| 468 | return true; |
| 469 | |
| 470 | Str = Str2; |
Chris Lattner | 68ee700 | 2009-09-19 19:47:14 +0000 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 474 | bool llvm::consumeSignedInteger(StringRef &Str, unsigned Radix, |
| 475 | long long &Result) { |
Chris Lattner | 84c1527 | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 476 | unsigned long long ULLVal; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 84c1527 | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 478 | // Handle positive strings first. |
Michael J. Spencer | cfa95f6 | 2012-03-10 23:02:54 +0000 | [diff] [blame] | 479 | if (Str.empty() || Str.front() != '-') { |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 480 | if (consumeUnsignedInteger(Str, Radix, ULLVal) || |
Chris Lattner | 84c1527 | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 481 | // Check for value so large it overflows a signed value. |
| 482 | (long long)ULLVal < 0) |
| 483 | return true; |
| 484 | Result = ULLVal; |
| 485 | return false; |
| 486 | } |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 84c1527 | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 488 | // Get the positive part of the value. |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 489 | StringRef Str2 = Str.drop_front(1); |
| 490 | if (consumeUnsignedInteger(Str2, Radix, ULLVal) || |
Chris Lattner | 84c1527 | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 491 | // Reject values so large they'd overflow as negative signed, but allow |
| 492 | // "-0". This negates the unsigned so that the negative isn't undefined |
| 493 | // on signed overflow. |
| 494 | (long long)-ULLVal > 0) |
| 495 | return true; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 496 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 497 | Str = Str2; |
Chris Lattner | 84c1527 | 2009-09-19 23:58:48 +0000 | [diff] [blame] | 498 | Result = -ULLVal; |
| 499 | return false; |
| 500 | } |
| 501 | |
Zachary Turner | 65fd2fc | 2016-09-22 15:05:19 +0000 | [diff] [blame] | 502 | /// GetAsUnsignedInteger - Workhorse method that converts a integer character |
| 503 | /// sequence of radix up to 36 to an unsigned long long value. |
| 504 | bool llvm::getAsUnsignedInteger(StringRef Str, unsigned Radix, |
| 505 | unsigned long long &Result) { |
| 506 | if (consumeUnsignedInteger(Str, Radix, Result)) |
| 507 | return true; |
| 508 | |
| 509 | // For getAsUnsignedInteger, we require the whole string to be consumed or |
| 510 | // else we consider it a failure. |
| 511 | return !Str.empty(); |
| 512 | } |
| 513 | |
| 514 | bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix, |
| 515 | long long &Result) { |
| 516 | if (consumeSignedInteger(Str, Radix, Result)) |
| 517 | return true; |
| 518 | |
| 519 | // For getAsSignedInteger, we require the whole string to be consumed or else |
| 520 | // we consider it a failure. |
| 521 | return !Str.empty(); |
| 522 | } |
| 523 | |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 524 | bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { |
| 525 | StringRef Str = *this; |
| 526 | |
| 527 | // Autosense radix if not specified. |
| 528 | if (Radix == 0) |
| 529 | Radix = GetAutoSenseRadix(Str); |
| 530 | |
| 531 | assert(Radix > 1 && Radix <= 36); |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 532 | |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 533 | // Empty strings (after the radix autosense) are invalid. |
| 534 | if (Str.empty()) return true; |
| 535 | |
| 536 | // Skip leading zeroes. This can be a significant improvement if |
| 537 | // it means we don't need > 64 bits. |
| 538 | while (!Str.empty() && Str.front() == '0') |
| 539 | Str = Str.substr(1); |
| 540 | |
| 541 | // If it was nothing but zeroes.... |
| 542 | if (Str.empty()) { |
| 543 | Result = APInt(64, 0); |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | // (Over-)estimate the required number of bits. |
| 548 | unsigned Log2Radix = 0; |
| 549 | while ((1U << Log2Radix) < Radix) Log2Radix++; |
| 550 | bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix); |
| 551 | |
| 552 | unsigned BitWidth = Log2Radix * Str.size(); |
| 553 | if (BitWidth < Result.getBitWidth()) |
| 554 | BitWidth = Result.getBitWidth(); // don't shrink the result |
Chris Lattner | 5e14666 | 2012-04-23 00:27:54 +0000 | [diff] [blame] | 555 | else if (BitWidth > Result.getBitWidth()) |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 556 | Result = Result.zext(BitWidth); |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 557 | |
| 558 | APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix |
| 559 | if (!IsPowerOf2Radix) { |
| 560 | // These must have the same bit-width as Result. |
| 561 | RadixAP = APInt(BitWidth, Radix); |
| 562 | CharAP = APInt(BitWidth, 0); |
| 563 | } |
| 564 | |
| 565 | // Parse all the bytes of the string given this radix. |
| 566 | Result = 0; |
| 567 | while (!Str.empty()) { |
| 568 | unsigned CharVal; |
| 569 | if (Str[0] >= '0' && Str[0] <= '9') |
| 570 | CharVal = Str[0]-'0'; |
| 571 | else if (Str[0] >= 'a' && Str[0] <= 'z') |
| 572 | CharVal = Str[0]-'a'+10; |
| 573 | else if (Str[0] >= 'A' && Str[0] <= 'Z') |
| 574 | CharVal = Str[0]-'A'+10; |
| 575 | else |
| 576 | return true; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 577 | |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 578 | // If the parsed value is larger than the integer radix, the string is |
| 579 | // invalid. |
| 580 | if (CharVal >= Radix) |
| 581 | return true; |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 582 | |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 583 | // Add in this character. |
| 584 | if (IsPowerOf2Radix) { |
| 585 | Result <<= Log2Radix; |
| 586 | Result |= CharVal; |
| 587 | } else { |
| 588 | Result *= RadixAP; |
| 589 | CharAP = CharVal; |
| 590 | Result += CharAP; |
| 591 | } |
| 592 | |
| 593 | Str = Str.substr(1); |
| 594 | } |
Michael J. Spencer | f13f442 | 2010-11-26 04:16:08 +0000 | [diff] [blame] | 595 | |
John McCall | 512b650 | 2010-02-28 09:55:58 +0000 | [diff] [blame] | 596 | return false; |
| 597 | } |
Chandler Carruth | ca99ad3 | 2012-03-04 10:55:27 +0000 | [diff] [blame] | 598 | |
Zachary Turner | 8bd42a1 | 2017-02-14 19:06:37 +0000 | [diff] [blame^] | 599 | bool StringRef::getAsDouble(double &Result, bool AllowInexact) const { |
| 600 | APFloat F(0.0); |
| 601 | APFloat::opStatus Status = |
| 602 | F.convertFromString(*this, APFloat::rmNearestTiesToEven); |
| 603 | if (Status != APFloat::opOK) { |
| 604 | if (!AllowInexact || Status != APFloat::opInexact) |
| 605 | return true; |
| 606 | } |
| 607 | |
| 608 | Result = F.convertToDouble(); |
| 609 | return false; |
| 610 | } |
Chandler Carruth | ca99ad3 | 2012-03-04 10:55:27 +0000 | [diff] [blame] | 611 | |
| 612 | // Implementation of StringRef hashing. |
| 613 | hash_code llvm::hash_value(StringRef S) { |
| 614 | return hash_combine_range(S.begin(), S.end()); |
| 615 | } |