[52d025c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_STRING
|
---|
| 30 | #define LIBCPP_STRING
|
---|
| 31 |
|
---|
[dc0fff11] | 32 | #include <algorithm>
|
---|
[52d025c] | 33 | #include <iosfwd>
|
---|
[dc0fff11] | 34 | #include <iterator>
|
---|
[52d025c] | 35 | #include <cstdio>
|
---|
| 36 | #include <cstdlib>
|
---|
| 37 | #include <cstring>
|
---|
[dc0fff11] | 38 | #include <memory>
|
---|
| 39 | #include <utility>
|
---|
| 40 |
|
---|
| 41 | #include <initializer_list>
|
---|
[52d025c] | 42 |
|
---|
| 43 | namespace std
|
---|
| 44 | {
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * 21.2, char_traits:
|
---|
| 48 | */
|
---|
| 49 |
|
---|
| 50 | template<class Char>
|
---|
| 51 | struct char_traits;
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * 21.2.3, char_traits specializations:
|
---|
| 55 | */
|
---|
| 56 |
|
---|
| 57 | template<>
|
---|
| 58 | struct char_traits<char>
|
---|
| 59 | {
|
---|
| 60 | using char_type = char;
|
---|
| 61 | using int_type = int;
|
---|
| 62 | using off_type = streamoff;
|
---|
| 63 | using pos_type = streampos;
|
---|
| 64 | /* using state_type = mbstate_t; */
|
---|
| 65 |
|
---|
[2d302d6] | 66 | static void assign(char_type& c1, const char_type& c2) noexcept
|
---|
[52d025c] | 67 | {
|
---|
| 68 | c1 = c2;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | static constexpr bool eq(char_type c1, char_type c2) noexcept
|
---|
| 72 | {
|
---|
| 73 | return c1 == c2;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | static constexpr bool lt(char_type c1, char_type c2) noexcept
|
---|
| 77 | {
|
---|
| 78 | return c1 < c2;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | static int compare(const char_type* s1, const char_type* s2, size_t n)
|
---|
| 82 | {
|
---|
| 83 | return std::str_lcmp(s1, s2, n);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static size_t length(const char_type* s)
|
---|
| 87 | {
|
---|
[2d302d6] | 88 | return std::str_size(s);
|
---|
[52d025c] | 89 | }
|
---|
| 90 |
|
---|
| 91 | static const char_type* find(const char_type* s, size_t n, const char_type& c)
|
---|
| 92 | {
|
---|
| 93 | size_t i{};
|
---|
| 94 | while (i++ < n)
|
---|
| 95 | {
|
---|
| 96 | if (s[i] == c)
|
---|
| 97 | return s + i;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return nullptr;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | static char_type* move(char_type* s1, const char_type* s2, size_t n)
|
---|
| 104 | {
|
---|
| 105 | return static_cast<char_type*>(memmove(s1, s2, n));
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static char_type* copy(char_type* s1, const char_type* s2, size_t n)
|
---|
| 109 | {
|
---|
| 110 | return static_cast<char_type*>(memcpy(s1, s2, n));
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static char_type* assign(char_type* s, size_t n, char_type c)
|
---|
| 114 | {
|
---|
| 115 | /**
|
---|
| 116 | * Note: Even though memset accepts int as its second argument,
|
---|
| 117 | * the actual implementation assigns that int to a dereferenced
|
---|
| 118 | * char pointer.
|
---|
| 119 | */
|
---|
| 120 | return static_cast<char_type*>(memset(s, static_cast<int>(c), n));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | static constexpr int_type not_eof(int_type c) noexcept
|
---|
| 124 | {
|
---|
| 125 | if (!eq_int_type(c, eof()))
|
---|
| 126 | return c;
|
---|
| 127 | else
|
---|
| 128 | return to_int_type('a'); // We just need something that is not eof.
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static constexpr char_type to_char_type(int_type c) noexcept
|
---|
| 132 | {
|
---|
| 133 | return static_cast<char_type>(c);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static constexpr int_type to_int_type(char_type c) noexcept
|
---|
| 137 | {
|
---|
| 138 | return static_cast<int_type>(c);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
|
---|
| 142 | {
|
---|
| 143 | return c1 == c2;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | static constexpr int_type eof() noexcept
|
---|
| 147 | {
|
---|
| 148 | return static_cast<int_type>(EOF);
|
---|
| 149 | }
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | template<>
|
---|
| 153 | struct char_traits<char16_t>
|
---|
[82b6716] | 154 | {
|
---|
| 155 | // TODO: implement
|
---|
| 156 | using char_type = char16_t;
|
---|
| 157 | using int_type = int16_t;
|
---|
| 158 | using off_type = streamoff;
|
---|
| 159 | using pos_type = streampos;
|
---|
| 160 | /* using state_type = mbstate_t; */
|
---|
| 161 |
|
---|
| 162 | static void assign(char_type& c1, const char_type& c2) noexcept
|
---|
| 163 | {
|
---|
| 164 | c1 = c2;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | static constexpr bool eq(char_type c1, char_type c2) noexcept
|
---|
| 168 | {
|
---|
| 169 | return c1 == c2;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | static constexpr bool lt(char_type c1, char_type c2) noexcept
|
---|
| 173 | {
|
---|
| 174 | return c1 < c2;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static int compare(const char_type* s1, const char_type* s2, size_t n)
|
---|
| 178 | {
|
---|
| 179 | // TODO: implement
|
---|
| 180 | return 0;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | static size_t length(const char_type* s)
|
---|
| 184 | {
|
---|
| 185 | // TODO: implement
|
---|
| 186 | return 0;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | static const char_type* find(const char_type* s, size_t n, const char_type& c)
|
---|
| 190 | {
|
---|
| 191 | // TODO: implement
|
---|
| 192 | return nullptr;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | static char_type* move(char_type* s1, const char_type* s2, size_t n)
|
---|
| 196 | {
|
---|
| 197 | // TODO: implement
|
---|
| 198 | return nullptr;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | static char_type* copy(char_type* s1, const char_type* s2, size_t n)
|
---|
| 202 | {
|
---|
| 203 | // TODO: implement
|
---|
| 204 | return nullptr;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | static char_type* assign(char_type* s, size_t n, char_type c)
|
---|
| 208 | {
|
---|
| 209 | // TODO: implement
|
---|
| 210 | return nullptr;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | static constexpr int_type not_eof(int_type c) noexcept
|
---|
| 214 | {
|
---|
| 215 | // TODO: implement
|
---|
| 216 | return int_type{};
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | static constexpr char_type to_char_type(int_type c) noexcept
|
---|
| 220 | {
|
---|
| 221 | return static_cast<char_type>(c);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | static constexpr int_type to_int_type(char_type c) noexcept
|
---|
| 225 | {
|
---|
| 226 | return static_cast<int_type>(c);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
|
---|
| 230 | {
|
---|
| 231 | return c1 == c2;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | static constexpr int_type eof() noexcept
|
---|
| 235 | {
|
---|
| 236 | return static_cast<int_type>(EOF);
|
---|
| 237 | }
|
---|
| 238 | };
|
---|
[52d025c] | 239 |
|
---|
| 240 | template<>
|
---|
| 241 | struct char_traits<char32_t>
|
---|
[82b6716] | 242 | {
|
---|
| 243 | // TODO: implement
|
---|
| 244 | using char_type = char32_t;
|
---|
| 245 | using int_type = int32_t;
|
---|
| 246 | using off_type = streamoff;
|
---|
| 247 | using pos_type = streampos;
|
---|
| 248 | /* using state_type = mbstate_t; */
|
---|
| 249 |
|
---|
| 250 | static void assign(char_type& c1, const char_type& c2) noexcept
|
---|
| 251 | {
|
---|
| 252 | c1 = c2;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | static constexpr bool eq(char_type c1, char_type c2) noexcept
|
---|
| 256 | {
|
---|
| 257 | return c1 == c2;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | static constexpr bool lt(char_type c1, char_type c2) noexcept
|
---|
| 261 | {
|
---|
| 262 | return c1 < c2;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | static int compare(const char_type* s1, const char_type* s2, size_t n)
|
---|
| 266 | {
|
---|
| 267 | // TODO: implement
|
---|
| 268 | return 0;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | static size_t length(const char_type* s)
|
---|
| 272 | {
|
---|
| 273 | // TODO: implement
|
---|
| 274 | return 0;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | static const char_type* find(const char_type* s, size_t n, const char_type& c)
|
---|
| 278 | {
|
---|
| 279 | // TODO: implement
|
---|
| 280 | return nullptr;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | static char_type* move(char_type* s1, const char_type* s2, size_t n)
|
---|
| 284 | {
|
---|
| 285 | // TODO: implement
|
---|
| 286 | return nullptr;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | static char_type* copy(char_type* s1, const char_type* s2, size_t n)
|
---|
| 290 | {
|
---|
| 291 | // TODO: implement
|
---|
| 292 | return nullptr;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | static char_type* assign(char_type* s, size_t n, char_type c)
|
---|
| 296 | {
|
---|
| 297 | // TODO: implement
|
---|
| 298 | return nullptr;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | static constexpr int_type not_eof(int_type c) noexcept
|
---|
| 302 | {
|
---|
| 303 | // TODO: implement
|
---|
| 304 | return int_type{};
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | static constexpr char_type to_char_type(int_type c) noexcept
|
---|
| 308 | {
|
---|
| 309 | return static_cast<char_type>(c);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | static constexpr int_type to_int_type(char_type c) noexcept
|
---|
| 313 | {
|
---|
| 314 | return static_cast<int_type>(c);
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
|
---|
| 318 | {
|
---|
| 319 | return c1 == c2;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | static constexpr int_type eof() noexcept
|
---|
| 323 | {
|
---|
| 324 | return static_cast<int_type>(EOF);
|
---|
| 325 | }
|
---|
| 326 | };
|
---|
[52d025c] | 327 |
|
---|
| 328 | template<>
|
---|
| 329 | struct char_traits<wchar_t>
|
---|
| 330 | {
|
---|
| 331 | using char_type = wchar_t;
|
---|
| 332 | using int_type = wint_t;
|
---|
| 333 | using off_type = streamoff;
|
---|
| 334 | using pos_type = wstreampos;
|
---|
| 335 | /* using state_type = mbstate_t; */
|
---|
| 336 |
|
---|
[2d302d6] | 337 | static void assign(char_type& c1, const char_type& c2) noexcept
|
---|
[52d025c] | 338 | {
|
---|
| 339 | c1 = c2;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | static constexpr bool eq(char_type c1, char_type c2) noexcept
|
---|
| 343 | {
|
---|
| 344 | return c1 == c2;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | static constexpr bool lt(char_type c1, char_type c2) noexcept
|
---|
| 348 | {
|
---|
| 349 | return c1 < c2;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | static int compare(const char_type* s1, const char_type* s2, size_t n)
|
---|
| 353 | {
|
---|
[dc0fff11] | 354 | // TODO: This function does not exits...
|
---|
| 355 | //return std::wstr_lcmp(s1, s2, n);
|
---|
| 356 | return 0;
|
---|
[52d025c] | 357 | }
|
---|
| 358 |
|
---|
| 359 | static size_t length(const char_type* s)
|
---|
| 360 | {
|
---|
[2d302d6] | 361 | return std::wstr_size(s);
|
---|
[52d025c] | 362 | }
|
---|
| 363 |
|
---|
| 364 | static const char_type* find(const char_type* s, size_t n, const char_type& c)
|
---|
| 365 | {
|
---|
| 366 | size_t i{};
|
---|
| 367 | while (i++ < n)
|
---|
| 368 | {
|
---|
| 369 | if (s[i] == c)
|
---|
| 370 | return s + i;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | return nullptr;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | static char_type* move(char_type* s1, const char_type* s2, size_t n)
|
---|
| 377 | {
|
---|
| 378 | return static_cast<char_type*>(memmove(s1, s2, n * sizeof(wchar_t)));
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | static char_type* copy(char_type* s1, const char_type* s2, size_t n)
|
---|
| 382 | {
|
---|
| 383 | return static_cast<char_type*>(memcpy(s1, s2, n * sizeof(wchar_t)));
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | static char_type* assign(char_type* s, size_t n, char_type c)
|
---|
| 387 | {
|
---|
| 388 | return static_cast<char_type*>(memset(s, static_cast<int>(c), n * sizeof(wchar_t)));
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | static constexpr int_type not_eof(int_type c) noexcept
|
---|
| 392 | {
|
---|
| 393 | if (!eq_int_type(c, eof()))
|
---|
| 394 | return c;
|
---|
| 395 | else
|
---|
| 396 | return to_int_type(L'a'); // We just need something that is not eof.
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | static constexpr char_type to_char_type(int_type c) noexcept
|
---|
| 400 | {
|
---|
| 401 | return static_cast<char_type>(c);
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | static constexpr int_type to_int_type(char_type c) noexcept
|
---|
| 405 | {
|
---|
| 406 | return static_cast<int_type>(c);
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept
|
---|
| 410 | {
|
---|
| 411 | return c1 == c2;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | static constexpr int_type eof() noexcept
|
---|
| 415 | {
|
---|
| 416 | return static_cast<int_type>(EOF);
|
---|
| 417 | }
|
---|
| 418 | };
|
---|
| 419 |
|
---|
| 420 | /**
|
---|
| 421 | * 21.4, class template basic_string:
|
---|
| 422 | */
|
---|
| 423 |
|
---|
| 424 | template<class Char, class Traits = char_traits<Char>,
|
---|
| 425 | class Allocator = allocator<Char>>
|
---|
| 426 | class basic_string
|
---|
| 427 | {
|
---|
[177a576] | 428 | public:
|
---|
| 429 | using traits_type = Traits;
|
---|
| 430 | using value_type = typename traits_type::char_type;
|
---|
| 431 | using allocator_type = Allocator;
|
---|
| 432 | using size_type = typename allocator_traits<allocator_type>::size_type;
|
---|
| 433 | using difference_type = typename allocator_traits<allocator_type>::difference_type;
|
---|
[52d025c] | 434 |
|
---|
[177a576] | 435 | using reference = value_type&;
|
---|
| 436 | using const_reference = const value_type&;
|
---|
[dc0fff11] | 437 | using pointer = typename allocator_traits<allocator_type>::pointer;
|
---|
| 438 | using const_pointer = typename allocator_traits<allocator_type>::const_pointer;
|
---|
[52d025c] | 439 |
|
---|
[177a576] | 440 | using iterator = pointer;
|
---|
| 441 | using const_iterator = const_pointer;
|
---|
| 442 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
| 443 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
---|
[52d025c] | 444 |
|
---|
[177a576] | 445 | static constexpr size_type npos = -1;
|
---|
[52d025c] | 446 |
|
---|
[177a576] | 447 | /**
|
---|
| 448 | * 21.4.2, construct/copy/destroy:
|
---|
| 449 | */
|
---|
| 450 | basic_string() noexcept
|
---|
| 451 | : basic_string(allocator_type{})
|
---|
| 452 | { /* DUMMY BODY */ }
|
---|
[52d025c] | 453 |
|
---|
[681fdcca] | 454 | explicit basic_string(const allocator_type& alloc)
|
---|
| 455 | : data_{}, size_{}, capacity_{}, allocator_{alloc}
|
---|
| 456 | {
|
---|
| 457 | /**
|
---|
| 458 | * Postconditions:
|
---|
| 459 | * data() = non-null copyable value that can have 0 added to it.
|
---|
| 460 | * size() = 0
|
---|
| 461 | * capacity() = unspecified
|
---|
| 462 | */
|
---|
[dc0fff11] | 463 | data_ = allocator_.allocate(default_capacity_);
|
---|
| 464 | capacity_ = default_capacity_;
|
---|
[681fdcca] | 465 | }
|
---|
[52d025c] | 466 |
|
---|
[681fdcca] | 467 | basic_string(const basic_string& other)
|
---|
| 468 | : data_{}, size_{other.size_}, capacity_{other.capacity_},
|
---|
| 469 | allocator_{other.allocator_}
|
---|
| 470 | {
|
---|
| 471 | init_(other.data(), size_);
|
---|
| 472 | }
|
---|
[52d025c] | 473 |
|
---|
[681fdcca] | 474 | basic_string(basic_string&& other)
|
---|
| 475 | : data_{other.data_}, size_{other.size_},
|
---|
| 476 | capacity_{other.capacity_}, allocator_{move(other.allocator_)}
|
---|
| 477 | {
|
---|
| 478 | other.data_ = nullptr;
|
---|
| 479 | other.size_ = 0;
|
---|
| 480 | other.capacity_ = 0;
|
---|
| 481 | }
|
---|
[52d025c] | 482 |
|
---|
[177a576] | 483 | basic_string(const basic_string& other, size_type pos, size_type n = npos,
|
---|
[681fdcca] | 484 | const allocator_type& alloc = allocator_type{})
|
---|
| 485 | : data_{}, size_{}, capacity_{}, allocator_{alloc}
|
---|
| 486 | {
|
---|
| 487 | // TODO: if pos < other.size() throw out_of_range.
|
---|
| 488 | auto len = min(n, other.size() - pos);
|
---|
| 489 | init_(other.data() + pos, len);
|
---|
| 490 | }
|
---|
[52d025c] | 491 |
|
---|
[dc0fff11] | 492 | basic_string(const value_type* str, size_type n, const allocator_type& alloc = allocator_type{})
|
---|
[681fdcca] | 493 | : data_{}, size_{n}, capacity_{n}, allocator_{alloc}
|
---|
| 494 | {
|
---|
| 495 | init_(str, size_);
|
---|
| 496 | }
|
---|
[52d025c] | 497 |
|
---|
[dc0fff11] | 498 | basic_string(const value_type* str, const allocator_type& alloc = allocator_type{})
|
---|
[681fdcca] | 499 | : data_{}, size_{}, capacity_{}, allocator_{alloc}
|
---|
| 500 | {
|
---|
[ed81b1f] | 501 | init_(str, traits_type::length(str));
|
---|
[681fdcca] | 502 | }
|
---|
[52d025c] | 503 |
|
---|
[dc0fff11] | 504 | basic_string(size_type n, value_type c, const allocator_type& alloc = allocator_type{})
|
---|
[681fdcca] | 505 | : data_{}, size_{n}, capacity_{n}, allocator_{alloc}
|
---|
| 506 | {
|
---|
| 507 | data_ = allocator_.allocate(capacity_);
|
---|
| 508 | for (size_type i = 0; i < size_; ++i)
|
---|
[53e8686] | 509 | traits_type::assign(data_[i], c);
|
---|
[681fdcca] | 510 | ensure_null_terminator_();
|
---|
| 511 | }
|
---|
[52d025c] | 512 |
|
---|
[177a576] | 513 | template<class InputIterator>
|
---|
| 514 | basic_string(InputIterator first, InputIterator last,
|
---|
[dc0fff11] | 515 | const allocator_type& alloc = allocator_type{})
|
---|
[681fdcca] | 516 | : data_{}, size_{}, capacity_{}, allocator_{alloc}
|
---|
| 517 | {
|
---|
[dc0fff11] | 518 | if constexpr (is_integral<InputIterator>::value)
|
---|
[681fdcca] | 519 | { // Required by the standard.
|
---|
| 520 | size_ = static_cast<size_type>(first);
|
---|
| 521 | capacity_ = size_;
|
---|
| 522 | data_ = allocator_.allocate(capacity_);
|
---|
| 523 |
|
---|
| 524 | for (size_type i = 0; i < size_; ++i)
|
---|
| 525 | traits_type::assign(data_[i], static_cast<value_type>(last));
|
---|
| 526 | ensure_null_terminator_();
|
---|
| 527 | }
|
---|
| 528 | else
|
---|
| 529 | {
|
---|
| 530 | auto len = static_cast<size_type>(last - first);
|
---|
[ed81b1f] | 531 | init_(first, len);
|
---|
[681fdcca] | 532 | }
|
---|
| 533 | }
|
---|
[52d025c] | 534 |
|
---|
[dc0fff11] | 535 | basic_string(initializer_list<value_type> init, const allocator_type& alloc = allocator_type{})
|
---|
[681fdcca] | 536 | : basic_string{init.begin(), init.size(), alloc}
|
---|
| 537 | { /* DUMMY BODY */ }
|
---|
[52d025c] | 538 |
|
---|
[681fdcca] | 539 | basic_string(const basic_string& other, const allocator_type& alloc)
|
---|
| 540 | : data_{}, size_{other.size_}, capacity_{other.capacity_}, allocator_{alloc}
|
---|
| 541 | {
|
---|
| 542 | init_(other.data(), size_);
|
---|
| 543 | }
|
---|
[52d025c] | 544 |
|
---|
[681fdcca] | 545 | basic_string(basic_string&& other, const allocator_type& alloc)
|
---|
| 546 | : data_{other.data_}, size_{other.size_}, capacity_{other.capacity_}, allocator_{alloc}
|
---|
| 547 | {
|
---|
| 548 | other.data_ = nullptr;
|
---|
| 549 | other.size_ = 0;
|
---|
| 550 | other.capacity_ = 0;
|
---|
| 551 | }
|
---|
[52d025c] | 552 |
|
---|
[681fdcca] | 553 | ~basic_string()
|
---|
| 554 | {
|
---|
| 555 | allocator_.deallocate(data_, capacity_);
|
---|
| 556 | }
|
---|
[52d025c] | 557 |
|
---|
[681fdcca] | 558 | basic_string& operator=(const basic_string& other)
|
---|
| 559 | {
|
---|
| 560 | if (this != &other)
|
---|
| 561 | {
|
---|
| 562 | basic_string tmp{other};
|
---|
| 563 | swap(tmp);
|
---|
| 564 | }
|
---|
[fbec99a] | 565 |
|
---|
| 566 | return *this;
|
---|
[681fdcca] | 567 | }
|
---|
[52d025c] | 568 |
|
---|
[177a576] | 569 | basic_string& operator=(basic_string&& other)
|
---|
| 570 | noexcept(allocator_traits<allocator_type>::propagate_on_container_move_assignment::value ||
|
---|
[681fdcca] | 571 | allocator_traits<allocator_type>::is_always_equal::value)
|
---|
| 572 | {
|
---|
| 573 | if (this != &other)
|
---|
| 574 | swap(other);
|
---|
[fbec99a] | 575 |
|
---|
| 576 | return *this;
|
---|
[681fdcca] | 577 | }
|
---|
[52d025c] | 578 |
|
---|
[681fdcca] | 579 | basic_string& operator=(const value_type* other)
|
---|
| 580 | {
|
---|
| 581 | *this = basic_string{other};
|
---|
[52d025c] | 582 |
|
---|
[681fdcca] | 583 | return *this;
|
---|
| 584 | }
|
---|
[52d025c] | 585 |
|
---|
[681fdcca] | 586 | basic_string& operator=(value_type c)
|
---|
| 587 | {
|
---|
| 588 | *this = basic_string{1, c};
|
---|
| 589 |
|
---|
| 590 | return *this;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | basic_string& operator=(initializer_list<value_type> init)
|
---|
| 594 | {
|
---|
| 595 | *this = basic_string{init};
|
---|
| 596 |
|
---|
| 597 | return *this;
|
---|
| 598 | }
|
---|
[52d025c] | 599 |
|
---|
[177a576] | 600 | /**
|
---|
| 601 | * 21.4.3, iterators:
|
---|
| 602 | */
|
---|
[52d025c] | 603 |
|
---|
[b08a62c] | 604 | iterator begin() noexcept
|
---|
| 605 | {
|
---|
| 606 | return &data_[0];
|
---|
| 607 | }
|
---|
[52d025c] | 608 |
|
---|
[b08a62c] | 609 | const_iterator begin() const noexcept
|
---|
| 610 | {
|
---|
| 611 | return &data_[0];
|
---|
| 612 | }
|
---|
[52d025c] | 613 |
|
---|
[b08a62c] | 614 | iterator end() noexcept
|
---|
| 615 | {
|
---|
| 616 | return begin() + size_;
|
---|
| 617 | }
|
---|
[52d025c] | 618 |
|
---|
[b08a62c] | 619 | const_iterator end() const noexcept
|
---|
| 620 | {
|
---|
| 621 | return begin() + size_;
|
---|
| 622 | }
|
---|
[52d025c] | 623 |
|
---|
[177a576] | 624 | reverse_iterator rbegin() noexcept
|
---|
| 625 | {
|
---|
[98c99ba] | 626 | return make_reverse_iterator(end());
|
---|
[177a576] | 627 | }
|
---|
[52d025c] | 628 |
|
---|
[177a576] | 629 | const_reverse_iterator rbegin() const noexcept
|
---|
| 630 | {
|
---|
[98c99ba] | 631 | return make_reverse_iterator(cend());
|
---|
[177a576] | 632 | }
|
---|
[52d025c] | 633 |
|
---|
[177a576] | 634 | reverse_iterator rend() noexcept
|
---|
| 635 | {
|
---|
[98c99ba] | 636 | return make_reverse_iterator(begin());
|
---|
[177a576] | 637 | }
|
---|
[52d025c] | 638 |
|
---|
[177a576] | 639 | const_reverse_iterator rend() const noexcept
|
---|
| 640 | {
|
---|
[98c99ba] | 641 | return make_reverse_iterator(cbegin());
|
---|
[177a576] | 642 | }
|
---|
[52d025c] | 643 |
|
---|
[b08a62c] | 644 | const_iterator cbegin() const noexcept
|
---|
| 645 | {
|
---|
| 646 | return &data_[0];
|
---|
| 647 | }
|
---|
[52d025c] | 648 |
|
---|
[b08a62c] | 649 | const_iterator cend() const noexcept
|
---|
| 650 | {
|
---|
| 651 | return cbegin() + size_;
|
---|
| 652 | }
|
---|
[52d025c] | 653 |
|
---|
[177a576] | 654 | const_reverse_iterator crbegin() const noexcept
|
---|
| 655 | {
|
---|
[98c99ba] | 656 | return rbegin();
|
---|
[177a576] | 657 | }
|
---|
[52d025c] | 658 |
|
---|
[177a576] | 659 | const_reverse_iterator crend() const noexcept
|
---|
| 660 | {
|
---|
[98c99ba] | 661 | return rend();
|
---|
[177a576] | 662 | }
|
---|
[52d025c] | 663 |
|
---|
[177a576] | 664 | /**
|
---|
| 665 | * 21.4.4, capacity:
|
---|
| 666 | */
|
---|
[52d025c] | 667 |
|
---|
[b08a62c] | 668 | size_type size() const noexcept
|
---|
| 669 | {
|
---|
| 670 | return size_;
|
---|
| 671 | }
|
---|
[52d025c] | 672 |
|
---|
[b08a62c] | 673 | size_type length() const noexcept
|
---|
| 674 | {
|
---|
[ed81b1f] | 675 | return size();
|
---|
[b08a62c] | 676 | }
|
---|
[52d025c] | 677 |
|
---|
[b08a62c] | 678 | size_type max_size() const noexcept
|
---|
| 679 | {
|
---|
| 680 | return allocator_traits<allocator_type>::max_size(allocator_);
|
---|
| 681 | }
|
---|
[52d025c] | 682 |
|
---|
[681fdcca] | 683 | void resize(size_type new_size, value_type c)
|
---|
| 684 | {
|
---|
| 685 | // TODO: if new_size > max_size() throw length_error.
|
---|
| 686 | if (new_size > size_)
|
---|
| 687 | {
|
---|
| 688 | ensure_free_space_(new_size - size_ + 1);
|
---|
| 689 | for (size_type i = size_; i < new_size; ++i)
|
---|
| 690 | traits_type::assign(data_[i], i);
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | size_ = new_size;
|
---|
| 694 | ensure_null_terminator_();
|
---|
| 695 | }
|
---|
[52d025c] | 696 |
|
---|
[681fdcca] | 697 | void resize(size_type new_size)
|
---|
| 698 | {
|
---|
| 699 | resize(new_size, value_type{});
|
---|
| 700 | }
|
---|
[52d025c] | 701 |
|
---|
[b08a62c] | 702 | size_type capacity() const noexcept
|
---|
| 703 | {
|
---|
| 704 | return capacity_;
|
---|
| 705 | }
|
---|
[52d025c] | 706 |
|
---|
[681fdcca] | 707 | void reserve(size_type new_capacity = 0)
|
---|
| 708 | {
|
---|
| 709 | // TODO: if new_capacity > max_size() throw
|
---|
| 710 | // length_error (this function shall have no
|
---|
| 711 | // effect in such case)
|
---|
| 712 | if (new_capacity > capacity_)
|
---|
| 713 | resize_with_copy_(size_, new_capacity);
|
---|
| 714 | else if (new_capacity < capacity)
|
---|
| 715 | shrink_to_fit(); // Non-binding request, but why not.
|
---|
| 716 | }
|
---|
[52d025c] | 717 |
|
---|
[681fdcca] | 718 | void shrink_to_fit()
|
---|
| 719 | {
|
---|
| 720 | if (size_ != capacity_)
|
---|
| 721 | resize_with_copy_(size_);
|
---|
| 722 | }
|
---|
[52d025c] | 723 |
|
---|
[681fdcca] | 724 | void clear() noexcept
|
---|
| 725 | {
|
---|
| 726 | size_ = 0;
|
---|
| 727 | }
|
---|
[52d025c] | 728 |
|
---|
[b08a62c] | 729 | bool empty() const noexcept
|
---|
| 730 | {
|
---|
| 731 | return size_ == 0;
|
---|
| 732 | }
|
---|
[52d025c] | 733 |
|
---|
[177a576] | 734 | /**
|
---|
| 735 | * 21.4.5, element access:
|
---|
| 736 | */
|
---|
[52d025c] | 737 |
|
---|
[b08a62c] | 738 | const_reference operator[](size_type idx) const
|
---|
| 739 | {
|
---|
| 740 | return data_[idx];
|
---|
| 741 | }
|
---|
[52d025c] | 742 |
|
---|
[b08a62c] | 743 | reference operator[](size_type idx)
|
---|
| 744 | {
|
---|
| 745 | return data_[idx];
|
---|
| 746 | }
|
---|
[52d025c] | 747 |
|
---|
[b08a62c] | 748 | const_reference at(size_type idx) const
|
---|
| 749 | {
|
---|
| 750 | // TODO: bounds checking
|
---|
| 751 | return data_[idx];
|
---|
| 752 | }
|
---|
[52d025c] | 753 |
|
---|
[b08a62c] | 754 | reference at(size_type idx)
|
---|
| 755 | {
|
---|
| 756 | // TODO: bounds checking
|
---|
| 757 | return data_[idx];
|
---|
| 758 | }
|
---|
[52d025c] | 759 |
|
---|
[b08a62c] | 760 | const_reference front() const
|
---|
| 761 | {
|
---|
| 762 | return at(0);
|
---|
| 763 | }
|
---|
[52d025c] | 764 |
|
---|
[b08a62c] | 765 | reference front()
|
---|
| 766 | {
|
---|
| 767 | return at(0);
|
---|
| 768 | }
|
---|
[52d025c] | 769 |
|
---|
[b08a62c] | 770 | const_reference back() const
|
---|
| 771 | {
|
---|
| 772 | return at(size_ - 1);
|
---|
| 773 | }
|
---|
[52d025c] | 774 |
|
---|
[b08a62c] | 775 | reference back()
|
---|
| 776 | {
|
---|
| 777 | return at(size_ - 1);
|
---|
| 778 | }
|
---|
[52d025c] | 779 |
|
---|
[177a576] | 780 | /**
|
---|
| 781 | * 21.4.6, modifiers:
|
---|
| 782 | */
|
---|
[52d025c] | 783 |
|
---|
[681fdcca] | 784 | basic_string& operator+=(const basic_string& str)
|
---|
| 785 | {
|
---|
| 786 | return append(str);
|
---|
| 787 | }
|
---|
[52d025c] | 788 |
|
---|
[681fdcca] | 789 | basic_string& operator+=(const value_type* str)
|
---|
| 790 | {
|
---|
| 791 | return append(str);
|
---|
| 792 | }
|
---|
[52d025c] | 793 |
|
---|
[681fdcca] | 794 | basic_string& operator+=(value_type c)
|
---|
| 795 | {
|
---|
| 796 | push_back(c);
|
---|
| 797 | return *this;
|
---|
| 798 | }
|
---|
[52d025c] | 799 |
|
---|
[681fdcca] | 800 | basic_string& operator+=(initializer_list<value_type> init)
|
---|
| 801 | {
|
---|
| 802 | return append(init.begin(), init.size());
|
---|
| 803 | }
|
---|
[52d025c] | 804 |
|
---|
[681fdcca] | 805 | basic_string& append(const basic_string& str)
|
---|
| 806 | {
|
---|
| 807 | return append(str.data(), str.size());
|
---|
| 808 | }
|
---|
[52d025c] | 809 |
|
---|
[dc0fff11] | 810 | basic_string& append(const basic_string& str, size_type pos,
|
---|
[681fdcca] | 811 | size_type n = npos)
|
---|
| 812 | {
|
---|
| 813 | if (pos < str.size())
|
---|
| 814 | {
|
---|
| 815 | auto len = min(n, str.size() - pos);
|
---|
| 816 |
|
---|
| 817 | return append(str.data() + pos, len);
|
---|
| 818 | }
|
---|
| 819 | // TODO: Else throw out_of_range.
|
---|
| 820 | }
|
---|
[52d025c] | 821 |
|
---|
[681fdcca] | 822 | basic_string& append(const value_type* str, size_type n)
|
---|
| 823 | {
|
---|
| 824 | // TODO: if (size_ + n > max_size()) throw length_error
|
---|
| 825 | ensure_free_space_(n);
|
---|
[ed81b1f] | 826 | traits_type::copy(data_ + size(), str, n);
|
---|
| 827 | size_ += n;
|
---|
[681fdcca] | 828 | ensure_null_terminator_();
|
---|
[52d025c] | 829 |
|
---|
[681fdcca] | 830 | return *this;
|
---|
| 831 | }
|
---|
[52d025c] | 832 |
|
---|
[681fdcca] | 833 | basic_string& append(const value_type* str)
|
---|
| 834 | {
|
---|
[ed81b1f] | 835 | return append(str, traits_type::length(str));
|
---|
[681fdcca] | 836 | }
|
---|
| 837 |
|
---|
| 838 | basic_string& append(size_type n, value_type c)
|
---|
| 839 | {
|
---|
| 840 | return append(basic_string(n, c));
|
---|
| 841 | }
|
---|
[52d025c] | 842 |
|
---|
[177a576] | 843 | template<class InputIterator>
|
---|
[681fdcca] | 844 | basic_string& append(InputIterator first, InputIterator last)
|
---|
| 845 | {
|
---|
[dc0fff11] | 846 | return append(basic_string(first, last));
|
---|
[681fdcca] | 847 | }
|
---|
[52d025c] | 848 |
|
---|
[681fdcca] | 849 | basic_string& append(initializer_list<value_type> init)
|
---|
| 850 | {
|
---|
| 851 | return append(init.begin(), init.size());
|
---|
| 852 | }
|
---|
[52d025c] | 853 |
|
---|
[681fdcca] | 854 | void push_back(value_type c)
|
---|
| 855 | {
|
---|
| 856 | ensure_free_space_(1);
|
---|
| 857 | traits_type::assign(data_[size_++], c);
|
---|
| 858 | ensure_null_terminator_();
|
---|
| 859 | }
|
---|
[52d025c] | 860 |
|
---|
[681fdcca] | 861 | basic_string& assign(const basic_string& str)
|
---|
| 862 | {
|
---|
| 863 | return assign(str, 0, npos);
|
---|
| 864 | }
|
---|
[52d025c] | 865 |
|
---|
[681fdcca] | 866 | basic_string& assign(basic_string&& str)
|
---|
| 867 | {
|
---|
| 868 | swap(str);
|
---|
| 869 |
|
---|
| 870 | return *this;
|
---|
| 871 | }
|
---|
[52d025c] | 872 |
|
---|
[177a576] | 873 | basic_string& assign(const basic_string& str, size_type pos,
|
---|
[681fdcca] | 874 | size_type n = npos)
|
---|
| 875 | {
|
---|
| 876 | if (pos < str.size())
|
---|
| 877 | {
|
---|
| 878 | auto len = min(n, str.size() - pos);
|
---|
| 879 | ensure_free_space_(len);
|
---|
| 880 |
|
---|
| 881 | return assign(str.data() + pos, len);
|
---|
| 882 | }
|
---|
| 883 | // TODO: Else throw out_of_range.
|
---|
[dc0fff11] | 884 |
|
---|
| 885 | return *this;
|
---|
[681fdcca] | 886 | }
|
---|
[52d025c] | 887 |
|
---|
[681fdcca] | 888 | basic_string& assign(const value_type* str, size_type n)
|
---|
| 889 | {
|
---|
| 890 | // TODO: if (n > max_size()) throw length_error.
|
---|
| 891 | resize_without_copy_(n);
|
---|
| 892 | traits_type::copy(begin(), str, n);
|
---|
[dc0fff11] | 893 | size_ = n;
|
---|
| 894 | ensure_null_terminator_();
|
---|
[681fdcca] | 895 |
|
---|
| 896 | return *this;
|
---|
| 897 | }
|
---|
[52d025c] | 898 |
|
---|
[681fdcca] | 899 | basic_string& assign(const value_type* str)
|
---|
| 900 | {
|
---|
| 901 | return assign(str, traits_type::length(str));
|
---|
| 902 | }
|
---|
[52d025c] | 903 |
|
---|
[681fdcca] | 904 | basic_string& assign(size_type n, value_type c)
|
---|
| 905 | {
|
---|
| 906 | return assign(basic_string(n, c));
|
---|
| 907 | }
|
---|
[52d025c] | 908 |
|
---|
[177a576] | 909 | template<class InputIterator>
|
---|
[681fdcca] | 910 | basic_string& assign(InputIterator first, InputIterator last)
|
---|
| 911 | {
|
---|
| 912 | return assign(basic_string(first, last));
|
---|
| 913 | }
|
---|
[52d025c] | 914 |
|
---|
[681fdcca] | 915 | basic_string& assign(initializer_list<value_type> init)
|
---|
| 916 | {
|
---|
| 917 | return assign(init.begin(), init.size());
|
---|
| 918 | }
|
---|
[52d025c] | 919 |
|
---|
[681fdcca] | 920 | basic_string& insert(size_type pos, const basic_string& str)
|
---|
| 921 | {
|
---|
| 922 | // TODO: if (pos > str.size()) throw out_of_range.
|
---|
| 923 | return insert(pos, str.data(), str.size());
|
---|
| 924 | }
|
---|
[52d025c] | 925 |
|
---|
[177a576] | 926 | basic_string& insert(size_type pos1, const basic_string& str,
|
---|
[681fdcca] | 927 | size_type pos2, size_type n = npos)
|
---|
| 928 | {
|
---|
| 929 | // TODO: if (pos1 > size() or pos2 > str.size()) throw
|
---|
| 930 | // out_of_range.
|
---|
| 931 | auto len = min(n, str.size() - pos2);
|
---|
[52d025c] | 932 |
|
---|
[681fdcca] | 933 | return insert(pos1, str.data() + pos2, len);
|
---|
| 934 | }
|
---|
[52d025c] | 935 |
|
---|
[681fdcca] | 936 | basic_string& insert(size_type pos, const value_type* str, size_type n)
|
---|
| 937 | {
|
---|
[2d302d6] | 938 | // TODO: throw out_of_range if pos > size()
|
---|
| 939 | // TODO: throw length_error if size() + n > max_size()
|
---|
| 940 | ensure_free_space_(n);
|
---|
| 941 |
|
---|
[681fdcca] | 942 | copy_backward_(begin() + pos, end(), end() + n);
|
---|
[2d302d6] | 943 | copy_(str, str + n, begin() + pos);
|
---|
| 944 | size_ += n;
|
---|
[52d025c] | 945 |
|
---|
[2d302d6] | 946 | ensure_null_terminator_();
|
---|
[681fdcca] | 947 | return *this;
|
---|
| 948 | }
|
---|
[52d025c] | 949 |
|
---|
[681fdcca] | 950 | basic_string& insert(size_type pos, const value_type* str)
|
---|
| 951 | {
|
---|
| 952 | return insert(pos, str, traits_type::length(str));
|
---|
| 953 | }
|
---|
[52d025c] | 954 |
|
---|
[681fdcca] | 955 | basic_string& insert(size_type pos, size_type n, value_type c)
|
---|
| 956 | {
|
---|
| 957 | return insert(pos, basic_string(n, c));
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | iterator insert(const_iterator pos, value_type c)
|
---|
| 961 | {
|
---|
| 962 | auto idx = static_cast<size_type>(pos - begin());
|
---|
| 963 |
|
---|
| 964 | ensure_free_space_(1);
|
---|
[2d302d6] | 965 | copy_backward_(begin() + idx, end(), end() + 1);
|
---|
| 966 | traits_type::assign(data_[idx], c);
|
---|
| 967 |
|
---|
| 968 | ++size_;
|
---|
[681fdcca] | 969 | ensure_null_terminator_();
|
---|
| 970 |
|
---|
| 971 | return begin() + idx;
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | iterator insert(const_iterator pos, size_type n, value_type c)
|
---|
| 975 | {
|
---|
| 976 | if (n == 0)
|
---|
| 977 | return const_cast<iterator>(pos);
|
---|
| 978 |
|
---|
| 979 | auto idx = static_cast<size_type>(pos - begin());
|
---|
| 980 |
|
---|
| 981 | ensure_free_space_(n);
|
---|
[2d302d6] | 982 | copy_backward_(begin() + idx, end(), end() + n);
|
---|
[681fdcca] | 983 |
|
---|
[2d302d6] | 984 | auto it = begin() + idx;
|
---|
[681fdcca] | 985 | for (size_type i = 0; i < n; ++i)
|
---|
[dc0fff11] | 986 | traits_type::assign(*it++, c);
|
---|
[2d302d6] | 987 | size_ += n;
|
---|
[681fdcca] | 988 | ensure_null_terminator_();
|
---|
| 989 |
|
---|
| 990 | return begin() + idx;
|
---|
| 991 | }
|
---|
[52d025c] | 992 |
|
---|
[177a576] | 993 | template<class InputIterator>
|
---|
| 994 | iterator insert(const_iterator pos, InputIterator first,
|
---|
[681fdcca] | 995 | InputIterator last)
|
---|
| 996 | {
|
---|
[2d302d6] | 997 | if (first == last)
|
---|
| 998 | return const_cast<iterator>(pos);
|
---|
| 999 |
|
---|
| 1000 | auto idx = static_cast<size_type>(pos - begin());
|
---|
| 1001 | auto str = basic_string{first, last};
|
---|
| 1002 | insert(idx, str);
|
---|
| 1003 |
|
---|
| 1004 | return begin() + idx;
|
---|
[681fdcca] | 1005 | }
|
---|
[52d025c] | 1006 |
|
---|
[681fdcca] | 1007 | iterator insert(const_iterator pos, initializer_list<value_type> init)
|
---|
| 1008 | {
|
---|
| 1009 | return insert(pos, init.begin(), init.end());
|
---|
| 1010 | }
|
---|
[52d025c] | 1011 |
|
---|
[dc0fff11] | 1012 | basic_string& erase(size_type pos = 0, size_type n = npos)
|
---|
[681fdcca] | 1013 | {
|
---|
| 1014 | auto len = min(n, size_ - pos);
|
---|
| 1015 | copy_(begin() + pos + n, end(), begin() + pos);
|
---|
| 1016 | size_ -= len;
|
---|
| 1017 | ensure_null_terminator_();
|
---|
[e07bbbc] | 1018 |
|
---|
| 1019 | return *this;
|
---|
[681fdcca] | 1020 | }
|
---|
[52d025c] | 1021 |
|
---|
[e07bbbc] | 1022 | iterator erase(const_iterator pos)
|
---|
| 1023 | {
|
---|
| 1024 | auto idx = static_cast<size_type>(pos - cbegin());
|
---|
[dc0fff11] | 1025 | erase(idx, 1);
|
---|
[52d025c] | 1026 |
|
---|
[e07bbbc] | 1027 | return begin() + idx;
|
---|
| 1028 | }
|
---|
| 1029 |
|
---|
| 1030 | iterator erase(const_iterator first, const_iterator last)
|
---|
| 1031 | {
|
---|
| 1032 | auto idx = static_cast<size_type>(first - cbegin());
|
---|
| 1033 | auto count = static_cast<size_type>(last - first);
|
---|
| 1034 | erase(idx, count);
|
---|
| 1035 |
|
---|
| 1036 | return begin() + idx;
|
---|
| 1037 | }
|
---|
[52d025c] | 1038 |
|
---|
[681fdcca] | 1039 | void pop_back()
|
---|
| 1040 | {
|
---|
| 1041 | --size_;
|
---|
| 1042 | ensure_null_terminator_();
|
---|
| 1043 | }
|
---|
[52d025c] | 1044 |
|
---|
[b0b46d59] | 1045 | basic_string& replace(size_type pos, size_type n, const basic_string& str)
|
---|
| 1046 | {
|
---|
| 1047 | // TODO: throw out_of_range if pos > size()
|
---|
| 1048 | return replace(pos, n, str.data(), str.size());
|
---|
| 1049 | }
|
---|
[52d025c] | 1050 |
|
---|
[dc0fff11] | 1051 | basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
|
---|
[b0b46d59] | 1052 | size_type pos2, size_type n2 = npos)
|
---|
| 1053 | {
|
---|
| 1054 | // TODO: throw out_of_range if pos1 > size() or pos2 > str.size()
|
---|
| 1055 | auto len = min(n2, str.size() - pos2);
|
---|
| 1056 | return replace(pos1, n1, str.data() + pos2, len);
|
---|
| 1057 | }
|
---|
[52d025c] | 1058 |
|
---|
[177a576] | 1059 | basic_string& replace(size_type pos, size_type n1, const value_type* str,
|
---|
[b0b46d59] | 1060 | size_type n2)
|
---|
| 1061 | {
|
---|
| 1062 | // TODO: throw out_of_range if pos > size()
|
---|
| 1063 | // TODO: if size() - len > max_size() - n2 throw length_error
|
---|
[53e8686] | 1064 | auto len = min(n1, size_ - pos);
|
---|
| 1065 |
|
---|
[b0b46d59] | 1066 | basic_string tmp{};
|
---|
| 1067 | tmp.resize_without_copy_(size_ - len + n2);
|
---|
[53e8686] | 1068 |
|
---|
| 1069 | // Prefix.
|
---|
[b0b46d59] | 1070 | copy_(begin(), begin() + pos, tmp.begin());
|
---|
[53e8686] | 1071 |
|
---|
| 1072 | // Substitution.
|
---|
| 1073 | traits_type::copy(tmp.begin() + pos, str, n2);
|
---|
| 1074 |
|
---|
| 1075 | // Suffix.
|
---|
| 1076 | copy_(begin() + pos + len, end(), tmp.begin() + pos + n2);
|
---|
| 1077 |
|
---|
| 1078 | tmp.size_ = size_ - len + n2;
|
---|
[b0b46d59] | 1079 | swap(tmp);
|
---|
| 1080 | return *this;
|
---|
| 1081 | }
|
---|
[52d025c] | 1082 |
|
---|
[b0b46d59] | 1083 | basic_string& replace(size_type pos, size_type n, const value_type* str)
|
---|
| 1084 | {
|
---|
| 1085 | return replace(pos, n, str, traits_type::length(str));
|
---|
| 1086 | }
|
---|
[52d025c] | 1087 |
|
---|
[177a576] | 1088 | basic_string& replace(size_type pos, size_type n1, size_type n2,
|
---|
[b0b46d59] | 1089 | value_type c)
|
---|
| 1090 | {
|
---|
| 1091 | return replace(pos, n1, basic_string(n2, c));
|
---|
| 1092 | }
|
---|
[52d025c] | 1093 |
|
---|
[177a576] | 1094 | basic_string& replace(const_iterator i1, const_iterator i2,
|
---|
[b0b46d59] | 1095 | const basic_string& str)
|
---|
| 1096 | {
|
---|
| 1097 | return replace(i1 - begin(), i2 - i1, str);
|
---|
| 1098 | }
|
---|
[52d025c] | 1099 |
|
---|
[177a576] | 1100 | basic_string& replace(const_iterator i1, const_iterator i2,
|
---|
[b0b46d59] | 1101 | const value_type* str, size_type n)
|
---|
| 1102 | {
|
---|
[dc0fff11] | 1103 | return replace(i1 - begin(), i2 - i1, str, n);
|
---|
[b0b46d59] | 1104 | }
|
---|
[52d025c] | 1105 |
|
---|
[177a576] | 1106 | basic_string& replace(const_iterator i1, const_iterator i2,
|
---|
[b0b46d59] | 1107 | const value_type* str)
|
---|
| 1108 | {
|
---|
| 1109 | return replace(i1 - begin(), i2 - i1, str, traits_type::length(str));
|
---|
| 1110 | }
|
---|
[52d025c] | 1111 |
|
---|
[177a576] | 1112 | basic_string& replace(const_iterator i1, const_iterator i2,
|
---|
[b0b46d59] | 1113 | size_type n, value_type c)
|
---|
| 1114 | {
|
---|
| 1115 | return replace(i1 - begin(), i2 - i1, basic_string(n, c));
|
---|
| 1116 | }
|
---|
[52d025c] | 1117 |
|
---|
[177a576] | 1118 | template<class InputIterator>
|
---|
| 1119 | basic_string& replace(const_iterator i1, const_iterator i2,
|
---|
[b0b46d59] | 1120 | InputIterator j1, InputIterator j2)
|
---|
| 1121 | {
|
---|
| 1122 | return replace(i1 - begin(), i2 - i1, basic_string(j1, j2));
|
---|
| 1123 | }
|
---|
[52d025c] | 1124 |
|
---|
[177a576] | 1125 | basic_string& replace(const_iterator i1, const_iterator i2,
|
---|
[b0b46d59] | 1126 | initializer_list<value_type> init)
|
---|
| 1127 | {
|
---|
| 1128 | return replace(i1 - begin(), i2 - i1, init.begin(), init.size());
|
---|
| 1129 | }
|
---|
[52d025c] | 1130 |
|
---|
[27473fb8] | 1131 | size_type copy(value_type* str, size_type n, size_type pos = 0) const
|
---|
| 1132 | {
|
---|
| 1133 | auto len = min(n , size_ - pos);
|
---|
| 1134 | for (size_type i = 0; i < len; ++i)
|
---|
| 1135 | traits_type::assign(str[i], data_[pos + i]);
|
---|
| 1136 |
|
---|
| 1137 | return len;
|
---|
| 1138 | }
|
---|
[52d025c] | 1139 |
|
---|
[177a576] | 1140 | void swap(basic_string& other)
|
---|
| 1141 | noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
|
---|
[53e8686] | 1142 | allocator_traits<allocator_type>::is_always_equal::value)
|
---|
[b08a62c] | 1143 | {
|
---|
| 1144 | std::swap(data_, other.data_);
|
---|
| 1145 | std::swap(size_, other.size_);
|
---|
| 1146 | std::swap(capacity_, other.capacity_);
|
---|
| 1147 | }
|
---|
[52d025c] | 1148 |
|
---|
[177a576] | 1149 | /**
|
---|
| 1150 | * 21.4.7, string operations:
|
---|
| 1151 | */
|
---|
[52d025c] | 1152 |
|
---|
[b08a62c] | 1153 | const value_type* c_str() const noexcept
|
---|
| 1154 | {
|
---|
| 1155 | return data_;
|
---|
| 1156 | }
|
---|
[52d025c] | 1157 |
|
---|
[b08a62c] | 1158 | const value_type* data() const noexcept
|
---|
| 1159 | {
|
---|
| 1160 | return data_;
|
---|
| 1161 | }
|
---|
[52d025c] | 1162 |
|
---|
[b08a62c] | 1163 | allocator_type get_allocator() const noexcept
|
---|
| 1164 | {
|
---|
| 1165 | return allocator_type{allocator_};
|
---|
| 1166 | }
|
---|
[52d025c] | 1167 |
|
---|
[e07bbbc] | 1168 | /**
|
---|
| 1169 | * Note: The following find functions have 4 versions each:
|
---|
| 1170 | * (1) takes basic_string
|
---|
| 1171 | * (2) takes c string and length
|
---|
| 1172 | * (3) takes c string
|
---|
| 1173 | * (4) takes value_type
|
---|
| 1174 | * According to the C++14 standard, only (1) is marked as
|
---|
| 1175 | * noexcept and the other three return the first one with
|
---|
| 1176 | * a newly allocated strings (and thus cannot be noexcept).
|
---|
| 1177 | * However, allocating a new string results in memory
|
---|
| 1178 | * allocation and copying of the source and thus we have
|
---|
| 1179 | * decided to follow C++17 signatures of these functions
|
---|
| 1180 | * (i.e. all of them being marked as noexcept) and use
|
---|
| 1181 | * (2) for the actual implementation (and avoiding any
|
---|
| 1182 | * allocations or copying in the process and also providing
|
---|
| 1183 | * stronger guarantees to the user).
|
---|
| 1184 | */
|
---|
| 1185 |
|
---|
| 1186 | size_type find(const basic_string& str, size_type pos = 0) const noexcept
|
---|
| 1187 | {
|
---|
| 1188 | return find(str.c_str(), pos, str.size());
|
---|
| 1189 | }
|
---|
| 1190 |
|
---|
| 1191 | size_type find(const value_type* str, size_type pos, size_type len) const noexcept
|
---|
| 1192 | {
|
---|
[a6ca1bc] | 1193 | if (empty() || len == 0 || len + pos > size())
|
---|
[e07bbbc] | 1194 | return npos;
|
---|
| 1195 |
|
---|
| 1196 | size_type idx{pos};
|
---|
| 1197 |
|
---|
| 1198 | while (idx + len < size_)
|
---|
| 1199 | {
|
---|
| 1200 | if (substr_starts_at_(idx, str, len))
|
---|
| 1201 | return idx;
|
---|
| 1202 | ++idx;
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
| 1205 | return npos;
|
---|
| 1206 | }
|
---|
| 1207 |
|
---|
| 1208 | size_type find(const value_type* str, size_type pos = 0) const noexcept
|
---|
| 1209 | {
|
---|
| 1210 | return find(str, pos, traits_type::length(str));
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 | size_type find(value_type c, size_type pos = 0) const noexcept
|
---|
| 1214 | {
|
---|
| 1215 | if (empty())
|
---|
| 1216 | return npos;
|
---|
| 1217 |
|
---|
| 1218 | for (size_type i = pos; i < size_; ++i)
|
---|
| 1219 | {
|
---|
| 1220 | if (traits_type::eq(c, data_[i]))
|
---|
| 1221 | return i;
|
---|
| 1222 | }
|
---|
| 1223 |
|
---|
| 1224 | return npos;
|
---|
| 1225 | }
|
---|
| 1226 |
|
---|
| 1227 | size_type rfind(const basic_string& str, size_type pos = npos) const noexcept
|
---|
| 1228 | {
|
---|
| 1229 | return rfind(str.c_str(), pos, str.size());
|
---|
| 1230 | }
|
---|
| 1231 |
|
---|
| 1232 | size_type rfind(const value_type* str, size_type pos, size_type len) const noexcept
|
---|
| 1233 | {
|
---|
[a6ca1bc] | 1234 | if (empty() || len == 0 || len + pos > size())
|
---|
[e07bbbc] | 1235 | return npos;
|
---|
| 1236 |
|
---|
| 1237 | size_type idx{min(pos, size_ - 1) + 1};
|
---|
| 1238 |
|
---|
| 1239 | while (idx > 0)
|
---|
| 1240 | {
|
---|
| 1241 | if (substr_starts_at_(idx - 1, str, len))
|
---|
| 1242 | return idx - 1;
|
---|
| 1243 | --idx;
|
---|
| 1244 | }
|
---|
| 1245 |
|
---|
| 1246 | return npos;
|
---|
| 1247 | }
|
---|
| 1248 |
|
---|
| 1249 | size_type rfind(const value_type* str, size_type pos = npos) const noexcept
|
---|
| 1250 | {
|
---|
| 1251 | return rfind(str, pos, traits_type::length(str));
|
---|
| 1252 | }
|
---|
| 1253 |
|
---|
| 1254 | size_type rfind(value_type c, size_type pos = npos) const noexcept
|
---|
| 1255 | {
|
---|
| 1256 | if (empty())
|
---|
| 1257 | return npos;
|
---|
| 1258 |
|
---|
[a6ca1bc] | 1259 | for (size_type i = min(pos + 1, size_ - 1) + 1; i > 0; --i)
|
---|
[e07bbbc] | 1260 | {
|
---|
| 1261 | if (traits_type::eq(c, data_[i - 1]))
|
---|
| 1262 | return i - 1;
|
---|
| 1263 | }
|
---|
| 1264 |
|
---|
| 1265 | return npos;
|
---|
| 1266 | }
|
---|
| 1267 |
|
---|
| 1268 | size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept
|
---|
| 1269 | {
|
---|
| 1270 | return find_first_of(str.c_str(), pos, str.size());
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
| 1273 | size_type find_first_of(const value_type* str, size_type pos, size_type len) const noexcept
|
---|
| 1274 | {
|
---|
[a6ca1bc] | 1275 | if (empty() || len == 0 || pos >= size())
|
---|
[e07bbbc] | 1276 | return npos;
|
---|
| 1277 |
|
---|
| 1278 | size_type idx{pos};
|
---|
| 1279 |
|
---|
| 1280 | while (idx < size_)
|
---|
| 1281 | {
|
---|
| 1282 | if (is_any_of_(idx, str, len))
|
---|
| 1283 | return idx;
|
---|
| 1284 | ++idx;
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
| 1287 | return npos;
|
---|
| 1288 | }
|
---|
| 1289 |
|
---|
| 1290 | size_type find_first_of(const value_type* str, size_type pos = 0) const noexcept
|
---|
| 1291 | {
|
---|
| 1292 | return find_first_of(str, pos, traits_type::length(str));
|
---|
| 1293 | }
|
---|
| 1294 |
|
---|
| 1295 | size_type find_first_of(value_type c, size_type pos = 0) const noexcept
|
---|
| 1296 | {
|
---|
| 1297 | return find(c, pos);
|
---|
| 1298 | }
|
---|
[52d025c] | 1299 |
|
---|
[e07bbbc] | 1300 | size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept
|
---|
| 1301 | {
|
---|
| 1302 | return find_last_of(str.c_str(), pos, str.size());
|
---|
| 1303 | }
|
---|
[52d025c] | 1304 |
|
---|
[e07bbbc] | 1305 | size_type find_last_of(const value_type* str, size_type pos, size_type len) const noexcept
|
---|
| 1306 | {
|
---|
[a6ca1bc] | 1307 | if (empty() || len == 0)
|
---|
[e07bbbc] | 1308 | return npos;
|
---|
[52d025c] | 1309 |
|
---|
[e07bbbc] | 1310 | for (size_type i = min(pos, size_ - 1) + 1; i > 0; --i)
|
---|
| 1311 | {
|
---|
| 1312 | if (is_any_of_(i - 1, str, len))
|
---|
| 1313 | return i - 1;
|
---|
| 1314 | }
|
---|
[52d025c] | 1315 |
|
---|
[e07bbbc] | 1316 | return npos;
|
---|
| 1317 | }
|
---|
[52d025c] | 1318 |
|
---|
[e07bbbc] | 1319 | size_type find_last_of(const value_type* str, size_type pos = npos) const noexcept
|
---|
| 1320 | {
|
---|
| 1321 | return find_last_of(str, pos, traits_type::length(str));
|
---|
| 1322 | }
|
---|
[52d025c] | 1323 |
|
---|
[e07bbbc] | 1324 | size_type find_last_of(value_type c, size_type pos = npos) const noexcept
|
---|
| 1325 | {
|
---|
| 1326 | return rfind(c, pos);
|
---|
| 1327 | }
|
---|
[52d025c] | 1328 |
|
---|
[e07bbbc] | 1329 | size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept
|
---|
| 1330 | {
|
---|
| 1331 | return find_first_not_of(str.c_str(), pos, str.size());
|
---|
| 1332 | }
|
---|
[52d025c] | 1333 |
|
---|
[e07bbbc] | 1334 | size_type find_first_not_of(const value_type* str, size_type pos, size_type len) const noexcept
|
---|
| 1335 | {
|
---|
[a6ca1bc] | 1336 | if (empty() || pos >= size())
|
---|
[e07bbbc] | 1337 | return npos;
|
---|
[52d025c] | 1338 |
|
---|
[e07bbbc] | 1339 | size_type idx{pos};
|
---|
[52d025c] | 1340 |
|
---|
[e07bbbc] | 1341 | while (idx < size_)
|
---|
| 1342 | {
|
---|
| 1343 | if (!is_any_of_(idx, str, len))
|
---|
| 1344 | return idx;
|
---|
| 1345 | ++idx;
|
---|
| 1346 | }
|
---|
[52d025c] | 1347 |
|
---|
[e07bbbc] | 1348 | return npos;
|
---|
| 1349 | }
|
---|
[52d025c] | 1350 |
|
---|
[e07bbbc] | 1351 | size_type find_first_not_of(const value_type* str, size_type pos = 0) const noexcept
|
---|
| 1352 | {
|
---|
[a6ca1bc] | 1353 | return find_first_not_of(str, pos, traits_type::length(str));
|
---|
[e07bbbc] | 1354 | }
|
---|
[52d025c] | 1355 |
|
---|
[e07bbbc] | 1356 | size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept
|
---|
| 1357 | {
|
---|
| 1358 | if (empty())
|
---|
| 1359 | return npos;
|
---|
[52d025c] | 1360 |
|
---|
[e07bbbc] | 1361 | for (size_type i = pos; i < size_; ++i)
|
---|
| 1362 | {
|
---|
| 1363 | if (!traits_type::eq(c, data_[i]))
|
---|
| 1364 | return i;
|
---|
| 1365 | }
|
---|
[52d025c] | 1366 |
|
---|
[e07bbbc] | 1367 | return npos;
|
---|
| 1368 | }
|
---|
[52d025c] | 1369 |
|
---|
[e07bbbc] | 1370 | size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept
|
---|
| 1371 | {
|
---|
| 1372 | return find_last_not_of(str.c_str(), pos, str.size());
|
---|
| 1373 | }
|
---|
[52d025c] | 1374 |
|
---|
[e07bbbc] | 1375 | size_type find_last_not_of(const value_type* str, size_type pos, size_type len) const noexcept
|
---|
| 1376 | {
|
---|
| 1377 | if (empty())
|
---|
| 1378 | return npos;
|
---|
[52d025c] | 1379 |
|
---|
[e07bbbc] | 1380 | for (size_type i = min(pos, size_ - 1) + 1; i > 0; --i)
|
---|
| 1381 | {
|
---|
[a6ca1bc] | 1382 | if (!is_any_of_(i - 1, str, len))
|
---|
[e07bbbc] | 1383 | return i - 1;
|
---|
| 1384 | }
|
---|
[52d025c] | 1385 |
|
---|
[e07bbbc] | 1386 | return npos;
|
---|
| 1387 | }
|
---|
[52d025c] | 1388 |
|
---|
[e07bbbc] | 1389 | size_type find_last_not_of(const value_type* str, size_type pos = npos) const noexcept
|
---|
| 1390 | {
|
---|
| 1391 | return find_last_not_of(str, pos, traits_type::length(str));
|
---|
| 1392 | }
|
---|
[52d025c] | 1393 |
|
---|
[e07bbbc] | 1394 | size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept
|
---|
| 1395 | {
|
---|
| 1396 | if (empty())
|
---|
| 1397 | return npos;
|
---|
[52d025c] | 1398 |
|
---|
[e07bbbc] | 1399 | pos = min(pos, size_ - 1);
|
---|
[52d025c] | 1400 |
|
---|
[e07bbbc] | 1401 | for (size_type i = min(pos, size_ - 1) + 1; i > 1; --i)
|
---|
| 1402 | {
|
---|
| 1403 | if (!traits_type::eq(c, data_[i - 1]))
|
---|
| 1404 | return i - 1;
|
---|
| 1405 | }
|
---|
[52d025c] | 1406 |
|
---|
[e07bbbc] | 1407 | return npos;
|
---|
| 1408 | }
|
---|
[52d025c] | 1409 |
|
---|
[e07bbbc] | 1410 | basic_string substr(size_type pos = 0, size_type n = npos) const
|
---|
| 1411 | {
|
---|
| 1412 | // TODO: throw out_of_range if pos > size().
|
---|
| 1413 | auto len = min(n, size_ - pos);
|
---|
| 1414 | return basic_string{data() + pos, len};
|
---|
| 1415 | }
|
---|
[52d025c] | 1416 |
|
---|
[e07bbbc] | 1417 | int compare(const basic_string& other) const noexcept
|
---|
| 1418 | {
|
---|
| 1419 | auto len = min(size(), other.size());
|
---|
| 1420 | auto comp = traits_type::compare(data_, other.data(), len);
|
---|
| 1421 |
|
---|
| 1422 | if (comp != 0)
|
---|
| 1423 | return comp;
|
---|
| 1424 | else if (size() == other.size())
|
---|
| 1425 | return 0;
|
---|
| 1426 | else if (size() > other.size())
|
---|
| 1427 | return 1;
|
---|
[4e6fb2f] | 1428 | else
|
---|
[e07bbbc] | 1429 | return -1;
|
---|
| 1430 | }
|
---|
| 1431 |
|
---|
| 1432 | int compare(size_type pos, size_type n, const basic_string& other) const
|
---|
| 1433 | {
|
---|
| 1434 | return basic_string{*this, pos, n}.compare(other);
|
---|
| 1435 | }
|
---|
[52d025c] | 1436 |
|
---|
[177a576] | 1437 | int compare(size_type pos1, size_type n1, const basic_string& other,
|
---|
[e07bbbc] | 1438 | size_type pos2, size_type n2 = npos) const
|
---|
| 1439 | {
|
---|
| 1440 | return basic_string{*this, pos1, n1}.compare(basic_string{other, pos2, n2});
|
---|
| 1441 | }
|
---|
[52d025c] | 1442 |
|
---|
[e07bbbc] | 1443 | int compare(const value_type* other) const
|
---|
| 1444 | {
|
---|
| 1445 | return compare(basic_string(other));
|
---|
| 1446 | }
|
---|
[52d025c] | 1447 |
|
---|
[e07bbbc] | 1448 | int compare(size_type pos, size_type n, const value_type* other) const
|
---|
| 1449 | {
|
---|
| 1450 | return basic_string{*this, pos, n}.compare(basic_string{other});
|
---|
| 1451 | }
|
---|
[52d025c] | 1452 |
|
---|
[e07bbbc] | 1453 | int compare(size_type pos, size_type n1,
|
---|
| 1454 | const value_type* other, size_type n2) const
|
---|
| 1455 | {
|
---|
| 1456 | return basic_string{*this, pos, n1}.compare(basic_string{other, n2});
|
---|
| 1457 | }
|
---|
[b08a62c] | 1458 |
|
---|
| 1459 | private:
|
---|
| 1460 | value_type* data_;
|
---|
| 1461 | size_type size_;
|
---|
| 1462 | size_type capacity_;
|
---|
| 1463 | allocator_type allocator_;
|
---|
[681fdcca] | 1464 |
|
---|
| 1465 | /**
|
---|
| 1466 | * Arbitrary value, standard just requires
|
---|
| 1467 | * data() to have some capacity.
|
---|
| 1468 | * (Well, we could've done data_ = &data_ and
|
---|
| 1469 | * set capacity to 0, but that would be too cryptic.)
|
---|
| 1470 | */
|
---|
[dc0fff11] | 1471 | static constexpr size_type default_capacity_{4};
|
---|
[681fdcca] | 1472 |
|
---|
| 1473 | void init_(const value_type* str, size_type size)
|
---|
| 1474 | {
|
---|
| 1475 | if (data_)
|
---|
| 1476 | allocator_.deallocate(data_, capacity_);
|
---|
| 1477 |
|
---|
| 1478 | size_ = size;
|
---|
| 1479 | capacity_ = size;
|
---|
| 1480 |
|
---|
| 1481 | data_ = allocator_.allocate(capacity_);
|
---|
| 1482 | traits_type::copy(data_, str, size);
|
---|
| 1483 | ensure_null_terminator_();
|
---|
| 1484 | }
|
---|
| 1485 |
|
---|
| 1486 | size_type next_capacity_(size_type hint = 0) const noexcept
|
---|
| 1487 | {
|
---|
| 1488 | if (hint != 0)
|
---|
| 1489 | return max(capacity_ * 2, hint);
|
---|
| 1490 | else
|
---|
| 1491 | return max(capacity_ * 2, 2ul);
|
---|
| 1492 | }
|
---|
| 1493 |
|
---|
| 1494 | void ensure_free_space_(size_type n)
|
---|
| 1495 | {
|
---|
| 1496 | /**
|
---|
| 1497 | * Note: We cannot use reserve like we
|
---|
| 1498 | * did in vector, because in string
|
---|
| 1499 | * reserve can cause shrinking.
|
---|
| 1500 | */
|
---|
[dc0fff11] | 1501 | if (size_ + 1 + n > capacity_)
|
---|
[681fdcca] | 1502 | resize_with_copy_(size_, max(size_ + 1 + n, next_capacity_()));
|
---|
| 1503 | }
|
---|
| 1504 |
|
---|
| 1505 | void resize_without_copy_(size_type capacity)
|
---|
| 1506 | {
|
---|
| 1507 | if (data_)
|
---|
| 1508 | allocator_.deallocate(data_, capacity_);
|
---|
| 1509 |
|
---|
| 1510 | data_ = allocator_.allocate(capacity);
|
---|
| 1511 | size_ = 0;
|
---|
| 1512 | capacity_ = capacity;
|
---|
| 1513 | ensure_null_terminator_();
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
| 1516 | void resize_with_copy_(size_type size, size_type capacity)
|
---|
| 1517 | {
|
---|
| 1518 | if(capacity_ == 0 || capacity_ < capacity)
|
---|
| 1519 | {
|
---|
| 1520 | auto new_data = allocator_.allocate(capacity);
|
---|
| 1521 |
|
---|
| 1522 | auto to_copy = min(size, size_);
|
---|
| 1523 | traits_type::move(new_data, data_, to_copy);
|
---|
| 1524 |
|
---|
| 1525 | std::swap(data_, new_data);
|
---|
| 1526 |
|
---|
| 1527 | allocator_.deallocate(new_data, capacity_);
|
---|
| 1528 | }
|
---|
| 1529 |
|
---|
| 1530 | capacity_ = capacity;
|
---|
| 1531 | size_ = size;
|
---|
| 1532 | ensure_null_terminator_();
|
---|
| 1533 | }
|
---|
| 1534 |
|
---|
[2d302d6] | 1535 | template<class Iterator1, class Iterator2>
|
---|
| 1536 | Iterator2 copy_(Iterator1 first, Iterator1 last,
|
---|
| 1537 | Iterator2 result)
|
---|
[681fdcca] | 1538 | {
|
---|
| 1539 | while (first != last)
|
---|
| 1540 | traits_type::assign(*result++, *first++);
|
---|
| 1541 |
|
---|
| 1542 | return result;
|
---|
| 1543 | }
|
---|
| 1544 |
|
---|
[2d302d6] | 1545 | template<class Iterator1, class Iterator2>
|
---|
| 1546 | Iterator2 copy_backward_(Iterator1 first, Iterator1 last,
|
---|
| 1547 | Iterator2 result)
|
---|
[681fdcca] | 1548 | {
|
---|
[2d302d6] | 1549 | while (last != first)
|
---|
| 1550 | traits_type::assign(*--result, *--last);
|
---|
[681fdcca] | 1551 |
|
---|
| 1552 | return result;
|
---|
| 1553 | }
|
---|
| 1554 |
|
---|
| 1555 | void ensure_null_terminator_()
|
---|
| 1556 | {
|
---|
[dc0fff11] | 1557 | value_type c{};
|
---|
| 1558 | traits_type::assign(data_[size_], c);
|
---|
[681fdcca] | 1559 | }
|
---|
[e07bbbc] | 1560 |
|
---|
[a6ca1bc] | 1561 | bool is_any_of_(size_type idx, const value_type* str, size_type len) const
|
---|
[e07bbbc] | 1562 | {
|
---|
[a6ca1bc] | 1563 | for (size_type i = 0; i < len; ++i)
|
---|
[e07bbbc] | 1564 | {
|
---|
[a6ca1bc] | 1565 | if (traits_type::eq(data_[idx], str[i]))
|
---|
[e07bbbc] | 1566 | return true;
|
---|
| 1567 | }
|
---|
| 1568 |
|
---|
| 1569 | return false;
|
---|
| 1570 | }
|
---|
| 1571 |
|
---|
| 1572 | bool substr_starts_at_(size_type idx, const value_type* str, size_type len) const
|
---|
| 1573 | {
|
---|
| 1574 | size_type i{};
|
---|
| 1575 | for (i = 0; i < len; ++i)
|
---|
| 1576 | {
|
---|
| 1577 | if (!traits_type::eq(data_[idx + i], str[i]))
|
---|
| 1578 | break;
|
---|
| 1579 | }
|
---|
| 1580 |
|
---|
| 1581 | return i == len;
|
---|
| 1582 | }
|
---|
[52d025c] | 1583 | };
|
---|
[dc0fff11] | 1584 |
|
---|
[82b6716] | 1585 | using string = basic_string<char>;
|
---|
| 1586 | using u16string = basic_string<char16_t>;
|
---|
| 1587 | using u32string = basic_string<char32_t>;
|
---|
| 1588 | using wstring = basic_string<wchar_t>;
|
---|
| 1589 |
|
---|
| 1590 | /**
|
---|
| 1591 | * 21.4.8, basic_string non-member functions:
|
---|
| 1592 | */
|
---|
| 1593 |
|
---|
| 1594 | /**
|
---|
| 1595 | * 21.4.8.1, operator+:
|
---|
| 1596 | */
|
---|
| 1597 |
|
---|
| 1598 | template<class Char, class Traits, class Allocator>
|
---|
| 1599 | basic_string<Char, Traits, Allocator>
|
---|
| 1600 | operator+(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1601 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
| 1602 | {
|
---|
| 1603 | return basic_string<Char, Traits, Allocator>{lhs}.append(rhs);
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
| 1606 | template<class Char, class Traits, class Allocator>
|
---|
| 1607 | basic_string<Char, Traits, Allocator>
|
---|
| 1608 | operator+(basic_string<Char, Traits, Allocator>&& lhs,
|
---|
| 1609 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
| 1610 | {
|
---|
| 1611 | return move(lhs.append(rhs));
|
---|
| 1612 | }
|
---|
| 1613 |
|
---|
| 1614 | template<class Char, class Traits, class Allocator>
|
---|
| 1615 | basic_string<Char, Traits, Allocator>
|
---|
| 1616 | operator+(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1617 | basic_string<Char, Traits, Allocator>&& rhs)
|
---|
| 1618 | {
|
---|
| 1619 | return move(rhs.insert(0, lhs));
|
---|
| 1620 | }
|
---|
| 1621 |
|
---|
| 1622 | template<class Char, class Traits, class Allocator>
|
---|
| 1623 | basic_string<Char, Traits, Allocator>
|
---|
| 1624 | operator+(basic_string<Char, Traits, Allocator>&& lhs,
|
---|
| 1625 | basic_string<Char, Traits, Allocator>&& rhs)
|
---|
| 1626 | {
|
---|
| 1627 | return move(lhs.append(rhs));
|
---|
| 1628 | }
|
---|
| 1629 |
|
---|
| 1630 | template<class Char, class Traits, class Allocator>
|
---|
| 1631 | basic_string<Char, Traits, Allocator>
|
---|
| 1632 | operator+(const Char* lhs,
|
---|
| 1633 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
| 1634 | {
|
---|
| 1635 | return basic_string<Char, Traits, Allocator>{lhs} + rhs;
|
---|
| 1636 | }
|
---|
| 1637 |
|
---|
| 1638 | template<class Char, class Traits, class Allocator>
|
---|
| 1639 | basic_string<Char, Traits, Allocator>
|
---|
| 1640 | operator+(const Char* lhs,
|
---|
| 1641 | basic_string<Char, Traits, Allocator>&& rhs)
|
---|
| 1642 | {
|
---|
| 1643 | return move(rhs.insert(0, lhs));
|
---|
| 1644 | }
|
---|
| 1645 |
|
---|
| 1646 | template<class Char, class Traits, class Allocator>
|
---|
| 1647 | basic_string<Char, Traits, Allocator>
|
---|
| 1648 | operator+(Char lhs,
|
---|
| 1649 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
| 1650 | {
|
---|
| 1651 | return basic_string<Char, Traits, Allocator>{1, lhs}.append(rhs);
|
---|
| 1652 | }
|
---|
| 1653 |
|
---|
| 1654 | template<class Char, class Traits, class Allocator>
|
---|
| 1655 | basic_string<Char, Traits, Allocator>
|
---|
| 1656 | operator+(Char lhs,
|
---|
| 1657 | basic_string<Char, Traits, Allocator>&& rhs)
|
---|
| 1658 | {
|
---|
| 1659 | return move(rhs.insert(0, 1, lhs));
|
---|
| 1660 | }
|
---|
| 1661 |
|
---|
| 1662 | template<class Char, class Traits, class Allocator>
|
---|
| 1663 | basic_string<Char, Traits, Allocator>
|
---|
| 1664 | operator+(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1665 | const Char* rhs)
|
---|
| 1666 | {
|
---|
| 1667 | return lhs + basic_string<Char, Traits, Allocator>{rhs};
|
---|
| 1668 | }
|
---|
| 1669 |
|
---|
| 1670 | template<class Char, class Traits, class Allocator>
|
---|
| 1671 | basic_string<Char, Traits, Allocator>
|
---|
| 1672 | operator+(basic_string<Char, Traits, Allocator>&& lhs,
|
---|
| 1673 | const Char* rhs)
|
---|
| 1674 | {
|
---|
| 1675 | return move(lhs.append(rhs));
|
---|
| 1676 | }
|
---|
| 1677 |
|
---|
| 1678 | template<class Char, class Traits, class Allocator>
|
---|
| 1679 | basic_string<Char, Traits, Allocator>
|
---|
| 1680 | operator+(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1681 | Char rhs)
|
---|
| 1682 | {
|
---|
| 1683 | return lhs + basic_string<Char, Traits, Allocator>{1, rhs};
|
---|
| 1684 | }
|
---|
| 1685 |
|
---|
| 1686 | template<class Char, class Traits, class Allocator>
|
---|
| 1687 | basic_string<Char, Traits, Allocator>
|
---|
| 1688 | operator+(basic_string<Char, Traits, Allocator>&& lhs,
|
---|
| 1689 | Char rhs)
|
---|
| 1690 | {
|
---|
| 1691 | return move(lhs.append(1, rhs));
|
---|
| 1692 | }
|
---|
| 1693 |
|
---|
| 1694 | /**
|
---|
| 1695 | * 21.4.8.2, operator==:
|
---|
| 1696 | */
|
---|
| 1697 |
|
---|
| 1698 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1699 | bool operator==(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1700 | const basic_string<Char, Traits, Allocator>& rhs) noexcept
|
---|
[82b6716] | 1701 | {
|
---|
| 1702 | return lhs.compare(rhs) == 0;
|
---|
| 1703 | }
|
---|
| 1704 |
|
---|
| 1705 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1706 | bool operator==(const Char* lhs,
|
---|
| 1707 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
[82b6716] | 1708 | {
|
---|
| 1709 | return rhs == lhs;
|
---|
| 1710 | }
|
---|
| 1711 |
|
---|
| 1712 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1713 | bool operator==(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1714 | const Char* rhs)
|
---|
[82b6716] | 1715 | {
|
---|
| 1716 | return lhs.compare(rhs) == 0;
|
---|
| 1717 | }
|
---|
| 1718 |
|
---|
| 1719 | /**
|
---|
| 1720 | * 21.4.8.3, operator!=:
|
---|
| 1721 | */
|
---|
| 1722 |
|
---|
| 1723 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1724 | bool operator!=(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1725 | const basic_string<Char, Traits, Allocator>& rhs) noexcept
|
---|
[82b6716] | 1726 | {
|
---|
| 1727 | return !(lhs == rhs);
|
---|
| 1728 | }
|
---|
| 1729 |
|
---|
| 1730 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1731 | bool operator!=(const Char* lhs,
|
---|
| 1732 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
[82b6716] | 1733 | {
|
---|
| 1734 | return rhs != lhs;
|
---|
| 1735 | }
|
---|
| 1736 |
|
---|
| 1737 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1738 | bool operator!=(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1739 | const Char* rhs)
|
---|
[82b6716] | 1740 | {
|
---|
| 1741 | return lhs.compare(rhs) != 0;
|
---|
| 1742 | }
|
---|
| 1743 |
|
---|
| 1744 | /**
|
---|
| 1745 | * 21.4.8.4, operator<:
|
---|
| 1746 | */
|
---|
| 1747 |
|
---|
| 1748 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1749 | bool operator<(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1750 | const basic_string<Char, Traits, Allocator>& rhs) noexcept
|
---|
[82b6716] | 1751 | {
|
---|
| 1752 | return lhs.compare(rhs) < 0;
|
---|
| 1753 | }
|
---|
| 1754 |
|
---|
| 1755 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1756 | bool operator<(const Char* lhs,
|
---|
| 1757 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
[82b6716] | 1758 | {
|
---|
| 1759 | return rhs.compare(lhs) > 0;
|
---|
| 1760 | }
|
---|
| 1761 |
|
---|
| 1762 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1763 | bool operator<(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1764 | const Char* rhs)
|
---|
[82b6716] | 1765 | {
|
---|
| 1766 | return lhs.compare(rhs) < 0;
|
---|
| 1767 | }
|
---|
| 1768 |
|
---|
| 1769 | /**
|
---|
| 1770 | * 21.4.8.5, operator>:
|
---|
| 1771 | */
|
---|
| 1772 |
|
---|
| 1773 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1774 | bool operator>(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1775 | const basic_string<Char, Traits, Allocator>& rhs) noexcept
|
---|
[82b6716] | 1776 | {
|
---|
| 1777 | return lhs.compare(rhs) > 0;
|
---|
| 1778 | }
|
---|
| 1779 |
|
---|
| 1780 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1781 | bool operator>(const Char* lhs,
|
---|
| 1782 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
[82b6716] | 1783 | {
|
---|
| 1784 | return rhs.compare(lhs) < 0;
|
---|
| 1785 | }
|
---|
| 1786 |
|
---|
| 1787 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1788 | bool operator>(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1789 | const Char* rhs)
|
---|
[82b6716] | 1790 | {
|
---|
| 1791 | return lhs.compare(rhs) > 0;
|
---|
| 1792 | }
|
---|
| 1793 |
|
---|
| 1794 | /**
|
---|
| 1795 | * 21.4.8.6, operator<=:
|
---|
| 1796 | */
|
---|
| 1797 |
|
---|
| 1798 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1799 | bool operator<=(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1800 | const basic_string<Char, Traits, Allocator>& rhs) noexcept
|
---|
[82b6716] | 1801 | {
|
---|
| 1802 | return lhs.compare(rhs) <= 0;
|
---|
| 1803 | }
|
---|
| 1804 |
|
---|
| 1805 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1806 | bool operator<=(const Char* lhs,
|
---|
| 1807 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
[82b6716] | 1808 | {
|
---|
| 1809 | return rhs.compare(lhs) >= 0;
|
---|
| 1810 | }
|
---|
| 1811 |
|
---|
| 1812 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1813 | bool operator<=(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1814 | const Char* rhs)
|
---|
[82b6716] | 1815 | {
|
---|
| 1816 | return lhs.compare(rhs) <= 0;
|
---|
| 1817 | }
|
---|
| 1818 |
|
---|
| 1819 | /**
|
---|
| 1820 | * 21.4.8.7, operator>=:
|
---|
| 1821 | */
|
---|
| 1822 |
|
---|
| 1823 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1824 | bool operator>=(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1825 | const basic_string<Char, Traits, Allocator>& rhs) noexcept
|
---|
[82b6716] | 1826 | {
|
---|
| 1827 | return lhs.compare(rhs) >= 0;
|
---|
| 1828 | }
|
---|
| 1829 |
|
---|
| 1830 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1831 | bool operator>=(const Char* lhs,
|
---|
| 1832 | const basic_string<Char, Traits, Allocator>& rhs)
|
---|
[82b6716] | 1833 | {
|
---|
| 1834 | return rhs.compare(lhs) <= 0;
|
---|
| 1835 | }
|
---|
| 1836 |
|
---|
| 1837 | template<class Char, class Traits, class Allocator>
|
---|
[fbec99a] | 1838 | bool operator>=(const basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1839 | const Char* rhs)
|
---|
[82b6716] | 1840 | {
|
---|
| 1841 | return lhs.compare(rhs) >= 0;
|
---|
| 1842 | }
|
---|
| 1843 |
|
---|
| 1844 | /**
|
---|
| 1845 | * 21.4.8.8, swap:
|
---|
| 1846 | */
|
---|
| 1847 |
|
---|
| 1848 | template<class Char, class Traits, class Allocator>
|
---|
| 1849 | void swap(basic_string<Char, Traits, Allocator>& lhs,
|
---|
| 1850 | basic_string<Char, Traits, Allocator>& rhs)
|
---|
| 1851 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 1852 | {
|
---|
| 1853 | lhs.swap(rhs);
|
---|
| 1854 | }
|
---|
| 1855 |
|
---|
| 1856 | /**
|
---|
| 1857 | * 21.4.8.9, inserters and extractors:
|
---|
| 1858 | */
|
---|
| 1859 |
|
---|
| 1860 | // TODO: implement
|
---|
| 1861 |
|
---|
| 1862 | /**
|
---|
| 1863 | * 21.5, numeric conversions:
|
---|
| 1864 | */
|
---|
| 1865 |
|
---|
| 1866 | int stoi(const string& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1867 | long stol(const string& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1868 | unsigned long stoul(const string& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1869 | long long stoll(const string& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1870 | unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1871 |
|
---|
| 1872 | float stof(const string& str, size_t* idx = nullptr);
|
---|
| 1873 | double stod(const string& str, size_t* idx = nullptr);
|
---|
| 1874 | long double stold(const string& str, size_t* idx = nullptr);
|
---|
| 1875 |
|
---|
| 1876 | string to_string(int val);
|
---|
| 1877 | string to_string(unsigned val);
|
---|
| 1878 | string to_string(long val);
|
---|
| 1879 | string to_string(unsigned long val);
|
---|
| 1880 | string to_string(long long val);
|
---|
| 1881 | string to_string(unsigned long long val);
|
---|
| 1882 | string to_string(float val);
|
---|
| 1883 | string to_string(double val);
|
---|
| 1884 | string to_string(long double val);
|
---|
| 1885 |
|
---|
| 1886 | int stoi(const wstring& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1887 | long stol(const wstring& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1888 | unsigned long stoul(const wstring& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1889 | long long stoll(const wstring& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1890 | unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
|
---|
| 1891 |
|
---|
| 1892 | float stof(const wstring& str, size_t* idx = nullptr);
|
---|
| 1893 | double stod(const wstring& str, size_t* idx = nullptr);
|
---|
| 1894 | long double stold(const wstring& str, size_t* idx = nullptr);
|
---|
| 1895 |
|
---|
| 1896 | wstring to_wstring(int val);
|
---|
| 1897 | wstring to_wstring(unsigned val);
|
---|
| 1898 | wstring to_wstring(long val);
|
---|
| 1899 | wstring to_wstring(unsigned long val);
|
---|
| 1900 | wstring to_wstring(long long val);
|
---|
| 1901 | wstring to_wstring(unsigned long long val);
|
---|
| 1902 | wstring to_wstring(float val);
|
---|
| 1903 | wstring to_wstring(double val);
|
---|
| 1904 | wstring to_wstring(long double val);
|
---|
| 1905 |
|
---|
| 1906 | /**
|
---|
| 1907 | * 21.6, hash support:
|
---|
| 1908 | */
|
---|
| 1909 |
|
---|
| 1910 | // TODO: implement
|
---|
| 1911 |
|
---|
| 1912 | /**
|
---|
| 1913 | * 21.7, suffix for basic_string literals:
|
---|
| 1914 | */
|
---|
| 1915 |
|
---|
| 1916 | /**
|
---|
| 1917 | * Note: According to the standard, literal suffixes that do not
|
---|
| 1918 | * start with an underscore are reserved for future standardization,
|
---|
| 1919 | * but since we are implementing the standard, we're going to ignore it.
|
---|
| 1920 | * This should work (according to their documentation) work for clang,
|
---|
| 1921 | * but that should be tested.
|
---|
| 1922 | */
|
---|
| 1923 | #pragma GCC diagnostic push
|
---|
| 1924 | #pragma GCC diagnostic ignored "-Wliteral-suffix"
|
---|
| 1925 | string operator "" s(const char* str, size_t len);
|
---|
| 1926 | u16string operator "" s(const char16_t* str, size_t len);
|
---|
| 1927 | u32string operator "" s(const char32_t* str, size_t len);
|
---|
| 1928 |
|
---|
| 1929 | /* Problem: wchar_t == int in HelenOS, but standard forbids it.
|
---|
| 1930 | wstring operator "" s(const wchar_t* str, size_t len);
|
---|
| 1931 | */
|
---|
| 1932 | #pragma GCC diagnostic pop
|
---|
[52d025c] | 1933 | }
|
---|
| 1934 |
|
---|
| 1935 | #endif
|
---|