| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file LICENSE.rst or https://cmake.org/licensing for details. */ |
| 3 |
|
| 4 |
#include <cm/string_view> // IWYU pragma: associated |
| 5 |
|
| 6 |
#ifndef CMake_HAVE_CXX_STRING_VIEW |
| 7 |
|
| 8 |
# include <algorithm> |
| 9 |
# include <ostream> |
| 10 |
# include <stdexcept> |
| 11 |
|
| 12 |
# include <cm3p/kwiml/int.h> |
| 13 |
|
| 14 |
namespace cm { |
| 15 |
|
| 16 |
string_view::const_reference string_view::at(size_type pos) const |
| 17 |
{ |
| 18 |
if (pos >= size_) { |
| 19 |
throw std::out_of_range("Index out of range in string_view::at"); |
| 20 |
} |
| 21 |
return data_[pos]; |
| 22 |
} |
| 23 |
|
| 24 |
string_view::size_type string_view::copy(char* dest, size_type count, |
| 25 |
size_type pos) const |
| 26 |
{ |
| 27 |
if (pos > size_) { |
| 28 |
throw std::out_of_range("Index out of range in string_view::copy"); |
| 29 |
} |
| 30 |
size_type const rcount = std::min(count, size_ - pos); |
| 31 |
traits_type::copy(dest, data_ + pos, rcount); |
| 32 |
return rcount; |
| 33 |
} |
| 34 |
|
| 35 |
string_view string_view::substr(size_type pos, size_type count) const |
| 36 |
{ |
| 37 |
if (pos > size_) { |
| 38 |
throw std::out_of_range("Index out of range in string_view::substr"); |
| 39 |
} |
| 40 |
size_type const rcount = std::min(count, size_ - pos); |
| 41 |
return string_view(data_ + pos, rcount); |
| 42 |
} |
| 43 |
|
| 44 |
int string_view::compare(string_view v) const noexcept |
| 45 |
{ |
| 46 |
size_type const rlen = std::min(size_, v.size_); |
| 47 |
int c = traits_type::compare(data_, v.data_, rlen); |
| 48 |
if (c == 0) { |
| 49 |
if (size_ < v.size_) { |
| 50 |
c = -1; |
| 51 |
} else if (size_ > v.size_) { |
| 52 |
c = 1; |
| 53 |
} |
| 54 |
} |
| 55 |
return c; |
| 56 |
} |
| 57 |
|
| 58 |
int string_view::compare(size_type pos1, size_type count1, string_view v) const |
| 59 |
{ |
| 60 |
return substr(pos1, count1).compare(v); |
| 61 |
} |
| 62 |
|
| 63 |
int string_view::compare(size_type pos1, size_type count1, string_view v, |
| 64 |
size_type pos2, size_type count2) const |
| 65 |
{ |
| 66 |
return substr(pos1, count1).compare(v.substr(pos2, count2)); |
| 67 |
} |
| 68 |
|
| 69 |
int string_view::compare(char const* s) const |
| 70 |
{ |
| 71 |
return compare(string_view(s)); |
| 72 |
} |
| 73 |
|
| 74 |
int string_view::compare(size_type pos1, size_type count1, char const* s) const |
| 75 |
{ |
| 76 |
return substr(pos1, count1).compare(string_view(s)); |
| 77 |
} |
| 78 |
|
| 79 |
int string_view::compare(size_type pos1, size_type count1, char const* s, |
| 80 |
size_type count2) const |
| 81 |
{ |
| 82 |
return substr(pos1, count1).compare(string_view(s, count2)); |
| 83 |
} |
| 84 |
|
| 85 |
string_view::size_type string_view::find(string_view v, |
| 86 |
size_type pos) const noexcept |
| 87 |
{ |
| 88 |
if (pos < size_) { |
| 89 |
for (; pos + v.size_ <= size_; ++pos) { |
| 90 |
if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) == |
| 91 |
0) { |
| 92 |
return pos; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
return npos; |
| 97 |
} |
| 98 |
|
| 99 |
string_view::size_type string_view::find(char c, size_type pos) const noexcept |
| 100 |
{ |
| 101 |
return find(string_view(&c, 1), pos); |
| 102 |
} |
| 103 |
|
| 104 |
string_view::size_type string_view::find(char const* s, size_type pos, |
| 105 |
size_type count) const |
| 106 |
{ |
| 107 |
return find(string_view(s, count), pos); |
| 108 |
} |
| 109 |
|
| 110 |
string_view::size_type string_view::find(char const* s, size_type pos) const |
| 111 |
{ |
| 112 |
return find(string_view(s), pos); |
| 113 |
} |
| 114 |
|
| 115 |
string_view::size_type string_view::rfind(string_view v, |
| 116 |
size_type pos) const noexcept |
| 117 |
{ |
| 118 |
if (size_ >= v.size_) { |
| 119 |
for (pos = std::min(pos, size_ - v.size_) + 1; pos > 0;) { |
| 120 |
--pos; |
| 121 |
if (std::char_traits<char>::compare(data_ + pos, v.data_, v.size_) == |
| 122 |
0) { |
| 123 |
return pos; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
return npos; |
| 128 |
} |
| 129 |
|
| 130 |
string_view::size_type string_view::rfind(char c, size_type pos) const noexcept |
| 131 |
{ |
| 132 |
return rfind(string_view(&c, 1), pos); |
| 133 |
} |
| 134 |
|
| 135 |
string_view::size_type string_view::rfind(char const* s, size_type pos, |
| 136 |
size_type count) const |
| 137 |
{ |
| 138 |
return rfind(string_view(s, count), pos); |
| 139 |
} |
| 140 |
|
| 141 |
string_view::size_type string_view::rfind(char const* s, size_type pos) const |
| 142 |
{ |
| 143 |
return rfind(string_view(s), pos); |
| 144 |
} |
| 145 |
|
| 146 |
string_view::size_type string_view::find_first_of(string_view v, |
| 147 |
size_type pos) const noexcept |
| 148 |
{ |
| 149 |
for (; pos < size_; ++pos) { |
| 150 |
if (traits_type::find(v.data_, v.size_, data_[pos])) { |
| 151 |
return pos; |
| 152 |
} |
| 153 |
} |
| 154 |
return npos; |
| 155 |
} |
| 156 |
|
| 157 |
string_view::size_type string_view::find_first_of(char c, |
| 158 |
size_type pos) const noexcept |
| 159 |
{ |
| 160 |
return find_first_of(string_view(&c, 1), pos); |
| 161 |
} |
| 162 |
|
| 163 |
string_view::size_type string_view::find_first_of(char const* s, size_type pos, |
| 164 |
size_type count) const |
| 165 |
{ |
| 166 |
return find_first_of(string_view(s, count), pos); |
| 167 |
} |
| 168 |
|
| 169 |
string_view::size_type string_view::find_first_of(char const* s, |
| 170 |
size_type pos) const |
| 171 |
{ |
| 172 |
return find_first_of(string_view(s), pos); |
| 173 |
} |
| 174 |
|
| 175 |
string_view::size_type string_view::find_last_of(string_view v, |
| 176 |
size_type pos) const noexcept |
| 177 |
{ |
| 178 |
if (size_ > 0) { |
| 179 |
for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) { |
| 180 |
--pos; |
| 181 |
if (traits_type::find(v.data_, v.size_, data_[pos])) { |
| 182 |
return pos; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
return npos; |
| 187 |
} |
| 188 |
|
| 189 |
string_view::size_type string_view::find_last_of(char c, |
| 190 |
size_type pos) const noexcept |
| 191 |
{ |
| 192 |
return find_last_of(string_view(&c, 1), pos); |
| 193 |
} |
| 194 |
|
| 195 |
string_view::size_type string_view::find_last_of(char const* s, size_type pos, |
| 196 |
size_type count) const |
| 197 |
{ |
| 198 |
return find_last_of(string_view(s, count), pos); |
| 199 |
} |
| 200 |
|
| 201 |
string_view::size_type string_view::find_last_of(char const* s, |
| 202 |
size_type pos) const |
| 203 |
{ |
| 204 |
return find_last_of(string_view(s), pos); |
| 205 |
} |
| 206 |
|
| 207 |
string_view::size_type string_view::find_first_not_of( |
| 208 |
string_view v, size_type pos) const noexcept |
| 209 |
{ |
| 210 |
for (; pos < size_; ++pos) { |
| 211 |
if (!traits_type::find(v.data_, v.size_, data_[pos])) { |
| 212 |
return pos; |
| 213 |
} |
| 214 |
} |
| 215 |
return npos; |
| 216 |
} |
| 217 |
|
| 218 |
string_view::size_type string_view::find_first_not_of( |
| 219 |
char c, size_type pos) const noexcept |
| 220 |
{ |
| 221 |
return find_first_not_of(string_view(&c, 1), pos); |
| 222 |
} |
| 223 |
|
| 224 |
string_view::size_type string_view::find_first_not_of(char const* s, |
| 225 |
size_type pos, |
| 226 |
size_type count) const |
| 227 |
{ |
| 228 |
return find_first_not_of(string_view(s, count), pos); |
| 229 |
} |
| 230 |
|
| 231 |
string_view::size_type string_view::find_first_not_of(char const* s, |
| 232 |
size_type pos) const |
| 233 |
{ |
| 234 |
return find_first_not_of(string_view(s), pos); |
| 235 |
} |
| 236 |
|
| 237 |
string_view::size_type string_view::find_last_not_of( |
| 238 |
string_view v, size_type pos) const noexcept |
| 239 |
{ |
| 240 |
if (size_ > 0) { |
| 241 |
for (pos = std::min(pos, size_ - 1) + 1; pos > 0;) { |
| 242 |
--pos; |
| 243 |
if (!traits_type::find(v.data_, v.size_, data_[pos])) { |
| 244 |
return pos; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
return npos; |
| 249 |
} |
| 250 |
|
| 251 |
string_view::size_type string_view::find_last_not_of( |
| 252 |
char c, size_type pos) const noexcept |
| 253 |
{ |
| 254 |
return find_last_not_of(string_view(&c, 1), pos); |
| 255 |
} |
| 256 |
|
| 257 |
string_view::size_type string_view::find_last_not_of(char const* s, |
| 258 |
size_type pos, |
| 259 |
size_type count) const |
| 260 |
{ |
| 261 |
return find_last_not_of(string_view(s, count), pos); |
| 262 |
} |
| 263 |
|
| 264 |
string_view::size_type string_view::find_last_not_of(char const* s, |
| 265 |
size_type pos) const |
| 266 |
{ |
| 267 |
return find_last_not_of(string_view(s), pos); |
| 268 |
} |
| 269 |
|
| 270 |
std::ostream& operator<<(std::ostream& o, string_view v) |
| 271 |
{ |
| 272 |
return o.write(v.data(), v.size()); |
| 273 |
} |
| 274 |
|
| 275 |
std::string& operator+=(std::string& s, string_view v) |
| 276 |
{ |
| 277 |
s.append(v.data(), v.size()); |
| 278 |
return s; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
std::hash<cm::string_view>::result_type std::hash<cm::string_view>::operator()( |
| 283 |
argument_type const& s) const noexcept |
| 284 |
{ |
| 285 |
// FNV-1a hash. |
| 286 |
static KWIML_INT_uint64_t const fnv_offset_basis = 0xcbf29ce484222325; |
| 287 |
static KWIML_INT_uint64_t const fnv_prime = 0x100000001b3; |
| 288 |
KWIML_INT_uint64_t h = fnv_offset_basis; |
| 289 |
for (char const& c : s) { |
| 290 |
h = h ^ KWIML_INT_uint64_t(KWIML_INT_uint8_t(c)); |
| 291 |
h = h * fnv_prime; |
| 292 |
} |
| 293 |
return result_type(h); |
| 294 |
} |
| 295 |
#else |
| 296 |
// Avoid empty translation unit. |
| 297 |
void cm_string_view_cxx() |
| 298 |
{ |
| 299 |
} |
| 300 |
#endif |