Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 14 | pmbaty | 1 | //===- StringRef.h - Constant String Reference Wrapper ----------*- C++ -*-===// |
| 2 | // |
||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
||
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
||
| 6 | // |
||
| 7 | //===----------------------------------------------------------------------===// |
||
| 8 | |||
| 9 | #ifndef LLVM_ADT_STRINGREF_H |
||
| 10 | #define LLVM_ADT_STRINGREF_H |
||
| 11 | |||
| 12 | #include "llvm/ADT/DenseMapInfo.h" |
||
| 13 | #include "llvm/ADT/STLFunctionalExtras.h" |
||
| 14 | #include "llvm/ADT/iterator_range.h" |
||
| 15 | #include "llvm/Support/Compiler.h" |
||
| 16 | #include <algorithm> |
||
| 17 | #include <cassert> |
||
| 18 | #include <cstddef> |
||
| 19 | #include <cstring> |
||
| 20 | #include <limits> |
||
| 21 | #include <string> |
||
| 22 | #include <string_view> |
||
| 23 | #include <type_traits> |
||
| 24 | #include <utility> |
||
| 25 | |||
| 26 | namespace llvm { |
||
| 27 | |||
| 28 | class APInt; |
||
| 29 | class hash_code; |
||
| 30 | template <typename T> class SmallVectorImpl; |
||
| 31 | class StringRef; |
||
| 32 | |||
| 33 | /// Helper functions for StringRef::getAsInteger. |
||
| 34 | bool getAsUnsignedInteger(StringRef Str, unsigned Radix, |
||
| 35 | unsigned long long &Result); |
||
| 36 | |||
| 37 | bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result); |
||
| 38 | |||
| 39 | bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, |
||
| 40 | unsigned long long &Result); |
||
| 41 | bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result); |
||
| 42 | |||
| 43 | /// StringRef - Represent a constant reference to a string, i.e. a character |
||
| 44 | /// array and a length, which need not be null terminated. |
||
| 45 | /// |
||
| 46 | /// This class does not own the string data, it is expected to be used in |
||
| 47 | /// situations where the character data resides in some other buffer, whose |
||
| 48 | /// lifetime extends past that of the StringRef. For this reason, it is not in |
||
| 49 | /// general safe to store a StringRef. |
||
| 50 | class LLVM_GSL_POINTER StringRef { |
||
| 51 | public: |
||
| 52 | static constexpr size_t npos = ~size_t(0); |
||
| 53 | |||
| 54 | using iterator = const char *; |
||
| 55 | using const_iterator = const char *; |
||
| 56 | using size_type = size_t; |
||
| 57 | |||
| 58 | private: |
||
| 59 | /// The start of the string, in an external buffer. |
||
| 60 | const char *Data = nullptr; |
||
| 61 | |||
| 62 | /// The length of the string. |
||
| 63 | size_t Length = 0; |
||
| 64 | |||
| 65 | // Workaround memcmp issue with null pointers (undefined behavior) |
||
| 66 | // by providing a specialized version |
||
| 67 | static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) { |
||
| 68 | if (Length == 0) { return 0; } |
||
| 69 | return ::memcmp(Lhs,Rhs,Length); |
||
| 70 | } |
||
| 71 | |||
| 72 | public: |
||
| 73 | /// @name Constructors |
||
| 74 | /// @{ |
||
| 75 | |||
| 76 | /// Construct an empty string ref. |
||
| 77 | /*implicit*/ StringRef() = default; |
||
| 78 | |||
| 79 | /// Disable conversion from nullptr. This prevents things like |
||
| 80 | /// if (S == nullptr) |
||
| 81 | StringRef(std::nullptr_t) = delete; |
||
| 82 | |||
| 83 | /// Construct a string ref from a cstring. |
||
| 84 | /*implicit*/ constexpr StringRef(const char *Str) |
||
| 85 | : Data(Str), Length(Str ? |
||
| 86 | // GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen. |
||
| 87 | #if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8 |
||
| 88 | __builtin_strlen(Str) |
||
| 89 | #else |
||
| 90 | std::char_traits<char>::length(Str) |
||
| 91 | #endif |
||
| 92 | : 0) { |
||
| 93 | } |
||
| 94 | |||
| 95 | /// Construct a string ref from a pointer and length. |
||
| 96 | /*implicit*/ constexpr StringRef(const char *data, size_t length) |
||
| 97 | : Data(data), Length(length) {} |
||
| 98 | |||
| 99 | /// Construct a string ref from an std::string. |
||
| 100 | /*implicit*/ StringRef(const std::string &Str) |
||
| 101 | : Data(Str.data()), Length(Str.length()) {} |
||
| 102 | |||
| 103 | /// Construct a string ref from an std::string_view. |
||
| 104 | /*implicit*/ constexpr StringRef(std::string_view Str) |
||
| 105 | : Data(Str.data()), Length(Str.size()) {} |
||
| 106 | |||
| 107 | /// @} |
||
| 108 | /// @name Iterators |
||
| 109 | /// @{ |
||
| 110 | |||
| 111 | iterator begin() const { return Data; } |
||
| 112 | |||
| 113 | iterator end() const { return Data + Length; } |
||
| 114 | |||
| 115 | const unsigned char *bytes_begin() const { |
||
| 116 | return reinterpret_cast<const unsigned char *>(begin()); |
||
| 117 | } |
||
| 118 | const unsigned char *bytes_end() const { |
||
| 119 | return reinterpret_cast<const unsigned char *>(end()); |
||
| 120 | } |
||
| 121 | iterator_range<const unsigned char *> bytes() const { |
||
| 122 | return make_range(bytes_begin(), bytes_end()); |
||
| 123 | } |
||
| 124 | |||
| 125 | /// @} |
||
| 126 | /// @name String Operations |
||
| 127 | /// @{ |
||
| 128 | |||
| 129 | /// data - Get a pointer to the start of the string (which may not be null |
||
| 130 | /// terminated). |
||
| 131 | [[nodiscard]] const char *data() const { return Data; } |
||
| 132 | |||
| 133 | /// empty - Check if the string is empty. |
||
| 134 | [[nodiscard]] constexpr bool empty() const { return Length == 0; } |
||
| 135 | |||
| 136 | /// size - Get the string size. |
||
| 137 | [[nodiscard]] constexpr size_t size() const { return Length; } |
||
| 138 | |||
| 139 | /// front - Get the first character in the string. |
||
| 140 | [[nodiscard]] char front() const { |
||
| 141 | assert(!empty()); |
||
| 142 | return Data[0]; |
||
| 143 | } |
||
| 144 | |||
| 145 | /// back - Get the last character in the string. |
||
| 146 | [[nodiscard]] char back() const { |
||
| 147 | assert(!empty()); |
||
| 148 | return Data[Length-1]; |
||
| 149 | } |
||
| 150 | |||
| 151 | // copy - Allocate copy in Allocator and return StringRef to it. |
||
| 152 | template <typename Allocator> |
||
| 153 | [[nodiscard]] StringRef copy(Allocator &A) const { |
||
| 154 | // Don't request a length 0 copy from the allocator. |
||
| 155 | if (empty()) |
||
| 156 | return StringRef(); |
||
| 157 | char *S = A.template Allocate<char>(Length); |
||
| 158 | std::copy(begin(), end(), S); |
||
| 159 | return StringRef(S, Length); |
||
| 160 | } |
||
| 161 | |||
| 162 | /// equals - Check for string equality, this is more efficient than |
||
| 163 | /// compare() when the relative ordering of inequal strings isn't needed. |
||
| 164 | [[nodiscard]] bool equals(StringRef RHS) const { |
||
| 165 | return (Length == RHS.Length && |
||
| 166 | compareMemory(Data, RHS.Data, RHS.Length) == 0); |
||
| 167 | } |
||
| 168 | |||
| 169 | /// Check for string equality, ignoring case. |
||
| 170 | [[nodiscard]] bool equals_insensitive(StringRef RHS) const { |
||
| 171 | return Length == RHS.Length && compare_insensitive(RHS) == 0; |
||
| 172 | } |
||
| 173 | |||
| 174 | /// compare - Compare two strings; the result is negative, zero, or positive |
||
| 175 | /// if this string is lexicographically less than, equal to, or greater than |
||
| 176 | /// the \p RHS. |
||
| 177 | [[nodiscard]] int compare(StringRef RHS) const { |
||
| 178 | // Check the prefix for a mismatch. |
||
| 179 | if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length))) |
||
| 180 | return Res < 0 ? -1 : 1; |
||
| 181 | |||
| 182 | // Otherwise the prefixes match, so we only need to check the lengths. |
||
| 183 | if (Length == RHS.Length) |
||
| 184 | return 0; |
||
| 185 | return Length < RHS.Length ? -1 : 1; |
||
| 186 | } |
||
| 187 | |||
| 188 | /// Compare two strings, ignoring case. |
||
| 189 | [[nodiscard]] int compare_insensitive(StringRef RHS) const; |
||
| 190 | |||
| 191 | /// compare_numeric - Compare two strings, treating sequences of digits as |
||
| 192 | /// numbers. |
||
| 193 | [[nodiscard]] int compare_numeric(StringRef RHS) const; |
||
| 194 | |||
| 195 | /// Determine the edit distance between this string and another |
||
| 196 | /// string. |
||
| 197 | /// |
||
| 198 | /// \param Other the string to compare this string against. |
||
| 199 | /// |
||
| 200 | /// \param AllowReplacements whether to allow character |
||
| 201 | /// replacements (change one character into another) as a single |
||
| 202 | /// operation, rather than as two operations (an insertion and a |
||
| 203 | /// removal). |
||
| 204 | /// |
||
| 205 | /// \param MaxEditDistance If non-zero, the maximum edit distance that |
||
| 206 | /// this routine is allowed to compute. If the edit distance will exceed |
||
| 207 | /// that maximum, returns \c MaxEditDistance+1. |
||
| 208 | /// |
||
| 209 | /// \returns the minimum number of character insertions, removals, |
||
| 210 | /// or (if \p AllowReplacements is \c true) replacements needed to |
||
| 211 | /// transform one of the given strings into the other. If zero, |
||
| 212 | /// the strings are identical. |
||
| 213 | [[nodiscard]] unsigned edit_distance(StringRef Other, |
||
| 214 | bool AllowReplacements = true, |
||
| 215 | unsigned MaxEditDistance = 0) const; |
||
| 216 | |||
| 217 | [[nodiscard]] unsigned |
||
| 218 | edit_distance_insensitive(StringRef Other, bool AllowReplacements = true, |
||
| 219 | unsigned MaxEditDistance = 0) const; |
||
| 220 | |||
| 221 | /// str - Get the contents as an std::string. |
||
| 222 | [[nodiscard]] std::string str() const { |
||
| 223 | if (!Data) return std::string(); |
||
| 224 | return std::string(Data, Length); |
||
| 225 | } |
||
| 226 | |||
| 227 | /// @} |
||
| 228 | /// @name Operator Overloads |
||
| 229 | /// @{ |
||
| 230 | |||
| 231 | [[nodiscard]] char operator[](size_t Index) const { |
||
| 232 | assert(Index < Length && "Invalid index!"); |
||
| 233 | return Data[Index]; |
||
| 234 | } |
||
| 235 | |||
| 236 | /// Disallow accidental assignment from a temporary std::string. |
||
| 237 | /// |
||
| 238 | /// The declaration here is extra complicated so that `stringRef = {}` |
||
| 239 | /// and `stringRef = "abc"` continue to select the move assignment operator. |
||
| 240 | template <typename T> |
||
| 241 | std::enable_if_t<std::is_same<T, std::string>::value, StringRef> & |
||
| 242 | operator=(T &&Str) = delete; |
||
| 243 | |||
| 244 | /// @} |
||
| 245 | /// @name Type Conversions |
||
| 246 | /// @{ |
||
| 247 | |||
| 248 | operator std::string_view() const { |
||
| 249 | return std::string_view(data(), size()); |
||
| 250 | } |
||
| 251 | |||
| 252 | /// @} |
||
| 253 | /// @name String Predicates |
||
| 254 | /// @{ |
||
| 255 | |||
| 256 | /// Check if this string starts with the given \p Prefix. |
||
| 257 | [[nodiscard]] bool starts_with(StringRef Prefix) const { |
||
| 258 | return Length >= Prefix.Length && |
||
| 259 | compareMemory(Data, Prefix.Data, Prefix.Length) == 0; |
||
| 260 | } |
||
| 261 | [[nodiscard]] bool startswith(StringRef Prefix) const { |
||
| 262 | return starts_with(Prefix); |
||
| 263 | } |
||
| 264 | |||
| 265 | /// Check if this string starts with the given \p Prefix, ignoring case. |
||
| 266 | [[nodiscard]] bool starts_with_insensitive(StringRef Prefix) const; |
||
| 267 | [[nodiscard]] bool startswith_insensitive(StringRef Prefix) const { |
||
| 268 | return starts_with_insensitive(Prefix); |
||
| 269 | } |
||
| 270 | |||
| 271 | /// Check if this string ends with the given \p Suffix. |
||
| 272 | [[nodiscard]] bool ends_with(StringRef Suffix) const { |
||
| 273 | return Length >= Suffix.Length && |
||
| 274 | compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == |
||
| 275 | 0; |
||
| 276 | } |
||
| 277 | [[nodiscard]] bool endswith(StringRef Suffix) const { |
||
| 278 | return ends_with(Suffix); |
||
| 279 | } |
||
| 280 | |||
| 281 | /// Check if this string ends with the given \p Suffix, ignoring case. |
||
| 282 | [[nodiscard]] bool ends_with_insensitive(StringRef Suffix) const; |
||
| 283 | [[nodiscard]] bool endswith_insensitive(StringRef Suffix) const { |
||
| 284 | return ends_with_insensitive(Suffix); |
||
| 285 | } |
||
| 286 | |||
| 287 | /// @} |
||
| 288 | /// @name String Searching |
||
| 289 | /// @{ |
||
| 290 | |||
| 291 | /// Search for the first character \p C in the string. |
||
| 292 | /// |
||
| 293 | /// \returns The index of the first occurrence of \p C, or npos if not |
||
| 294 | /// found. |
||
| 295 | [[nodiscard]] size_t find(char C, size_t From = 0) const { |
||
| 296 | return std::string_view(*this).find(C, From); |
||
| 297 | } |
||
| 298 | |||
| 299 | /// Search for the first character \p C in the string, ignoring case. |
||
| 300 | /// |
||
| 301 | /// \returns The index of the first occurrence of \p C, or npos if not |
||
| 302 | /// found. |
||
| 303 | [[nodiscard]] size_t find_insensitive(char C, size_t From = 0) const; |
||
| 304 | |||
| 305 | /// Search for the first character satisfying the predicate \p F |
||
| 306 | /// |
||
| 307 | /// \returns The index of the first character satisfying \p F starting from |
||
| 308 | /// \p From, or npos if not found. |
||
| 309 | [[nodiscard]] size_t find_if(function_ref<bool(char)> F, |
||
| 310 | size_t From = 0) const { |
||
| 311 | StringRef S = drop_front(From); |
||
| 312 | while (!S.empty()) { |
||
| 313 | if (F(S.front())) |
||
| 314 | return size() - S.size(); |
||
| 315 | S = S.drop_front(); |
||
| 316 | } |
||
| 317 | return npos; |
||
| 318 | } |
||
| 319 | |||
| 320 | /// Search for the first character not satisfying the predicate \p F |
||
| 321 | /// |
||
| 322 | /// \returns The index of the first character not satisfying \p F starting |
||
| 323 | /// from \p From, or npos if not found. |
||
| 324 | [[nodiscard]] size_t find_if_not(function_ref<bool(char)> F, |
||
| 325 | size_t From = 0) const { |
||
| 326 | return find_if([F](char c) { return !F(c); }, From); |
||
| 327 | } |
||
| 328 | |||
| 329 | /// Search for the first string \p Str in the string. |
||
| 330 | /// |
||
| 331 | /// \returns The index of the first occurrence of \p Str, or npos if not |
||
| 332 | /// found. |
||
| 333 | [[nodiscard]] size_t find(StringRef Str, size_t From = 0) const; |
||
| 334 | |||
| 335 | /// Search for the first string \p Str in the string, ignoring case. |
||
| 336 | /// |
||
| 337 | /// \returns The index of the first occurrence of \p Str, or npos if not |
||
| 338 | /// found. |
||
| 339 | [[nodiscard]] size_t find_insensitive(StringRef Str, size_t From = 0) const; |
||
| 340 | |||
| 341 | /// Search for the last character \p C in the string. |
||
| 342 | /// |
||
| 343 | /// \returns The index of the last occurrence of \p C, or npos if not |
||
| 344 | /// found. |
||
| 345 | [[nodiscard]] size_t rfind(char C, size_t From = npos) const { |
||
| 346 | From = std::min(From, Length); |
||
| 347 | size_t i = From; |
||
| 348 | while (i != 0) { |
||
| 349 | --i; |
||
| 350 | if (Data[i] == C) |
||
| 351 | return i; |
||
| 352 | } |
||
| 353 | return npos; |
||
| 354 | } |
||
| 355 | |||
| 356 | /// Search for the last character \p C in the string, ignoring case. |
||
| 357 | /// |
||
| 358 | /// \returns The index of the last occurrence of \p C, or npos if not |
||
| 359 | /// found. |
||
| 360 | [[nodiscard]] size_t rfind_insensitive(char C, size_t From = npos) const; |
||
| 361 | |||
| 362 | /// Search for the last string \p Str in the string. |
||
| 363 | /// |
||
| 364 | /// \returns The index of the last occurrence of \p Str, or npos if not |
||
| 365 | /// found. |
||
| 366 | [[nodiscard]] size_t rfind(StringRef Str) const; |
||
| 367 | |||
| 368 | /// Search for the last string \p Str in the string, ignoring case. |
||
| 369 | /// |
||
| 370 | /// \returns The index of the last occurrence of \p Str, or npos if not |
||
| 371 | /// found. |
||
| 372 | [[nodiscard]] size_t rfind_insensitive(StringRef Str) const; |
||
| 373 | |||
| 374 | /// Find the first character in the string that is \p C, or npos if not |
||
| 375 | /// found. Same as find. |
||
| 376 | [[nodiscard]] size_t find_first_of(char C, size_t From = 0) const { |
||
| 377 | return find(C, From); |
||
| 378 | } |
||
| 379 | |||
| 380 | /// Find the first character in the string that is in \p Chars, or npos if |
||
| 381 | /// not found. |
||
| 382 | /// |
||
| 383 | /// Complexity: O(size() + Chars.size()) |
||
| 384 | [[nodiscard]] size_t find_first_of(StringRef Chars, size_t From = 0) const; |
||
| 385 | |||
| 386 | /// Find the first character in the string that is not \p C or npos if not |
||
| 387 | /// found. |
||
| 388 | [[nodiscard]] size_t find_first_not_of(char C, size_t From = 0) const; |
||
| 389 | |||
| 390 | /// Find the first character in the string that is not in the string |
||
| 391 | /// \p Chars, or npos if not found. |
||
| 392 | /// |
||
| 393 | /// Complexity: O(size() + Chars.size()) |
||
| 394 | [[nodiscard]] size_t find_first_not_of(StringRef Chars, |
||
| 395 | size_t From = 0) const; |
||
| 396 | |||
| 397 | /// Find the last character in the string that is \p C, or npos if not |
||
| 398 | /// found. |
||
| 399 | [[nodiscard]] size_t find_last_of(char C, size_t From = npos) const { |
||
| 400 | return rfind(C, From); |
||
| 401 | } |
||
| 402 | |||
| 403 | /// Find the last character in the string that is in \p C, or npos if not |
||
| 404 | /// found. |
||
| 405 | /// |
||
| 406 | /// Complexity: O(size() + Chars.size()) |
||
| 407 | [[nodiscard]] size_t find_last_of(StringRef Chars, |
||
| 408 | size_t From = npos) const; |
||
| 409 | |||
| 410 | /// Find the last character in the string that is not \p C, or npos if not |
||
| 411 | /// found. |
||
| 412 | [[nodiscard]] size_t find_last_not_of(char C, size_t From = npos) const; |
||
| 413 | |||
| 414 | /// Find the last character in the string that is not in \p Chars, or |
||
| 415 | /// npos if not found. |
||
| 416 | /// |
||
| 417 | /// Complexity: O(size() + Chars.size()) |
||
| 418 | [[nodiscard]] size_t find_last_not_of(StringRef Chars, |
||
| 419 | size_t From = npos) const; |
||
| 420 | |||
| 421 | /// Return true if the given string is a substring of *this, and false |
||
| 422 | /// otherwise. |
||
| 423 | [[nodiscard]] bool contains(StringRef Other) const { |
||
| 424 | return find(Other) != npos; |
||
| 425 | } |
||
| 426 | |||
| 427 | /// Return true if the given character is contained in *this, and false |
||
| 428 | /// otherwise. |
||
| 429 | [[nodiscard]] bool contains(char C) const { |
||
| 430 | return find_first_of(C) != npos; |
||
| 431 | } |
||
| 432 | |||
| 433 | /// Return true if the given string is a substring of *this, and false |
||
| 434 | /// otherwise. |
||
| 435 | [[nodiscard]] bool contains_insensitive(StringRef Other) const { |
||
| 436 | return find_insensitive(Other) != npos; |
||
| 437 | } |
||
| 438 | |||
| 439 | /// Return true if the given character is contained in *this, and false |
||
| 440 | /// otherwise. |
||
| 441 | [[nodiscard]] bool contains_insensitive(char C) const { |
||
| 442 | return find_insensitive(C) != npos; |
||
| 443 | } |
||
| 444 | |||
| 445 | /// @} |
||
| 446 | /// @name Helpful Algorithms |
||
| 447 | /// @{ |
||
| 448 | |||
| 449 | /// Return the number of occurrences of \p C in the string. |
||
| 450 | [[nodiscard]] size_t count(char C) const { |
||
| 451 | size_t Count = 0; |
||
| 452 | for (size_t i = 0, e = Length; i != e; ++i) |
||
| 453 | if (Data[i] == C) |
||
| 454 | ++Count; |
||
| 455 | return Count; |
||
| 456 | } |
||
| 457 | |||
| 458 | /// Return the number of non-overlapped occurrences of \p Str in |
||
| 459 | /// the string. |
||
| 460 | size_t count(StringRef Str) const; |
||
| 461 | |||
| 462 | /// Parse the current string as an integer of the specified radix. If |
||
| 463 | /// \p Radix is specified as zero, this does radix autosensing using |
||
| 464 | /// extended C rules: 0 is octal, 0x is hex, 0b is binary. |
||
| 465 | /// |
||
| 466 | /// If the string is invalid or if only a subset of the string is valid, |
||
| 467 | /// this returns true to signify the error. The string is considered |
||
| 468 | /// erroneous if empty or if it overflows T. |
||
| 469 | template <typename T> bool getAsInteger(unsigned Radix, T &Result) const { |
||
| 470 | if constexpr (std::numeric_limits<T>::is_signed) { |
||
| 471 | long long LLVal; |
||
| 472 | if (getAsSignedInteger(*this, Radix, LLVal) || |
||
| 473 | static_cast<T>(LLVal) != LLVal) |
||
| 474 | return true; |
||
| 475 | Result = LLVal; |
||
| 476 | } else { |
||
| 477 | unsigned long long ULLVal; |
||
| 478 | // The additional cast to unsigned long long is required to avoid the |
||
| 479 | // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type |
||
| 480 | // 'unsigned __int64' when instantiating getAsInteger with T = bool. |
||
| 481 | if (getAsUnsignedInteger(*this, Radix, ULLVal) || |
||
| 482 | static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal) |
||
| 483 | return true; |
||
| 484 | Result = ULLVal; |
||
| 485 | } |
||
| 486 | return false; |
||
| 487 | } |
||
| 488 | |||
| 489 | /// Parse the current string as an integer of the specified radix. If |
||
| 490 | /// \p Radix is specified as zero, this does radix autosensing using |
||
| 491 | /// extended C rules: 0 is octal, 0x is hex, 0b is binary. |
||
| 492 | /// |
||
| 493 | /// If the string does not begin with a number of the specified radix, |
||
| 494 | /// this returns true to signify the error. The string is considered |
||
| 495 | /// erroneous if empty or if it overflows T. |
||
| 496 | /// The portion of the string representing the discovered numeric value |
||
| 497 | /// is removed from the beginning of the string. |
||
| 498 | template <typename T> bool consumeInteger(unsigned Radix, T &Result) { |
||
| 499 | if constexpr (std::numeric_limits<T>::is_signed) { |
||
| 500 | long long LLVal; |
||
| 501 | if (consumeSignedInteger(*this, Radix, LLVal) || |
||
| 502 | static_cast<long long>(static_cast<T>(LLVal)) != LLVal) |
||
| 503 | return true; |
||
| 504 | Result = LLVal; |
||
| 505 | } else { |
||
| 506 | unsigned long long ULLVal; |
||
| 507 | if (consumeUnsignedInteger(*this, Radix, ULLVal) || |
||
| 508 | static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal) |
||
| 509 | return true; |
||
| 510 | Result = ULLVal; |
||
| 511 | } |
||
| 512 | return false; |
||
| 513 | } |
||
| 514 | |||
| 515 | /// Parse the current string as an integer of the specified \p Radix, or of |
||
| 516 | /// an autosensed radix if the \p Radix given is 0. The current value in |
||
| 517 | /// \p Result is discarded, and the storage is changed to be wide enough to |
||
| 518 | /// store the parsed integer. |
||
| 519 | /// |
||
| 520 | /// \returns true if the string does not solely consist of a valid |
||
| 521 | /// non-empty number in the appropriate base. |
||
| 522 | /// |
||
| 523 | /// APInt::fromString is superficially similar but assumes the |
||
| 524 | /// string is well-formed in the given radix. |
||
| 525 | bool getAsInteger(unsigned Radix, APInt &Result) const; |
||
| 526 | |||
| 527 | /// Parse the current string as an IEEE double-precision floating |
||
| 528 | /// point value. The string must be a well-formed double. |
||
| 529 | /// |
||
| 530 | /// If \p AllowInexact is false, the function will fail if the string |
||
| 531 | /// cannot be represented exactly. Otherwise, the function only fails |
||
| 532 | /// in case of an overflow or underflow, or an invalid floating point |
||
| 533 | /// representation. |
||
| 534 | bool getAsDouble(double &Result, bool AllowInexact = true) const; |
||
| 535 | |||
| 536 | /// @} |
||
| 537 | /// @name String Operations |
||
| 538 | /// @{ |
||
| 539 | |||
| 540 | // Convert the given ASCII string to lowercase. |
||
| 541 | [[nodiscard]] std::string lower() const; |
||
| 542 | |||
| 543 | /// Convert the given ASCII string to uppercase. |
||
| 544 | [[nodiscard]] std::string upper() const; |
||
| 545 | |||
| 546 | /// @} |
||
| 547 | /// @name Substring Operations |
||
| 548 | /// @{ |
||
| 549 | |||
| 550 | /// Return a reference to the substring from [Start, Start + N). |
||
| 551 | /// |
||
| 552 | /// \param Start The index of the starting character in the substring; if |
||
| 553 | /// the index is npos or greater than the length of the string then the |
||
| 554 | /// empty substring will be returned. |
||
| 555 | /// |
||
| 556 | /// \param N The number of characters to included in the substring. If N |
||
| 557 | /// exceeds the number of characters remaining in the string, the string |
||
| 558 | /// suffix (starting with \p Start) will be returned. |
||
| 559 | [[nodiscard]] constexpr StringRef substr(size_t Start, |
||
| 560 | size_t N = npos) const { |
||
| 561 | Start = std::min(Start, Length); |
||
| 562 | return StringRef(Data + Start, std::min(N, Length - Start)); |
||
| 563 | } |
||
| 564 | |||
| 565 | /// Return a StringRef equal to 'this' but with only the first \p N |
||
| 566 | /// elements remaining. If \p N is greater than the length of the |
||
| 567 | /// string, the entire string is returned. |
||
| 568 | [[nodiscard]] StringRef take_front(size_t N = 1) const { |
||
| 569 | if (N >= size()) |
||
| 570 | return *this; |
||
| 571 | return drop_back(size() - N); |
||
| 572 | } |
||
| 573 | |||
| 574 | /// Return a StringRef equal to 'this' but with only the last \p N |
||
| 575 | /// elements remaining. If \p N is greater than the length of the |
||
| 576 | /// string, the entire string is returned. |
||
| 577 | [[nodiscard]] StringRef take_back(size_t N = 1) const { |
||
| 578 | if (N >= size()) |
||
| 579 | return *this; |
||
| 580 | return drop_front(size() - N); |
||
| 581 | } |
||
| 582 | |||
| 583 | /// Return the longest prefix of 'this' such that every character |
||
| 584 | /// in the prefix satisfies the given predicate. |
||
| 585 | [[nodiscard]] StringRef take_while(function_ref<bool(char)> F) const { |
||
| 586 | return substr(0, find_if_not(F)); |
||
| 587 | } |
||
| 588 | |||
| 589 | /// Return the longest prefix of 'this' such that no character in |
||
| 590 | /// the prefix satisfies the given predicate. |
||
| 591 | [[nodiscard]] StringRef take_until(function_ref<bool(char)> F) const { |
||
| 592 | return substr(0, find_if(F)); |
||
| 593 | } |
||
| 594 | |||
| 595 | /// Return a StringRef equal to 'this' but with the first \p N elements |
||
| 596 | /// dropped. |
||
| 597 | [[nodiscard]] StringRef drop_front(size_t N = 1) const { |
||
| 598 | assert(size() >= N && "Dropping more elements than exist"); |
||
| 599 | return substr(N); |
||
| 600 | } |
||
| 601 | |||
| 602 | /// Return a StringRef equal to 'this' but with the last \p N elements |
||
| 603 | /// dropped. |
||
| 604 | [[nodiscard]] StringRef drop_back(size_t N = 1) const { |
||
| 605 | assert(size() >= N && "Dropping more elements than exist"); |
||
| 606 | return substr(0, size()-N); |
||
| 607 | } |
||
| 608 | |||
| 609 | /// Return a StringRef equal to 'this', but with all characters satisfying |
||
| 610 | /// the given predicate dropped from the beginning of the string. |
||
| 611 | [[nodiscard]] StringRef drop_while(function_ref<bool(char)> F) const { |
||
| 612 | return substr(find_if_not(F)); |
||
| 613 | } |
||
| 614 | |||
| 615 | /// Return a StringRef equal to 'this', but with all characters not |
||
| 616 | /// satisfying the given predicate dropped from the beginning of the string. |
||
| 617 | [[nodiscard]] StringRef drop_until(function_ref<bool(char)> F) const { |
||
| 618 | return substr(find_if(F)); |
||
| 619 | } |
||
| 620 | |||
| 621 | /// Returns true if this StringRef has the given prefix and removes that |
||
| 622 | /// prefix. |
||
| 623 | bool consume_front(StringRef Prefix) { |
||
| 624 | if (!starts_with(Prefix)) |
||
| 625 | return false; |
||
| 626 | |||
| 627 | *this = drop_front(Prefix.size()); |
||
| 628 | return true; |
||
| 629 | } |
||
| 630 | |||
| 631 | /// Returns true if this StringRef has the given prefix, ignoring case, |
||
| 632 | /// and removes that prefix. |
||
| 633 | bool consume_front_insensitive(StringRef Prefix) { |
||
| 634 | if (!startswith_insensitive(Prefix)) |
||
| 635 | return false; |
||
| 636 | |||
| 637 | *this = drop_front(Prefix.size()); |
||
| 638 | return true; |
||
| 639 | } |
||
| 640 | |||
| 641 | /// Returns true if this StringRef has the given suffix and removes that |
||
| 642 | /// suffix. |
||
| 643 | bool consume_back(StringRef Suffix) { |
||
| 644 | if (!ends_with(Suffix)) |
||
| 645 | return false; |
||
| 646 | |||
| 647 | *this = drop_back(Suffix.size()); |
||
| 648 | return true; |
||
| 649 | } |
||
| 650 | |||
| 651 | /// Returns true if this StringRef has the given suffix, ignoring case, |
||
| 652 | /// and removes that suffix. |
||
| 653 | bool consume_back_insensitive(StringRef Suffix) { |
||
| 654 | if (!endswith_insensitive(Suffix)) |
||
| 655 | return false; |
||
| 656 | |||
| 657 | *this = drop_back(Suffix.size()); |
||
| 658 | return true; |
||
| 659 | } |
||
| 660 | |||
| 661 | /// Return a reference to the substring from [Start, End). |
||
| 662 | /// |
||
| 663 | /// \param Start The index of the starting character in the substring; if |
||
| 664 | /// the index is npos or greater than the length of the string then the |
||
| 665 | /// empty substring will be returned. |
||
| 666 | /// |
||
| 667 | /// \param End The index following the last character to include in the |
||
| 668 | /// substring. If this is npos or exceeds the number of characters |
||
| 669 | /// remaining in the string, the string suffix (starting with \p Start) |
||
| 670 | /// will be returned. If this is less than \p Start, an empty string will |
||
| 671 | /// be returned. |
||
| 672 | [[nodiscard]] StringRef slice(size_t Start, size_t End) const { |
||
| 673 | Start = std::min(Start, Length); |
||
| 674 | End = std::min(std::max(Start, End), Length); |
||
| 675 | return StringRef(Data + Start, End - Start); |
||
| 676 | } |
||
| 677 | |||
| 678 | /// Split into two substrings around the first occurrence of a separator |
||
| 679 | /// character. |
||
| 680 | /// |
||
| 681 | /// If \p Separator is in the string, then the result is a pair (LHS, RHS) |
||
| 682 | /// such that (*this == LHS + Separator + RHS) is true and RHS is |
||
| 683 | /// maximal. If \p Separator is not in the string, then the result is a |
||
| 684 | /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). |
||
| 685 | /// |
||
| 686 | /// \param Separator The character to split on. |
||
| 687 | /// \returns The split substrings. |
||
| 688 | [[nodiscard]] std::pair<StringRef, StringRef> split(char Separator) const { |
||
| 689 | return split(StringRef(&Separator, 1)); |
||
| 690 | } |
||
| 691 | |||
| 692 | /// Split into two substrings around the first occurrence of a separator |
||
| 693 | /// string. |
||
| 694 | /// |
||
| 695 | /// If \p Separator is in the string, then the result is a pair (LHS, RHS) |
||
| 696 | /// such that (*this == LHS + Separator + RHS) is true and RHS is |
||
| 697 | /// maximal. If \p Separator is not in the string, then the result is a |
||
| 698 | /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). |
||
| 699 | /// |
||
| 700 | /// \param Separator - The string to split on. |
||
| 701 | /// \return - The split substrings. |
||
| 702 | [[nodiscard]] std::pair<StringRef, StringRef> |
||
| 703 | split(StringRef Separator) const { |
||
| 704 | size_t Idx = find(Separator); |
||
| 705 | if (Idx == npos) |
||
| 706 | return std::make_pair(*this, StringRef()); |
||
| 707 | return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos)); |
||
| 708 | } |
||
| 709 | |||
| 710 | /// Split into two substrings around the last occurrence of a separator |
||
| 711 | /// string. |
||
| 712 | /// |
||
| 713 | /// If \p Separator is in the string, then the result is a pair (LHS, RHS) |
||
| 714 | /// such that (*this == LHS + Separator + RHS) is true and RHS is |
||
| 715 | /// minimal. If \p Separator is not in the string, then the result is a |
||
| 716 | /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). |
||
| 717 | /// |
||
| 718 | /// \param Separator - The string to split on. |
||
| 719 | /// \return - The split substrings. |
||
| 720 | [[nodiscard]] std::pair<StringRef, StringRef> |
||
| 721 | rsplit(StringRef Separator) const { |
||
| 722 | size_t Idx = rfind(Separator); |
||
| 723 | if (Idx == npos) |
||
| 724 | return std::make_pair(*this, StringRef()); |
||
| 725 | return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos)); |
||
| 726 | } |
||
| 727 | |||
| 728 | /// Split into substrings around the occurrences of a separator string. |
||
| 729 | /// |
||
| 730 | /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most |
||
| 731 | /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1 |
||
| 732 | /// elements are added to A. |
||
| 733 | /// If \p KeepEmpty is false, empty strings are not added to \p A. They |
||
| 734 | /// still count when considering \p MaxSplit |
||
| 735 | /// An useful invariant is that |
||
| 736 | /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true |
||
| 737 | /// |
||
| 738 | /// \param A - Where to put the substrings. |
||
| 739 | /// \param Separator - The string to split on. |
||
| 740 | /// \param MaxSplit - The maximum number of times the string is split. |
||
| 741 | /// \param KeepEmpty - True if empty substring should be added. |
||
| 742 | void split(SmallVectorImpl<StringRef> &A, |
||
| 743 | StringRef Separator, int MaxSplit = -1, |
||
| 744 | bool KeepEmpty = true) const; |
||
| 745 | |||
| 746 | /// Split into substrings around the occurrences of a separator character. |
||
| 747 | /// |
||
| 748 | /// Each substring is stored in \p A. If \p MaxSplit is >= 0, at most |
||
| 749 | /// \p MaxSplit splits are done and consequently <= \p MaxSplit + 1 |
||
| 750 | /// elements are added to A. |
||
| 751 | /// If \p KeepEmpty is false, empty strings are not added to \p A. They |
||
| 752 | /// still count when considering \p MaxSplit |
||
| 753 | /// An useful invariant is that |
||
| 754 | /// Separator.join(A) == *this if MaxSplit == -1 and KeepEmpty == true |
||
| 755 | /// |
||
| 756 | /// \param A - Where to put the substrings. |
||
| 757 | /// \param Separator - The string to split on. |
||
| 758 | /// \param MaxSplit - The maximum number of times the string is split. |
||
| 759 | /// \param KeepEmpty - True if empty substring should be added. |
||
| 760 | void split(SmallVectorImpl<StringRef> &A, char Separator, int MaxSplit = -1, |
||
| 761 | bool KeepEmpty = true) const; |
||
| 762 | |||
| 763 | /// Split into two substrings around the last occurrence of a separator |
||
| 764 | /// character. |
||
| 765 | /// |
||
| 766 | /// If \p Separator is in the string, then the result is a pair (LHS, RHS) |
||
| 767 | /// such that (*this == LHS + Separator + RHS) is true and RHS is |
||
| 768 | /// minimal. If \p Separator is not in the string, then the result is a |
||
| 769 | /// pair (LHS, RHS) where (*this == LHS) and (RHS == ""). |
||
| 770 | /// |
||
| 771 | /// \param Separator - The character to split on. |
||
| 772 | /// \return - The split substrings. |
||
| 773 | [[nodiscard]] std::pair<StringRef, StringRef> rsplit(char Separator) const { |
||
| 774 | return rsplit(StringRef(&Separator, 1)); |
||
| 775 | } |
||
| 776 | |||
| 777 | /// Return string with consecutive \p Char characters starting from the |
||
| 778 | /// the left removed. |
||
| 779 | [[nodiscard]] StringRef ltrim(char Char) const { |
||
| 780 | return drop_front(std::min(Length, find_first_not_of(Char))); |
||
| 781 | } |
||
| 782 | |||
| 783 | /// Return string with consecutive characters in \p Chars starting from |
||
| 784 | /// the left removed. |
||
| 785 | [[nodiscard]] StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const { |
||
| 786 | return drop_front(std::min(Length, find_first_not_of(Chars))); |
||
| 787 | } |
||
| 788 | |||
| 789 | /// Return string with consecutive \p Char characters starting from the |
||
| 790 | /// right removed. |
||
| 791 | [[nodiscard]] StringRef rtrim(char Char) const { |
||
| 792 | return drop_back(Length - std::min(Length, find_last_not_of(Char) + 1)); |
||
| 793 | } |
||
| 794 | |||
| 795 | /// Return string with consecutive characters in \p Chars starting from |
||
| 796 | /// the right removed. |
||
| 797 | [[nodiscard]] StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const { |
||
| 798 | return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1)); |
||
| 799 | } |
||
| 800 | |||
| 801 | /// Return string with consecutive \p Char characters starting from the |
||
| 802 | /// left and right removed. |
||
| 803 | [[nodiscard]] StringRef trim(char Char) const { |
||
| 804 | return ltrim(Char).rtrim(Char); |
||
| 805 | } |
||
| 806 | |||
| 807 | /// Return string with consecutive characters in \p Chars starting from |
||
| 808 | /// the left and right removed. |
||
| 809 | [[nodiscard]] StringRef trim(StringRef Chars = " \t\n\v\f\r") const { |
||
| 810 | return ltrim(Chars).rtrim(Chars); |
||
| 811 | } |
||
| 812 | |||
| 813 | /// Detect the line ending style of the string. |
||
| 814 | /// |
||
| 815 | /// If the string contains a line ending, return the line ending character |
||
| 816 | /// sequence that is detected. Otherwise return '\n' for unix line endings. |
||
| 817 | /// |
||
| 818 | /// \return - The line ending character sequence. |
||
| 819 | [[nodiscard]] StringRef detectEOL() const { |
||
| 820 | size_t Pos = find('\r'); |
||
| 821 | if (Pos == npos) { |
||
| 822 | // If there is no carriage return, assume unix |
||
| 823 | return "\n"; |
||
| 824 | } |
||
| 825 | if (Pos + 1 < Length && Data[Pos + 1] == '\n') |
||
| 826 | return "\r\n"; // Windows |
||
| 827 | if (Pos > 0 && Data[Pos - 1] == '\n') |
||
| 828 | return "\n\r"; // You monster! |
||
| 829 | return "\r"; // Classic Mac |
||
| 830 | } |
||
| 831 | /// @} |
||
| 832 | }; |
||
| 833 | |||
| 834 | /// A wrapper around a string literal that serves as a proxy for constructing |
||
| 835 | /// global tables of StringRefs with the length computed at compile time. |
||
| 836 | /// In order to avoid the invocation of a global constructor, StringLiteral |
||
| 837 | /// should *only* be used in a constexpr context, as such: |
||
| 838 | /// |
||
| 839 | /// constexpr StringLiteral S("test"); |
||
| 840 | /// |
||
| 841 | class StringLiteral : public StringRef { |
||
| 842 | private: |
||
| 843 | constexpr StringLiteral(const char *Str, size_t N) : StringRef(Str, N) { |
||
| 844 | } |
||
| 845 | |||
| 846 | public: |
||
| 847 | template <size_t N> |
||
| 848 | constexpr StringLiteral(const char (&Str)[N]) |
||
| 849 | #if defined(__clang__) && __has_attribute(enable_if) |
||
| 850 | #pragma clang diagnostic push |
||
| 851 | #pragma clang diagnostic ignored "-Wgcc-compat" |
||
| 852 | __attribute((enable_if(__builtin_strlen(Str) == N - 1, |
||
| 853 | "invalid string literal"))) |
||
| 854 | #pragma clang diagnostic pop |
||
| 855 | #endif |
||
| 856 | : StringRef(Str, N - 1) { |
||
| 857 | } |
||
| 858 | |||
| 859 | // Explicit construction for strings like "foo\0bar". |
||
| 860 | template <size_t N> |
||
| 861 | static constexpr StringLiteral withInnerNUL(const char (&Str)[N]) { |
||
| 862 | return StringLiteral(Str, N - 1); |
||
| 863 | } |
||
| 864 | }; |
||
| 865 | |||
| 866 | /// @name StringRef Comparison Operators |
||
| 867 | /// @{ |
||
| 868 | |||
| 869 | inline bool operator==(StringRef LHS, StringRef RHS) { |
||
| 870 | return LHS.equals(RHS); |
||
| 871 | } |
||
| 872 | |||
| 873 | inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); } |
||
| 874 | |||
| 875 | inline bool operator<(StringRef LHS, StringRef RHS) { |
||
| 876 | return LHS.compare(RHS) < 0; |
||
| 877 | } |
||
| 878 | |||
| 879 | inline bool operator<=(StringRef LHS, StringRef RHS) { |
||
| 880 | return LHS.compare(RHS) <= 0; |
||
| 881 | } |
||
| 882 | |||
| 883 | inline bool operator>(StringRef LHS, StringRef RHS) { |
||
| 884 | return LHS.compare(RHS) > 0; |
||
| 885 | } |
||
| 886 | |||
| 887 | inline bool operator>=(StringRef LHS, StringRef RHS) { |
||
| 888 | return LHS.compare(RHS) >= 0; |
||
| 889 | } |
||
| 890 | |||
| 891 | inline std::string &operator+=(std::string &buffer, StringRef string) { |
||
| 892 | return buffer.append(string.data(), string.size()); |
||
| 893 | } |
||
| 894 | |||
| 895 | /// @} |
||
| 896 | |||
| 897 | /// Compute a hash_code for a StringRef. |
||
| 898 | [[nodiscard]] hash_code hash_value(StringRef S); |
||
| 899 | |||
| 900 | // Provide DenseMapInfo for StringRefs. |
||
| 901 | template <> struct DenseMapInfo<StringRef, void> { |
||
| 902 | static inline StringRef getEmptyKey() { |
||
| 903 | return StringRef( |
||
| 904 | reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)), 0); |
||
| 905 | } |
||
| 906 | |||
| 907 | static inline StringRef getTombstoneKey() { |
||
| 908 | return StringRef( |
||
| 909 | reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)), 0); |
||
| 910 | } |
||
| 911 | |||
| 912 | static unsigned getHashValue(StringRef Val); |
||
| 913 | |||
| 914 | static bool isEqual(StringRef LHS, StringRef RHS) { |
||
| 915 | if (RHS.data() == getEmptyKey().data()) |
||
| 916 | return LHS.data() == getEmptyKey().data(); |
||
| 917 | if (RHS.data() == getTombstoneKey().data()) |
||
| 918 | return LHS.data() == getTombstoneKey().data(); |
||
| 919 | return LHS == RHS; |
||
| 920 | } |
||
| 921 | }; |
||
| 922 | |||
| 923 | } // end namespace llvm |
||
| 924 | |||
| 925 | #endif // LLVM_ADT_STRINGREF_H |