| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <cstddef> |
| 7 | #include <mos/cpp_support.hpp> |
| 8 | |
| 9 | namespace mos |
| 10 | { |
| 11 | template<typename CharT> |
| 12 | constexpr auto generic_strlen(const CharT *c) |
| 13 | { |
| 14 | if (!c) |
| 15 | return size_t(0); |
| 16 | size_t len = 0; |
| 17 | while (*(c++)) |
| 18 | { |
| 19 | len++; |
| 20 | } |
| 21 | return len; |
| 22 | } |
| 23 | |
| 24 | template<typename CharT> |
| 25 | constexpr auto generic_strnlen(const CharT *c, size_t max) |
| 26 | { |
| 27 | size_t len = 0; |
| 28 | while (len < max && *(c++)) |
| 29 | { |
| 30 | len++; |
| 31 | } |
| 32 | return len; |
| 33 | } |
| 34 | |
| 35 | template<typename CharT> |
| 36 | constexpr auto generic_strncmp(const CharT *a, const CharT *b, size_t n) |
| 37 | { |
| 38 | for (size_t i = 0; i < n; i++) |
| 39 | { |
| 40 | if (a[i] != b[i]) |
| 41 | return a[i] - b[i]; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | template<typename CharT> |
| 47 | class basic_string_view |
| 48 | { |
| 49 | public: |
| 50 | static constexpr auto npos = size_t(-1); |
| 51 | basic_string_view(std::nullptr_t) = delete; |
| 52 | constexpr basic_string_view() : _pointer(nullptr), _length(0) {}; |
| 53 | constexpr basic_string_view(const CharT *cs) : _pointer(cs), _length(generic_strlen(cs)) |
| 54 | { |
| 55 | if (_pointer == nullptr) |
| 56 | mos::__raise_null_pointer_exception(); |
| 57 | } |
| 58 | |
| 59 | constexpr basic_string_view(const CharT *s, size_t length) : _pointer(s), _length(length) |
| 60 | { |
| 61 | if (_pointer == nullptr) |
| 62 | mos::__raise_null_pointer_exception(); |
| 63 | } |
| 64 | |
| 65 | constexpr basic_string_view(const CharT *begin, const CharT *end) : _pointer(begin), _length(end - begin) |
| 66 | { |
| 67 | if (_pointer == nullptr) |
| 68 | mos::__raise_null_pointer_exception(); |
| 69 | } |
| 70 | |
| 71 | const CharT *data() const |
| 72 | { |
| 73 | return _pointer; |
| 74 | } |
| 75 | |
| 76 | const CharT &operator[](size_t index) const |
| 77 | { |
| 78 | return _pointer[index]; |
| 79 | } |
| 80 | |
| 81 | size_t size() const |
| 82 | { |
| 83 | return _length; |
| 84 | } |
| 85 | |
| 86 | bool begins_with(CharT c) const |
| 87 | { |
| 88 | return _length > 0 && _pointer[0] == c; |
| 89 | } |
| 90 | |
| 91 | bool begins_with(basic_string_view str) const |
| 92 | { |
| 93 | if (_length < str._length) |
| 94 | return false; |
| 95 | |
| 96 | return generic_strncmp(_pointer, str._pointer, str._length) == 0; |
| 97 | } |
| 98 | |
| 99 | bool ends_with(CharT c) const |
| 100 | { |
| 101 | return _length > 0 && _pointer[_length - 1] == c; |
| 102 | } |
| 103 | |
| 104 | bool ends_with(basic_string_view str) const |
| 105 | { |
| 106 | if (_length < str._length) |
| 107 | return false; |
| 108 | |
| 109 | return generic_strncmp(_pointer + _length - str._length, str._pointer, str._length) == 0; |
| 110 | } |
| 111 | |
| 112 | constexpr bool empty() const |
| 113 | { |
| 114 | return _length == 0; |
| 115 | } |
| 116 | |
| 117 | bool operator==(basic_string_view other) const |
| 118 | { |
| 119 | if (_length != other._length) |
| 120 | return false; |
| 121 | |
| 122 | return generic_strncmp(_pointer, other._pointer, _length) == 0; |
| 123 | } |
| 124 | |
| 125 | constexpr basic_string_view substr(size_t start, size_t end = -1) const |
| 126 | { |
| 127 | const auto len = std::min<size_t>(a: end, b: _length - start); |
| 128 | return basic_string_view(_pointer + start, len); |
| 129 | } |
| 130 | |
| 131 | constexpr size_t find(CharT c, size_t start = 0) const |
| 132 | { |
| 133 | if (start >= _length) |
| 134 | return npos; |
| 135 | |
| 136 | for (size_t i = start; i < _length; i++) |
| 137 | { |
| 138 | if (_pointer[i] == c) |
| 139 | return i; |
| 140 | } |
| 141 | return npos; |
| 142 | } |
| 143 | |
| 144 | constexpr size_t find(basic_string_view str) const |
| 145 | { |
| 146 | for (size_t i = 0; i < _length; i++) |
| 147 | { |
| 148 | if (generic_strncmp(_pointer + i, str._pointer, str._length) == 0) |
| 149 | return i; |
| 150 | } |
| 151 | return npos; |
| 152 | } |
| 153 | |
| 154 | constexpr bool starts_with(basic_string_view str) const |
| 155 | { |
| 156 | return generic_strncmp(_pointer, str._pointer, str._length) == 0; |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | const CharT *_pointer; |
| 161 | size_t _length; |
| 162 | }; |
| 163 | |
| 164 | typedef basic_string_view<char> string_view; |
| 165 | |
| 166 | namespace string_literals |
| 167 | { |
| 168 | constexpr string_view operator"" _sv(const char *str, size_t len) |
| 169 | { |
| 170 | return string_view(str, len); |
| 171 | } |
| 172 | } // namespace string_literals |
| 173 | } // namespace mos |
| 174 | |