2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___STRING
11 #define _LIBCPP___STRING
13 #include <__algorithm/copy.h>
14 #include <__algorithm/copy_backward.h>
15 #include <__algorithm/copy_n.h>
16 #include <__algorithm/fill_n.h>
17 #include <__algorithm/find_end.h>
18 #include <__algorithm/find_first_of.h>
19 #include <__algorithm/min.h>
21 #include <__functional/hash.h> // for __murmur2_or_cityhash
22 #include <__iterator/iterator_traits.h>
23 #include <cstdint> // for uint_least16_t
24 #include <cstdio> // for EOF
25 #include <cstring> // for memcpy
26 #include <iosfwd> // for streampos & friends
27 #include <type_traits> // for __libcpp_is_constant_evaluated
29 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
30 # include <cwchar> // for wmemcpy
35 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
36 #pragma GCC system_header
40 #include <__undef_macros>
43 _LIBCPP_BEGIN_NAMESPACE_STD
45 // The extern template ABI lists are kept outside of <string> to improve the
46 // readability of that header. We maintain 2 ABI lists:
47 // - _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST
48 // - _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST
49 // As the name implies, the ABI lists define the V1 (Stable) and unstable ABI.
51 // For unstable, we may explicitly remove function that are external in V1,
52 // and add (new) external functions to better control inlining and compiler
53 // optimization opportunities.
55 // For stable, the ABI list should rarely change, except for adding new
56 // functions supporting new c++ version / API changes. Typically entries
57 // must never be removed from the stable list.
58 #define _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_Func, _CharType) \
59 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*, size_type)) \
60 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type const*, size_type, size_type) const) \
61 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type, size_type)) \
62 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&)) \
63 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*)) \
64 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, allocator<_CharType> const&)) \
65 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_not_of(value_type const*, size_type, size_type) const) \
66 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::~basic_string()) \
67 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_not_of(value_type const*, size_type, size_type) const) \
68 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, size_type, value_type)) \
69 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(value_type)) \
70 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type)) \
71 _Func(_LIBCPP_FUNC_VIS const _CharType& basic_string<_CharType>::at(size_type) const) \
72 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*, size_type)) \
73 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_of(value_type const*, size_type, size_type) const) \
74 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, size_type, value_type)) \
75 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(value_type const*, size_type)) \
76 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::reserve(size_type)) \
77 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*, size_type)) \
78 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(basic_string const&, size_type, size_type)) \
79 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::copy(value_type*, size_type, size_type) const) \
80 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, size_type, size_type, allocator<_CharType> const&)) \
81 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type, size_type) const) \
82 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(size_type, value_type)) \
83 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*)) \
84 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_of(value_type const*, size_type, size_type) const) \
85 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by(size_type, size_type, size_type, size_type, size_type, size_type)) \
86 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by_and_replace(size_type, size_type, size_type, size_type, size_type, size_type, value_type const*)) \
87 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::push_back(value_type)) \
88 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(size_type, value_type)) \
89 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \
90 _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \
91 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \
92 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::erase(size_type, size_type)) \
93 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \
94 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \
95 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \
96 _Func(_LIBCPP_FUNC_VIS _CharType& basic_string<_CharType>::at(size_type)) \
97 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(value_type const*)) \
98 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type const*, size_type, size_type) const) \
99 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, basic_string const&, size_type, size_type) const) \
100 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*, size_type) const) \
101 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(basic_string const&)) \
102 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*)) \
103 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, basic_string const&, size_type, size_type)) \
104 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::iterator basic_string<_CharType>::insert(basic_string::const_iterator, value_type)) \
105 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::resize(size_type, value_type)) \
106 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, basic_string const&, size_type, size_type))
108 #define _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_Func, _CharType) \
109 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*, size_type)) \
110 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type const*, size_type, size_type) const) \
111 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type, size_type)) \
112 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*)) \
113 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_not_of(value_type const*, size_type, size_type) const) \
114 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::~basic_string()) \
115 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_not_of(value_type const*, size_type, size_type) const) \
116 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, size_type, value_type)) \
117 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(value_type)) \
118 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type)) \
119 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init_copy_ctor_external(value_type const*, size_type)) \
120 _Func(_LIBCPP_FUNC_VIS const _CharType& basic_string<_CharType>::at(size_type) const) \
121 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*, size_type)) \
122 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_of(value_type const*, size_type, size_type) const) \
123 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, size_type, value_type)) \
124 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_external(value_type const*, size_type)) \
125 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_external(value_type const*)) \
126 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::reserve(size_type)) \
127 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*, size_type)) \
128 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(basic_string const&, size_type, size_type)) \
129 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::copy(value_type*, size_type, size_type) const) \
130 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, size_type, size_type, allocator<_CharType> const&)) \
131 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type, size_type) const) \
132 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(size_type, value_type)) \
133 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*)) \
134 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_of(value_type const*, size_type, size_type) const) \
135 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by(size_type, size_type, size_type, size_type, size_type, size_type)) \
136 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by_and_replace(size_type, size_type, size_type, size_type, size_type, size_type, value_type const*)) \
137 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_no_alias<false>(value_type const*, size_type)) \
138 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_no_alias<true>(value_type const*, size_type)) \
139 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::push_back(value_type)) \
140 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(size_type, value_type)) \
141 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \
142 _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \
143 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \
144 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__erase_external_with_move(size_type, size_type)) \
145 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \
146 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \
147 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \
148 _Func(_LIBCPP_FUNC_VIS _CharType& basic_string<_CharType>::at(size_type)) \
149 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type const*, size_type, size_type) const) \
150 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, basic_string const&, size_type, size_type) const) \
151 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*, size_type) const) \
152 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*)) \
153 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, basic_string const&, size_type, size_type)) \
154 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::iterator basic_string<_CharType>::insert(basic_string::const_iterator, value_type)) \
155 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::resize(size_type, value_type)) \
156 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, basic_string const&, size_type, size_type))
161 template <class _CharT>
162 struct _LIBCPP_TEMPLATE_VIS char_traits
164 typedef _CharT char_type;
165 typedef int int_type;
166 typedef streamoff off_type;
167 typedef streampos pos_type;
168 typedef mbstate_t state_type;
170 static inline void _LIBCPP_CONSTEXPR_AFTER_CXX14
171 assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
172 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
173 {return __c1 == __c2;}
174 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
175 {return __c1 < __c2;}
177 static _LIBCPP_CONSTEXPR_AFTER_CXX14
178 int compare(const char_type* __s1, const char_type* __s2, size_t __n);
179 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
180 size_t length(const char_type* __s);
181 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
182 const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
183 static _LIBCPP_CONSTEXPR_AFTER_CXX17
184 char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
185 _LIBCPP_INLINE_VISIBILITY
186 static _LIBCPP_CONSTEXPR_AFTER_CXX17
187 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
188 _LIBCPP_INLINE_VISIBILITY
189 static _LIBCPP_CONSTEXPR_AFTER_CXX17
190 char_type* assign(char_type* __s, size_t __n, char_type __a);
192 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
193 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
194 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
195 {return char_type(__c);}
196 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
197 {return int_type(__c);}
198 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
199 {return __c1 == __c2;}
200 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
201 {return int_type(EOF);}
204 template <class _CharT>
205 _LIBCPP_CONSTEXPR_AFTER_CXX14 int
206 char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
208 for (; __n; --__n, ++__s1, ++__s2)
210 if (lt(*__s1, *__s2))
212 if (lt(*__s2, *__s1))
218 template <class _CharT>
220 _LIBCPP_CONSTEXPR_AFTER_CXX14 size_t
221 char_traits<_CharT>::length(const char_type* __s)
224 for (; !eq(*__s, char_type(0)); ++__s)
229 template <class _CharT>
231 _LIBCPP_CONSTEXPR_AFTER_CXX14 const _CharT*
232 char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
243 template <class _CharT>
244 _LIBCPP_CONSTEXPR_AFTER_CXX17 _CharT*
245 char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
247 if (__n == 0) return __s1;
248 char_type* __r = __s1;
251 for (; __n; --__n, ++__s1, ++__s2)
252 assign(*__s1, *__s2);
254 else if (__s2 < __s1)
259 assign(*--__s1, *--__s2);
264 template <class _CharT>
265 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
267 char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
269 if (!__libcpp_is_constant_evaluated()) {
270 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
272 char_type* __r = __s1;
273 for (; __n; --__n, ++__s1, ++__s2)
274 assign(*__s1, *__s2);
278 template <class _CharT>
279 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
281 char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
283 char_type* __r = __s;
284 for (; __n; --__n, ++__s)
289 // constexpr versions of move/copy/assign.
291 template <class _CharT>
292 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
293 _CharT* __copy_constexpr(_CharT* __dest, const _CharT* __source, size_t __n) _NOEXCEPT
295 _LIBCPP_ASSERT(__libcpp_is_constant_evaluated(), "__copy_constexpr() should always be constant evaluated");
296 _VSTD::copy_n(__source, __n, __dest);
300 template <class _CharT>
301 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
302 _CharT* __move_constexpr(_CharT* __dest, const _CharT* __source, size_t __n) _NOEXCEPT
304 _LIBCPP_ASSERT(__libcpp_is_constant_evaluated(), "__move_constexpr() should always be constant evaluated");
307 _CharT* __allocation = new _CharT[__n];
308 _VSTD::__copy_constexpr(__allocation, __source, __n);
309 _VSTD::__copy_constexpr(__dest, static_cast<const _CharT*>(__allocation), __n);
310 delete[] __allocation;
314 template <class _CharT>
315 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
316 _CharT* __assign_constexpr(_CharT* __s, size_t __n, _CharT __a) _NOEXCEPT
318 _LIBCPP_ASSERT(__libcpp_is_constant_evaluated(), "__assign_constexpr() should always be constant evaluated");
319 _VSTD::fill_n(__s, __n, __a);
326 struct _LIBCPP_TEMPLATE_VIS char_traits<char>
328 typedef char char_type;
329 typedef int int_type;
330 typedef streamoff off_type;
331 typedef streampos pos_type;
332 typedef mbstate_t state_type;
334 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
335 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
336 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
337 {return __c1 == __c2;}
338 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
339 {return (unsigned char)__c1 < (unsigned char)__c2;}
341 static _LIBCPP_CONSTEXPR_AFTER_CXX14
342 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
344 static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14 length(const char_type* __s) _NOEXCEPT {
345 // GCC currently does not support __builtin_strlen during constant evaluation.
346 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70816
347 #ifdef _LIBCPP_COMPILER_GCC
348 if (__libcpp_is_constant_evaluated()) {
350 for (; __s[__i] != char_type('\0'); ++__i)
355 return __builtin_strlen(__s);
358 static _LIBCPP_CONSTEXPR_AFTER_CXX14
359 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
360 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
361 char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
363 return __libcpp_is_constant_evaluated()
364 ? _VSTD::__move_constexpr(__s1, __s2, __n)
365 : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n);
367 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
368 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
370 if (!__libcpp_is_constant_evaluated()) {
371 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
373 return __libcpp_is_constant_evaluated()
374 ? _VSTD::__copy_constexpr(__s1, __s2, __n)
375 : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n);
377 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
378 char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
380 return __libcpp_is_constant_evaluated()
381 ? _VSTD::__assign_constexpr(__s, __n, __a)
382 : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n);
385 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
386 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
387 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
388 {return char_type(__c);}
389 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
390 {return int_type((unsigned char)__c);}
391 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
392 {return __c1 == __c2;}
393 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
394 {return int_type(EOF);}
397 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
399 char_traits<char>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
403 #if __has_feature(cxx_constexpr_string_builtins)
404 return __builtin_memcmp(__s1, __s2, __n);
405 #elif _LIBCPP_STD_VER <= 14
406 return _VSTD::memcmp(__s1, __s2, __n);
408 for (; __n; --__n, ++__s1, ++__s2)
410 if (lt(*__s1, *__s2))
412 if (lt(*__s2, *__s1))
419 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
421 char_traits<char>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
425 #if __has_feature(cxx_constexpr_string_builtins)
426 return __builtin_char_memchr(__s, to_int_type(__a), __n);
427 #elif _LIBCPP_STD_VER <= 14
428 return (const char_type*) _VSTD::memchr(__s, to_int_type(__a), __n);
441 // char_traits<wchar_t>
443 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
445 struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
447 typedef wchar_t char_type;
448 typedef wint_t int_type;
449 typedef streamoff off_type;
450 typedef streampos pos_type;
451 typedef mbstate_t state_type;
453 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
454 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
455 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
456 {return __c1 == __c2;}
457 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
458 {return __c1 < __c2;}
460 static _LIBCPP_CONSTEXPR_AFTER_CXX14
461 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
462 static _LIBCPP_CONSTEXPR_AFTER_CXX14
463 size_t length(const char_type* __s) _NOEXCEPT;
464 static _LIBCPP_CONSTEXPR_AFTER_CXX14
465 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
466 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
467 char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
469 return __libcpp_is_constant_evaluated()
470 ? _VSTD::__move_constexpr(__s1, __s2, __n)
471 : __n == 0 ? __s1 : _VSTD::wmemmove(__s1, __s2, __n);
473 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
474 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
476 if (!__libcpp_is_constant_evaluated()) {
477 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
479 return __libcpp_is_constant_evaluated()
480 ? _VSTD::__copy_constexpr(__s1, __s2, __n)
481 : __n == 0 ? __s1 : _VSTD::wmemcpy(__s1, __s2, __n);
483 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
484 char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
486 return __libcpp_is_constant_evaluated()
487 ? _VSTD::__assign_constexpr(__s, __n, __a)
488 : __n == 0 ? __s : _VSTD::wmemset(__s, __a, __n);
490 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
491 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
492 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
493 {return char_type(__c);}
494 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
495 {return int_type(__c);}
496 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
497 {return __c1 == __c2;}
498 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
499 {return int_type(WEOF);}
502 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
504 char_traits<wchar_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
508 #if __has_feature(cxx_constexpr_string_builtins)
509 return __builtin_wmemcmp(__s1, __s2, __n);
510 #elif _LIBCPP_STD_VER <= 14
511 return _VSTD::wmemcmp(__s1, __s2, __n);
513 for (; __n; --__n, ++__s1, ++__s2)
515 if (lt(*__s1, *__s2))
517 if (lt(*__s2, *__s1))
524 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
526 char_traits<wchar_t>::length(const char_type* __s) _NOEXCEPT
528 #if __has_feature(cxx_constexpr_string_builtins)
529 return __builtin_wcslen(__s);
530 #elif _LIBCPP_STD_VER <= 14
531 return _VSTD::wcslen(__s);
534 for (; !eq(*__s, char_type(0)); ++__s)
540 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
542 char_traits<wchar_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
546 #if __has_feature(cxx_constexpr_string_builtins)
547 return __builtin_wmemchr(__s, __a, __n);
548 #elif _LIBCPP_STD_VER <= 14
549 return _VSTD::wmemchr(__s, __a, __n);
560 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
562 template <class _Traits>
563 _LIBCPP_INLINE_VISIBILITY
565 inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
566 #if _LIBCPP_DEBUG_LEVEL >= 1
567 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);
569 return _Traits::length(__s);
573 #ifndef _LIBCPP_HAS_NO_CHAR8_T
576 struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>
578 typedef char8_t char_type;
579 typedef unsigned int int_type;
580 typedef streamoff off_type;
581 typedef u8streampos pos_type;
582 typedef mbstate_t state_type;
584 static inline constexpr void assign(char_type& __c1, const char_type& __c2) noexcept
586 static inline constexpr bool eq(char_type __c1, char_type __c2) noexcept
587 {return __c1 == __c2;}
588 static inline constexpr bool lt(char_type __c1, char_type __c2) noexcept
589 {return __c1 < __c2;}
592 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
595 size_t length(const char_type* __s) _NOEXCEPT;
597 _LIBCPP_INLINE_VISIBILITY static constexpr
598 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
600 static _LIBCPP_CONSTEXPR_AFTER_CXX17
601 char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
603 return __libcpp_is_constant_evaluated()
604 ? _VSTD::__move_constexpr(__s1, __s2, __n)
605 : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n);
608 static _LIBCPP_CONSTEXPR_AFTER_CXX17
609 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
611 if (!__libcpp_is_constant_evaluated()) {
612 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
614 return __libcpp_is_constant_evaluated()
615 ? _VSTD::__copy_constexpr(__s1, __s2, __n)
616 : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n);
619 static _LIBCPP_CONSTEXPR_AFTER_CXX17
620 char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
622 return __libcpp_is_constant_evaluated()
623 ? _VSTD::__assign_constexpr(__s, __n, __a)
624 : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n);
627 static inline constexpr int_type not_eof(int_type __c) noexcept
628 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
629 static inline constexpr char_type to_char_type(int_type __c) noexcept
630 {return char_type(__c);}
631 static inline constexpr int_type to_int_type(char_type __c) noexcept
632 {return int_type(__c);}
633 static inline constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept
634 {return __c1 == __c2;}
635 static inline constexpr int_type eof() noexcept
636 {return int_type(EOF);}
639 // TODO use '__builtin_strlen' if it ever supports char8_t ??
642 char_traits<char8_t>::length(const char_type* __s) _NOEXCEPT
645 for (; !eq(*__s, char_type(0)); ++__s)
652 char_traits<char8_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
654 #if __has_feature(cxx_constexpr_string_builtins)
655 return __builtin_memcmp(__s1, __s2, __n);
657 for (; __n; --__n, ++__s1, ++__s2)
659 if (lt(*__s1, *__s2))
661 if (lt(*__s2, *__s1))
668 // TODO use '__builtin_char_memchr' if it ever supports char8_t ??
671 char_traits<char8_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
682 #endif // #_LIBCPP_HAS_NO_CHAR8_T
684 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
687 struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
689 typedef char16_t char_type;
690 typedef uint_least16_t int_type;
691 typedef streamoff off_type;
692 typedef u16streampos pos_type;
693 typedef mbstate_t state_type;
695 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
696 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
697 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
698 {return __c1 == __c2;}
699 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
700 {return __c1 < __c2;}
702 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
703 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
704 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
705 size_t length(const char_type* __s) _NOEXCEPT;
706 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
707 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
709 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
710 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
711 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
712 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
713 static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
715 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
716 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
717 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
718 {return char_type(__c);}
719 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
720 {return int_type(__c);}
721 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
722 {return __c1 == __c2;}
723 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
724 {return int_type(0xFFFF);}
727 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
729 char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
731 for (; __n; --__n, ++__s1, ++__s2)
733 if (lt(*__s1, *__s2))
735 if (lt(*__s2, *__s1))
741 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
743 char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
746 for (; !eq(*__s, char_type(0)); ++__s)
751 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
753 char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
764 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
766 char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
768 if (__n == 0) return __s1;
769 char_type* __r = __s1;
772 for (; __n; --__n, ++__s1, ++__s2)
773 assign(*__s1, *__s2);
775 else if (__s2 < __s1)
780 assign(*--__s1, *--__s2);
785 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
787 char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
789 if (!__libcpp_is_constant_evaluated()) {
790 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
792 char_type* __r = __s1;
793 for (; __n; --__n, ++__s1, ++__s2)
794 assign(*__s1, *__s2);
798 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
800 char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
802 char_type* __r = __s;
803 for (; __n; --__n, ++__s)
809 struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
811 typedef char32_t char_type;
812 typedef uint_least32_t int_type;
813 typedef streamoff off_type;
814 typedef u32streampos pos_type;
815 typedef mbstate_t state_type;
817 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
818 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
819 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
820 {return __c1 == __c2;}
821 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
822 {return __c1 < __c2;}
824 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
825 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
826 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
827 size_t length(const char_type* __s) _NOEXCEPT;
828 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
829 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
830 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
831 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
832 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
833 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
834 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
835 static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
837 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
838 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
839 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
840 {return char_type(__c);}
841 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
842 {return int_type(__c);}
843 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
844 {return __c1 == __c2;}
845 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
846 {return int_type(0xFFFFFFFF);}
849 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
851 char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
853 for (; __n; --__n, ++__s1, ++__s2)
855 if (lt(*__s1, *__s2))
857 if (lt(*__s2, *__s1))
863 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
865 char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
868 for (; !eq(*__s, char_type(0)); ++__s)
873 inline _LIBCPP_CONSTEXPR_AFTER_CXX14
875 char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
886 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
888 char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
890 if (__n == 0) return __s1;
891 char_type* __r = __s1;
894 for (; __n; --__n, ++__s1, ++__s2)
895 assign(*__s1, *__s2);
897 else if (__s2 < __s1)
902 assign(*--__s1, *--__s2);
907 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
909 char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
911 if (!__libcpp_is_constant_evaluated()) {
912 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
914 char_type* __r = __s1;
915 for (; __n; --__n, ++__s1, ++__s2)
916 assign(*__s1, *__s2);
920 inline _LIBCPP_CONSTEXPR_AFTER_CXX17
922 char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
924 char_type* __r = __s;
925 for (; __n; --__n, ++__s)
930 #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
932 // helper fns for basic_string and string_view
935 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
936 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
937 __str_find(const _CharT *__p, _SizeT __sz,
938 _CharT __c, _SizeT __pos) _NOEXCEPT
942 const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
945 return static_cast<_SizeT>(__r - __p);
948 template <class _CharT, class _Traits>
949 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT *
950 __search_substring(const _CharT *__first1, const _CharT *__last1,
951 const _CharT *__first2, const _CharT *__last2) _NOEXCEPT {
952 // Take advantage of knowing source and pattern lengths.
953 // Stop short when source is smaller than pattern.
954 const ptrdiff_t __len2 = __last2 - __first2;
958 ptrdiff_t __len1 = __last1 - __first1;
962 // First element of __first2 is loop invariant.
963 _CharT __f2 = *__first2;
965 __len1 = __last1 - __first1;
966 // Check whether __first1 still has at least __len2 bytes.
970 // Find __f2 the first byte matching in __first1.
971 __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2);
972 if (__first1 == nullptr)
975 // It is faster to compare from the first byte of __first1 even if we
976 // already know that it matches the first byte of __first2: this is because
977 // __first2 is most likely aligned, as it is user's "pattern" string, and
978 // __first1 + 1 is most likely not aligned, as the match is in the middle of
980 if (_Traits::compare(__first1, __first2, __len2) == 0)
987 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
988 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
989 __str_find(const _CharT *__p, _SizeT __sz,
990 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
995 if (__n == 0) // There is nothing to search, just return __pos.
998 const _CharT *__r = __search_substring<_CharT, _Traits>(
999 __p + __pos, __p + __sz, __s, __s + __n);
1001 if (__r == __p + __sz)
1003 return static_cast<_SizeT>(__r - __p);
1009 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1010 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1011 __str_rfind(const _CharT *__p, _SizeT __sz,
1012 _CharT __c, _SizeT __pos) _NOEXCEPT
1020 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1022 if (_Traits::eq(*--__ps, __c))
1023 return static_cast<_SizeT>(__ps - __p);
1028 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1029 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1030 __str_rfind(const _CharT *__p, _SizeT __sz,
1031 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1033 __pos = _VSTD::min(__pos, __sz);
1034 if (__n < __sz - __pos)
1038 const _CharT* __r = _VSTD::__find_end(
1039 __p, __p + __pos, __s, __s + __n, _Traits::eq,
1040 random_access_iterator_tag(), random_access_iterator_tag());
1041 if (__n > 0 && __r == __p + __pos)
1043 return static_cast<_SizeT>(__r - __p);
1046 // __str_find_first_of
1047 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1048 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1049 __str_find_first_of(const _CharT *__p, _SizeT __sz,
1050 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1052 if (__pos >= __sz || __n == 0)
1054 const _CharT* __r = _VSTD::__find_first_of_ce
1055 (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
1056 if (__r == __p + __sz)
1058 return static_cast<_SizeT>(__r - __p);
1062 // __str_find_last_of
1063 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1064 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1065 __str_find_last_of(const _CharT *__p, _SizeT __sz,
1066 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1074 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1076 const _CharT* __r = _Traits::find(__s, __n, *--__ps);
1078 return static_cast<_SizeT>(__ps - __p);
1085 // __str_find_first_not_of
1086 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1087 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1088 __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
1089 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1093 const _CharT* __pe = __p + __sz;
1094 for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
1095 if (_Traits::find(__s, __n, *__ps) == nullptr)
1096 return static_cast<_SizeT>(__ps - __p);
1102 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1103 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1104 __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
1105 _CharT __c, _SizeT __pos) _NOEXCEPT
1109 const _CharT* __pe = __p + __sz;
1110 for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
1111 if (!_Traits::eq(*__ps, __c))
1112 return static_cast<_SizeT>(__ps - __p);
1118 // __str_find_last_not_of
1119 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1120 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1121 __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
1122 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1128 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1129 if (_Traits::find(__s, __n, *--__ps) == nullptr)
1130 return static_cast<_SizeT>(__ps - __p);
1135 template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1136 inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1137 __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
1138 _CharT __c, _SizeT __pos) _NOEXCEPT
1144 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1145 if (!_Traits::eq(*--__ps, __c))
1146 return static_cast<_SizeT>(__ps - __p);
1150 template<class _Ptr>
1151 inline _LIBCPP_INLINE_VISIBILITY
1152 size_t __do_string_hash(_Ptr __p, _Ptr __e)
1154 typedef typename iterator_traits<_Ptr>::value_type value_type;
1155 return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
1158 template <class _CharT, class _Iter, class _Traits=char_traits<_CharT> >
1159 struct __quoted_output_proxy
1166 __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
1167 : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
1168 // This would be a nice place for a string_ref
1171 _LIBCPP_END_NAMESPACE_STD
1175 #endif // _LIBCPP___STRING