Louis Dionne | eb8650a | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Nikolas Klauser | 976f370 | 2022-06-06 23:35:24 +0200 | [diff] [blame] | 9 | #ifndef _LIBCPP___STRING_CHAR_TRAITS_H |
| 10 | #define _LIBCPP___STRING_CHAR_TRAITS_H |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 11 | |
Christopher Di Bella | 050b064 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 12 | #include <__algorithm/fill_n.h> |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 13 | #include <__algorithm/find.h> |
Christopher Di Bella | 050b064 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 14 | #include <__algorithm/find_end.h> |
Arthur O'Dwyer | 4d81a46 | 2022-01-07 09:45:05 -0500 | [diff] [blame] | 15 | #include <__algorithm/find_first_of.h> |
Christopher Di Bella | 050b064 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 16 | #include <__algorithm/min.h> |
Louis Dionne | 37dca60 | 2024-02-29 10:12:22 -0500 | [diff] [blame] | 17 | #include <__assert> |
Nikolas Klauser | 7afa159 | 2022-09-17 14:30:29 +0200 | [diff] [blame] | 18 | #include <__compare/ordering.h> |
Arthur O'Dwyer | 4d81a46 | 2022-01-07 09:45:05 -0500 | [diff] [blame] | 19 | #include <__config> |
Louis Dionne | 88f8993 | 2024-11-01 08:48:32 -0400 | [diff] [blame] | 20 | #include <__cstddef/ptrdiff_t.h> |
Nikolas Klauser | 976f370 | 2022-06-06 23:35:24 +0200 | [diff] [blame] | 21 | #include <__functional/hash.h> |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 22 | #include <__functional/identity.h> |
Christopher Di Bella | 050b064 | 2021-07-01 09:25:35 -0400 | [diff] [blame] | 23 | #include <__iterator/iterator_traits.h> |
Louis Dionne | 09e3a36 | 2024-09-16 15:06:20 -0400 | [diff] [blame] | 24 | #include <__std_mbstate_t.h> |
Nikolas Klauser | e8cb355 | 2023-02-05 00:21:11 +0100 | [diff] [blame] | 25 | #include <__string/constexpr_c_functions.h> |
Nikolas Klauser | 7afa159 | 2022-09-17 14:30:29 +0200 | [diff] [blame] | 26 | #include <__type_traits/is_constant_evaluated.h> |
philnik777 | 5a305cea | 2023-11-19 16:49:40 +0100 | [diff] [blame] | 27 | #include <__utility/is_pointer_in_range.h> |
Nikolas Klauser | 976f370 | 2022-06-06 23:35:24 +0200 | [diff] [blame] | 28 | #include <cstdint> |
| 29 | #include <cstdio> |
Nikolas Klauser | 976f370 | 2022-06-06 23:35:24 +0200 | [diff] [blame] | 30 | #include <iosfwd> |
Louis Dionne | f4c1258 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 31 | |
Nikolas Klauser | c6f3b7b | 2024-11-06 10:39:19 +0100 | [diff] [blame] | 32 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 33 | # include <cwchar> // for wmemcpy |
Louis Dionne | f4c1258 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 34 | #endif |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 35 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 36 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Arthur O'Dwyer | fa6b9e4 | 2022-02-01 20:16:40 -0500 | [diff] [blame] | 37 | # pragma GCC system_header |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 40 | _LIBCPP_PUSH_MACROS |
| 41 | #include <__undef_macros> |
| 42 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 43 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 44 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 45 | template <class _CharT> |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 46 | struct char_traits; |
| 47 | /* |
| 48 | The Standard does not define the base template for char_traits because it is impossible to provide |
| 49 | a correct definition for arbitrary character types. Instead, it requires implementations to provide |
| 50 | specializations for predefined character types like `char`, `wchar_t` and others. We provide this as |
| 51 | exposition-only to document what members a char_traits specialization should provide: |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 52 | { |
Mark de Wever | 2928b23 | 2022-08-04 19:44:21 +0200 | [diff] [blame] | 53 | using char_type = _CharT; |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 54 | using int_type = ...; |
| 55 | using off_type = ...; |
| 56 | using pos_type = ...; |
| 57 | using state_type = ...; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 58 | |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 59 | static void assign(char_type&, const char_type&); |
| 60 | static bool eq(char_type, char_type); |
| 61 | static bool lt(char_type, char_type); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 62 | |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 63 | static int compare(const char_type*, const char_type*, size_t); |
| 64 | static size_t length(const char_type*); |
| 65 | static const char_type* find(const char_type*, size_t, const char_type&); |
| 66 | static char_type* move(char_type*, const char_type*, size_t); |
Louis Dionne | 08a0faf | 2022-11-23 14:06:14 -0500 | [diff] [blame] | 67 | static char_type* copy(char_type*, const char_type*, size_t); |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 68 | static char_type* assign(char_type*, size_t, char_type); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 69 | |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 70 | static int_type not_eof(int_type); |
| 71 | static char_type to_char_type(int_type); |
| 72 | static int_type to_int_type(char_type); |
| 73 | static bool eq_int_type(int_type, int_type); |
| 74 | static int_type eof(); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 75 | }; |
Louis Dionne | aeecef0 | 2022-11-17 10:47:18 -0500 | [diff] [blame] | 76 | */ |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 77 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 78 | // char_traits<char> |
| 79 | |
| 80 | template <> |
Nikolas Klauser | af9c04f | 2025-04-09 23:47:57 +0200 | [diff] [blame] | 81 | struct char_traits<char> { |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 82 | using char_type = char; |
| 83 | using int_type = int; |
| 84 | using off_type = streamoff; |
| 85 | using pos_type = streampos; |
| 86 | using state_type = mbstate_t; |
Nikolas Klauser | 4f15267 | 2023-02-14 00:56:09 +0100 | [diff] [blame] | 87 | #if _LIBCPP_STD_VER >= 20 |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 88 | using comparison_category = strong_ordering; |
Mark de Wever | 3818b4d | 2022-07-19 07:56:23 +0200 | [diff] [blame] | 89 | #endif |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 90 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 91 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void |
| 92 | assign(char_type& __c1, const char_type& __c2) _NOEXCEPT { |
| 93 | __c1 = __c2; |
| 94 | } |
Nikolas Klauser | 83ce139 | 2023-01-23 10:27:14 +0100 | [diff] [blame] | 95 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 96 | // TODO: Make this _LIBCPP_HIDE_FROM_ABI |
| 97 | static inline _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT { |
| 98 | return __c1 == __c2; |
| 99 | } |
| 100 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT { |
| 101 | return (unsigned char)__c1 < (unsigned char)__c2; |
| 102 | } |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 103 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 104 | // __constexpr_memcmp requires a trivially lexicographically comparable type, but char is not when char is a signed |
| 105 | // type |
| 106 | static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 int |
| 107 | compare(const char_type* __lhs, const char_type* __rhs, size_t __count) _NOEXCEPT { |
| 108 | if (__libcpp_is_constant_evaluated()) { |
Nikolas Klauser | 746cf7e | 2023-05-07 11:18:32 -0700 | [diff] [blame] | 109 | #ifdef _LIBCPP_COMPILER_CLANG_BASED |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 110 | return __builtin_memcmp(__lhs, __rhs, __count); |
Nikolas Klauser | 746cf7e | 2023-05-07 11:18:32 -0700 | [diff] [blame] | 111 | #else |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 112 | while (__count != 0) { |
| 113 | if (lt(*__lhs, *__rhs)) |
| 114 | return -1; |
| 115 | if (lt(*__rhs, *__lhs)) |
| 116 | return 1; |
Nikolas Klauser | 148ef80 | 2021-12-15 21:12:24 +0100 | [diff] [blame] | 117 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 118 | __count -= sizeof(char_type); |
| 119 | ++__lhs; |
| 120 | ++__rhs; |
Nikolas Klauser | 746cf7e | 2023-05-07 11:18:32 -0700 | [diff] [blame] | 121 | } |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 122 | return 0; |
| 123 | #endif // _LIBCPP_COMPILER_CLANG_BASED |
| 124 | } else { |
| 125 | return __builtin_memcmp(__lhs, __rhs, __count); |
Nikolas Klauser | 746cf7e | 2023-05-07 11:18:32 -0700 | [diff] [blame] | 126 | } |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 127 | } |
Nikolas Klauser | 148ef80 | 2021-12-15 21:12:24 +0100 | [diff] [blame] | 128 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 129 | static inline _LIBCPP_HIDE_FROM_ABI size_t _LIBCPP_CONSTEXPR_SINCE_CXX17 length(const char_type* __s) _NOEXCEPT { |
| 130 | return std::__constexpr_strlen(__s); |
| 131 | } |
Nikolas Klauser | 746cf7e | 2023-05-07 11:18:32 -0700 | [diff] [blame] | 132 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 133 | static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const char_type* |
| 134 | find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT { |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 135 | return std::__constexpr_memchr(__s, __a, __n); |
| 136 | } |
Nikolas Klauser | 993a22e | 2022-06-13 17:54:20 +0200 | [diff] [blame] | 137 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 138 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 char_type* |
| 139 | move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT { |
| 140 | return std::__constexpr_memmove(__s1, __s2, __element_count(__n)); |
| 141 | } |
Nikolas Klauser | 993a22e | 2022-06-13 17:54:20 +0200 | [diff] [blame] | 142 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 143 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 char_type* |
| 144 | copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT { |
| 145 | _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__s1, __s1 + __n, __s2), |
| 146 | "char_traits::copy: source and destination ranges overlap"); |
Nikolas Klauser | 2096f37 | 2024-03-21 11:57:07 +0100 | [diff] [blame] | 147 | std::__constexpr_memmove(__s1, __s2, __element_count(__n)); |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 148 | return __s1; |
| 149 | } |
Nikolas Klauser | 993a22e | 2022-06-13 17:54:20 +0200 | [diff] [blame] | 150 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 151 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 char_type* |
| 152 | assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT { |
| 153 | std::fill_n(__s, __n, __a); |
| 154 | return __s; |
| 155 | } |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 156 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 157 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT { |
| 158 | return eq_int_type(__c, eof()) ? ~eof() : __c; |
| 159 | } |
| 160 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT { |
| 161 | return char_type(__c); |
| 162 | } |
| 163 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT { |
| 164 | return int_type((unsigned char)__c); |
| 165 | } |
| 166 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT { |
| 167 | return __c1 == __c2; |
| 168 | } |
| 169 | static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT { return int_type(EOF); } |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Nikolas Klauser | 2da0055 | 2024-07-06 15:35:13 +0200 | [diff] [blame] | 172 | template <class _CharT, class _IntT, _IntT _EOFVal> |
| 173 | struct __char_traits_base { |
| 174 | using char_type = _CharT; |
| 175 | using int_type = _IntT; |
| 176 | using off_type = streamoff; |
| 177 | using state_type = mbstate_t; |
| 178 | #if _LIBCPP_STD_VER >= 20 |
| 179 | using comparison_category = strong_ordering; |
| 180 | #endif |
| 181 | |
| 182 | // There are different aliases for the different char types, but they are all aliases to this type |
| 183 | using pos_type = fpos<mbstate_t>; |
| 184 | |
| 185 | _LIBCPP_HIDE_FROM_ABI static inline _LIBCPP_CONSTEXPR_SINCE_CXX17 void |
| 186 | assign(char_type& __lhs, const char_type& __rhs) _NOEXCEPT { |
| 187 | __lhs = __rhs; |
| 188 | } |
| 189 | |
| 190 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR bool eq(char_type __lhs, char_type __rhs) _NOEXCEPT { |
| 191 | return __lhs == __rhs; |
| 192 | } |
| 193 | |
| 194 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR bool lt(char_type __lhs, char_type __rhs) _NOEXCEPT { |
| 195 | return __lhs < __rhs; |
| 196 | } |
| 197 | |
| 198 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 char_type* |
| 199 | move(char_type* __dest, const char_type* __src, size_t __n) _NOEXCEPT { |
| 200 | return std::__constexpr_memmove(__dest, __src, __element_count(__n)); |
| 201 | } |
| 202 | |
| 203 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 char_type* |
| 204 | copy(char_type* __dest, const char_type* __src, size_t __n) _NOEXCEPT { |
| 205 | _LIBCPP_ASSERT_NON_OVERLAPPING_RANGES(!std::__is_pointer_in_range(__dest, __dest + __n, __src), |
| 206 | "char_traits::copy: source and destination ranges overlap"); |
| 207 | return std::__constexpr_memmove(__dest, __src, __element_count(__n)); |
| 208 | } |
| 209 | |
| 210 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 char_type* |
| 211 | assign(char_type* __str, size_t __n, char_type __fill_char) _NOEXCEPT { |
| 212 | std::fill_n(__str, __n, __fill_char); |
| 213 | return __str; |
| 214 | } |
| 215 | |
| 216 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT { |
| 217 | return char_type(__c); |
| 218 | } |
| 219 | |
| 220 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT { return int_type(__c); } |
| 221 | |
| 222 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR bool eq_int_type(int_type __lhs, int_type __rhs) _NOEXCEPT { |
| 223 | return __lhs == __rhs; |
| 224 | } |
| 225 | |
| 226 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT { return _EOFVal; } |
| 227 | |
| 228 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT { |
| 229 | return eq_int_type(__c, eof()) ? static_cast<int_type>(~eof()) : __c; |
| 230 | } |
| 231 | }; |
| 232 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 233 | // char_traits<wchar_t> |
| 234 | |
Nikolas Klauser | c6f3b7b | 2024-11-06 10:39:19 +0100 | [diff] [blame] | 235 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 236 | template <> |
Nikolas Klauser | af9c04f | 2025-04-09 23:47:57 +0200 | [diff] [blame] | 237 | struct char_traits<wchar_t> : __char_traits_base<wchar_t, wint_t, static_cast<wint_t>(WEOF)> { |
Nikolas Klauser | 83ce139 | 2023-01-23 10:27:14 +0100 | [diff] [blame] | 238 | static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 int |
| 239 | compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT { |
Nikolas Klauser | 7217346 | 2022-12-07 18:31:14 +0100 | [diff] [blame] | 240 | if (__n == 0) |
Nikolas Klauser | 83ce139 | 2023-01-23 10:27:14 +0100 | [diff] [blame] | 241 | return 0; |
Nikolas Klauser | 7217346 | 2022-12-07 18:31:14 +0100 | [diff] [blame] | 242 | return std::__constexpr_wmemcmp(__s1, __s2, __n); |
| 243 | } |
| 244 | |
Nikolas Klauser | 83ce139 | 2023-01-23 10:27:14 +0100 | [diff] [blame] | 245 | static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 size_t length(const char_type* __s) _NOEXCEPT { |
Nikolas Klauser | 7217346 | 2022-12-07 18:31:14 +0100 | [diff] [blame] | 246 | return std::__constexpr_wcslen(__s); |
| 247 | } |
| 248 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 249 | static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 const char_type* |
| 250 | find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT { |
Nikolas Klauser | 7217346 | 2022-12-07 18:31:14 +0100 | [diff] [blame] | 251 | return std::__constexpr_wmemchr(__s, __a, __n); |
| 252 | } |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 253 | }; |
Nikolas Klauser | c6f3b7b | 2024-11-06 10:39:19 +0100 | [diff] [blame] | 254 | #endif // _LIBCPP_HAS_WIDE_CHARACTERS |
Marshall Clow | 05d57fa | 2017-01-12 04:37:14 +0000 | [diff] [blame] | 255 | |
Nikolas Klauser | ba87515 | 2024-10-12 09:49:52 +0200 | [diff] [blame] | 256 | #if _LIBCPP_HAS_CHAR8_T |
Marshall Clow | 7dad0bd | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 257 | |
| 258 | template <> |
Nikolas Klauser | af9c04f | 2025-04-09 23:47:57 +0200 | [diff] [blame] | 259 | struct char_traits<char8_t> : __char_traits_base<char8_t, unsigned int, static_cast<unsigned int>(EOF)> { |
Nikolas Klauser | 7217346 | 2022-12-07 18:31:14 +0100 | [diff] [blame] | 260 | static _LIBCPP_HIDE_FROM_ABI constexpr int |
Nikolas Klauser | 1f98ac0 | 2024-06-23 22:03:41 +0200 | [diff] [blame] | 261 | compare(const char_type* __s1, const char_type* __s2, size_t __n) noexcept { |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 262 | return std::__constexpr_memcmp(__s1, __s2, __element_count(__n)); |
Nikolas Klauser | 7217346 | 2022-12-07 18:31:14 +0100 | [diff] [blame] | 263 | } |
Marshall Clow | 7dad0bd | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 264 | |
Nikolas Klauser | 1f98ac0 | 2024-06-23 22:03:41 +0200 | [diff] [blame] | 265 | static _LIBCPP_HIDE_FROM_ABI constexpr size_t length(const char_type* __str) noexcept { |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 266 | return std::__constexpr_strlen(__str); |
| 267 | } |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 268 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 269 | _LIBCPP_HIDE_FROM_ABI static constexpr const char_type* |
Nikolas Klauser | 1f98ac0 | 2024-06-23 22:03:41 +0200 | [diff] [blame] | 270 | find(const char_type* __s, size_t __n, const char_type& __a) noexcept { |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 271 | return std::__constexpr_memchr(__s, __a, __n); |
| 272 | } |
Marshall Clow | 7dad0bd | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Nikolas Klauser | ba87515 | 2024-10-12 09:49:52 +0200 | [diff] [blame] | 275 | #endif // _LIBCPP_HAS_CHAR8_T |
Marshall Clow | 7dad0bd | 2018-12-11 04:35:44 +0000 | [diff] [blame] | 276 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 277 | template <> |
Nikolas Klauser | af9c04f | 2025-04-09 23:47:57 +0200 | [diff] [blame] | 278 | struct char_traits<char16_t> : __char_traits_base<char16_t, uint_least16_t, static_cast<uint_least16_t>(0xFFFF)> { |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 279 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX17 int |
| 280 | compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
| 281 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX17 size_t length(const char_type* __s) _NOEXCEPT; |
Nikolas Klauser | 993a22e | 2022-06-13 17:54:20 +0200 | [diff] [blame] | 282 | |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 283 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX17 const char_type* |
| 284 | find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT { |
| 285 | __identity __proj; |
Nikolas Klauser | 8400324 | 2024-04-27 09:51:59 +0200 | [diff] [blame] | 286 | const char_type* __match = std::__find(__s, __s + __n, __a, __proj); |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 287 | if (__match == __s + __n) |
| 288 | return nullptr; |
| 289 | return __match; |
| 290 | } |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 293 | inline _LIBCPP_CONSTEXPR_SINCE_CXX17 int |
| 294 | char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT { |
| 295 | for (; __n; --__n, ++__s1, ++__s2) { |
| 296 | if (lt(*__s1, *__s2)) |
| 297 | return -1; |
| 298 | if (lt(*__s2, *__s1)) |
| 299 | return 1; |
| 300 | } |
| 301 | return 0; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 304 | inline _LIBCPP_CONSTEXPR_SINCE_CXX17 size_t char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT { |
| 305 | size_t __len = 0; |
| 306 | for (; !eq(*__s, char_type(0)); ++__s) |
| 307 | ++__len; |
| 308 | return __len; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 311 | template <> |
Nikolas Klauser | af9c04f | 2025-04-09 23:47:57 +0200 | [diff] [blame] | 312 | struct char_traits<char32_t> : __char_traits_base<char32_t, uint_least32_t, static_cast<uint_least32_t>(0xFFFFFFFF)> { |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 313 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX17 int |
| 314 | compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT; |
| 315 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX17 size_t length(const char_type* __s) _NOEXCEPT; |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 316 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 317 | _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX17 const char_type* |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 318 | find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT { |
| 319 | __identity __proj; |
Nikolas Klauser | 8400324 | 2024-04-27 09:51:59 +0200 | [diff] [blame] | 320 | const char_type* __match = std::__find(__s, __s + __n, __a, __proj); |
Nikolas Klauser | 29312d3 | 2024-04-20 11:40:18 +0200 | [diff] [blame] | 321 | if (__match == __s + __n) |
| 322 | return nullptr; |
| 323 | return __match; |
| 324 | } |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 327 | inline _LIBCPP_CONSTEXPR_SINCE_CXX17 int |
| 328 | char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT { |
| 329 | for (; __n; --__n, ++__s1, ++__s2) { |
| 330 | if (lt(*__s1, *__s2)) |
| 331 | return -1; |
| 332 | if (lt(*__s2, *__s1)) |
| 333 | return 1; |
| 334 | } |
| 335 | return 0; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 338 | inline _LIBCPP_CONSTEXPR_SINCE_CXX17 size_t char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT { |
| 339 | size_t __len = 0; |
| 340 | for (; !eq(*__s, char_type(0)); ++__s) |
| 341 | ++__len; |
| 342 | return __len; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 345 | // helper fns for basic_string and string_view |
| 346 | |
| 347 | // __str_find |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 348 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 349 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 350 | __str_find(const _CharT* __p, _SizeT __sz, _CharT __c, _SizeT __pos) _NOEXCEPT { |
Nikolas Klauser | 38595fb | 2025-05-07 16:54:34 +0200 | [diff] [blame^] | 351 | if (__pos > __sz) |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 352 | return __npos; |
| 353 | const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c); |
| 354 | if (__r == nullptr) |
| 355 | return __npos; |
| 356 | return static_cast<_SizeT>(__r - __p); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Sebastian Pop | 9070500 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 359 | template <class _CharT, class _Traits> |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 360 | _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR_SINCE_CXX14 const _CharT* __search_substring( |
| 361 | const _CharT* __first1, const _CharT* __last1, const _CharT* __first2, const _CharT* __last2) _NOEXCEPT { |
Sebastian Pop | 9070500 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 362 | // Take advantage of knowing source and pattern lengths. |
| 363 | // Stop short when source is smaller than pattern. |
| 364 | const ptrdiff_t __len2 = __last2 - __first2; |
| 365 | if (__len2 == 0) |
| 366 | return __first1; |
| 367 | |
| 368 | ptrdiff_t __len1 = __last1 - __first1; |
| 369 | if (__len1 < __len2) |
| 370 | return __last1; |
| 371 | |
| 372 | // First element of __first2 is loop invariant. |
| 373 | _CharT __f2 = *__first2; |
| 374 | while (true) { |
| 375 | __len1 = __last1 - __first1; |
| 376 | // Check whether __first1 still has at least __len2 bytes. |
| 377 | if (__len1 < __len2) |
| 378 | return __last1; |
| 379 | |
| 380 | // Find __f2 the first byte matching in __first1. |
| 381 | __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2); |
Bruce Mitchener | 527a7fd | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 382 | if (__first1 == nullptr) |
Sebastian Pop | 9070500 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 383 | return __last1; |
| 384 | |
| 385 | // It is faster to compare from the first byte of __first1 even if we |
| 386 | // already know that it matches the first byte of __first2: this is because |
| 387 | // __first2 is most likely aligned, as it is user's "pattern" string, and |
| 388 | // __first1 + 1 is most likely not aligned, as the match is in the middle of |
| 389 | // the string. |
| 390 | if (_Traits::compare(__first1, __first2, __len2) == 0) |
| 391 | return __first1; |
| 392 | |
| 393 | ++__first1; |
| 394 | } |
| 395 | } |
| 396 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 397 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 398 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 399 | __str_find(const _CharT* __p, _SizeT __sz, const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT { |
| 400 | if (__pos > __sz) |
| 401 | return __npos; |
Sebastian Pop | 9070500 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 402 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 403 | if (__n == 0) // There is nothing to search, just return __pos. |
| 404 | return __pos; |
Sebastian Pop | 9070500 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 405 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 406 | const _CharT* __r = std::__search_substring<_CharT, _Traits>(__p + __pos, __p + __sz, __s, __s + __n); |
Sebastian Pop | 9070500 | 2016-12-30 18:01:36 +0000 | [diff] [blame] | 407 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 408 | if (__r == __p + __sz) |
| 409 | return __npos; |
| 410 | return static_cast<_SizeT>(__r - __p); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 413 | // __str_rfind |
| 414 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 415 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 416 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 417 | __str_rfind(const _CharT* __p, _SizeT __sz, _CharT __c, _SizeT __pos) _NOEXCEPT { |
| 418 | if (__sz < 1) |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 419 | return __npos; |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 420 | if (__pos < __sz) |
| 421 | ++__pos; |
| 422 | else |
| 423 | __pos = __sz; |
| 424 | for (const _CharT* __ps = __p + __pos; __ps != __p;) { |
| 425 | if (_Traits::eq(*--__ps, __c)) |
| 426 | return static_cast<_SizeT>(__ps - __p); |
| 427 | } |
| 428 | return __npos; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 431 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 432 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 433 | __str_rfind(const _CharT* __p, _SizeT __sz, const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT { |
| 434 | __pos = std::min(__pos, __sz); |
| 435 | if (__n < __sz - __pos) |
| 436 | __pos += __n; |
| 437 | else |
| 438 | __pos = __sz; |
| 439 | const _CharT* __r = std::__find_end_classic(__p, __p + __pos, __s, __s + __n, _Traits::eq); |
| 440 | if (__n > 0 && __r == __p + __pos) |
| 441 | return __npos; |
| 442 | return static_cast<_SizeT>(__r - __p); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // __str_find_first_of |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 446 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 447 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 448 | __str_find_first_of(const _CharT* __p, _SizeT __sz, const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT { |
| 449 | if (__pos >= __sz || __n == 0) |
| 450 | return __npos; |
| 451 | const _CharT* __r = std::__find_first_of_ce(__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq); |
| 452 | if (__r == __p + __sz) |
| 453 | return __npos; |
| 454 | return static_cast<_SizeT>(__r - __p); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 457 | // __str_find_last_of |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 458 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 459 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 460 | __str_find_last_of(const _CharT* __p, _SizeT __sz, const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT { |
| 461 | if (__n != 0) { |
| 462 | if (__pos < __sz) |
| 463 | ++__pos; |
| 464 | else |
| 465 | __pos = __sz; |
| 466 | for (const _CharT* __ps = __p + __pos; __ps != __p;) { |
| 467 | const _CharT* __r = _Traits::find(__s, __n, *--__ps); |
| 468 | if (__r) |
| 469 | return static_cast<_SizeT>(__ps - __p); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 470 | } |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 471 | } |
| 472 | return __npos; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 475 | // __str_find_first_not_of |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 476 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 477 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 478 | __str_find_first_not_of(const _CharT* __p, _SizeT __sz, const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT { |
| 479 | if (__pos < __sz) { |
| 480 | const _CharT* __pe = __p + __sz; |
| 481 | for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps) |
| 482 | if (_Traits::find(__s, __n, *__ps) == nullptr) |
| 483 | return static_cast<_SizeT>(__ps - __p); |
| 484 | } |
| 485 | return __npos; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 488 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 489 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 490 | __str_find_first_not_of(const _CharT* __p, _SizeT __sz, _CharT __c, _SizeT __pos) _NOEXCEPT { |
| 491 | if (__pos < __sz) { |
| 492 | const _CharT* __pe = __p + __sz; |
| 493 | for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps) |
| 494 | if (!_Traits::eq(*__ps, __c)) |
| 495 | return static_cast<_SizeT>(__ps - __p); |
| 496 | } |
| 497 | return __npos; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 500 | // __str_find_last_not_of |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 501 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 502 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 503 | __str_find_last_not_of(const _CharT* __p, _SizeT __sz, const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT { |
| 504 | if (__pos < __sz) |
| 505 | ++__pos; |
| 506 | else |
| 507 | __pos = __sz; |
| 508 | for (const _CharT* __ps = __p + __pos; __ps != __p;) |
| 509 | if (_Traits::find(__s, __n, *--__ps) == nullptr) |
| 510 | return static_cast<_SizeT>(__ps - __p); |
| 511 | return __npos; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 514 | template <class _CharT, class _SizeT, class _Traits, _SizeT __npos> |
Louis Dionne | 4c19854 | 2023-12-04 10:25:14 -0500 | [diff] [blame] | 515 | inline _SizeT _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 516 | __str_find_last_not_of(const _CharT* __p, _SizeT __sz, _CharT __c, _SizeT __pos) _NOEXCEPT { |
| 517 | if (__pos < __sz) |
| 518 | ++__pos; |
| 519 | else |
| 520 | __pos = __sz; |
| 521 | for (const _CharT* __ps = __p + __pos; __ps != __p;) |
| 522 | if (!_Traits::eq(*--__ps, __c)) |
| 523 | return static_cast<_SizeT>(__ps - __p); |
| 524 | return __npos; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Louis Dionne | 9783f28 | 2023-12-18 14:01:33 -0500 | [diff] [blame] | 527 | template <class _Ptr> |
| 528 | inline _LIBCPP_HIDE_FROM_ABI size_t __do_string_hash(_Ptr __p, _Ptr __e) { |
| 529 | typedef typename iterator_traits<_Ptr>::value_type value_type; |
Nikolas Klauser | 17d0569 | 2025-03-27 13:19:59 +0100 | [diff] [blame] | 530 | return std::__hash_memory(__p, (__e - __p) * sizeof(value_type)); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 533 | _LIBCPP_END_NAMESPACE_STD |
| 534 | |
Eric Fiselier | a016efb | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 535 | _LIBCPP_POP_MACROS |
| 536 | |
Nikolas Klauser | 976f370 | 2022-06-06 23:35:24 +0200 | [diff] [blame] | 537 | #endif // _LIBCPP___STRING_CHAR_TRAITS_H |