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