| 40 | // but this one allows tight control and optimization of memory layout. |
| 41 | template <uint8_t N> |
| 42 | class SmallString { |
| 43 | public: |
| 44 | SmallString() : length_(0) {} |
| 45 | |
| 46 | template <typename T> |
| 47 | SmallString(const T& v) { // NOLINT implicit constructor |
| 48 | *this = std::string_view(v); |
| 49 | } |
| 50 | |
| 51 | SmallString& operator=(const std::string_view s) { |
| 52 | #ifndef NDEBUG |
| 53 | CheckSize(s.size()); |
| 54 | #endif |
| 55 | length_ = static_cast<uint8_t>(s.size()); |
| 56 | std::memcpy(data_, s.data(), length_); |
| 57 | return *this; |
| 58 | } |
| 59 | |
| 60 | SmallString& operator=(const std::string& s) { |
| 61 | *this = std::string_view(s); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | SmallString& operator=(const char* s) { |
| 66 | *this = std::string_view(s); |
| 67 | return *this; |
| 68 | } |
| 69 | |
| 70 | explicit operator std::string_view() const { return std::string_view(data_, length_); } |
| 71 | |
| 72 | const char* data() const { return data_; } |
| 73 | size_t length() const { return length_; } |
| 74 | bool empty() const { return length_ == 0; } |
| 75 | char operator[](size_t pos) const { |
| 76 | #ifdef NDEBUG |
| 77 | assert(pos <= length_); |
| 78 | #endif |
| 79 | return data_[pos]; |
| 80 | } |
| 81 | |
| 82 | SmallString substr(size_t pos) const { |
| 83 | return SmallString(std::string_view(*this).substr(pos)); |
| 84 | } |
| 85 | |
| 86 | SmallString substr(size_t pos, size_t count) const { |
| 87 | return SmallString(std::string_view(*this).substr(pos, count)); |
| 88 | } |
| 89 | |
| 90 | template <typename T> |
| 91 | bool operator==(T&& other) const { |
| 92 | return std::string_view(*this) == std::string_view(std::forward<T>(other)); |
| 93 | } |
| 94 | |
| 95 | template <typename T> |
| 96 | bool operator!=(T&& other) const { |
| 97 | return std::string_view(*this) != std::string_view(std::forward<T>(other)); |
| 98 | } |
| 99 | |