A non-owning string class (similar to the forthcoming std::string_view) Note that, because a StringRef may be a substring of another string, it may not be null terminated. c_str() must return a null terminated string, however, and so the StringRef will internally take ownership (taking a copy), if necessary. In theory this ownership is not externally visible - but it does mean (substring) StringRe
| 389 | /// visible - but it does mean (substring) StringRefs should not be shared between |
| 390 | /// threads. |
| 391 | class StringRef { |
| 392 | public: |
| 393 | using size_type = std::size_t; |
| 394 | |
| 395 | private: |
| 396 | friend struct StringRefTestAccess; |
| 397 | |
| 398 | char const *m_start; |
| 399 | size_type m_size; |
| 400 | |
| 401 | char *m_data = nullptr; |
| 402 | |
| 403 | void takeOwnership(); |
| 404 | |
| 405 | static constexpr char const *const s_empty = ""; |
| 406 | |
| 407 | public: // construction/ assignment |
| 408 | StringRef() noexcept : StringRef(s_empty, 0) { |
| 409 | } |
| 410 | |
| 411 | StringRef(StringRef const &other) noexcept : m_start(other.m_start), m_size(other.m_size) { |
| 412 | } |
| 413 | |
| 414 | StringRef(StringRef &&other) noexcept : m_start(other.m_start), m_size(other.m_size), m_data(other.m_data) { |
| 415 | other.m_data = nullptr; |
| 416 | } |
| 417 | |
| 418 | StringRef(char const *rawChars) noexcept; |
| 419 | |
| 420 | StringRef(char const *rawChars, size_type size) noexcept : m_start(rawChars), m_size(size) { |
| 421 | } |
| 422 | |
| 423 | StringRef(std::string const &stdString) noexcept : m_start(stdString.c_str()), m_size(stdString.size()) { |
| 424 | } |
| 425 | |
| 426 | ~StringRef() noexcept { |
| 427 | delete[] m_data; |
| 428 | } |
| 429 | |
| 430 | auto operator=(StringRef const &other) noexcept -> StringRef & { |
| 431 | delete[] m_data; |
| 432 | m_data = nullptr; |
| 433 | m_start = other.m_start; |
| 434 | m_size = other.m_size; |
| 435 | return *this; |
| 436 | } |
| 437 | |
| 438 | operator std::string() const; |
| 439 | |
| 440 | void swap(StringRef &other) noexcept; |
| 441 | |
| 442 | public: // operators |
| 443 | auto operator==(StringRef const &other) const noexcept -> bool; |
| 444 | auto operator!=(StringRef const &other) const noexcept -> bool; |
| 445 | |
| 446 | auto operator[](size_type index) const noexcept -> char; |
| 447 | |
| 448 | public: // named queries |
no outgoing calls