Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | 7642bb1 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___STRING |
| 11 | #define _LIBCPP___STRING |
| 12 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 13 | #include <__config> |
Christopher Di Bella | 55d7a82 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 14 | #include <__algorithm/copy.h> |
| 15 | #include <__algorithm/copy_backward.h> |
| 16 | #include <__algorithm/copy_n.h> |
| 17 | #include <__algorithm/fill_n.h> |
| 18 | #include <__algorithm/find_first_of.h> |
| 19 | #include <__algorithm/find_end.h> |
| 20 | #include <__algorithm/min.h> |
| 21 | #include <__functional/hash.h> // for __murmur2_or_cityhash |
| 22 | #include <__iterator/iterator_traits.h> |
| 23 | #include <cstdio> // for EOF |
| 24 | #include <cstdint> // for uint_least16_t |
| 25 | #include <cstring> // for memcpy |
Christopher Di Bella | 55d7a82 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 26 | #include <type_traits> // for __libcpp_is_constant_evaluated |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 27 | #include <iosfwd> // for streampos & friends |
| 28 | |
| 29 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
| 30 | # include <cwchar> // for wmemcpy |
| 31 | #endif |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 32 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 33 | #include <__debug> |
| 34 | |
| 35 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 36 | #pragma GCC system_header |
| 37 | #endif |
| 38 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 39 | _LIBCPP_PUSH_MACROS |
| 40 | #include <__undef_macros> |
| 41 | |
| 42 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 43 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 44 | |
Martijn Vels | 1e63ba0 | 2020-02-19 16:27:50 -0500 | [diff] [blame] | 45 | // The extern template ABI lists are kept outside of <string> to improve the |
| 46 | // readability of that header. We maintain 2 ABI lists: |
| 47 | // - _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST |
| 48 | // - _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST |
| 49 | // As the name implies, the ABI lists define the V1 (Stable) and unstable ABI. |
| 50 | // |
| 51 | // For unstable, we may explicitly remove function that are external in V1, |
| 52 | // and add (new) external functions to better control inlining and compiler |
| 53 | // optimization opportunities. |
| 54 | // |
| 55 | // For stable, the ABI list should rarely change, except for adding new |
| 56 | // functions supporting new c++ version / API changes. Typically entries |
| 57 | // must never be removed from the stable list. |
| 58 | #define _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_Func, _CharType) \ |
| 59 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*, size_type)) \ |
| 60 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type const*, size_type, size_type) const) \ |
| 61 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type, size_type)) \ |
| 62 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&)) \ |
| 63 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*)) \ |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 64 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, allocator<_CharType> const&)) \ |
Martijn Vels | 1e63ba0 | 2020-02-19 16:27:50 -0500 | [diff] [blame] | 65 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_not_of(value_type const*, size_type, size_type) const) \ |
| 66 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::~basic_string()) \ |
| 67 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_not_of(value_type const*, size_type, size_type) const) \ |
| 68 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, size_type, value_type)) \ |
| 69 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(value_type)) \ |
| 70 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type)) \ |
| 71 | _Func(_LIBCPP_FUNC_VIS const _CharType& basic_string<_CharType>::at(size_type) const) \ |
| 72 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*, size_type)) \ |
| 73 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_of(value_type const*, size_type, size_type) const) \ |
| 74 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, size_type, value_type)) \ |
| 75 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(value_type const*, size_type)) \ |
| 76 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::reserve(size_type)) \ |
| 77 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*, size_type)) \ |
| 78 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(basic_string const&, size_type, size_type)) \ |
| 79 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::copy(value_type*, size_type, size_type) const) \ |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 80 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, size_type, size_type, allocator<_CharType> const&)) \ |
Martijn Vels | 1e63ba0 | 2020-02-19 16:27:50 -0500 | [diff] [blame] | 81 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type, size_type) const) \ |
| 82 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(size_type, value_type)) \ |
| 83 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*)) \ |
| 84 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_of(value_type const*, size_type, size_type) const) \ |
| 85 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by(size_type, size_type, size_type, size_type, size_type, size_type)) \ |
| 86 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by_and_replace(size_type, size_type, size_type, size_type, size_type, size_type, value_type const*)) \ |
| 87 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::push_back(value_type)) \ |
| 88 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(size_type, value_type)) \ |
| 89 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \ |
| 90 | _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \ |
| 91 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \ |
| 92 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::erase(size_type, size_type)) \ |
| 93 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \ |
| 94 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \ |
| 95 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \ |
| 96 | _Func(_LIBCPP_FUNC_VIS _CharType& basic_string<_CharType>::at(size_type)) \ |
| 97 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(value_type const*)) \ |
| 98 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type const*, size_type, size_type) const) \ |
| 99 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, basic_string const&, size_type, size_type) const) \ |
| 100 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*, size_type) const) \ |
| 101 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(basic_string const&)) \ |
| 102 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*)) \ |
| 103 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, basic_string const&, size_type, size_type)) \ |
| 104 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::iterator basic_string<_CharType>::insert(basic_string::const_iterator, value_type)) \ |
| 105 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::resize(size_type, value_type)) \ |
| 106 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, basic_string const&, size_type, size_type)) |
| 107 | |
| 108 | #define _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_Func, _CharType) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 109 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*, size_type)) \ |
| 110 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type const*, size_type, size_type) const) \ |
| 111 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type, size_type)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 112 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 113 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_not_of(value_type const*, size_type, size_type) const) \ |
| 114 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::~basic_string()) \ |
| 115 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_not_of(value_type const*, size_type, size_type) const) \ |
| 116 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, size_type, value_type)) \ |
| 117 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(value_type)) \ |
| 118 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type)) \ |
Martijn Vels | 5e7c975 | 2020-03-04 17:52:46 -0500 | [diff] [blame] | 119 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init_copy_ctor_external(value_type const*, size_type)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 120 | _Func(_LIBCPP_FUNC_VIS const _CharType& basic_string<_CharType>::at(size_type) const) \ |
| 121 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*, size_type)) \ |
| 122 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_of(value_type const*, size_type, size_type) const) \ |
| 123 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, size_type, value_type)) \ |
Martijn Vels | da7d94f | 2020-06-19 14:24:03 -0400 | [diff] [blame] | 124 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_external(value_type const*, size_type)) \ |
| 125 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_external(value_type const*)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 126 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::reserve(size_type)) \ |
| 127 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*, size_type)) \ |
| 128 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(basic_string const&, size_type, size_type)) \ |
| 129 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::copy(value_type*, size_type, size_type) const) \ |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 130 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, size_type, size_type, allocator<_CharType> const&)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 131 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type, size_type) const) \ |
| 132 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(size_type, value_type)) \ |
| 133 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*)) \ |
| 134 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_of(value_type const*, size_type, size_type) const) \ |
| 135 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by(size_type, size_type, size_type, size_type, size_type, size_type)) \ |
| 136 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by_and_replace(size_type, size_type, size_type, size_type, size_type, size_type, value_type const*)) \ |
Martijn Vels | b6a08b6 | 2020-04-10 18:36:31 -0400 | [diff] [blame] | 137 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_no_alias<false>(value_type const*, size_type)) \ |
| 138 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_no_alias<true>(value_type const*, size_type)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 139 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::push_back(value_type)) \ |
| 140 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(size_type, value_type)) \ |
| 141 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \ |
| 142 | _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \ |
| 143 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \ |
Martijn Vels | a81fc79 | 2020-02-26 13:25:43 -0500 | [diff] [blame] | 144 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__erase_external_with_move(size_type, size_type)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 145 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \ |
| 146 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \ |
| 147 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \ |
| 148 | _Func(_LIBCPP_FUNC_VIS _CharType& basic_string<_CharType>::at(size_type)) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 149 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type const*, size_type, size_type) const) \ |
| 150 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, basic_string const&, size_type, size_type) const) \ |
| 151 | _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*, size_type) const) \ |
Eric Fiselier | 5dcce5c | 2020-01-15 17:12:09 -0500 | [diff] [blame] | 152 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*)) \ |
| 153 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, basic_string const&, size_type, size_type)) \ |
| 154 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::iterator basic_string<_CharType>::insert(basic_string::const_iterator, value_type)) \ |
| 155 | _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::resize(size_type, value_type)) \ |
| 156 | _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, basic_string const&, size_type, size_type)) |
| 157 | |
| 158 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 159 | // char_traits |
| 160 | |
| 161 | template <class _CharT> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 162 | struct _LIBCPP_TEMPLATE_VIS char_traits |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 163 | { |
| 164 | typedef _CharT char_type; |
| 165 | typedef int int_type; |
| 166 | typedef streamoff off_type; |
| 167 | typedef streampos pos_type; |
| 168 | typedef mbstate_t state_type; |
| 169 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 170 | static inline void _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 171 | assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 172 | static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT |
| 173 | {return __c1 == __c2;} |
| 174 | static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT |
| 175 | {return __c1 < __c2;} |
| 176 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 177 | static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 178 | int compare(const char_type* __s1, const char_type* __s2, size_t __n); |
| 179 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 180 | size_t length(const char_type* __s); |
| 181 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 182 | const char_type* find(const char_type* __s, size_t __n, const char_type& __a); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 183 | static _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 184 | char_type* move(char_type* __s1, const char_type* __s2, size_t __n); |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 185 | _LIBCPP_INLINE_VISIBILITY |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 186 | static _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 187 | char_type* copy(char_type* __s1, const char_type* __s2, size_t __n); |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 188 | _LIBCPP_INLINE_VISIBILITY |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 189 | static _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 190 | char_type* assign(char_type* __s, size_t __n, char_type __a); |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 191 | |
| 192 | static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT |
| 193 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 194 | static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT |
| 195 | {return char_type(__c);} |
| 196 | static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT |
| 197 | {return int_type(__c);} |
| 198 | static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT |
| 199 | {return __c1 == __c2;} |
| 200 | static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT |
| 201 | {return int_type(EOF);} |
| 202 | }; |
| 203 | |
| 204 | template <class _CharT> |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 205 | _LIBCPP_CONSTEXPR_AFTER_CXX14 int |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 206 | char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n) |
| 207 | { |
| 208 | for (; __n; --__n, ++__s1, ++__s2) |
| 209 | { |
| 210 | if (lt(*__s1, *__s2)) |
| 211 | return -1; |
| 212 | if (lt(*__s2, *__s1)) |
| 213 | return 1; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | template <class _CharT> |
| 219 | inline |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 220 | _LIBCPP_CONSTEXPR_AFTER_CXX14 size_t |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 221 | char_traits<_CharT>::length(const char_type* __s) |
| 222 | { |
| 223 | size_t __len = 0; |
| 224 | for (; !eq(*__s, char_type(0)); ++__s) |
| 225 | ++__len; |
| 226 | return __len; |
| 227 | } |
| 228 | |
| 229 | template <class _CharT> |
| 230 | inline |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 231 | _LIBCPP_CONSTEXPR_AFTER_CXX14 const _CharT* |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 232 | char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a) |
| 233 | { |
| 234 | for (; __n; --__n) |
| 235 | { |
| 236 | if (eq(*__s, __a)) |
| 237 | return __s; |
| 238 | ++__s; |
| 239 | } |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 240 | return nullptr; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | template <class _CharT> |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 244 | _LIBCPP_CONSTEXPR_AFTER_CXX17 _CharT* |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 245 | char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n) |
| 246 | { |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 247 | if (__n == 0) return __s1; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 248 | char_type* __r = __s1; |
| 249 | if (__s1 < __s2) |
| 250 | { |
| 251 | for (; __n; --__n, ++__s1, ++__s2) |
| 252 | assign(*__s1, *__s2); |
| 253 | } |
| 254 | else if (__s2 < __s1) |
| 255 | { |
| 256 | __s1 += __n; |
| 257 | __s2 += __n; |
| 258 | for (; __n; --__n) |
| 259 | assign(*--__s1, *--__s2); |
| 260 | } |
| 261 | return __r; |
| 262 | } |
| 263 | |
| 264 | template <class _CharT> |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 265 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 266 | _CharT* |
| 267 | char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n) |
| 268 | { |
Arthur O'Dwyer | e9a5512 | 2021-04-20 22:24:24 -0400 | [diff] [blame] | 269 | if (!__libcpp_is_constant_evaluated()) { |
| 270 | _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); |
| 271 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 272 | char_type* __r = __s1; |
| 273 | for (; __n; --__n, ++__s1, ++__s2) |
| 274 | assign(*__s1, *__s2); |
| 275 | return __r; |
| 276 | } |
| 277 | |
| 278 | template <class _CharT> |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 279 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 280 | _CharT* |
| 281 | char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a) |
| 282 | { |
| 283 | char_type* __r = __s; |
| 284 | for (; __n; --__n, ++__s) |
| 285 | assign(*__s, __a); |
| 286 | return __r; |
| 287 | } |
| 288 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 289 | // constexpr versions of move/copy/assign. |
| 290 | |
| 291 | template <class _CharT> |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 292 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 293 | _CharT* __move_constexpr(_CharT* __s1, const _CharT* __s2, size_t __n) _NOEXCEPT |
| 294 | { |
| 295 | if (__n == 0) return __s1; |
| 296 | if (__s1 < __s2) { |
| 297 | _VSTD::copy(__s2, __s2 + __n, __s1); |
| 298 | } else if (__s2 < __s1) { |
| 299 | _VSTD::copy_backward(__s2, __s2 + __n, __s1 + __n); |
| 300 | } |
| 301 | return __s1; |
| 302 | } |
| 303 | |
| 304 | template <class _CharT> |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 305 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 306 | _CharT* __copy_constexpr(_CharT* __s1, const _CharT* __s2, size_t __n) _NOEXCEPT |
| 307 | { |
| 308 | _VSTD::copy_n(__s2, __n, __s1); |
| 309 | return __s1; |
| 310 | } |
| 311 | |
| 312 | template <class _CharT> |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 313 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 314 | _CharT* __assign_constexpr(_CharT* __s, size_t __n, _CharT __a) _NOEXCEPT |
| 315 | { |
| 316 | _VSTD::fill_n(__s, __n, __a); |
| 317 | return __s; |
| 318 | } |
| 319 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 320 | // char_traits<char> |
| 321 | |
| 322 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 323 | struct _LIBCPP_TEMPLATE_VIS char_traits<char> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 324 | { |
| 325 | typedef char char_type; |
| 326 | typedef int int_type; |
| 327 | typedef streamoff off_type; |
| 328 | typedef streampos pos_type; |
| 329 | typedef mbstate_t state_type; |
| 330 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 331 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 332 | void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 333 | static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT |
| 334 | {return __c1 == __c2;} |
| 335 | static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT |
| 336 | {return (unsigned char)__c1 < (unsigned char)__c2;} |
| 337 | |
Marshall Clow | aa32f16 | 2017-01-12 05:40:58 +0000 | [diff] [blame] | 338 | static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 339 | int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
Nikolas Klauser | ce79c5d | 2021-12-15 21:12:24 +0100 | [diff] [blame^] | 340 | |
| 341 | static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14 length(const char_type* __s) _NOEXCEPT { |
| 342 | // GCC currently does not support __builtin_strlen during constant evaluation. |
| 343 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816 |
| 344 | #ifdef _LIBCPP_COMPILER_GCC |
| 345 | if (__libcpp_is_constant_evaluated()) { |
| 346 | size_t __i = 0; |
| 347 | for (; __s[__i] != char_type('\0'); ++__i) |
| 348 | ; |
| 349 | return __i; |
| 350 | } |
| 351 | #endif |
| 352 | return __builtin_strlen(__s); |
| 353 | } |
| 354 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 355 | static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 356 | const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT; |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 357 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 358 | char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
| 359 | { |
| 360 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 361 | ? _VSTD::__move_constexpr(__s1, __s2, __n) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 362 | : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 363 | } |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 364 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 365 | char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 366 | { |
Arthur O'Dwyer | e9a5512 | 2021-04-20 22:24:24 -0400 | [diff] [blame] | 367 | if (!__libcpp_is_constant_evaluated()) { |
| 368 | _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); |
| 369 | } |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 370 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 371 | ? _VSTD::__copy_constexpr(__s1, __s2, __n) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 372 | : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n); |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 373 | } |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 374 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 375 | char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT |
| 376 | { |
| 377 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 378 | ? _VSTD::__assign_constexpr(__s, __n, __a) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 379 | : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 380 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 381 | |
| 382 | static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT |
| 383 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 384 | static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT |
| 385 | {return char_type(__c);} |
| 386 | static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT |
| 387 | {return int_type((unsigned char)__c);} |
| 388 | static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT |
| 389 | {return __c1 == __c2;} |
| 390 | static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT |
| 391 | {return int_type(EOF);} |
| 392 | }; |
| 393 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 394 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | aa32f16 | 2017-01-12 05:40:58 +0000 | [diff] [blame] | 395 | int |
| 396 | char_traits<char>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
| 397 | { |
| 398 | if (__n == 0) |
| 399 | return 0; |
Marshall Clow | b344c36 | 2017-01-26 06:58:29 +0000 | [diff] [blame] | 400 | #if __has_feature(cxx_constexpr_string_builtins) |
Marshall Clow | aa32f16 | 2017-01-12 05:40:58 +0000 | [diff] [blame] | 401 | return __builtin_memcmp(__s1, __s2, __n); |
| 402 | #elif _LIBCPP_STD_VER <= 14 |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 403 | return _VSTD::memcmp(__s1, __s2, __n); |
Marshall Clow | aa32f16 | 2017-01-12 05:40:58 +0000 | [diff] [blame] | 404 | #else |
| 405 | for (; __n; --__n, ++__s1, ++__s2) |
| 406 | { |
| 407 | if (lt(*__s1, *__s2)) |
| 408 | return -1; |
| 409 | if (lt(*__s2, *__s1)) |
| 410 | return 1; |
| 411 | } |
| 412 | return 0; |
| 413 | #endif |
| 414 | } |
| 415 | |
| 416 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 417 | const char* |
| 418 | char_traits<char>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT |
| 419 | { |
| 420 | if (__n == 0) |
Marshall Clow | 32043ac | 2018-02-06 18:58:05 +0000 | [diff] [blame] | 421 | return nullptr; |
Marshall Clow | b344c36 | 2017-01-26 06:58:29 +0000 | [diff] [blame] | 422 | #if __has_feature(cxx_constexpr_string_builtins) |
| 423 | return __builtin_char_memchr(__s, to_int_type(__a), __n); |
| 424 | #elif _LIBCPP_STD_VER <= 14 |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 425 | return (const char_type*) _VSTD::memchr(__s, to_int_type(__a), __n); |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 426 | #else |
| 427 | for (; __n; --__n) |
| 428 | { |
| 429 | if (eq(*__s, __a)) |
| 430 | return __s; |
| 431 | ++__s; |
| 432 | } |
Marshall Clow | 32043ac | 2018-02-06 18:58:05 +0000 | [diff] [blame] | 433 | return nullptr; |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 434 | #endif |
| 435 | } |
| 436 | |
| 437 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 438 | // char_traits<wchar_t> |
| 439 | |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 440 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 441 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 442 | struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 443 | { |
| 444 | typedef wchar_t char_type; |
| 445 | typedef wint_t int_type; |
| 446 | typedef streamoff off_type; |
| 447 | typedef streampos pos_type; |
| 448 | typedef mbstate_t state_type; |
| 449 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 450 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 451 | void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 452 | static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT |
| 453 | {return __c1 == __c2;} |
| 454 | static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT |
| 455 | {return __c1 < __c2;} |
| 456 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 457 | static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 458 | int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
| 459 | static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 460 | size_t length(const char_type* __s) _NOEXCEPT; |
| 461 | static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 462 | const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT; |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 463 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 464 | char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
| 465 | { |
| 466 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 467 | ? _VSTD::__move_constexpr(__s1, __s2, __n) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 468 | : __n == 0 ? __s1 : _VSTD::wmemmove(__s1, __s2, __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 469 | } |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 470 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 471 | char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 472 | { |
Arthur O'Dwyer | e9a5512 | 2021-04-20 22:24:24 -0400 | [diff] [blame] | 473 | if (!__libcpp_is_constant_evaluated()) { |
| 474 | _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); |
| 475 | } |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 476 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 477 | ? _VSTD::__copy_constexpr(__s1, __s2, __n) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 478 | : __n == 0 ? __s1 : _VSTD::wmemcpy(__s1, __s2, __n); |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 479 | } |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 480 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 481 | char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT |
| 482 | { |
| 483 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 484 | ? _VSTD::__assign_constexpr(__s, __n, __a) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 485 | : __n == 0 ? __s : _VSTD::wmemset(__s, __a, __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 486 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 487 | static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT |
| 488 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 489 | static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT |
| 490 | {return char_type(__c);} |
| 491 | static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT |
| 492 | {return int_type(__c);} |
| 493 | static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT |
| 494 | {return __c1 == __c2;} |
| 495 | static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT |
| 496 | {return int_type(WEOF);} |
| 497 | }; |
| 498 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 499 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 500 | int |
| 501 | char_traits<wchar_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
| 502 | { |
| 503 | if (__n == 0) |
| 504 | return 0; |
Marshall Clow | b344c36 | 2017-01-26 06:58:29 +0000 | [diff] [blame] | 505 | #if __has_feature(cxx_constexpr_string_builtins) |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 506 | return __builtin_wmemcmp(__s1, __s2, __n); |
| 507 | #elif _LIBCPP_STD_VER <= 14 |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 508 | return _VSTD::wmemcmp(__s1, __s2, __n); |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 509 | #else |
| 510 | for (; __n; --__n, ++__s1, ++__s2) |
| 511 | { |
| 512 | if (lt(*__s1, *__s2)) |
| 513 | return -1; |
| 514 | if (lt(*__s2, *__s1)) |
| 515 | return 1; |
| 516 | } |
| 517 | return 0; |
| 518 | #endif |
| 519 | } |
| 520 | |
| 521 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 522 | size_t |
| 523 | char_traits<wchar_t>::length(const char_type* __s) _NOEXCEPT |
| 524 | { |
Marshall Clow | b344c36 | 2017-01-26 06:58:29 +0000 | [diff] [blame] | 525 | #if __has_feature(cxx_constexpr_string_builtins) |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 526 | return __builtin_wcslen(__s); |
| 527 | #elif _LIBCPP_STD_VER <= 14 |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 528 | return _VSTD::wcslen(__s); |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 529 | #else |
| 530 | size_t __len = 0; |
| 531 | for (; !eq(*__s, char_type(0)); ++__s) |
| 532 | ++__len; |
| 533 | return __len; |
| 534 | #endif |
| 535 | } |
| 536 | |
| 537 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 538 | const wchar_t* |
| 539 | char_traits<wchar_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT |
| 540 | { |
| 541 | if (__n == 0) |
Marshall Clow | 32043ac | 2018-02-06 18:58:05 +0000 | [diff] [blame] | 542 | return nullptr; |
Marshall Clow | b344c36 | 2017-01-26 06:58:29 +0000 | [diff] [blame] | 543 | #if __has_feature(cxx_constexpr_string_builtins) |
Marshall Clow | 32043ac | 2018-02-06 18:58:05 +0000 | [diff] [blame] | 544 | return __builtin_wmemchr(__s, __a, __n); |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 545 | #elif _LIBCPP_STD_VER <= 14 |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 546 | return _VSTD::wmemchr(__s, __a, __n); |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 547 | #else |
| 548 | for (; __n; --__n) |
| 549 | { |
| 550 | if (eq(*__s, __a)) |
| 551 | return __s; |
| 552 | ++__s; |
| 553 | } |
Marshall Clow | 32043ac | 2018-02-06 18:58:05 +0000 | [diff] [blame] | 554 | return nullptr; |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 555 | #endif |
| 556 | } |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 557 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 558 | |
Louis Dionne | 76f4e0b | 2021-09-09 13:23:26 -0400 | [diff] [blame] | 559 | template <class _Traits> |
| 560 | _LIBCPP_INLINE_VISIBILITY |
| 561 | _LIBCPP_CONSTEXPR |
| 562 | inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT { |
| 563 | #if _LIBCPP_DEBUG_LEVEL >= 1 |
| 564 | return __s ? _Traits::length(__s) : (_VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, "p == nullptr", "null pointer pass to non-null argument of char_traits<...>::length")), 0); |
| 565 | #else |
| 566 | return _Traits::length(__s); |
| 567 | #endif |
| 568 | } |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 569 | |
Arthur O'Dwyer | afa5d5f | 2021-04-18 21:47:08 -0400 | [diff] [blame] | 570 | #ifndef _LIBCPP_HAS_NO_CHAR8_T |
Marshall Clow | 8732fed | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 571 | |
| 572 | template <> |
| 573 | struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t> |
| 574 | { |
| 575 | typedef char8_t char_type; |
| 576 | typedef unsigned int int_type; |
| 577 | typedef streamoff off_type; |
| 578 | typedef u8streampos pos_type; |
| 579 | typedef mbstate_t state_type; |
| 580 | |
| 581 | static inline constexpr void assign(char_type& __c1, const char_type& __c2) noexcept |
| 582 | {__c1 = __c2;} |
| 583 | static inline constexpr bool eq(char_type __c1, char_type __c2) noexcept |
| 584 | {return __c1 == __c2;} |
| 585 | static inline constexpr bool lt(char_type __c1, char_type __c2) noexcept |
| 586 | {return __c1 < __c2;} |
| 587 | |
| 588 | static constexpr |
| 589 | int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
| 590 | |
| 591 | static constexpr |
| 592 | size_t length(const char_type* __s) _NOEXCEPT; |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 593 | |
Marshall Clow | 8732fed | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 594 | _LIBCPP_INLINE_VISIBILITY static constexpr |
| 595 | const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT; |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 596 | |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 597 | static _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 598 | char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
| 599 | { |
| 600 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 601 | ? _VSTD::__move_constexpr(__s1, __s2, __n) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 602 | : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 603 | } |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 604 | |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 605 | static _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 606 | char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Arthur O'Dwyer | e9a5512 | 2021-04-20 22:24:24 -0400 | [diff] [blame] | 607 | { |
| 608 | if (!__libcpp_is_constant_evaluated()) { |
| 609 | _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); |
| 610 | } |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 611 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 612 | ? _VSTD::__copy_constexpr(__s1, __s2, __n) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 613 | : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 614 | } |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 615 | |
Arthur O'Dwyer | 14606d1 | 2020-11-23 12:44:41 -0500 | [diff] [blame] | 616 | static _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 617 | char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT |
| 618 | { |
| 619 | return __libcpp_is_constant_evaluated() |
Arthur O'Dwyer | e75dcfa | 2020-11-22 13:21:11 -0500 | [diff] [blame] | 620 | ? _VSTD::__assign_constexpr(__s, __n, __a) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame] | 621 | : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n); |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 622 | } |
Marshall Clow | 8732fed | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 623 | |
| 624 | static inline constexpr int_type not_eof(int_type __c) noexcept |
| 625 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 626 | static inline constexpr char_type to_char_type(int_type __c) noexcept |
| 627 | {return char_type(__c);} |
| 628 | static inline constexpr int_type to_int_type(char_type __c) noexcept |
| 629 | {return int_type(__c);} |
| 630 | static inline constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept |
| 631 | {return __c1 == __c2;} |
| 632 | static inline constexpr int_type eof() noexcept |
| 633 | {return int_type(EOF);} |
| 634 | }; |
| 635 | |
| 636 | // TODO use '__builtin_strlen' if it ever supports char8_t ?? |
| 637 | inline constexpr |
| 638 | size_t |
| 639 | char_traits<char8_t>::length(const char_type* __s) _NOEXCEPT |
| 640 | { |
| 641 | size_t __len = 0; |
| 642 | for (; !eq(*__s, char_type(0)); ++__s) |
| 643 | ++__len; |
| 644 | return __len; |
| 645 | } |
| 646 | |
| 647 | inline constexpr |
| 648 | int |
| 649 | char_traits<char8_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
| 650 | { |
| 651 | #if __has_feature(cxx_constexpr_string_builtins) |
| 652 | return __builtin_memcmp(__s1, __s2, __n); |
| 653 | #else |
| 654 | for (; __n; --__n, ++__s1, ++__s2) |
| 655 | { |
| 656 | if (lt(*__s1, *__s2)) |
| 657 | return -1; |
| 658 | if (lt(*__s2, *__s1)) |
| 659 | return 1; |
| 660 | } |
| 661 | return 0; |
| 662 | #endif |
| 663 | } |
| 664 | |
| 665 | // TODO use '__builtin_char_memchr' if it ever supports char8_t ?? |
| 666 | inline constexpr |
| 667 | const char8_t* |
| 668 | char_traits<char8_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT |
| 669 | { |
| 670 | for (; __n; --__n) |
| 671 | { |
| 672 | if (eq(*__s, __a)) |
| 673 | return __s; |
| 674 | ++__s; |
| 675 | } |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 676 | return nullptr; |
Marshall Clow | 8732fed | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Arthur O'Dwyer | afa5d5f | 2021-04-18 21:47:08 -0400 | [diff] [blame] | 679 | #endif // #_LIBCPP_HAS_NO_CHAR8_T |
Marshall Clow | 8732fed | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 680 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 681 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 682 | |
| 683 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 684 | struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 685 | { |
| 686 | typedef char16_t char_type; |
| 687 | typedef uint_least16_t int_type; |
| 688 | typedef streamoff off_type; |
| 689 | typedef u16streampos pos_type; |
| 690 | typedef mbstate_t state_type; |
| 691 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 692 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 693 | void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 694 | static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT |
| 695 | {return __c1 == __c2;} |
| 696 | static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT |
| 697 | {return __c1 < __c2;} |
| 698 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 699 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 700 | int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
| 701 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 702 | size_t length(const char_type* __s) _NOEXCEPT; |
| 703 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 704 | const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT; |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 705 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 706 | static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 707 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 708 | static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 709 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 710 | static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 711 | |
| 712 | static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT |
| 713 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 714 | static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT |
| 715 | {return char_type(__c);} |
| 716 | static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT |
| 717 | {return int_type(__c);} |
| 718 | static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT |
| 719 | {return __c1 == __c2;} |
| 720 | static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT |
| 721 | {return int_type(0xFFFF);} |
| 722 | }; |
| 723 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 724 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 725 | int |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 726 | char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 727 | { |
| 728 | for (; __n; --__n, ++__s1, ++__s2) |
| 729 | { |
| 730 | if (lt(*__s1, *__s2)) |
| 731 | return -1; |
| 732 | if (lt(*__s2, *__s1)) |
| 733 | return 1; |
| 734 | } |
| 735 | return 0; |
| 736 | } |
| 737 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 738 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 739 | size_t |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 740 | char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 741 | { |
| 742 | size_t __len = 0; |
| 743 | for (; !eq(*__s, char_type(0)); ++__s) |
| 744 | ++__len; |
| 745 | return __len; |
| 746 | } |
| 747 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 748 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 749 | const char16_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 750 | char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 751 | { |
| 752 | for (; __n; --__n) |
| 753 | { |
| 754 | if (eq(*__s, __a)) |
| 755 | return __s; |
| 756 | ++__s; |
| 757 | } |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 758 | return nullptr; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 761 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 762 | char16_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 763 | char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 764 | { |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 765 | if (__n == 0) return __s1; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 766 | char_type* __r = __s1; |
| 767 | if (__s1 < __s2) |
| 768 | { |
| 769 | for (; __n; --__n, ++__s1, ++__s2) |
| 770 | assign(*__s1, *__s2); |
| 771 | } |
| 772 | else if (__s2 < __s1) |
| 773 | { |
| 774 | __s1 += __n; |
| 775 | __s2 += __n; |
| 776 | for (; __n; --__n) |
| 777 | assign(*--__s1, *--__s2); |
| 778 | } |
| 779 | return __r; |
| 780 | } |
| 781 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 782 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 783 | char16_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 784 | char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 785 | { |
Arthur O'Dwyer | e9a5512 | 2021-04-20 22:24:24 -0400 | [diff] [blame] | 786 | if (!__libcpp_is_constant_evaluated()) { |
| 787 | _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); |
| 788 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 789 | char_type* __r = __s1; |
| 790 | for (; __n; --__n, ++__s1, ++__s2) |
| 791 | assign(*__s1, *__s2); |
| 792 | return __r; |
| 793 | } |
| 794 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 795 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 796 | char16_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 797 | char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 798 | { |
| 799 | char_type* __r = __s; |
| 800 | for (; __n; --__n, ++__s) |
| 801 | assign(*__s, __a); |
| 802 | return __r; |
| 803 | } |
| 804 | |
| 805 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 806 | struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t> |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 807 | { |
| 808 | typedef char32_t char_type; |
| 809 | typedef uint_least32_t int_type; |
| 810 | typedef streamoff off_type; |
| 811 | typedef u32streampos pos_type; |
| 812 | typedef mbstate_t state_type; |
| 813 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 814 | static inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 815 | void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 816 | static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT |
| 817 | {return __c1 == __c2;} |
| 818 | static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT |
| 819 | {return __c1 < __c2;} |
| 820 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 821 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 822 | int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
| 823 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 824 | size_t length(const char_type* __s) _NOEXCEPT; |
| 825 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 826 | const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT; |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 827 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 828 | static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 829 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 830 | static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 831 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 832 | static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 833 | |
| 834 | static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT |
| 835 | {return eq_int_type(__c, eof()) ? ~eof() : __c;} |
| 836 | static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT |
| 837 | {return char_type(__c);} |
| 838 | static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT |
| 839 | {return int_type(__c);} |
| 840 | static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT |
| 841 | {return __c1 == __c2;} |
| 842 | static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT |
| 843 | {return int_type(0xFFFFFFFF);} |
| 844 | }; |
| 845 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 846 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 847 | int |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 848 | char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 849 | { |
| 850 | for (; __n; --__n, ++__s1, ++__s2) |
| 851 | { |
| 852 | if (lt(*__s1, *__s2)) |
| 853 | return -1; |
| 854 | if (lt(*__s2, *__s1)) |
| 855 | return 1; |
| 856 | } |
| 857 | return 0; |
| 858 | } |
| 859 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 860 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 861 | size_t |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 862 | char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 863 | { |
| 864 | size_t __len = 0; |
| 865 | for (; !eq(*__s, char_type(0)); ++__s) |
| 866 | ++__len; |
| 867 | return __len; |
| 868 | } |
| 869 | |
Marshall Clow | 951035b | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 870 | inline _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 871 | const char32_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 872 | char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 873 | { |
| 874 | for (; __n; --__n) |
| 875 | { |
| 876 | if (eq(*__s, __a)) |
| 877 | return __s; |
| 878 | ++__s; |
| 879 | } |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 880 | return nullptr; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 883 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 884 | char32_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 885 | char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 886 | { |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 887 | if (__n == 0) return __s1; |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 888 | char_type* __r = __s1; |
| 889 | if (__s1 < __s2) |
| 890 | { |
| 891 | for (; __n; --__n, ++__s1, ++__s2) |
| 892 | assign(*__s1, *__s2); |
| 893 | } |
| 894 | else if (__s2 < __s1) |
| 895 | { |
| 896 | __s1 += __n; |
| 897 | __s2 += __n; |
| 898 | for (; __n; --__n) |
| 899 | assign(*--__s1, *--__s2); |
| 900 | } |
| 901 | return __r; |
| 902 | } |
| 903 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 904 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 905 | char32_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 906 | char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 907 | { |
Arthur O'Dwyer | e9a5512 | 2021-04-20 22:24:24 -0400 | [diff] [blame] | 908 | if (!__libcpp_is_constant_evaluated()) { |
| 909 | _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); |
| 910 | } |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 911 | char_type* __r = __s1; |
| 912 | for (; __n; --__n, ++__s1, ++__s2) |
| 913 | assign(*__s1, *__s2); |
| 914 | return __r; |
| 915 | } |
| 916 | |
Michael Park | b5f4447 | 2019-11-08 13:42:26 +0000 | [diff] [blame] | 917 | inline _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 918 | char32_t* |
Marshall Clow | 36097db | 2016-07-28 04:52:02 +0000 | [diff] [blame] | 919 | char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 920 | { |
| 921 | char_type* __r = __s; |
| 922 | for (; __n; --__n, ++__s) |
| 923 | assign(*__s, __a); |
| 924 | return __r; |
| 925 | } |
| 926 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 927 | #endif // _LIBCPP_HAS_NO_UNICODE_CHARS |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 928 | |
| 929 | // helper fns for basic_string and string_view |
| 930 | |
| 931 | // __str_find |
| 932 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 933 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 934 | __str_find(const _CharT *__p, _SizeT __sz, |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 935 | _CharT __c, _SizeT __pos) _NOEXCEPT |
| 936 | { |
| 937 | if (__pos >= __sz) |
| 938 | return __npos; |
| 939 | const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c); |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 940 | if (__r == nullptr) |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 941 | return __npos; |
| 942 | return static_cast<_SizeT>(__r - __p); |
| 943 | } |
| 944 | |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 945 | template <class _CharT, class _Traits> |
| 946 | inline _LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT * |
| 947 | __search_substring(const _CharT *__first1, const _CharT *__last1, |
zoecarver | 1997e0a | 2021-02-05 11:54:47 -0800 | [diff] [blame] | 948 | const _CharT *__first2, const _CharT *__last2) _NOEXCEPT { |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 949 | // Take advantage of knowing source and pattern lengths. |
| 950 | // Stop short when source is smaller than pattern. |
| 951 | const ptrdiff_t __len2 = __last2 - __first2; |
| 952 | if (__len2 == 0) |
| 953 | return __first1; |
| 954 | |
| 955 | ptrdiff_t __len1 = __last1 - __first1; |
| 956 | if (__len1 < __len2) |
| 957 | return __last1; |
| 958 | |
| 959 | // First element of __first2 is loop invariant. |
| 960 | _CharT __f2 = *__first2; |
| 961 | while (true) { |
| 962 | __len1 = __last1 - __first1; |
| 963 | // Check whether __first1 still has at least __len2 bytes. |
| 964 | if (__len1 < __len2) |
| 965 | return __last1; |
| 966 | |
| 967 | // Find __f2 the first byte matching in __first1. |
| 968 | __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2); |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 969 | if (__first1 == nullptr) |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 970 | return __last1; |
| 971 | |
| 972 | // It is faster to compare from the first byte of __first1 even if we |
| 973 | // already know that it matches the first byte of __first2: this is because |
| 974 | // __first2 is most likely aligned, as it is user's "pattern" string, and |
| 975 | // __first1 + 1 is most likely not aligned, as the match is in the middle of |
| 976 | // the string. |
| 977 | if (_Traits::compare(__first1, __first2, __len2) == 0) |
| 978 | return __first1; |
| 979 | |
| 980 | ++__first1; |
| 981 | } |
| 982 | } |
| 983 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 984 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 985 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 986 | __str_find(const _CharT *__p, _SizeT __sz, |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 987 | const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT |
| 988 | { |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 989 | if (__pos > __sz) |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 990 | return __npos; |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 991 | |
| 992 | if (__n == 0) // There is nothing to search, just return __pos. |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 993 | return __pos; |
Sebastian Pop | 6511520 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 994 | |
| 995 | const _CharT *__r = __search_substring<_CharT, _Traits>( |
| 996 | __p + __pos, __p + __sz, __s, __s + __n); |
| 997 | |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 998 | if (__r == __p + __sz) |
| 999 | return __npos; |
| 1000 | return static_cast<_SizeT>(__r - __p); |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | // __str_rfind |
| 1005 | |
| 1006 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1007 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 1008 | __str_rfind(const _CharT *__p, _SizeT __sz, |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1009 | _CharT __c, _SizeT __pos) _NOEXCEPT |
| 1010 | { |
| 1011 | if (__sz < 1) |
| 1012 | return __npos; |
| 1013 | if (__pos < __sz) |
| 1014 | ++__pos; |
| 1015 | else |
| 1016 | __pos = __sz; |
| 1017 | for (const _CharT* __ps = __p + __pos; __ps != __p;) |
| 1018 | { |
| 1019 | if (_Traits::eq(*--__ps, __c)) |
| 1020 | return static_cast<_SizeT>(__ps - __p); |
| 1021 | } |
| 1022 | return __npos; |
| 1023 | } |
| 1024 | |
| 1025 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1026 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 1027 | __str_rfind(const _CharT *__p, _SizeT __sz, |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1028 | const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT |
| 1029 | { |
| 1030 | __pos = _VSTD::min(__pos, __sz); |
| 1031 | if (__n < __sz - __pos) |
| 1032 | __pos += __n; |
| 1033 | else |
| 1034 | __pos = __sz; |
| 1035 | const _CharT* __r = _VSTD::__find_end( |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 1036 | __p, __p + __pos, __s, __s + __n, _Traits::eq, |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1037 | random_access_iterator_tag(), random_access_iterator_tag()); |
| 1038 | if (__n > 0 && __r == __p + __pos) |
| 1039 | return __npos; |
| 1040 | return static_cast<_SizeT>(__r - __p); |
| 1041 | } |
| 1042 | |
| 1043 | // __str_find_first_of |
| 1044 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1045 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 1046 | __str_find_first_of(const _CharT *__p, _SizeT __sz, |
| 1047 | const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT |
| 1048 | { |
| 1049 | if (__pos >= __sz || __n == 0) |
| 1050 | return __npos; |
| 1051 | const _CharT* __r = _VSTD::__find_first_of_ce |
| 1052 | (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq ); |
| 1053 | if (__r == __p + __sz) |
| 1054 | return __npos; |
| 1055 | return static_cast<_SizeT>(__r - __p); |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | // __str_find_last_of |
| 1060 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1061 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 1062 | __str_find_last_of(const _CharT *__p, _SizeT __sz, |
| 1063 | const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT |
| 1064 | { |
| 1065 | if (__n != 0) |
| 1066 | { |
| 1067 | if (__pos < __sz) |
| 1068 | ++__pos; |
| 1069 | else |
| 1070 | __pos = __sz; |
| 1071 | for (const _CharT* __ps = __p + __pos; __ps != __p;) |
| 1072 | { |
| 1073 | const _CharT* __r = _Traits::find(__s, __n, *--__ps); |
| 1074 | if (__r) |
| 1075 | return static_cast<_SizeT>(__ps - __p); |
| 1076 | } |
| 1077 | } |
| 1078 | return __npos; |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | // __str_find_first_not_of |
| 1083 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1084 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 1085 | __str_find_first_not_of(const _CharT *__p, _SizeT __sz, |
| 1086 | const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT |
| 1087 | { |
| 1088 | if (__pos < __sz) |
| 1089 | { |
| 1090 | const _CharT* __pe = __p + __sz; |
| 1091 | for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1092 | if (_Traits::find(__s, __n, *__ps) == nullptr) |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1093 | return static_cast<_SizeT>(__ps - __p); |
| 1094 | } |
| 1095 | return __npos; |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1100 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 1101 | __str_find_first_not_of(const _CharT *__p, _SizeT __sz, |
| 1102 | _CharT __c, _SizeT __pos) _NOEXCEPT |
| 1103 | { |
| 1104 | if (__pos < __sz) |
| 1105 | { |
| 1106 | const _CharT* __pe = __p + __sz; |
| 1107 | for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps) |
| 1108 | if (!_Traits::eq(*__ps, __c)) |
| 1109 | return static_cast<_SizeT>(__ps - __p); |
| 1110 | } |
| 1111 | return __npos; |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | // __str_find_last_not_of |
| 1116 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1117 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 1118 | __str_find_last_not_of(const _CharT *__p, _SizeT __sz, |
| 1119 | const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT |
| 1120 | { |
| 1121 | if (__pos < __sz) |
| 1122 | ++__pos; |
| 1123 | else |
| 1124 | __pos = __sz; |
| 1125 | for (const _CharT* __ps = __p + __pos; __ps != __p;) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1126 | if (_Traits::find(__s, __n, *--__ps) == nullptr) |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1127 | return static_cast<_SizeT>(__ps - __p); |
| 1128 | return __npos; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | template<class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
| 1133 | inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY |
| 1134 | __str_find_last_not_of(const _CharT *__p, _SizeT __sz, |
| 1135 | _CharT __c, _SizeT __pos) _NOEXCEPT |
| 1136 | { |
| 1137 | if (__pos < __sz) |
| 1138 | ++__pos; |
| 1139 | else |
| 1140 | __pos = __sz; |
| 1141 | for (const _CharT* __ps = __p + __pos; __ps != __p;) |
| 1142 | if (!_Traits::eq(*--__ps, __c)) |
| 1143 | return static_cast<_SizeT>(__ps - __p); |
| 1144 | return __npos; |
| 1145 | } |
| 1146 | |
| 1147 | template<class _Ptr> |
Eric Fiselier | 1b57fa8 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 1148 | inline _LIBCPP_INLINE_VISIBILITY |
| 1149 | size_t __do_string_hash(_Ptr __p, _Ptr __e) |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1150 | { |
| 1151 | typedef typename iterator_traits<_Ptr>::value_type value_type; |
| 1152 | return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type)); |
| 1153 | } |
| 1154 | |
| 1155 | template <class _CharT, class _Iter, class _Traits=char_traits<_CharT> > |
| 1156 | struct __quoted_output_proxy |
| 1157 | { |
| 1158 | _Iter __first; |
| 1159 | _Iter __last; |
| 1160 | _CharT __delim; |
| 1161 | _CharT __escape; |
| 1162 | |
| 1163 | __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e) |
| 1164 | : __first(__f), __last(__l), __delim(__d), __escape(__e) {} |
Louis Dionne | fd77e4f | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 1165 | // This would be a nice place for a string_ref |
Marshall Clow | df63a6d | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1166 | }; |
| 1167 | |
| 1168 | _LIBCPP_END_NAMESPACE_STD |
| 1169 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1170 | _LIBCPP_POP_MACROS |
| 1171 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 1172 | #endif // _LIBCPP___STRING |