2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author: Eric Niebler (eniebler)
18 // Fixed-size string type, for constexpr string handling.
24 #include <initializer_list>
28 #include <type_traits>
31 #include <folly/portability/BitsFunctexcept.h>
32 #include <folly/portability/Constexpr.h>
34 // Define FOLLY_USE_CPP14_CONSTEXPR to be true if the compiler's C++14
35 // constexpr support is "good enough".
36 #ifndef FOLLY_USE_CPP14_CONSTEXPR
37 #if defined(__clang__)
38 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201300L
39 #elif defined(__GNUC__)
40 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201304L
42 #define FOLLY_USE_CPP14_CONSTEXPR 0 // MSVC?
46 #if FOLLY_USE_CPP14_CONSTEXPR
47 #define FOLLY_CPP14_CONSTEXPR constexpr
49 #define FOLLY_CPP14_CONSTEXPR inline
54 template <class Char, std::size_t N>
55 class BasicFixedString;
57 template <std::size_t N>
58 using FixedString = BasicFixedString<char, N>;
61 namespace fixedstring {
63 // This is a template so that the class static npos can be defined in the
65 template <class = void>
66 struct FixedStringBase_ {
67 static constexpr std::size_t npos = static_cast<std::size_t>(-1);
71 constexpr std::size_t FixedStringBase_<Void>::npos;
73 using FixedStringBase = FixedStringBase_<>;
75 template <class Char, std::size_t N>
76 constexpr std::size_t size(const Char (&)[N]) noexcept {
80 template <class Char, std::size_t N>
81 constexpr std::size_t size(const BasicFixedString<Char, N>& s) noexcept {
85 // Intentionally NOT constexpr. By making this not constexpr, we make
86 // checkOverflow below ill-formed in a constexpr context when the condition
87 // it's testing for fails. In this way, precondition violations are reported
88 // at compile-time instead of at runtime.
89 [[noreturn]] inline void assertOutOfBounds() {
90 assert(false && "Array index out of bounds in BasicFixedString");
91 std::__throw_out_of_range("Array index out of bounds in BasicFixedString");
94 constexpr std::size_t checkOverflow(std::size_t i, std::size_t max) {
95 return i <= max ? i : (assertOutOfBounds(), max);
98 constexpr std::size_t checkOverflowOrNpos(std::size_t i, std::size_t max) {
99 return i == FixedStringBase::npos
101 : (i <= max ? i : (assertOutOfBounds(), max));
104 // Intentionally NOT constexpr. See note above for assertOutOfBounds
105 inline void assertOutOfBoundsNothrow() {
106 assert(false && "Array index out of bounds in BasicFixedString");
109 constexpr std::size_t checkOverflowNothrow(std::size_t i, std::size_t max) {
110 return i <= max ? i : (assertOutOfBoundsNothrow(), i);
113 // Intentionally NOT constexpr. See note above for assertOutOfBounds
114 inline void assertNotNullTerminated() noexcept {
117 "Non-null terminated string used to initialize a BasicFixedString");
120 // Parsing help for human readers: the following is a constexpr noexcept
121 // function that accepts a reference to an array as a parameter and returns
122 // a reference to the same array.
123 template <class Char, std::size_t N>
124 constexpr const Char (&checkNullTerminated(const Char (&a)[N]) noexcept)[N] {
125 // Strange decltype(a)(a) used to make MSVC happy.
126 return a[N - 1u] == Char(0)
128 // In Debug mode, guard against embedded nulls:
129 && N - 1u == folly::detail::constexpr_strlen_internal(a, 0u)
132 : (assertNotNullTerminated(), decltype(a)(a));
135 enum class Cmp : int { LT = -1, EQ = 0, GT = 1 };
137 template <class Left, class Right>
138 constexpr Cmp compare_(
140 std::size_t left_pos,
141 std::size_t left_size,
143 std::size_t right_pos,
144 std::size_t right_size) noexcept {
145 return left_pos == left_size
146 ? (right_pos == right_size ? Cmp::EQ : Cmp::LT)
147 : (right_pos == right_size ? Cmp::GT
148 : (left[left_pos] < right[right_pos]
150 : (left[left_pos] > right[right_pos]
152 : fixedstring::compare_(
161 template <class Left, class Right>
162 constexpr bool equal_(
164 std::size_t left_size,
166 std::size_t right_size) noexcept {
167 return left_size == right_size &&
168 Cmp::EQ == compare_(left, 0u, left_size, right, 0u, right_size);
171 template <class Char, class Left, class Right>
173 char_at_(const Left& left, const Right& right, std::size_t i) noexcept {
174 return i < fixedstring::size(left)
176 : i < (fixedstring::size(left) + fixedstring::size(right))
177 ? right[i - fixedstring::size(left)]
181 template <class Char, class Left, class Right>
182 constexpr Char char_at_(
184 std::size_t left_pos,
185 std::size_t left_count,
187 std::size_t right_pos,
188 std::size_t right_count,
189 std::size_t i) noexcept {
192 : (i < right_count + left_pos
193 ? right[i - left_pos + right_pos]
194 : (i < fixedstring::size(left) - left_count + right_count
195 ? left[i - right_count + left_count]
199 template <class Left, class Right>
200 constexpr bool find_at_(
204 std::size_t count) noexcept {
205 return 0u == count || (left[pos + count - 1u] == right[count - 1u] &&
206 find_at_(left, right, pos, count - 1u));
209 template <class Char, class Right>
211 find_one_of_at_(Char ch, const Right& right, std::size_t pos) noexcept {
213 (ch == right[pos - 1u] || find_one_of_at_(ch, right, pos - 1u));
216 template <class Left, class Right>
217 constexpr std::size_t find_(
221 std::size_t count) noexcept {
222 return find_at_(left, right, pos, count)
224 : fixedstring::size(left) <= pos + count
225 ? FixedStringBase::npos
226 : find_(left, right, pos + 1u, count);
229 template <class Left, class Right>
230 constexpr std::size_t rfind_(
234 std::size_t count) noexcept {
235 return find_at_(left, right, pos, count)
237 : 0u == pos ? FixedStringBase::npos
238 : rfind_(left, right, pos - 1u, count);
241 template <class Left, class Right>
242 constexpr std::size_t find_first_of_(
246 std::size_t count) noexcept {
247 return find_one_of_at_(left[pos], right, count)
249 : fixedstring::size(left) <= pos + 1u
250 ? FixedStringBase::npos
251 : find_first_of_(left, right, pos + 1u, count);
254 template <class Left, class Right>
255 constexpr std::size_t find_first_not_of_(
259 std::size_t count) noexcept {
260 return !find_one_of_at_(left[pos], right, count)
262 : fixedstring::size(left) <= pos + 1u
263 ? FixedStringBase::npos
264 : find_first_not_of_(left, right, pos + 1u, count);
267 template <class Left, class Right>
268 constexpr std::size_t find_last_of_(
272 std::size_t count) noexcept {
273 return find_one_of_at_(left[pos], right, count)
275 : 0u == pos ? FixedStringBase::npos
276 : find_last_of_(left, right, pos - 1u, count);
279 template <class Left, class Right>
280 constexpr std::size_t find_last_not_of_(
284 std::size_t count) noexcept {
285 return !find_one_of_at_(left[pos], right, count)
287 : 0u == pos ? FixedStringBase::npos
288 : find_last_not_of_(left, right, pos - 1u, count);
292 template <class Char, class Left, class Right, std::size_t... Is>
293 static constexpr BasicFixedString<Char, sizeof...(Is)> concat_(
296 std::index_sequence<Is...> is) noexcept {
297 return {left, right, is};
300 template <class Char, class Left, class Right, std::size_t... Is>
301 static constexpr BasicFixedString<Char, sizeof...(Is)> replace_(
303 std::size_t left_pos,
304 std::size_t left_count,
306 std::size_t right_pos,
307 std::size_t right_count,
308 std::index_sequence<Is...> is) noexcept {
309 return {left, left_pos, left_count, right, right_pos, right_count, is};
314 FOLLY_CPP14_CONSTEXPR void constexpr_swap(T& a, T& b) noexcept(
315 noexcept(a = T(std::move(a)))) {
316 T tmp((std::move(a)));
321 // FUTURE: use const_log2 to fold instantiations of BasicFixedString together.
322 // All BasicFixedString<C, N> instantiations could share the implementation
323 // of BasicFixedString<C, M>, where M is the next highest power of 2 after N.
325 // Also, because of alignment of the data_ and size_ members, N should never be
326 // smaller than `(alignof(std::size_t)/sizeof(C))-1` (-1 because of the null
327 // terminator). OR, create a specialization for BasicFixedString<C, 0u> that
328 // does not have a size_ member, since it is unnecessary.
329 constexpr std::size_t const_log2(std::size_t N, std::size_t log2 = 0u) {
330 return N / 2u == 0u ? log2 : const_log2(N / 2u, log2 + 1u);
333 // For constexpr reverse iteration over a BasicFixedString
335 struct ReverseIterator {
341 using other = typename std::conditional<
342 std::is_const<T>::value,
343 ReverseIterator<typename std::remove_const<T>::type>,
347 using value_type = typename std::remove_const<T>::type;
348 using reference = T&;
350 using difference_type = std::ptrdiff_t;
351 using iterator_category = std::random_access_iterator_tag;
353 constexpr ReverseIterator() = default;
354 constexpr ReverseIterator(const ReverseIterator&) = default;
355 FOLLY_CPP14_CONSTEXPR ReverseIterator& operator=(const ReverseIterator&) =
357 constexpr explicit ReverseIterator(T* p) noexcept : p_(p) {}
358 constexpr /* implicit */ ReverseIterator(const other& that) noexcept
360 friend constexpr bool operator==(
362 ReverseIterator b) noexcept {
365 friend constexpr bool operator!=(
367 ReverseIterator b) noexcept {
370 constexpr reference operator*() const {
373 FOLLY_CPP14_CONSTEXPR ReverseIterator& operator++() noexcept {
377 FOLLY_CPP14_CONSTEXPR ReverseIterator operator++(int)noexcept {
382 FOLLY_CPP14_CONSTEXPR ReverseIterator& operator--() noexcept {
386 FOLLY_CPP14_CONSTEXPR ReverseIterator operator--(int)noexcept {
391 FOLLY_CPP14_CONSTEXPR ReverseIterator& operator+=(std::ptrdiff_t i) noexcept {
395 friend constexpr ReverseIterator operator+(
397 ReverseIterator that) noexcept {
398 return ReverseIterator{that.p_ - i};
400 friend constexpr ReverseIterator operator+(
401 ReverseIterator that,
402 std::ptrdiff_t i) noexcept {
403 return ReverseIterator{that.p_ - i};
405 FOLLY_CPP14_CONSTEXPR ReverseIterator& operator-=(std::ptrdiff_t i) noexcept {
409 friend constexpr ReverseIterator operator-(
410 ReverseIterator that,
411 std::ptrdiff_t i) noexcept {
412 return ReverseIterator{that.p_ + i};
414 friend constexpr std::ptrdiff_t operator-(
416 ReverseIterator b) noexcept {
419 constexpr reference operator[](std::ptrdiff_t i) const noexcept {
424 } // namespace fixedstring
425 } // namespace detail
427 // Defined in folly/Range.h
428 template <class Iter>
431 // Defined in folly/Hash.h
432 std::uint32_t hsieh_hash32_buf(const void* buf, std::size_t len);
434 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
435 * \class BasicFixedString
437 * \tparam Char The character type. Must be a scalar type.
438 * \tparam N The capacity and max size of string instances of this type.
440 * \brief A class for holding up to `N` characters of type `Char` that is
441 * amenable to `constexpr` string manipulation. It is guaranteed to not
442 * perform any dynamic allocation.
444 * `BasicFixedString` is a `std::string` work-alike that stores characters in an
445 * internal buffer. It has minor interface differences that make it easy to work
446 * with strings in a `constexpr` context.
451 * constexpr auto hello = makeFixedString("hello"); // a FixedString<5>
452 * constexpr auto world = makeFixedString("world"); // a FixedString<5>
453 * constexpr auto hello_world = hello + ' ' + world + '!'; // a FixedString<12>
454 * static_assert(hello_world == "hello world!", "neato!");
457 * `FixedString<N>` is an alias for `BasicFixedString<char, N>`.
459 * \par Constexpr and In-place Mutation
461 * On a C++14 compiler, `BasicFixedString` supports the full `std::string`
462 * interface as `constexpr` member functions. On a C++11 compiler, the mutating
463 * members are not `constexpr`, but non-mutating alternatives, which create a
464 * new string, can be used instead. For example, instead of this:
467 * constexpr FixedString<10> replace_example_cpp14() {
468 * FixedString<10> test{"****"};
469 * test.replace(1, 2, "!!!!");
470 * return test; // returns "*!!!!*"
474 * You might write this instead:
477 * constexpr FixedString<10> replace_example_cpp11() {
478 * // GNU compilers have an extension that make it possible to create
479 * // FixedString objects with a `""_fs` user-defined literal.
480 * using namespace folly;
481 * return makeFixedString("****").creplace(1, 2, "!!!!"); // "*!!!!*"
485 * \par User-defined Literals
486 * Instead of using the `folly::makeFixedString` helper function, you can use
487 * a user-defined literal to make `FixedString` instances. The UDL feature of
488 * C++ has some limitations that make this less than ideal; you must tell the
489 * compiler roughly how many characters are in the string. The suffixes `_fs4`,
490 * `_fs8`, `_fs16`, `_fs32`, `_fs64`, and `_fs128` exist to create instances
491 * of types `FixedString<4>`, `FixedString<8>`, etc. For example:
494 * using namespace folly::StringLiterals;
495 * constexpr auto hello = "hello"_fs8; // A FixedString<8> containing "hello"
498 * See Error Handling below for what to expect when you try to exceed the
499 * capacity of a `FixedString` by storing too many characters in it.
501 * If your compiler supports GNU extensions, there is one additional suffix you
502 * can use: `_fs`. This suffix always creates `FixedString` objects of exactly
503 * the right size. For example:
506 * using namespace folly::StringLiterals;
507 * // NOTE: Only works on compilers with GNU extensions enabled. Clang and
508 * // gcc support this (-Wgnu-string-literal-operator-template):
509 * constexpr auto hello = "hello"_fs; // A FixedString<5> containing "hello"
512 * \par Error Handling:
513 * The capacity of a `BasicFixedString` is set at compile time. When the user
514 * asks the string to exceed its capacity, one of three things will happen,
515 * depending on the context:
517 * -# If the attempt is made while evaluating a constant expression, the
518 * program will fail to compile.
519 * -# Otherwise, if the program is being run in debug mode, it will `assert`.
520 * -# Otherwise, the failed operation will throw a `std::out_of_range`
523 * This is also the case if an invalid offset is passed to any member function,
524 * or if `pop_back` or `cpop_back` is called on an empty `BasicFixedString`.
526 * Member functions documented as having preconditions will assert in Debug
527 * mode (`!defined(NDEBUG)`) on precondition failures. Those documented with
528 * \b Throws clauses will throw the specified exception on failure. Those with
529 * both a precondition and a \b Throws clause will assert in Debug and throw
532 template <class Char, std::size_t N>
533 class BasicFixedString : private detail::fixedstring::FixedStringBase {
535 template <class, std::size_t>
536 friend class BasicFixedString;
537 friend struct detail::fixedstring::Helper;
539 Char data_[N + 1u]; // +1 for the null terminator
540 std::size_t size_; // Nbr of chars, not incl. null terminator. size_ <= N.
542 using Indices = std::make_index_sequence<N>;
544 template <class That, std::size_t... Is>
545 constexpr BasicFixedString(
548 std::index_sequence<Is...>,
550 std::size_t count = npos) noexcept
551 : data_{(Is < (size - pos) && Is < count ? that[Is + pos] : Char(0))...,
553 size_{folly::constexpr_min(size - pos, count)} {}
555 template <std::size_t... Is>
556 constexpr BasicFixedString(
559 std::index_sequence<Is...>) noexcept
560 : data_{((Is < count) ? ch : Char(0))..., Char(0)}, size_{count} {}
562 // Concatenation constructor
563 template <class Left, class Right, std::size_t... Is>
564 constexpr BasicFixedString(
567 std::index_sequence<Is...>) noexcept
568 : data_{detail::fixedstring::char_at_<Char>(left, right, Is)..., Char(0)},
569 size_{detail::fixedstring::size(left) +
570 detail::fixedstring::size(right)} {}
572 // Replace constructor
573 template <class Left, class Right, std::size_t... Is>
574 constexpr BasicFixedString(
576 std::size_t left_pos,
577 std::size_t left_count,
579 std::size_t right_pos,
580 std::size_t right_count,
581 std::index_sequence<Is...>) noexcept
582 : data_{detail::fixedstring::char_at_<Char>(
591 size_{detail::fixedstring::size(left) - left_count + right_count} {}
594 using size_type = std::size_t;
595 using difference_type = std::ptrdiff_t;
596 using reference = Char&;
597 using const_reference = const Char&;
598 using pointer = Char*;
599 using const_pointer = const Char*;
600 using iterator = Char*;
601 using const_iterator = const Char*;
602 using reverse_iterator = detail::fixedstring::ReverseIterator<Char>;
603 using const_reverse_iterator =
604 detail::fixedstring::ReverseIterator<const Char>;
606 using detail::fixedstring::FixedStringBase::npos;
608 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
610 * \post `size() == 0`
611 * \post `at(0) == Char(0)`
613 constexpr BasicFixedString() : data_{}, size_{} {}
615 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
617 * \post `size() == that.size()`
618 * \post `0 == strncmp(data(), that.data(), size())`
619 * \post `at(size()) == Char(0)`
621 constexpr BasicFixedString(const BasicFixedString& /*that*/) = default;
623 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
624 * Construct from a differently-sized BasicFixedString
625 * \pre `that.size() <= N`
626 * \post `size() == that.size()`
627 * \post `0 == strncmp(data(), that.data(), size())`
628 * \post `at(size()) == Char(0)`
629 * \throw std::out_of_range when that.size() > N. When M <= N, this
630 * constructor will never throw.
631 * \note Conversions from larger-capacity BasicFixedString objects to smaller
632 * ones (`M > N`) are allowed as long as the *size()* of the source string
635 template <std::size_t M>
636 constexpr /* implicit */ BasicFixedString(
637 const BasicFixedString<Char, M>& that) noexcept(M <= N)
638 : BasicFixedString{that, 0u, that.size_} {}
640 // Why is this deleted? To avoid confusion with the constructor that takes
641 // a const Char* and a count.
642 template <std::size_t M>
643 constexpr BasicFixedString(
644 const BasicFixedString<Char, M>& that,
645 std::size_t pos) noexcept(false) = delete;
647 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
648 * Construct from an BasicFixedString, an offset, and a count
649 * \param that The source string
650 * \param pos The starting position in `that`
651 * \param count The number of characters to copy. If `npos`, `count` is taken
652 * to be `that.size()-pos`.
653 * \pre `pos <= that.size()`
654 * \pre `count <= that.size()-pos && count <= N`
655 * \post `size() == count`
656 * \post `0 == strncmp(data(), that.data()+pos, size())`
657 * \post `at(size()) == Char(0)`
658 * \throw std::out_of_range when pos+count > that.size(), or when
661 template <std::size_t M>
662 constexpr BasicFixedString(
663 const BasicFixedString<Char, M>& that,
665 std::size_t count) noexcept(false)
669 std::make_index_sequence<(M < N ? M : N)>{},
671 detail::fixedstring::checkOverflow(
672 detail::fixedstring::checkOverflowOrNpos(
675 detail::fixedstring::checkOverflow(pos, that.size_)),
678 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
679 * Construct from a string literal
681 * \pre `that[M-1] == Char(0)`
682 * \post `0 == strncmp(data(), that, M-1)`
683 * \post `size() == M-1`
684 * \post `at(size()) == Char(0)`
686 template <std::size_t M, class = typename std::enable_if<(M - 1u <= N)>::type>
687 constexpr /* implicit */ BasicFixedString(const Char (&that)[M]) noexcept
688 : BasicFixedString{detail::fixedstring::checkNullTerminated(that),
690 std::make_index_sequence<M - 1u>{}} {}
692 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
693 * Construct from a `const Char*` and count
694 * \pre `that` points to an array of at least `count` characters.
696 * \post `size() == count`
697 * \post `0 == strncmp(data(), that, size())`
698 * \post `at(size()) == Char(0)`
699 * \throw std::out_of_range when count > N
701 constexpr BasicFixedString(const Char* that, std::size_t count) noexcept(
703 : BasicFixedString{that,
704 detail::fixedstring::checkOverflow(count, N),
707 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
708 * Construct an BasicFixedString that contains `count` characters, all
711 * \post `size() == count`
712 * \post `npos == find_first_not_of(ch)`
713 * \post `at(size()) == Char(0)`
714 * \throw std::out_of_range when count > N
716 constexpr BasicFixedString(std::size_t count, Char ch) noexcept(false)
717 : BasicFixedString{detail::fixedstring::checkOverflow(count, N),
721 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
722 * Construct an BasicFixedString from a `std::initializer_list` of
724 * \pre `il.size() <= N`
725 * \post `size() == count`
726 * \post `0 == strncmp(data(), il.begin(), size())`
727 * \post `at(size()) == Char(0)`
728 * \throw std::out_of_range when il.size() > N
730 constexpr BasicFixedString(std::initializer_list<Char> il) noexcept(false)
731 : BasicFixedString{il.begin(), il.size()} {}
733 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator=(
734 const BasicFixedString&) noexcept = default;
736 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
737 * Assign from a `BasicFixedString<Char, M>`.
738 * \pre `that.size() <= N`
739 * \post `size() == that.size()`
740 * \post `0 == strncmp(data(), that.begin(), size())`
741 * \post `at(size()) == Char(0)`
742 * \throw std::out_of_range when that.size() > N. When M <= N, this
743 * assignment operator will never throw.
744 * \note Assignments from larger-capacity BasicFixedString objects to smaller
745 * ones (`M > N`) are allowed as long as the *size* of the source string is
749 template <std::size_t M>
750 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator=(
751 const BasicFixedString<Char, M>& that) noexcept(M <= N) {
752 detail::fixedstring::checkOverflow(that.size_, N);
753 size_ = that.copy(data_, that.size_);
754 data_[size_] = Char(0);
758 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
759 * Assign from a null-terminated array of characters.
761 * \pre `that` has no embedded null characters
762 * \pre `that[M-1]==Char(0)`
763 * \post `size() == M-1`
764 * \post `0 == strncmp(data(), that, size())`
765 * \post `at(size()) == Char(0)`
768 template <std::size_t M, class = typename std::enable_if<(M - 1u <= N)>::type>
769 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator=(
770 const Char (&that)[M]) noexcept {
771 return assign(detail::fixedstring::checkNullTerminated(that), M - 1u);
774 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
775 * Assign from an `initializer_list` of characters.
776 * \pre `il.size() <= N`
777 * \post `size() == il.size()`
778 * \post `0 == strncmp(data(), il.begin(), size())`
779 * \post `at(size()) == Char(0)`
780 * \throw std::out_of_range when il.size() > N
783 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator=(
784 std::initializer_list<Char> il) noexcept(false) {
785 detail::fixedstring::checkOverflow(il.size(), N);
786 for (std::size_t i = 0u; i < il.size(); ++i) {
787 data_[i] = il.begin()[i];
790 data_[size_] = Char(0);
794 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
795 * Conversion to folly::Range
796 * \return `Range<Iter>{begin(), end()}`
800 class = typename std::enable_if<
801 std::is_convertible<Char*, Iter>::value>::type>
802 FOLLY_CPP14_CONSTEXPR /* implicit */ operator Range<Iter>() noexcept {
803 return {begin(), end()};
811 class = typename std::enable_if<
812 std::is_convertible<const Char*, Iter>::value>::type>
813 constexpr /* implicit */ operator Range<Iter>() const noexcept {
814 return {begin(), end()};
817 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
818 * Conversion to folly::Range
819 * \return `Range<Char*>{begin(), end()}`
821 FOLLY_CPP14_CONSTEXPR Range<Char*> toRange() noexcept {
822 return {begin(), end()};
828 constexpr Range<const Char*> toRange() const noexcept {
829 return {begin(), end()};
832 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
833 * Conversion to std::basic_string<Char>
834 * \return `std::basic_string<Char>{begin(), end()}`
836 /* implicit */ operator std::basic_string<Char>() const noexcept(false) {
837 return std::basic_string<Char>{begin(), end()};
840 std::basic_string<Char> toStdString() const noexcept(false) {
841 return std::basic_string<Char>{begin(), end()};
844 // Think hard about whether this is a good idea. It's certainly better than
845 // an implicit conversion to `const Char*` since `delete "hi"_fs` will fail
846 // to compile. But it creates ambiguities when passing a FixedString to an
847 // API that has overloads for `const char*` and `folly::Range`, for instance.
848 // using ArrayType = Char[N];
849 // FOLLY_CPP14_CONSTEXPR /* implicit */ operator ArrayType&() noexcept {
853 // using ConstArrayType = const Char[N];
854 // constexpr /* implicit */ operator ConstArrayType&() const noexcept {
858 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
859 * Assigns a sequence of `count` characters of value `ch`.
860 * \param count The count of characters.
863 * \post `size() == count`
864 * \post `npos == find_first_not_of(ch)`
865 * \post `at(size()) == Char(0)`
866 * \throw std::out_of_range when count > N
869 FOLLY_CPP14_CONSTEXPR BasicFixedString& assign(
871 Char ch) noexcept(false) {
872 detail::fixedstring::checkOverflow(count, N);
873 for (std::size_t i = 0u; i < count; ++i) {
877 data_[size_] = Char(0);
881 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
882 * Assigns characters from an `BasicFixedString` to this object.
883 * \note Equivalent to `assign(that, 0, that.size())`
885 template <std::size_t M>
886 FOLLY_CPP14_CONSTEXPR BasicFixedString& assign(
887 const BasicFixedString<Char, M>& that) noexcept(M <= N) {
891 // Why is this overload deleted? So users aren't confused by the difference
892 // between str.assign("foo", N) and str.assign("foo"_fs, N). In the former,
893 // N is a count of characters. In the latter, it would be a position, which
894 // totally changes the meaning of the code.
895 template <std::size_t M>
896 FOLLY_CPP14_CONSTEXPR BasicFixedString& assign(
897 const BasicFixedString<Char, M>& that,
898 std::size_t pos) noexcept(false) = delete;
900 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
901 * Assigns `count` characters from an `BasicFixedString` to this object,
902 * starting at position `pos` in the source object.
903 * \param that The source string.
904 * \param pos The starting position in the source string.
905 * \param count The number of characters to copy. If `npos`, `count` is taken
906 * to be `that.size()-pos`.
907 * \pre `pos <= that.size()`
908 * \pre `count <= that.size()-pos`
910 * \post `size() == count`
911 * \post `0 == strncmp(data(), that.begin() + pos, count)`
912 * \post `at(size()) == Char(0)`
913 * \throw std::out_of_range when pos > that.size() or count > that.size()-pos
917 template <std::size_t M>
918 FOLLY_CPP14_CONSTEXPR BasicFixedString& assign(
919 const BasicFixedString<Char, M>& that,
921 std::size_t count) noexcept(false) {
922 detail::fixedstring::checkOverflow(pos, that.size_);
925 detail::fixedstring::checkOverflowOrNpos(count, that.size_ - pos));
928 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
929 * Assigns characters from an `BasicFixedString` to this object.
930 * \pre `that` contains no embedded nulls.
931 * \pre `that[M-1] == Char(0)`
932 * \note Equivalent to `assign(that, M - 1)`
934 template <std::size_t M, class = typename std::enable_if<(M - 1u <= N)>::type>
935 FOLLY_CPP14_CONSTEXPR BasicFixedString& assign(
936 const Char (&that)[M]) noexcept {
937 return assign(detail::fixedstring::checkNullTerminated(that), M - 1u);
940 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
941 * Assigns `count` characters from a range of characters to this object.
942 * \param that A pointer to a range of characters.
943 * \param count The number of characters to copy.
944 * \pre `that` points to at least `count` characters.
946 * \post `size() == count`
947 * \post `0 == strncmp(data(), that, count)`
948 * \post `at(size()) == Char(0)`
949 * \throw std::out_of_range when count > N
952 FOLLY_CPP14_CONSTEXPR BasicFixedString& assign(
954 std::size_t count) noexcept(false) {
955 detail::fixedstring::checkOverflow(count, N);
956 for (std::size_t i = 0u; i < count; ++i) {
960 data_[size_] = Char(0);
964 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
965 * Swap the contents of this string with `that`.
967 FOLLY_CPP14_CONSTEXPR void swap(BasicFixedString& that) noexcept {
968 // less-than-or-equal here to copy the null terminator:
969 for (std::size_t i = 0u; i <= folly::constexpr_max(size_, that.size_);
971 detail::fixedstring::constexpr_swap(data_[i], that.data_[i]);
973 detail::fixedstring::constexpr_swap(size_, that.size_);
976 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
977 * Return a pointer to a range of `size()+1` characters, the last of which
980 FOLLY_CPP14_CONSTEXPR Char* data() noexcept {
987 constexpr const Char* data() const noexcept {
991 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
994 constexpr const Char* c_str() const noexcept {
998 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1001 FOLLY_CPP14_CONSTEXPR Char* begin() noexcept {
1008 constexpr const Char* begin() const noexcept {
1012 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1015 constexpr const Char* cbegin() const noexcept {
1019 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1020 * \return `data() + size()`.
1022 FOLLY_CPP14_CONSTEXPR Char* end() noexcept {
1023 return data_ + size_;
1029 constexpr const Char* end() const noexcept {
1030 return data_ + size_;
1033 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1034 * \return `data() + size()`.
1036 constexpr const Char* cend() const noexcept {
1040 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1041 * Returns a reverse iterator to the first character of the reversed string.
1042 * It corresponds to the last + 1 character of the non-reversed string.
1044 FOLLY_CPP14_CONSTEXPR reverse_iterator rbegin() noexcept {
1045 return reverse_iterator{data_ + size_};
1051 constexpr const_reverse_iterator rbegin() const noexcept {
1052 return const_reverse_iterator{data_ + size_};
1056 * \note Equivalent to `rbegin()` on a const-qualified reference to `*this`.
1058 constexpr const_reverse_iterator crbegin() const noexcept {
1062 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1063 * Returns a reverse iterator to the last + 1 character of the reversed
1064 * string. It corresponds to the first character of the non-reversed string.
1066 FOLLY_CPP14_CONSTEXPR reverse_iterator rend() noexcept {
1067 return reverse_iterator{data_};
1073 constexpr const_reverse_iterator rend() const noexcept {
1074 return const_reverse_iterator{data_};
1078 * \note Equivalent to `rend()` on a const-qualified reference to `*this`.
1080 constexpr const_reverse_iterator crend() const noexcept {
1085 * \return The number of `Char` elements in the string.
1087 constexpr std::size_t size() const noexcept {
1092 * \return The number of `Char` elements in the string.
1094 constexpr std::size_t length() const noexcept {
1099 * \return True if and only if `size() == 0`.
1101 constexpr bool empty() const noexcept {
1108 static constexpr std::size_t capacity() noexcept {
1115 static constexpr std::size_t max_size() noexcept {
1119 // We would need to reimplement folly::Hash to make this
1121 std::uint32_t hash() const noexcept {
1122 return folly::hsieh_hash32_buf(data_, size_);
1126 * \note `at(size())` is allowed will return `Char(0)`.
1127 * \return `*(data() + i)`
1128 * \throw std::out_of_range when i > size()
1130 FOLLY_CPP14_CONSTEXPR Char& at(std::size_t i) noexcept(false) {
1133 : (std::__throw_out_of_range("Out of range in BasicFixedString::at"),
1140 constexpr const Char& at(std::size_t i) const noexcept(false) {
1143 : (std::__throw_out_of_range("Out of range in BasicFixedString::at"),
1148 * \pre `i <= size()`
1149 * \note `(*this)[size()]` is allowed will return `Char(0)`.
1150 * \return `*(data() + i)`
1152 FOLLY_CPP14_CONSTEXPR Char& operator[](std::size_t i) noexcept {
1156 return data_[detail::fixedstring::checkOverflowNothrow(i, size_)];
1163 constexpr const Char& operator[](std::size_t i) const noexcept {
1167 return data_[detail::fixedstring::checkOverflowNothrow(i, size_)];
1172 * \note Equivalent to `(*this)[0]`
1174 FOLLY_CPP14_CONSTEXPR Char& front() noexcept(false) {
1181 constexpr const Char& front() const noexcept(false) {
1186 * \note Equivalent to `at(size()-1)`
1189 FOLLY_CPP14_CONSTEXPR Char& back() noexcept(false) {
1191 return data_[size_ - 1u];
1193 return data_[size_ - detail::fixedstring::checkOverflowNothrow(1u, size_)];
1200 constexpr const Char& back() const noexcept(false) {
1202 return data_[size_ - 1u];
1204 return data_[size_ - detail::fixedstring::checkOverflowNothrow(1u, size_)];
1209 * Clears the contents of this string.
1210 * \post `size() == 0u`
1211 * \post `at(size()) == Char(0)`
1213 FOLLY_CPP14_CONSTEXPR void clear() noexcept {
1214 data_[0u] = Char(0);
1219 * \note Equivalent to `append(1u, ch)`.
1221 FOLLY_CPP14_CONSTEXPR void push_back(Char ch) noexcept(false) {
1222 detail::fixedstring::checkOverflow(1u, N - size_);
1224 data_[++size_] = Char(0);
1228 * \note Equivalent to `cappend(1u, ch)`.
1230 constexpr BasicFixedString<Char, N + 1u> cpush_back(Char ch) const noexcept {
1235 * Removes the last character from the string.
1237 * \post `size()` is one fewer than before calling `pop_back()`.
1238 * \post `at(size()) == Char(0)`
1239 * \post The characters in the half-open range `[0,size()-1)` are unmodified.
1240 * \throw std::out_of_range if empty().
1242 FOLLY_CPP14_CONSTEXPR void pop_back() noexcept(false) {
1243 detail::fixedstring::checkOverflow(1u, size_);
1245 data_[size_] = Char(0);
1249 * Returns a new string with the first `size()-1` characters from this string.
1251 * \note Equivalent to `BasicFixedString<Char, N-1u>{*this, 0u, size()-1u}`
1252 * \throw std::out_of_range if empty().
1254 constexpr BasicFixedString<Char, N - 1u> cpop_back() const noexcept(false) {
1255 return {*this, 0u, size_ - detail::fixedstring::checkOverflow(1u, size_)};
1258 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1259 * Appends `count` copies of `ch` to this string.
1260 * \pre `count + old_size <= N`
1261 * \post The first `old_size` characters of the string are unmodified.
1262 * \post `size() == old_size + count`
1263 * \throw std::out_of_range if count > N - size().
1265 FOLLY_CPP14_CONSTEXPR BasicFixedString& append(
1267 Char ch) noexcept(false) {
1268 detail::fixedstring::checkOverflow(count, N - size_);
1269 for (std::size_t i = 0u; i < count; ++i)
1270 data_[size_ + i] = ch;
1272 data_[size_] = Char(0);
1277 * \note Equivalent to `append(*this, 0, that.size())`.
1279 template <std::size_t M>
1280 FOLLY_CPP14_CONSTEXPR BasicFixedString& append(
1281 const BasicFixedString<Char, M>& that) noexcept(false) {
1282 return append(that, 0u, that.size_);
1285 // Why is this overload deleted? So as not to get confused with
1286 // append("null-terminated", N), where N would be a count instead
1288 template <std::size_t M>
1289 FOLLY_CPP14_CONSTEXPR BasicFixedString& append(
1290 const BasicFixedString<Char, M>& that,
1291 std::size_t pos) noexcept(false) = delete;
1294 * Appends `count` characters from another string to this one, starting at a
1295 * given offset, `pos`.
1296 * \param that The source string.
1297 * \param pos The starting position in the source string.
1298 * \param count The number of characters to append. If `npos`, `count` is
1299 * taken to be `that.size()-pos`.
1300 * \pre `pos <= that.size()`
1301 * \pre `count <= that.size() - pos`
1302 * \pre `old_size + count <= N`
1303 * \post The first `old_size` characters of the string are unmodified.
1304 * \post `size() == old_size + count`
1305 * \post `at(size()) == Char(0)`
1306 * \throw std::out_of_range if pos + count > that.size() or if
1307 * `old_size + count > N`.
1309 template <std::size_t M>
1310 FOLLY_CPP14_CONSTEXPR BasicFixedString& append(
1311 const BasicFixedString<Char, M>& that,
1313 std::size_t count) noexcept(false) {
1314 detail::fixedstring::checkOverflow(pos, that.size_);
1315 count = detail::fixedstring::checkOverflowOrNpos(count, that.size_ - pos);
1316 detail::fixedstring::checkOverflow(count, N - size_);
1317 for (std::size_t i = 0u; i < count; ++i)
1318 data_[size_ + i] = that[pos + i];
1320 data_[size_] = Char(0);
1325 * \note Equivalent to `append(that, strlen(that))`.
1327 FOLLY_CPP14_CONSTEXPR BasicFixedString& append(const Char* that) noexcept(
1329 return append(that, folly::constexpr_strlen(that));
1333 * Appends `count` characters from the specified character array.
1334 * \pre `that` points to a range of at least `count` characters.
1335 * \pre `count + old_size <= N`
1336 * \post The first `old_size` characters of the string are unmodified.
1337 * \post `size() == old_size + count`
1338 * \post `at(size()) == Char(0)`
1339 * \throw std::out_of_range if old_size + count > N.
1341 FOLLY_CPP14_CONSTEXPR BasicFixedString& append(
1343 std::size_t count) noexcept(false) {
1344 detail::fixedstring::checkOverflow(count, N - size_);
1345 for (std::size_t i = 0u; i < count; ++i)
1346 data_[size_ + i] = that[i];
1348 data_[size_] = Char(0);
1352 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1353 * Creates a new string by appending a character to an existing string, which
1354 * is left unmodified.
1355 * \note Equivalent to `*this + ch`
1357 constexpr BasicFixedString<Char, N + 1u> cappend(Char ch) const noexcept {
1362 * Creates a new string by appending a string to an existing string, which
1363 * is left unmodified.
1364 * \note Equivalent to `*this + ch`
1366 template <std::size_t M>
1367 constexpr BasicFixedString<Char, N + M> cappend(
1368 const BasicFixedString<Char, M>& that) const noexcept {
1369 return *this + that;
1372 // Deleted to avoid confusion with append("char*", N), where N is a count
1373 // instead of a position.
1374 template <std::size_t M>
1375 constexpr BasicFixedString<Char, N + M> cappend(
1376 const BasicFixedString<Char, M>& that,
1377 std::size_t pos) const noexcept(false) = delete;
1380 * Creates a new string by appending characters from one string to another,
1381 * which is left unmodified.
1382 * \note Equivalent to `*this + that.substr(pos, count)`
1384 template <std::size_t M>
1385 constexpr BasicFixedString<Char, N + M> cappend(
1386 const BasicFixedString<Char, M>& that,
1388 std::size_t count) const noexcept(false) {
1389 return creplace(size_, 0u, that, pos, count);
1393 * Creates a new string by appending a string literal to a string,
1394 * which is left unmodified.
1395 * \note Equivalent to `*this + that`
1397 template <std::size_t M>
1398 constexpr BasicFixedString<Char, N + M - 1u> cappend(
1399 const Char (&that)[M]) const noexcept {
1400 return creplace(size_, 0u, that);
1403 // Deleted to avoid confusion with append("char*", N), where N is a count
1404 // instead of a position
1405 template <std::size_t M>
1406 constexpr BasicFixedString<Char, N + M - 1u> cappend(
1407 const Char (&that)[M],
1408 std::size_t pos) const noexcept(false) = delete;
1411 * Creates a new string by appending characters from one string to another,
1412 * which is left unmodified.
1413 * \note Equivalent to `*this + makeFixedString(that).substr(pos, count)`
1415 template <std::size_t M>
1416 constexpr BasicFixedString<Char, N + M - 1u>
1417 cappend(const Char (&that)[M], std::size_t pos, std::size_t count) const
1419 return creplace(size_, 0u, that, pos, count);
1422 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1423 * Appends characters from a null-terminated string literal to this string.
1424 * \note Equivalent to `append(that)`.
1426 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator+=(const Char* that) noexcept(
1428 return append(that);
1432 * Appends characters from another string to this one.
1433 * \note Equivalent to `append(that)`.
1435 template <std::size_t M>
1436 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator+=(
1437 const BasicFixedString<Char, M>& that) noexcept(false) {
1438 return append(that, 0u, that.size_);
1442 * Appends a character to this string.
1443 * \note Equivalent to `push_back(ch)`.
1445 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator+=(Char ch) noexcept(false) {
1451 * Appends characters from an `initializer_list` to this string.
1452 * \note Equivalent to `append(il.begin(), il.size())`.
1454 FOLLY_CPP14_CONSTEXPR BasicFixedString& operator+=(
1455 std::initializer_list<Char> il) noexcept(false) {
1456 return append(il.begin(), il.size());
1459 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1460 * Erase all characters from this string.
1461 * \note Equivalent to `clear()`
1464 FOLLY_CPP14_CONSTEXPR BasicFixedString& erase() noexcept {
1470 * Erases `count` characters from position `pos`. If `count` is `npos`,
1471 * erases from `pos` to the end of the string.
1472 * \pre `pos <= size()`
1473 * \pre `count <= size() - pos || count == npos`
1474 * \post `size() == old_size - min(count, old_size - pos)`
1475 * \post `at(size()) == Char(0)`
1477 * \throw std::out_of_range when pos > size().
1479 FOLLY_CPP14_CONSTEXPR BasicFixedString& erase(
1481 std::size_t count = npos) noexcept(false) {
1482 using A = const Char[1];
1485 detail::fixedstring::checkOverflowOrNpos(
1486 count, size_ - detail::fixedstring::checkOverflow(pos, size_)),
1492 * \note Equivalent to `erase(first - data(), 1)`
1493 * \return A pointer to the first character after the erased character.
1495 FOLLY_CPP14_CONSTEXPR Char* erase(const Char* first) noexcept(false) {
1496 erase(first - data_, 1u);
1497 return data_ + (first - data_);
1501 * \note Equivalent to `erase(first - data(), last - first)`
1502 * \return A pointer to the first character after the erased characters.
1504 FOLLY_CPP14_CONSTEXPR Char* erase(
1506 const Char* last) noexcept(false) {
1507 erase(first - data_, last - first);
1508 return data_ + (first - data_);
1511 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1512 * Create a new string by erasing all the characters from this string.
1513 * \note Equivalent to `BasicFixedString<Char, 0>{}`
1515 constexpr BasicFixedString<Char, 0u> cerase() const noexcept {
1520 * Create a new string by erasing all the characters after position `pos` from
1522 * \note Equivalent to `creplace(pos, min(count, pos - size()), "")`
1524 constexpr BasicFixedString cerase(std::size_t pos, std::size_t count = npos)
1525 const noexcept(false) {
1526 using A = const Char[1];
1529 detail::fixedstring::checkOverflowOrNpos(
1530 count, size_ - detail::fixedstring::checkOverflow(pos, size_)),
1534 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1535 * Compare two strings for lexicographical ordering.
1536 * \note Equivalent to
1537 * `compare(0, size(), that.data(), that.size())`
1539 template <std::size_t M>
1540 constexpr int compare(const BasicFixedString<Char, M>& that) const noexcept {
1541 return compare(0u, size_, that, 0u, that.size_);
1545 * Compare two strings for lexicographical ordering.
1546 * \note Equivalent to
1547 * `compare(this_pos, this_count, that.data(), that.size())`
1549 template <std::size_t M>
1550 constexpr int compare(
1551 std::size_t this_pos,
1552 std::size_t this_count,
1553 const BasicFixedString<Char, M>& that) const noexcept(false) {
1554 return compare(this_pos, this_count, that, 0u, that.size_);
1558 * Compare two strings for lexicographical ordering.
1559 * \note Equivalent to
1560 * `compare(this_pos, this_count, that.data() + that_pos, that_count)`
1562 template <std::size_t M>
1563 constexpr int compare(
1564 std::size_t this_pos,
1565 std::size_t this_count,
1566 const BasicFixedString<Char, M>& that,
1567 std::size_t that_pos,
1568 std::size_t that_count) const noexcept(false) {
1569 return static_cast<int>(detail::fixedstring::compare_(
1571 detail::fixedstring::checkOverflow(this_pos, size_),
1572 detail::fixedstring::checkOverflow(this_count, size_ - this_pos) +
1575 detail::fixedstring::checkOverflow(that_pos, that.size_),
1576 detail::fixedstring::checkOverflow(that_count, that.size_ - that_pos) +
1581 * Compare two strings for lexicographical ordering.
1582 * \note Equivalent to `compare(0, size(), that, strlen(that))`
1584 constexpr int compare(const Char* that) const noexcept {
1585 return compare(0u, size_, that, folly::constexpr_strlen(that));
1589 * Compare two strings for lexicographical ordering.
1590 * \note Equivalent to
1591 * `compare(this_pos, this_count, that, strlen(that))`
1593 constexpr int compare(
1594 std::size_t this_pos,
1595 std::size_t this_count,
1596 const Char* that) const noexcept(false) {
1597 return compare(this_pos, this_count, that, folly::constexpr_strlen(that));
1601 * Compare two strings for lexicographical ordering.
1603 * Let `A` be the the
1604 * character sequence {`(*this)[this_pos]`, ...
1605 * `(*this)[this_pos + this_count - 1]`}. Let `B` be the character sequence
1606 * {`that[0]`, ...`that[count - 1]`}. Then...
1609 * - `< 0` if `A` is ordered before the `B`
1610 * - `> 0` if `B` is ordered before `A`
1611 * - `0` if `A` equals `B`.
1613 * \throw std::out_of_range if this_pos + this_count > size().
1615 constexpr int compare(
1616 std::size_t this_pos,
1617 std::size_t this_count,
1619 std::size_t that_count) const noexcept(false) {
1620 return static_cast<int>(detail::fixedstring::compare_(
1622 detail::fixedstring::checkOverflow(this_pos, size_),
1623 detail::fixedstring::checkOverflowOrNpos(this_count, size_ - this_pos) +
1630 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1631 * Return a substring from `pos` to the end of the string.
1632 * \note Equivalent to `BasicFixedString{*this, pos}`
1634 constexpr BasicFixedString substr(std::size_t pos) const noexcept(false) {
1635 return {*this, pos};
1639 * Return a substring from `pos` to the end of the string.
1640 * \note Equivalent to `BasicFixedString{*this, pos, count}`
1642 constexpr BasicFixedString substr(std::size_t pos, std::size_t count) const
1644 return {*this, pos, count};
1647 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1648 * Replace the characters in the range denoted by the half-open range
1649 * [`first`, `last`) with the string `that`.
1650 * \pre `first` and `last` point to characters within this string (including
1651 * the terminating null).
1652 * \note Equivalent to
1653 * `replace(first - data(), last - first, that.data(), that.size())`
1655 template <std::size_t M>
1656 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1659 const BasicFixedString<Char, M>& that) noexcept(false) {
1660 return replace(first - data_, last - first, that, 0u, that.size_);
1664 * Replace `this_count` characters starting from position `this_pos` with the
1665 * characters from string `that` starting at position `that_pos`.
1666 * \pre `that_pos <= that.size()`
1667 * \note Equivalent to
1668 * <tt>replace(this_pos, this_count, that.data() + that_pos,
1669 * that.size() - that_pos)</tt>
1671 template <std::size_t M>
1672 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1673 std::size_t this_pos,
1674 std::size_t this_count,
1675 const BasicFixedString<Char, M>& that,
1676 std::size_t that_pos = 0u) noexcept(false) {
1677 return replace(this_pos, this_count, that, that_pos, that.size_ - that_pos);
1681 * Replace `this_count` characters starting from position `this_pos` with
1682 * `that_count` characters from string `that` starting at position
1684 * \pre `that_pos <= that.size() && that_count <= that.size() - that_pos`
1685 * \note Equivalent to
1686 * `replace(this_pos, this_count, that.data() + that_pos, that_count)`
1688 template <std::size_t M>
1689 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1690 std::size_t this_pos,
1691 std::size_t this_count,
1692 const BasicFixedString<Char, M>& that,
1693 std::size_t that_pos,
1694 std::size_t that_count) noexcept(false) {
1695 return *this = creplace(this_pos, this_count, that, that_pos, that_count);
1699 * Replace `this_count` characters starting from position `this_pos` with
1700 * the characters from the string literal `that`.
1701 * \note Equivalent to
1702 * `replace(this_pos, this_count, that, strlen(that))`
1704 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1705 std::size_t this_pos,
1706 std::size_t this_count,
1707 const Char* that) noexcept(false) {
1708 return replace(this_pos, this_count, that, folly::constexpr_strlen(that));
1712 * Replace the characters denoted by the half-open range [`first`,`last`) with
1713 * the characters from the string literal `that`.
1714 * \pre `first` and `last` point to characters within this string (including
1715 * the terminating null).
1716 * \note Equivalent to
1717 * `replace(first - data(), last - first, that, strlen(that))`
1719 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1722 const Char* that) noexcept(false) {
1724 first - data_, last - first, that, folly::constexpr_strlen(that));
1728 * Replace `this_count` characters starting from position `this_pos` with
1729 * `that_count` characters from the character sequence pointed to by `that`.
1730 * \param this_pos The starting offset within `*this` of the first character
1732 * \param this_count The number of characters to be replaced. If `npos`,
1733 * it is treated as if `this_count` were `size() - this_pos`.
1734 * \param that A pointer to the replacement string.
1735 * \param that_count The number of characters in the replacement string.
1736 * \pre `this_pos <= size() && this_count <= size() - this_pos`
1737 * \pre `that` points to a contiguous sequence of at least `that_count`
1739 * \throw std::out_of_range on any of the following conditions:
1740 * - `this_pos > size()`
1741 * - `this_count > size() - this_pos`
1742 * - `size() - this_count + that_count > N`
1744 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1745 std::size_t this_pos,
1746 std::size_t this_count,
1748 std::size_t that_count) noexcept(false) {
1749 return *this = detail::fixedstring::Helper::replace_<Char>(
1751 detail::fixedstring::checkOverflow(this_pos, size_),
1752 detail::fixedstring::checkOverflowOrNpos(
1753 this_count, size_ - this_pos),
1761 * Replace `this_count` characters starting from position `this_pos` with
1762 * `that_count` characters `ch`.
1763 * \note Equivalent to
1764 * `replace(this_pos, this_count, BasicFixedString{that_count, ch})`
1766 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1767 std::size_t this_pos,
1768 std::size_t this_count,
1769 std::size_t that_count,
1770 Char ch) noexcept(false) {
1771 return replace(this_pos, this_count, BasicFixedString{that_count, ch});
1775 * Replace the characters denoted by the half-open range [`first`,`last`)
1776 * with `that_count` characters `ch`.
1777 * \note Equivalent to
1778 * `replace(first - data(), last - first, BasicFixedString{that_count, ch})`
1780 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1783 std::size_t that_count,
1784 Char ch) noexcept(false) {
1786 first - data_, last - first, BasicFixedString{that_count, ch});
1790 * Replace the characters denoted by the half-open range [`first`,`last`) with
1791 * the characters from the string literal `that`.
1792 * \pre `first` and `last` point to characters within this string (including
1793 * the terminating null).
1794 * \note Equivalent to
1795 * `replace(this_pos, this_count, il.begin(), il.size())`
1797 FOLLY_CPP14_CONSTEXPR BasicFixedString& replace(
1800 std::initializer_list<Char> il) noexcept(false) {
1801 return replace(first - data_, last - first, il.begin(), il.size());
1804 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
1805 * Construct a new string by replacing `this_count` characters starting from
1806 * position `this_pos` within this string with the characters from string
1807 * `that` starting at position `that_pos`.
1808 * \pre `that_pos <= that.size()`
1809 * \note Equivalent to
1810 * <tt>creplace(this_pos, this_count, that, that_pos,
1811 * that.size() - that_pos)</tt>
1813 template <std::size_t M>
1814 constexpr BasicFixedString<Char, N + M> creplace(
1815 std::size_t this_pos,
1816 std::size_t this_count,
1817 const BasicFixedString<Char, M>& that,
1818 std::size_t that_pos = 0u) const noexcept(false) {
1824 that.size_ - detail::fixedstring::checkOverflow(that_pos, that.size_));
1828 * Construct a new string by replacing `this_count` characters starting from
1829 * position `this_pos` within this string with `that_count` characters from
1830 * string `that` starting at position `that_pos`.
1831 * \param this_pos The starting offset within `*this` of the first character
1833 * \param this_count The number of characters to be replaced. If `npos`,
1834 * it is treated as if `this_count` were `size() - this_pos`.
1835 * \param that A string that contains the replacement string.
1836 * \param that_pos The offset to the first character in the replacement
1838 * \param that_count The number of characters in the replacement string.
1839 * \pre `this_pos <= size() && this_count <= size() - this_pos`
1840 * \pre `that_pos <= that.size() && that_count <= that.size() - that_pos`
1841 * \post The size of the returned string is `size() - this_count + that_count`
1842 * \note Equivalent to <tt>BasicFixedString<Char, N + M>{substr(0, this_pos) +
1843 * that.substr(that_pos, that_count) + substr(this_pos + this_count)}</tt>
1844 * \throw std::out_of_range on any of the following conditions:
1845 * - `this_pos > size()`
1846 * - `this_count > size() - this_pos`
1847 * - `that_pos > that.size()`
1848 * - `that_count > that.size() - that_pos`
1850 template <std::size_t M>
1851 constexpr BasicFixedString<Char, N + M> creplace(
1852 std::size_t this_pos,
1853 std::size_t this_count,
1854 const BasicFixedString<Char, M>& that,
1855 std::size_t that_pos,
1856 std::size_t that_count) const noexcept(false) {
1857 return detail::fixedstring::Helper::replace_<Char>(
1859 detail::fixedstring::checkOverflow(this_pos, size_),
1860 detail::fixedstring::checkOverflowOrNpos(this_count, size_ - this_pos),
1862 detail::fixedstring::checkOverflow(that_pos, that.size_),
1863 detail::fixedstring::checkOverflowOrNpos(
1864 that_count, that.size_ - that_pos),
1865 std::make_index_sequence<N + M>{});
1869 * Construct a new string by replacing the characters denoted by the half-open
1870 * range [`first`,`last`) within this string with the characters from string
1871 * `that` starting at position `that_pos`.
1872 * \pre `that_pos <= that.size()`
1873 * \note Equivalent to
1874 * <tt>creplace(first - data(), last - first, that, that_pos,
1875 * that.size() - that_pos)</tt>
1877 template <std::size_t M>
1878 constexpr BasicFixedString<Char, N + M> creplace(
1881 const BasicFixedString<Char, M>& that,
1882 std::size_t that_pos = 0u) const noexcept(false) {
1888 that.size_ - detail::fixedstring::checkOverflow(that_pos, that.size_));
1892 * Construct a new string by replacing the characters denoted by the half-open
1893 * range [`first`,`last`) within this string with the `that_count`
1894 * characters from string `that` starting at position `that_pos`.
1895 * \note Equivalent to
1896 * <tt>creplace(first - data(), last - first, that, that_pos,
1899 template <std::size_t M>
1900 constexpr BasicFixedString<Char, N + M> creplace(
1903 const BasicFixedString<Char, M>& that,
1904 std::size_t that_pos,
1905 std::size_t that_count) const noexcept(false) {
1906 return creplace(first - data_, last - first, that, that_pos, that_count);
1910 * Construct a new string by replacing `this_count` characters starting from
1911 * position `this_pos` within this string with `M-1` characters from
1912 * character array `that`.
1913 * \pre `strlen(that) == M-1`
1914 * \note Equivalent to
1915 * <tt>creplace(this_pos, this_count, that, 0, M - 1)</tt>
1917 template <std::size_t M>
1918 constexpr BasicFixedString<Char, N + M - 1u> creplace(
1919 std::size_t this_pos,
1920 std::size_t this_count,
1921 const Char (&that)[M]) const noexcept(false) {
1922 return creplace(this_pos, this_count, that, 0u, M - 1u);
1926 * Replace `this_count` characters starting from position `this_pos` with
1927 * `that_count` characters from the character array `that` starting at
1928 * position `that_pos`.
1929 * \param this_pos The starting offset within `*this` of the first character
1931 * \param this_count The number of characters to be replaced. If `npos`,
1932 * it is treated as if `this_count` were `size() - this_pos`.
1933 * \param that An array of characters containing the replacement string.
1934 * \param that_pos The starting offset of the replacement string.
1935 * \param that_count The number of characters in the replacement string. If
1936 * `npos`, it is treated as if `that_count` were `M - 1 - that_pos`
1937 * \pre `this_pos <= size() && this_count <= size() - this_pos`
1938 * \pre `that_pos <= M - 1 && that_count <= M - 1 - that_pos`
1939 * \post The size of the returned string is `size() - this_count + that_count`
1940 * \note Equivalent to <tt>BasicFixedString<Char, N + M - 1>{
1941 * substr(0, this_pos) +
1942 * makeFixedString(that).substr(that_pos, that_count) +
1943 * substr(this_pos + this_count)}</tt>
1944 * \throw std::out_of_range on any of the following conditions:
1945 * - `this_pos > size()`
1946 * - `this_count > size() - this_pos`
1948 * - `that_count >= M - that_pos`
1950 template <std::size_t M>
1951 constexpr BasicFixedString<Char, N + M - 1u> creplace(
1952 std::size_t this_pos,
1953 std::size_t this_count,
1954 const Char (&that)[M],
1955 std::size_t that_pos,
1956 std::size_t that_count) const noexcept(false) {
1957 return detail::fixedstring::Helper::replace_<Char>(
1959 detail::fixedstring::checkOverflow(this_pos, size_),
1960 detail::fixedstring::checkOverflowOrNpos(this_count, size_ - this_pos),
1961 detail::fixedstring::checkNullTerminated(that),
1962 detail::fixedstring::checkOverflow(that_pos, M - 1u),
1963 detail::fixedstring::checkOverflowOrNpos(that_count, M - 1u - that_pos),
1964 std::make_index_sequence<N + M - 1u>{});
1968 * Construct a new string by replacing the characters denoted by the half-open
1969 * range [`first`,`last`) within this string with the first `M-1`
1970 * characters from the character array `that`.
1971 * \pre `strlen(that) == M-1`
1972 * \note Equivalent to
1973 * <tt>creplace(first - data(), last - first, that, 0, M-1)</tt>
1975 template <std::size_t M>
1976 constexpr BasicFixedString<Char, N + M - 1u>
1977 creplace(const Char* first, const Char* last, const Char (&that)[M]) const
1979 return creplace(first - data_, last - first, that, 0u, M - 1u);
1983 * Construct a new string by replacing the characters denoted by the half-open
1984 * range [`first`,`last`) within this string with the `that_count`
1985 * characters from the character array `that` starting at position
1987 * \pre `strlen(that) == M-1`
1988 * \note Equivalent to
1989 * `creplace(first - data(), last - first, that, that_pos, that_count)`
1991 template <std::size_t M>
1992 constexpr BasicFixedString<Char, N + M - 1u> creplace(
1995 const Char (&that)[M],
1996 std::size_t that_pos,
1997 std::size_t that_count) const noexcept(false) {
1998 return creplace(first - data_, last - first, that, that_pos, that_count);
2001 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2002 * Copies `min(count, size())` characters starting from offset `0`
2003 * from this string into the buffer pointed to by `dest`.
2004 * \return The number of characters copied.
2006 FOLLY_CPP14_CONSTEXPR std::size_t copy(Char* dest, std::size_t count) const
2008 return copy(dest, count, 0u);
2012 * Copies `min(count, size() - pos)` characters starting from offset `pos`
2013 * from this string into the buffer pointed to by `dest`.
2014 * \pre `pos <= size()`
2015 * \return The number of characters copied.
2016 * \throw std::out_of_range if `pos > size()`
2018 FOLLY_CPP14_CONSTEXPR std::size_t
2019 copy(Char* dest, std::size_t count, std::size_t pos) const noexcept(false) {
2020 detail::fixedstring::checkOverflow(pos, size_);
2021 for (std::size_t i = 0u; i < count; ++i) {
2022 if (i + pos == size_)
2024 dest[i] = data_[i + pos];
2029 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2030 * Resizes the current string.
2031 * \note Equivalent to `resize(count, Char(0))`
2033 FOLLY_CPP14_CONSTEXPR void resize(std::size_t count) noexcept(false) {
2034 resize(count, Char(0));
2038 * Resizes the current string by setting the size to `count` and setting
2039 * `data()[count]` to `Char(0)`. If `count > old_size`, the characters
2040 * in the range [`old_size`,`count`) are set to `ch`.
2042 FOLLY_CPP14_CONSTEXPR void resize(std::size_t count, Char ch) noexcept(
2044 detail::fixedstring::checkOverflow(count, N);
2045 if (count == size_) {
2046 } else if (count < size_) {
2048 data_[size_] = Char(0);
2050 for (; size_ < count; ++size_) {
2053 data_[size_] = Char(0);
2057 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2058 * Finds the first occurrence of the character sequence `that` in this string.
2059 * \note Equivalent to `find(that.data(), 0, that.size())`
2061 template <std::size_t M>
2062 constexpr std::size_t find(const BasicFixedString<Char, M>& that) const
2064 return find(that, 0u);
2068 * Finds the first occurrence of the character sequence `that` in this string,
2069 * starting at offset `pos`.
2070 * \pre `pos <= size()`
2071 * \note Equivalent to `find(that.data(), pos, that.size())`
2073 template <std::size_t M>
2074 constexpr std::size_t find(
2075 const BasicFixedString<Char, M>& that,
2076 std::size_t pos) const noexcept(false) {
2077 return that.size_ <= size_ - detail::fixedstring::checkOverflow(pos, size_)
2078 ? detail::fixedstring::find_(*this, that, pos, that.size_)
2083 * Finds the first occurrence of the character sequence `that` in this string.
2084 * \note Equivalent to `find(that.data(), 0, strlen(that))`
2086 constexpr std::size_t find(const Char* that) const noexcept {
2087 return find(that, 0u, folly::constexpr_strlen(that));
2091 * Finds the first occurrence of the character sequence `that` in this string,
2092 * starting at offset `pos`.
2093 * \pre `pos <= size()`
2094 * \note Equivalent to `find(that.data(), pos, strlen(that))`
2096 constexpr std::size_t find(const Char* that, std::size_t pos) const
2098 return find(that, pos, folly::constexpr_strlen(that));
2102 * Finds the first occurrence of the first `count` characters in the buffer
2103 * pointed to by `that` in this string, starting at offset `pos`.
2104 * \pre `pos <= size()`
2105 * \pre `that` points to a buffer containing at least `count` contiguous
2107 * \return The lowest offset `i` such that `i >= pos` and
2108 * `0 == strncmp(data() + i, that, count)`; or `npos` if there is no such
2110 * \throw std::out_of_range when `pos > size()`
2112 constexpr std::size_t find(
2115 std::size_t count) const noexcept(false) {
2116 return count <= size_ - detail::fixedstring::checkOverflow(pos, size_)
2117 ? detail::fixedstring::find_(*this, that, pos, count)
2122 * Finds the first occurrence of the character `ch` in this string.
2123 * \note Equivalent to `find(&ch, 0, 1)`
2125 constexpr std::size_t find(Char ch) const noexcept {
2126 return find(ch, 0u);
2130 * Finds the first occurrence of the character character `c` in this string,
2131 * starting at offset `pos`.
2132 * \pre `pos <= size()`
2133 * \note Equivalent to `find(&ch, pos, 1)`
2135 constexpr std::size_t find(Char ch, std::size_t pos) const noexcept(false) {
2136 using A = const Char[1u];
2137 return 0u == size_ - detail::fixedstring::checkOverflow(pos, size_)
2139 : detail::fixedstring::find_(*this, A{ch}, pos, 1u);
2142 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2143 * Finds the last occurrence of characters in the string
2144 * `that` in this string.
2145 * \note Equivalent to `rfind(that.data(), size(), that.size())`
2147 template <std::size_t M>
2148 constexpr std::size_t rfind(const BasicFixedString<Char, M>& that) const
2150 return rfind(that, size_);
2154 * Finds the last occurrence of characters in the string
2155 * `that` in this string, starting at offset `pos`.
2156 * \note Equivalent to `rfind(that.data(), pos, that.size())`
2158 template <std::size_t M>
2159 constexpr std::size_t rfind(
2160 const BasicFixedString<Char, M>& that,
2161 std::size_t pos) const noexcept(false) {
2162 return that.size_ <= size_
2163 ? detail::fixedstring::rfind_(
2166 folly::constexpr_min(
2167 detail::fixedstring::checkOverflow(pos, size_),
2168 size_ - that.size_),
2174 * Finds the last occurrence of characters in the buffer
2175 * pointed to by `that` in this string.
2176 * \note Equivalent to `rfind(that, size(), strlen(that))`
2178 constexpr std::size_t rfind(const Char* that) const noexcept {
2179 return rfind(that, size_, folly::constexpr_strlen(that));
2183 * Finds the last occurrence of characters in the buffer
2184 * pointed to by `that` in this string, starting at offset `pos`.
2185 * \note Equivalent to `rfind(that, pos, strlen(that))`
2187 constexpr std::size_t rfind(const Char* that, std::size_t pos) const
2189 return rfind(that, pos, folly::constexpr_strlen(that));
2193 * Finds the last occurrence of the first `count` characters in the buffer
2194 * pointed to by `that` in this string, starting at offset `pos`.
2195 * \pre `pos <= size()`
2196 * \pre `that` points to a buffer containing at least `count` contiguous
2198 * \return The largest offset `i` such that `i <= pos` and
2199 * `i + count <= size()` and `0 == strncmp(data() + i, that, count)`; or
2200 * `npos` if there is no such offset `i`.
2201 * \throw std::out_of_range when `pos > size()`
2203 constexpr std::size_t rfind(
2206 std::size_t count) const noexcept(false) {
2207 return count <= size_
2208 ? detail::fixedstring::rfind_(
2211 folly::constexpr_min(
2212 detail::fixedstring::checkOverflow(pos, size_),
2219 * Finds the last occurrence of the character character `ch` in this string.
2220 * \note Equivalent to `rfind(&ch, size(), 1)`
2222 constexpr std::size_t rfind(Char ch) const noexcept {
2223 return rfind(ch, size_);
2227 * Finds the last occurrence of the character character `ch` in this string,
2228 * starting at offset `pos`.
2229 * \pre `pos <= size()`
2230 * \note Equivalent to `rfind(&ch, pos, 1)`
2232 constexpr std::size_t rfind(Char ch, std::size_t pos) const noexcept(false) {
2233 using A = const Char[1u];
2236 : detail::fixedstring::rfind_(
2239 folly::constexpr_min(
2240 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2244 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2245 * Finds the first occurrence of any character in `that` in this string.
2246 * \note Equivalent to `find_first_of(that.data(), 0, that.size())`
2248 template <std::size_t M>
2249 constexpr std::size_t find_first_of(
2250 const BasicFixedString<Char, M>& that) const noexcept {
2251 return find_first_of(that, 0u);
2255 * Finds the first occurrence of any character in `that` in this string,
2256 * starting at offset `pos`
2257 * \note Equivalent to `find_first_of(that.data(), pos, that.size())`
2259 template <std::size_t M>
2260 constexpr std::size_t find_first_of(
2261 const BasicFixedString<Char, M>& that,
2262 std::size_t pos) const noexcept(false) {
2263 return size_ == detail::fixedstring::checkOverflow(pos, size_)
2265 : detail::fixedstring::find_first_of_(*this, that, pos, that.size_);
2269 * Finds the first occurrence of any character in the null-terminated
2270 * character sequence pointed to by `that` in this string.
2271 * \note Equivalent to `find_first_of(that, 0, strlen(that))`
2273 constexpr std::size_t find_first_of(const Char* that) const noexcept {
2274 return find_first_of(that, 0u, folly::constexpr_strlen(that));
2278 * Finds the first occurrence of any character in the null-terminated
2279 * character sequence pointed to by `that` in this string,
2280 * starting at offset `pos`
2281 * \note Equivalent to `find_first_of(that, pos, strlen(that))`
2283 constexpr std::size_t find_first_of(const Char* that, std::size_t pos) const
2285 return find_first_of(that, pos, folly::constexpr_strlen(that));
2289 * Finds the first occurrence of any character in the first `count` characters
2290 * in the buffer pointed to by `that` in this string, starting at offset
2292 * \pre `pos <= size()`
2293 * \pre `that` points to a buffer containing at least `count` contiguous
2295 * \return The smallest offset `i` such that `i >= pos` and
2296 * `std::find(that, that+count, at(i)) != that+count`; or
2297 * `npos` if there is no such offset `i`.
2298 * \throw std::out_of_range when `pos > size()`
2300 constexpr std::size_t find_first_of(
2303 std::size_t count) const noexcept(false) {
2304 return size_ == detail::fixedstring::checkOverflow(pos, size_)
2306 : detail::fixedstring::find_first_of_(*this, that, pos, count);
2310 * Finds the first occurrence of `ch` in this string.
2311 * \note Equivalent to `find_first_of(&ch, 0, 1)`
2313 constexpr std::size_t find_first_of(Char ch) const noexcept {
2314 return find_first_of(ch, 0u);
2318 * Finds the first occurrence of `ch` in this string,
2319 * starting at offset `pos`.
2320 * \note Equivalent to `find_first_of(&ch, pos, 1)`
2322 constexpr std::size_t find_first_of(Char ch, std::size_t pos) const
2324 using A = const Char[1u];
2325 return size_ == detail::fixedstring::checkOverflow(pos, size_)
2327 : detail::fixedstring::find_first_of_(*this, A{ch}, pos, 1u);
2330 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2331 * Finds the first occurrence of any character not in `that` in this string.
2332 * \note Equivalent to `find_first_not_of(that.data(), 0, that.size())`
2334 template <std::size_t M>
2335 constexpr std::size_t find_first_not_of(
2336 const BasicFixedString<Char, M>& that) const noexcept {
2337 return find_first_not_of(that, 0u);
2340 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2341 * Finds the first occurrence of any character not in `that` in this string.
2342 * \note Equivalent to `find_first_not_of(that.data(), 0, that.size())`
2344 template <std::size_t M>
2345 constexpr std::size_t find_first_not_of(
2346 const BasicFixedString<Char, M>& that,
2347 std::size_t pos) const noexcept(false) {
2348 return size_ == detail::fixedstring::checkOverflow(pos, size_)
2350 : detail::fixedstring::find_first_not_of_(*this, that, pos, that.size_);
2354 * Finds the first occurrence of any character not in the null-terminated
2355 * character sequence pointed to by `that` in this string.
2356 * \note Equivalent to `find_first_not_of(that, 0, strlen(that))`
2358 constexpr std::size_t find_first_not_of(const Char* that) const noexcept {
2359 return find_first_not_of(that, 0u, folly::constexpr_strlen(that));
2363 * Finds the first occurrence of any character not in the null-terminated
2364 * character sequence pointed to by `that` in this string,
2365 * starting at offset `pos`
2366 * \note Equivalent to `find_first_not_of(that, pos, strlen(that))`
2368 constexpr std::size_t find_first_not_of(const Char* that, std::size_t pos)
2369 const noexcept(false) {
2370 return find_first_not_of(that, pos, folly::constexpr_strlen(that));
2374 * Finds the first occurrence of any character not in the first `count`
2375 * characters in the buffer pointed to by `that` in this string, starting at
2377 * \pre `pos <= size()`
2378 * \pre `that` points to a buffer containing at least `count` contiguous
2380 * \return The smallest offset `i` such that `i >= pos` and
2381 * `std::find(that, that+count, at(i)) == that+count`; or
2382 * `npos` if there is no such offset `i`.
2383 * \throw std::out_of_range when `pos > size()`
2385 constexpr std::size_t find_first_not_of(
2388 std::size_t count) const noexcept(false) {
2389 return size_ == detail::fixedstring::checkOverflow(pos, size_)
2391 : detail::fixedstring::find_first_not_of_(*this, that, pos, count);
2395 * Finds the first occurrence of any character other than `ch` in this string.
2396 * \note Equivalent to `find_first_not_of(&ch, 0, 1)`
2398 constexpr std::size_t find_first_not_of(Char ch) const noexcept {
2399 return find_first_not_of(ch, 0u);
2403 * Finds the first occurrence of any character other than `ch` in this string,
2404 * starting at offset `pos`.
2405 * \note Equivalent to `find_first_not_of(&ch, pos, 1)`
2407 constexpr std::size_t find_first_not_of(Char ch, std::size_t pos) const
2409 using A = const Char[1u];
2410 return 1u <= size_ - detail::fixedstring::checkOverflow(pos, size_)
2411 ? detail::fixedstring::find_first_not_of_(*this, A{ch}, pos, 1u)
2415 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2416 * Finds the last occurrence of any character in `that` in this string.
2417 * \note Equivalent to `find_last_of(that.data(), size(), that.size())`
2419 template <std::size_t M>
2420 constexpr std::size_t find_last_of(
2421 const BasicFixedString<Char, M>& that) const noexcept {
2422 return find_last_of(that, size_);
2426 * Finds the last occurrence of any character in `that` in this string,
2427 * starting at offset `pos`
2428 * \note Equivalent to `find_last_of(that.data(), pos, that.size())`
2430 template <std::size_t M>
2431 constexpr std::size_t find_last_of(
2432 const BasicFixedString<Char, M>& that,
2433 std::size_t pos) const noexcept(false) {
2436 : detail::fixedstring::find_last_of_(
2439 folly::constexpr_min(
2440 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2445 * Finds the last occurrence of any character in the null-terminated
2446 * character sequence pointed to by `that` in this string.
2447 * \note Equivalent to `find_last_of(that, size(), strlen(that))`
2449 constexpr std::size_t find_last_of(const Char* that) const noexcept {
2450 return find_last_of(that, size_, folly::constexpr_strlen(that));
2454 * Finds the last occurrence of any character in the null-terminated
2455 * character sequence pointed to by `that` in this string,
2456 * starting at offset `pos`
2457 * \note Equivalent to `find_last_of(that, pos, strlen(that))`
2459 constexpr std::size_t find_last_of(const Char* that, std::size_t pos) const
2461 return find_last_of(that, pos, folly::constexpr_strlen(that));
2465 * Finds the last occurrence of any character in the first `count` characters
2466 * in the buffer pointed to by `that` in this string, starting at offset
2468 * \pre `pos <= size()`
2469 * \pre `that` points to a buffer containing at least `count` contiguous
2471 * \return The largest offset `i` such that `i <= pos` and
2472 * `i < size()` and `std::find(that, that+count, at(i)) != that+count`; or
2473 * `npos` if there is no such offset `i`.
2474 * \throw std::out_of_range when `pos > size()`
2476 constexpr std::size_t find_last_of(
2479 std::size_t count) const noexcept(false) {
2482 : detail::fixedstring::find_last_of_(
2485 folly::constexpr_min(
2486 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2491 * Finds the last occurrence of `ch` in this string.
2492 * \note Equivalent to `find_last_of(&ch, size(), 1)`
2494 constexpr std::size_t find_last_of(Char ch) const noexcept {
2495 return find_last_of(ch, size_);
2499 * Finds the last occurrence of `ch` in this string,
2500 * starting at offset `pos`.
2501 * \note Equivalent to `find_last_of(&ch, pos, 1)`
2503 constexpr std::size_t find_last_of(Char ch, std::size_t pos) const
2505 using A = const Char[1u];
2508 : detail::fixedstring::find_last_of_(
2511 folly::constexpr_min(
2512 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2516 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2517 * Finds the last occurrence of any character not in `that` in this string.
2518 * \note Equivalent to `find_last_not_of(that.data(), size(), that.size())`
2520 template <std::size_t M>
2521 constexpr std::size_t find_last_not_of(
2522 const BasicFixedString<Char, M>& that) const noexcept {
2523 return find_last_not_of(that, size_);
2527 * Finds the last occurrence of any character not in `that` in this string,
2528 * starting at offset `pos`
2529 * \note Equivalent to `find_last_not_of(that.data(), pos, that.size())`
2531 template <std::size_t M>
2532 constexpr std::size_t find_last_not_of(
2533 const BasicFixedString<Char, M>& that,
2534 std::size_t pos) const noexcept(false) {
2537 : detail::fixedstring::find_last_not_of_(
2540 folly::constexpr_min(
2541 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2546 * Finds the last occurrence of any character not in the null-terminated
2547 * character sequence pointed to by `that` in this string.
2548 * \note Equivalent to `find_last_not_of(that, size(), strlen(that))`
2550 constexpr std::size_t find_last_not_of(const Char* that) const noexcept {
2551 return find_last_not_of(that, size_, folly::constexpr_strlen(that));
2555 * Finds the last occurrence of any character not in the null-terminated
2556 * character sequence pointed to by `that` in this string,
2557 * starting at offset `pos`
2558 * \note Equivalent to `find_last_not_of(that, pos, strlen(that))`
2560 constexpr std::size_t find_last_not_of(const Char* that, std::size_t pos)
2561 const noexcept(false) {
2562 return find_last_not_of(that, pos, folly::constexpr_strlen(that));
2566 * Finds the last occurrence of any character not in the first `count`
2567 * characters in the buffer pointed to by `that` in this string, starting at
2569 * \pre `pos <= size()`
2570 * \pre `that` points to a buffer containing at least `count` contiguous
2572 * \return The largest offset `i` such that `i <= pos` and
2573 * `i < size()` and `std::find(that, that+count, at(i)) == that+count`; or
2574 * `npos` if there is no such offset `i`.
2575 * \throw std::out_of_range when `pos > size()`
2577 constexpr std::size_t find_last_not_of(
2580 std::size_t count) const noexcept(false) {
2583 : detail::fixedstring::find_last_not_of_(
2586 folly::constexpr_min(
2587 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2592 * Finds the last occurrence of any character other than `ch` in this string.
2593 * \note Equivalent to `find_last_not_of(&ch, size(), 1)`
2595 constexpr std::size_t find_last_not_of(Char ch) const noexcept {
2596 return find_last_not_of(ch, size_);
2600 * Finds the last occurrence of any character other than `ch` in this string,
2601 * starting at offset `pos`.
2602 * \note Equivalent to `find_last_not_of(&ch, pos, 1)`
2604 constexpr std::size_t find_last_not_of(Char ch, std::size_t pos) const
2606 using A = const Char[1u];
2609 : detail::fixedstring::find_last_not_of_(
2612 folly::constexpr_min(
2613 detail::fixedstring::checkOverflow(pos, size_), size_ - 1u),
2617 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2618 * Asymmetric relational operators
2620 friend constexpr bool operator==(
2622 const BasicFixedString& b) noexcept {
2623 return detail::fixedstring::equal_(
2624 a, folly::constexpr_strlen(a), b, b.size());
2630 friend constexpr bool operator==(
2631 const BasicFixedString& a,
2632 const Char* b) noexcept {
2636 friend constexpr bool operator!=(
2638 const BasicFixedString& b) noexcept {
2645 friend constexpr bool operator!=(
2646 const BasicFixedString& a,
2647 const Char* b) noexcept {
2651 friend constexpr bool operator<(
2653 const BasicFixedString& b) noexcept {
2654 return detail::fixedstring::Cmp::LT ==
2655 detail::fixedstring::compare_(
2656 a, 0u, folly::constexpr_strlen(a), b, 0u, b.size_);
2662 friend constexpr bool operator<(
2663 const BasicFixedString& a,
2664 const Char* b) noexcept {
2665 return detail::fixedstring::Cmp::LT ==
2666 detail::fixedstring::compare_(
2667 a, 0u, a.size_, b, 0u, folly::constexpr_strlen(b));
2670 friend constexpr bool operator>(
2672 const BasicFixedString& b) noexcept {
2679 friend constexpr bool operator>(
2680 const BasicFixedString& a,
2681 const Char* b) noexcept {
2685 friend constexpr bool operator<=(
2687 const BasicFixedString& b) noexcept {
2694 friend constexpr bool operator<=(
2695 const BasicFixedString& a,
2696 const Char* b) noexcept {
2700 friend constexpr bool operator>=(
2702 const BasicFixedString& b) noexcept {
2709 friend constexpr bool operator>=(
2710 const BasicFixedString& a,
2711 const Char* b) noexcept {
2715 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2716 * Asymmetric concatenation
2718 template <std::size_t M>
2719 friend constexpr BasicFixedString<Char, N + M - 1u> operator+(
2721 const BasicFixedString& b) noexcept {
2722 return detail::fixedstring::Helper::concat_<Char>(
2723 detail::fixedstring::checkNullTerminated(a),
2725 std::make_index_sequence<N + M - 1u>{});
2731 template <std::size_t M>
2732 friend constexpr BasicFixedString<Char, N + M - 1u> operator+(
2733 const BasicFixedString& a,
2734 const Char (&b)[M]) noexcept {
2735 return detail::fixedstring::Helper::concat_<Char>(
2737 detail::fixedstring::checkNullTerminated(b),
2738 std::make_index_sequence<N + M - 1u>{});
2744 friend constexpr BasicFixedString<Char, N + 1u> operator+(
2746 const BasicFixedString& b) noexcept {
2747 using A = const Char[2u];
2748 return detail::fixedstring::Helper::concat_<Char>(
2749 A{a, Char(0)}, b, std::make_index_sequence<N + 1u>{});
2755 friend constexpr BasicFixedString<Char, N + 1u> operator+(
2756 const BasicFixedString& a,
2758 using A = const Char[2u];
2759 return detail::fixedstring::Helper::concat_<Char>(
2760 a, A{b, Char(0)}, std::make_index_sequence<N + 1u>{});
2764 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2765 * Symmetric relational operators
2767 template <class Char, std::size_t A, std::size_t B>
2768 constexpr bool operator==(
2769 const BasicFixedString<Char, A>& a,
2770 const BasicFixedString<Char, B>& b) noexcept {
2771 return detail::fixedstring::equal_(a, a.size(), b, b.size());
2774 template <class Char, std::size_t A, std::size_t B>
2775 constexpr bool operator!=(
2776 const BasicFixedString<Char, A>& a,
2777 const BasicFixedString<Char, B>& b) {
2781 template <class Char, std::size_t A, std::size_t B>
2782 constexpr bool operator<(
2783 const BasicFixedString<Char, A>& a,
2784 const BasicFixedString<Char, B>& b) noexcept {
2785 return detail::fixedstring::Cmp::LT ==
2786 detail::fixedstring::compare_(a, 0u, a.size(), b, 0u, b.size());
2789 template <class Char, std::size_t A, std::size_t B>
2790 constexpr bool operator>(
2791 const BasicFixedString<Char, A>& a,
2792 const BasicFixedString<Char, B>& b) noexcept {
2796 template <class Char, std::size_t A, std::size_t B>
2797 constexpr bool operator<=(
2798 const BasicFixedString<Char, A>& a,
2799 const BasicFixedString<Char, B>& b) noexcept {
2803 template <class Char, std::size_t A, std::size_t B>
2804 constexpr bool operator>=(
2805 const BasicFixedString<Char, A>& a,
2806 const BasicFixedString<Char, B>& b) noexcept {
2810 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2811 * Symmetric concatenation
2813 template <class Char, std::size_t N, std::size_t M>
2814 constexpr BasicFixedString<Char, N + M> operator+(
2815 const BasicFixedString<Char, N>& a,
2816 const BasicFixedString<Char, M>& b) noexcept {
2817 return detail::fixedstring::Helper::concat_<Char>(
2818 a, b, std::make_index_sequence<N + M>{});
2821 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2822 * Construct a `BasicFixedString` object from a null-terminated array of
2823 * characters. The capacity and size of the string will be equal to one less
2824 * than the size of the array.
2825 * \pre `a` contains no embedded null characters.
2826 * \pre `a[N-1] == Char(0)`
2827 * \post For a returned string `s`, `s[i]==a[i]` for every `i` in [`0`,`N-1`].
2829 template <class Char, std::size_t N>
2830 constexpr BasicFixedString<Char, N - 1u> makeFixedString(
2831 const Char (&a)[N]) noexcept {
2835 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2838 template <class Char, std::size_t N>
2839 FOLLY_CPP14_CONSTEXPR void swap(
2840 BasicFixedString<Char, N>& a,
2841 BasicFixedString<Char, N>& b) noexcept {
2845 inline namespace Literals {
2846 inline namespace StringLiterals {
2848 // "const std::size_t&" is so that folly::npos has the same address in every
2849 // translation unit. This is to avoid potential violations of the ODR.
2850 constexpr const std::size_t& npos = detail::fixedstring::FixedStringBase::npos;
2853 #if defined(__GNUC__)
2854 #pragma GCC diagnostic push
2855 #pragma GCC diagnostic ignored "-Wpragmas"
2856 #pragma GCC diagnostic ignored "-Wgnu-string-literal-operator-template"
2858 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
2859 * User-defined literals for creating FixedString objects from string literals
2860 * on the compilers that support it.
2865 * using namespace folly::StringLiterals;
2866 * constexpr auto hello = "hello world!"_fs;
2869 * \note This requires a GNU compiler extension
2870 * (-Wgnu-string-literal-operator-template) supported by clang and gcc,
2871 * proposed for standardization in
2872 * <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0424r0.pdf>.
2874 * For portable code, prefer the suffixes `_fs4`, `_fs8`, `_fs16`, `_fs32`,
2875 * `_fs64`, and `_fs128` for creating instances of types `FixedString<4>`,
2876 * `FixedString<8>`, `FixedString<16>`, etc.
2878 template <class Char, Char... Cs>
2879 constexpr BasicFixedString<Char, sizeof...(Cs)> operator"" _fs() noexcept {
2880 using A = const Char[sizeof...(Cs) + 1u];
2881 // The `+` in `+A{etc}` forces the array type to decay to a pointer
2882 return {+A{Cs..., Char(0)}, sizeof...(Cs)};
2885 #pragma GCC diagnostic pop
2888 #define FOLLY_DEFINE_FIXED_STRING_UDL(N) \
2889 constexpr FixedString<N> operator"" _fs##N( \
2890 const char* that, std::size_t count) noexcept(false) { \
2891 return {that, count}; \
2895 // Define UDLs _fs4, _fs8, _fs16, etc for FixedString<[4, 8, 16, ...]>
2896 FOLLY_DEFINE_FIXED_STRING_UDL(4)
2897 FOLLY_DEFINE_FIXED_STRING_UDL(8)
2898 FOLLY_DEFINE_FIXED_STRING_UDL(16)
2899 FOLLY_DEFINE_FIXED_STRING_UDL(32)
2900 FOLLY_DEFINE_FIXED_STRING_UDL(64)
2901 FOLLY_DEFINE_FIXED_STRING_UDL(128)
2903 #undef FOLLY_DEFINE_FIXED_STRING_UDL
2908 // // numeric conversions:
2909 // template <std::size_t N>
2910 // constexpr int stoi(const FixedString<N>& str, int base = 10);
2911 // template <std::size_t N>
2912 // constexpr unsigned stou(const FixedString<N>& str, int base = 10);
2913 // template <std::size_t N>
2914 // constexpr long stol(const FixedString<N>& str, int base = 10);
2915 // template <std::size_t N>
2916 // constexpr unsigned long stoul(const FixedString<N>& str, int base = 10;
2917 // template <std::size_t N>
2918 // constexpr long long stoll(const FixedString<N>& str, int base = 10);
2919 // template <std::size_t N>
2920 // constexpr unsigned long long stoull(const FixedString<N>& str,
2922 // template <std::size_t N>
2923 // constexpr float stof(const FixedString<N>& str);
2924 // template <std::size_t N>
2925 // constexpr double stod(const FixedString<N>& str);
2926 // template <std::size_t N>
2927 // constexpr long double stold(const FixedString<N>& str);
2928 // template <int val>
2929 // constexpr FixedString</*...*/> to_fixed_string_i() noexcept;
2930 // template <unsigned val>
2931 // constexpr FixedString</*...*/> to_fixed_string_u() noexcept;
2932 // template <long val>
2933 // constexpr FixedString</*...*/> to_fixed_string_l() noexcept;
2934 // template <unsigned long val>
2935 // constexpr FixedString</*...*/> to_fixed_string_ul() noexcept;
2936 // template <long long val>
2937 // constexpr FixedString</*...*/> to_fixed_string_ll() noexcept
2938 // template <unsigned long long val>
2939 // constexpr FixedString</*...*/> to_fixed_string_ull() noexcept;