blob: e3fd0c270b2bcd1971af12bc0c3159753faae2f2 [file] [log] [blame]
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// 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 Clowdf63a6d2016-07-21 05:31:24 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___STRING
11#define _LIBCPP___STRING
12
Marshall Clowdf63a6d2016-07-21 05:31:24 +000013#include <__config>
Christopher Di Bella55d7a822021-07-01 09:25:35 -040014#include <__algorithm/copy.h>
15#include <__algorithm/copy_backward.h>
16#include <__algorithm/copy_n.h>
17#include <__algorithm/fill_n.h>
18#include <__algorithm/find_first_of.h>
19#include <__algorithm/find_end.h>
20#include <__algorithm/min.h>
21#include <__functional/hash.h> // for __murmur2_or_cityhash
22#include <__iterator/iterator_traits.h>
23#include <cstdio> // for EOF
24#include <cstdint> // for uint_least16_t
25#include <cstring> // for memcpy
Christopher Di Bella55d7a822021-07-01 09:25:35 -040026#include <type_traits> // for __libcpp_is_constant_evaluated
Louis Dionne89258142021-08-23 15:32:36 -040027#include <iosfwd> // for streampos & friends
28
29#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
30# include <cwchar> // for wmemcpy
31#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +000032
Marshall Clowdf63a6d2016-07-21 05:31:24 +000033#include <__debug>
34
35#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
36#pragma GCC system_header
37#endif
38
Eric Fiselierf4433a32017-05-31 22:07:49 +000039_LIBCPP_PUSH_MACROS
40#include <__undef_macros>
41
42
Marshall Clowdf63a6d2016-07-21 05:31:24 +000043_LIBCPP_BEGIN_NAMESPACE_STD
44
Martijn Vels1e63ba02020-02-19 16:27:50 -050045// The extern template ABI lists are kept outside of <string> to improve the
46// readability of that header. We maintain 2 ABI lists:
47// - _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST
48// - _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST
49// As the name implies, the ABI lists define the V1 (Stable) and unstable ABI.
50//
51// For unstable, we may explicitly remove function that are external in V1,
52// and add (new) external functions to better control inlining and compiler
53// optimization opportunities.
54//
55// For stable, the ABI list should rarely change, except for adding new
56// functions supporting new c++ version / API changes. Typically entries
57// must never be removed from the stable list.
58#define _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_Func, _CharType) \
59 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*, size_type)) \
60 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type const*, size_type, size_type) const) \
61 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type, size_type)) \
62 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&)) \
63 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*)) \
Arthur O'Dwyer07b22492020-11-27 11:02:06 -050064 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, allocator<_CharType> const&)) \
Martijn Vels1e63ba02020-02-19 16:27:50 -050065 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_not_of(value_type const*, size_type, size_type) const) \
66 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::~basic_string()) \
67 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_not_of(value_type const*, size_type, size_type) const) \
68 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, size_type, value_type)) \
69 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(value_type)) \
70 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type)) \
71 _Func(_LIBCPP_FUNC_VIS const _CharType& basic_string<_CharType>::at(size_type) const) \
72 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*, size_type)) \
73 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_of(value_type const*, size_type, size_type) const) \
74 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, size_type, value_type)) \
75 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(value_type const*, size_type)) \
76 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::reserve(size_type)) \
77 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*, size_type)) \
78 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(basic_string const&, size_type, size_type)) \
79 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::copy(value_type*, size_type, size_type) const) \
Arthur O'Dwyer07b22492020-11-27 11:02:06 -050080 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, size_type, size_type, allocator<_CharType> const&)) \
Martijn Vels1e63ba02020-02-19 16:27:50 -050081 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type, size_type) const) \
82 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(size_type, value_type)) \
83 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*)) \
84 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_of(value_type const*, size_type, size_type) const) \
85 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by(size_type, size_type, size_type, size_type, size_type, size_type)) \
86 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by_and_replace(size_type, size_type, size_type, size_type, size_type, size_type, value_type const*)) \
87 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::push_back(value_type)) \
88 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(size_type, value_type)) \
89 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \
90 _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \
91 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \
92 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::erase(size_type, size_type)) \
93 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \
94 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \
95 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \
96 _Func(_LIBCPP_FUNC_VIS _CharType& basic_string<_CharType>::at(size_type)) \
97 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(value_type const*)) \
98 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type const*, size_type, size_type) const) \
99 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, basic_string const&, size_type, size_type) const) \
100 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*, size_type) const) \
101 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(basic_string const&)) \
102 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*)) \
103 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, basic_string const&, size_type, size_type)) \
104 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::iterator basic_string<_CharType>::insert(basic_string::const_iterator, value_type)) \
105 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::resize(size_type, value_type)) \
106 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, basic_string const&, size_type, size_type))
107
108#define _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_Func, _CharType) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500109 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*, size_type)) \
110 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type const*, size_type, size_type) const) \
111 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type, size_type)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500112 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, value_type const*)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500113 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_not_of(value_type const*, size_type, size_type) const) \
114 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::~basic_string()) \
115 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_not_of(value_type const*, size_type, size_type) const) \
116 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, size_type, value_type)) \
117 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::operator=(value_type)) \
118 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(value_type const*, size_type)) \
Martijn Vels5e7c9752020-03-04 17:52:46 -0500119 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init_copy_ctor_external(value_type const*, size_type)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500120 _Func(_LIBCPP_FUNC_VIS const _CharType& basic_string<_CharType>::at(size_type) const) \
121 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*, size_type)) \
122 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_first_of(value_type const*, size_type, size_type) const) \
123 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, size_type, value_type)) \
Martijn Velsda7d94f2020-06-19 14:24:03 -0400124 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_external(value_type const*, size_type)) \
125 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_external(value_type const*)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500126 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::reserve(size_type)) \
127 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*, size_type)) \
128 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(basic_string const&, size_type, size_type)) \
129 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::copy(value_type*, size_type, size_type) const) \
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500130 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::basic_string(basic_string const&, size_type, size_type, allocator<_CharType> const&)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500131 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type, size_type) const) \
132 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__init(size_type, value_type)) \
133 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, value_type const*)) \
134 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find_last_of(value_type const*, size_type, size_type) const) \
135 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by(size_type, size_type, size_type, size_type, size_type, size_type)) \
136 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__grow_by_and_replace(size_type, size_type, size_type, size_type, size_type, size_type, value_type const*)) \
Martijn Velsb6a08b62020-04-10 18:36:31 -0400137 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_no_alias<false>(value_type const*, size_type)) \
138 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::__assign_no_alias<true>(value_type const*, size_type)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500139 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::push_back(value_type)) \
140 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(size_type, value_type)) \
141 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \
142 _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \
143 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \
Martijn Velsa81fc792020-02-26 13:25:43 -0500144 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__erase_external_with_move(size_type, size_type)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500145 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \
146 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \
147 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \
148 _Func(_LIBCPP_FUNC_VIS _CharType& basic_string<_CharType>::at(size_type)) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500149 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::find(value_type const*, size_type, size_type) const) \
150 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, basic_string const&, size_type, size_type) const) \
151 _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*, size_type) const) \
Eric Fiselier5dcce5c2020-01-15 17:12:09 -0500152 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(value_type const*)) \
153 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::replace(size_type, size_type, basic_string const&, size_type, size_type)) \
154 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::iterator basic_string<_CharType>::insert(basic_string::const_iterator, value_type)) \
155 _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::resize(size_type, value_type)) \
156 _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::insert(size_type, basic_string const&, size_type, size_type))
157
158
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000159// char_traits
160
161template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000162struct _LIBCPP_TEMPLATE_VIS char_traits
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000163{
164 typedef _CharT char_type;
165 typedef int int_type;
166 typedef streamoff off_type;
167 typedef streampos pos_type;
168 typedef mbstate_t state_type;
169
Marshall Clow951035b2017-01-12 04:37:14 +0000170 static inline void _LIBCPP_CONSTEXPR_AFTER_CXX14
171 assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000172 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
173 {return __c1 == __c2;}
174 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
175 {return __c1 < __c2;}
176
Marshall Clow951035b2017-01-12 04:37:14 +0000177 static _LIBCPP_CONSTEXPR_AFTER_CXX14
178 int compare(const char_type* __s1, const char_type* __s2, size_t __n);
179 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
180 size_t length(const char_type* __s);
181 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
182 const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
Michael Parkb5f44472019-11-08 13:42:26 +0000183 static _LIBCPP_CONSTEXPR_AFTER_CXX17
184 char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000185 _LIBCPP_INLINE_VISIBILITY
Michael Parkb5f44472019-11-08 13:42:26 +0000186 static _LIBCPP_CONSTEXPR_AFTER_CXX17
187 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000188 _LIBCPP_INLINE_VISIBILITY
Michael Parkb5f44472019-11-08 13:42:26 +0000189 static _LIBCPP_CONSTEXPR_AFTER_CXX17
190 char_type* assign(char_type* __s, size_t __n, char_type __a);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000191
192 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
193 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
194 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
195 {return char_type(__c);}
196 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
197 {return int_type(__c);}
198 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
199 {return __c1 == __c2;}
200 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
201 {return int_type(EOF);}
202};
203
204template <class _CharT>
Marshall Clow951035b2017-01-12 04:37:14 +0000205_LIBCPP_CONSTEXPR_AFTER_CXX14 int
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000206char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
207{
208 for (; __n; --__n, ++__s1, ++__s2)
209 {
210 if (lt(*__s1, *__s2))
211 return -1;
212 if (lt(*__s2, *__s1))
213 return 1;
214 }
215 return 0;
216}
217
218template <class _CharT>
219inline
Marshall Clow951035b2017-01-12 04:37:14 +0000220_LIBCPP_CONSTEXPR_AFTER_CXX14 size_t
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000221char_traits<_CharT>::length(const char_type* __s)
222{
223 size_t __len = 0;
224 for (; !eq(*__s, char_type(0)); ++__s)
225 ++__len;
226 return __len;
227}
228
229template <class _CharT>
230inline
Marshall Clow951035b2017-01-12 04:37:14 +0000231_LIBCPP_CONSTEXPR_AFTER_CXX14 const _CharT*
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000232char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
233{
234 for (; __n; --__n)
235 {
236 if (eq(*__s, __a))
237 return __s;
238 ++__s;
239 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500240 return nullptr;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000241}
242
243template <class _CharT>
Michael Parkb5f44472019-11-08 13:42:26 +0000244_LIBCPP_CONSTEXPR_AFTER_CXX17 _CharT*
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000245char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
246{
Michael Parkb5f44472019-11-08 13:42:26 +0000247 if (__n == 0) return __s1;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000248 char_type* __r = __s1;
249 if (__s1 < __s2)
250 {
251 for (; __n; --__n, ++__s1, ++__s2)
252 assign(*__s1, *__s2);
253 }
254 else if (__s2 < __s1)
255 {
256 __s1 += __n;
257 __s2 += __n;
258 for (; __n; --__n)
259 assign(*--__s1, *--__s2);
260 }
261 return __r;
262}
263
264template <class _CharT>
Michael Parkb5f44472019-11-08 13:42:26 +0000265inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000266_CharT*
267char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
268{
Arthur O'Dwyere9a55122021-04-20 22:24:24 -0400269 if (!__libcpp_is_constant_evaluated()) {
270 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
271 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000272 char_type* __r = __s1;
273 for (; __n; --__n, ++__s1, ++__s2)
274 assign(*__s1, *__s2);
275 return __r;
276}
277
278template <class _CharT>
Michael Parkb5f44472019-11-08 13:42:26 +0000279inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000280_CharT*
281char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
282{
283 char_type* __r = __s;
284 for (; __n; --__n, ++__s)
285 assign(*__s, __a);
286 return __r;
287}
288
Michael Parkb5f44472019-11-08 13:42:26 +0000289// constexpr versions of move/copy/assign.
290
291template <class _CharT>
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500292static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000293_CharT* __move_constexpr(_CharT* __s1, const _CharT* __s2, size_t __n) _NOEXCEPT
294{
295 if (__n == 0) return __s1;
296 if (__s1 < __s2) {
297 _VSTD::copy(__s2, __s2 + __n, __s1);
298 } else if (__s2 < __s1) {
299 _VSTD::copy_backward(__s2, __s2 + __n, __s1 + __n);
300 }
301 return __s1;
302}
303
304template <class _CharT>
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500305static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000306_CharT* __copy_constexpr(_CharT* __s1, const _CharT* __s2, size_t __n) _NOEXCEPT
307{
308 _VSTD::copy_n(__s2, __n, __s1);
309 return __s1;
310}
311
312template <class _CharT>
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500313static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000314_CharT* __assign_constexpr(_CharT* __s, size_t __n, _CharT __a) _NOEXCEPT
315{
316 _VSTD::fill_n(__s, __n, __a);
317 return __s;
318}
319
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000320// char_traits<char>
321
322template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000323struct _LIBCPP_TEMPLATE_VIS char_traits<char>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000324{
325 typedef char char_type;
326 typedef int int_type;
327 typedef streamoff off_type;
328 typedef streampos pos_type;
329 typedef mbstate_t state_type;
330
Marshall Clow951035b2017-01-12 04:37:14 +0000331 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
332 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000333 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
334 {return __c1 == __c2;}
335 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
336 {return (unsigned char)__c1 < (unsigned char)__c2;}
337
Marshall Clowaa32f162017-01-12 05:40:58 +0000338 static _LIBCPP_CONSTEXPR_AFTER_CXX14
339 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
Marshall Clow951035b2017-01-12 04:37:14 +0000340 static inline size_t _LIBCPP_CONSTEXPR_AFTER_CXX14
341 length(const char_type* __s) _NOEXCEPT {return __builtin_strlen(__s);}
342 static _LIBCPP_CONSTEXPR_AFTER_CXX14
343 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500344 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000345 char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
346 {
347 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500348 ? _VSTD::__move_constexpr(__s1, __s2, __n)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500349 : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000350 }
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500351 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000352 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000353 {
Arthur O'Dwyere9a55122021-04-20 22:24:24 -0400354 if (!__libcpp_is_constant_evaluated()) {
355 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
356 }
Michael Parkb5f44472019-11-08 13:42:26 +0000357 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500358 ? _VSTD::__copy_constexpr(__s1, __s2, __n)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500359 : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000360 }
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500361 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000362 char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
363 {
364 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500365 ? _VSTD::__assign_constexpr(__s, __n, __a)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500366 : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000367 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000368
369 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
370 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
371 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
372 {return char_type(__c);}
373 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
374 {return int_type((unsigned char)__c);}
375 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
376 {return __c1 == __c2;}
377 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
378 {return int_type(EOF);}
379};
380
Marshall Clow951035b2017-01-12 04:37:14 +0000381inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowaa32f162017-01-12 05:40:58 +0000382int
383char_traits<char>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
384{
385 if (__n == 0)
386 return 0;
Marshall Clowb344c362017-01-26 06:58:29 +0000387#if __has_feature(cxx_constexpr_string_builtins)
Marshall Clowaa32f162017-01-12 05:40:58 +0000388 return __builtin_memcmp(__s1, __s2, __n);
389#elif _LIBCPP_STD_VER <= 14
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500390 return _VSTD::memcmp(__s1, __s2, __n);
Marshall Clowaa32f162017-01-12 05:40:58 +0000391#else
392 for (; __n; --__n, ++__s1, ++__s2)
393 {
394 if (lt(*__s1, *__s2))
395 return -1;
396 if (lt(*__s2, *__s1))
397 return 1;
398 }
399 return 0;
400#endif
401}
402
403inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow951035b2017-01-12 04:37:14 +0000404const char*
405char_traits<char>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
406{
407 if (__n == 0)
Marshall Clow32043ac2018-02-06 18:58:05 +0000408 return nullptr;
Marshall Clowb344c362017-01-26 06:58:29 +0000409#if __has_feature(cxx_constexpr_string_builtins)
410 return __builtin_char_memchr(__s, to_int_type(__a), __n);
411#elif _LIBCPP_STD_VER <= 14
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500412 return (const char_type*) _VSTD::memchr(__s, to_int_type(__a), __n);
Marshall Clow951035b2017-01-12 04:37:14 +0000413#else
414 for (; __n; --__n)
415 {
416 if (eq(*__s, __a))
417 return __s;
418 ++__s;
419 }
Marshall Clow32043ac2018-02-06 18:58:05 +0000420 return nullptr;
Marshall Clow951035b2017-01-12 04:37:14 +0000421#endif
422}
423
424
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000425// char_traits<wchar_t>
426
Louis Dionne89258142021-08-23 15:32:36 -0400427#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000428template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000429struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000430{
431 typedef wchar_t char_type;
432 typedef wint_t int_type;
433 typedef streamoff off_type;
434 typedef streampos pos_type;
435 typedef mbstate_t state_type;
436
Marshall Clow951035b2017-01-12 04:37:14 +0000437 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
438 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000439 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
440 {return __c1 == __c2;}
441 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
442 {return __c1 < __c2;}
443
Marshall Clow951035b2017-01-12 04:37:14 +0000444 static _LIBCPP_CONSTEXPR_AFTER_CXX14
445 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
446 static _LIBCPP_CONSTEXPR_AFTER_CXX14
447 size_t length(const char_type* __s) _NOEXCEPT;
448 static _LIBCPP_CONSTEXPR_AFTER_CXX14
449 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500450 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000451 char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
452 {
453 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500454 ? _VSTD::__move_constexpr(__s1, __s2, __n)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500455 : __n == 0 ? __s1 : _VSTD::wmemmove(__s1, __s2, __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000456 }
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500457 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000458 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000459 {
Arthur O'Dwyere9a55122021-04-20 22:24:24 -0400460 if (!__libcpp_is_constant_evaluated()) {
461 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
462 }
Michael Parkb5f44472019-11-08 13:42:26 +0000463 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500464 ? _VSTD::__copy_constexpr(__s1, __s2, __n)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500465 : __n == 0 ? __s1 : _VSTD::wmemcpy(__s1, __s2, __n);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000466 }
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500467 static inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000468 char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
469 {
470 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500471 ? _VSTD::__assign_constexpr(__s, __n, __a)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500472 : __n == 0 ? __s : _VSTD::wmemset(__s, __a, __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000473 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000474 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
475 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
476 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
477 {return char_type(__c);}
478 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
479 {return int_type(__c);}
480 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
481 {return __c1 == __c2;}
482 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
483 {return int_type(WEOF);}
484};
485
Marshall Clow951035b2017-01-12 04:37:14 +0000486inline _LIBCPP_CONSTEXPR_AFTER_CXX14
487int
488char_traits<wchar_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
489{
490 if (__n == 0)
491 return 0;
Marshall Clowb344c362017-01-26 06:58:29 +0000492#if __has_feature(cxx_constexpr_string_builtins)
Marshall Clow951035b2017-01-12 04:37:14 +0000493 return __builtin_wmemcmp(__s1, __s2, __n);
494#elif _LIBCPP_STD_VER <= 14
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500495 return _VSTD::wmemcmp(__s1, __s2, __n);
Marshall Clow951035b2017-01-12 04:37:14 +0000496#else
497 for (; __n; --__n, ++__s1, ++__s2)
498 {
499 if (lt(*__s1, *__s2))
500 return -1;
501 if (lt(*__s2, *__s1))
502 return 1;
503 }
504 return 0;
505#endif
506}
507
508inline _LIBCPP_CONSTEXPR_AFTER_CXX14
509size_t
510char_traits<wchar_t>::length(const char_type* __s) _NOEXCEPT
511{
Marshall Clowb344c362017-01-26 06:58:29 +0000512#if __has_feature(cxx_constexpr_string_builtins)
Marshall Clow951035b2017-01-12 04:37:14 +0000513 return __builtin_wcslen(__s);
514#elif _LIBCPP_STD_VER <= 14
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500515 return _VSTD::wcslen(__s);
Marshall Clow951035b2017-01-12 04:37:14 +0000516#else
517 size_t __len = 0;
518 for (; !eq(*__s, char_type(0)); ++__s)
519 ++__len;
520 return __len;
521#endif
522}
523
524inline _LIBCPP_CONSTEXPR_AFTER_CXX14
525const wchar_t*
526char_traits<wchar_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
527{
528 if (__n == 0)
Marshall Clow32043ac2018-02-06 18:58:05 +0000529 return nullptr;
Marshall Clowb344c362017-01-26 06:58:29 +0000530#if __has_feature(cxx_constexpr_string_builtins)
Marshall Clow32043ac2018-02-06 18:58:05 +0000531 return __builtin_wmemchr(__s, __a, __n);
Marshall Clow951035b2017-01-12 04:37:14 +0000532#elif _LIBCPP_STD_VER <= 14
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500533 return _VSTD::wmemchr(__s, __a, __n);
Marshall Clow951035b2017-01-12 04:37:14 +0000534#else
535 for (; __n; --__n)
536 {
537 if (eq(*__s, __a))
538 return __s;
539 ++__s;
540 }
Marshall Clow32043ac2018-02-06 18:58:05 +0000541 return nullptr;
Marshall Clow951035b2017-01-12 04:37:14 +0000542#endif
543}
Louis Dionne89258142021-08-23 15:32:36 -0400544#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow951035b2017-01-12 04:37:14 +0000545
Louis Dionne76f4e0b2021-09-09 13:23:26 -0400546template <class _Traits>
547_LIBCPP_INLINE_VISIBILITY
548_LIBCPP_CONSTEXPR
549inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
550#if _LIBCPP_DEBUG_LEVEL >= 1
551 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);
552#else
553 return _Traits::length(__s);
554#endif
555}
Marshall Clow951035b2017-01-12 04:37:14 +0000556
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400557#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +0000558
559template <>
560struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>
561{
562 typedef char8_t char_type;
563 typedef unsigned int int_type;
564 typedef streamoff off_type;
565 typedef u8streampos pos_type;
566 typedef mbstate_t state_type;
567
568 static inline constexpr void assign(char_type& __c1, const char_type& __c2) noexcept
569 {__c1 = __c2;}
570 static inline constexpr bool eq(char_type __c1, char_type __c2) noexcept
571 {return __c1 == __c2;}
572 static inline constexpr bool lt(char_type __c1, char_type __c2) noexcept
573 {return __c1 < __c2;}
574
575 static constexpr
576 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
577
578 static constexpr
579 size_t length(const char_type* __s) _NOEXCEPT;
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700580
Marshall Clow8732fed2018-12-11 04:35:44 +0000581 _LIBCPP_INLINE_VISIBILITY static constexpr
582 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700583
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500584 static _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000585 char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
586 {
587 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500588 ? _VSTD::__move_constexpr(__s1, __s2, __n)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500589 : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000590 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700591
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500592 static _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000593 char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Arthur O'Dwyere9a55122021-04-20 22:24:24 -0400594 {
595 if (!__libcpp_is_constant_evaluated()) {
596 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
597 }
Michael Parkb5f44472019-11-08 13:42:26 +0000598 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500599 ? _VSTD::__copy_constexpr(__s1, __s2, __n)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500600 : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000601 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700602
Arthur O'Dwyer14606d12020-11-23 12:44:41 -0500603 static _LIBCPP_CONSTEXPR_AFTER_CXX17
Michael Parkb5f44472019-11-08 13:42:26 +0000604 char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
605 {
606 return __libcpp_is_constant_evaluated()
Arthur O'Dwyere75dcfa2020-11-22 13:21:11 -0500607 ? _VSTD::__assign_constexpr(__s, __n, __a)
Arthur O'Dwyer22236632020-12-07 21:50:15 -0500608 : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n);
Michael Parkb5f44472019-11-08 13:42:26 +0000609 }
Marshall Clow8732fed2018-12-11 04:35:44 +0000610
611 static inline constexpr int_type not_eof(int_type __c) noexcept
612 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
613 static inline constexpr char_type to_char_type(int_type __c) noexcept
614 {return char_type(__c);}
615 static inline constexpr int_type to_int_type(char_type __c) noexcept
616 {return int_type(__c);}
617 static inline constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept
618 {return __c1 == __c2;}
619 static inline constexpr int_type eof() noexcept
620 {return int_type(EOF);}
621};
622
623// TODO use '__builtin_strlen' if it ever supports char8_t ??
624inline constexpr
625size_t
626char_traits<char8_t>::length(const char_type* __s) _NOEXCEPT
627{
628 size_t __len = 0;
629 for (; !eq(*__s, char_type(0)); ++__s)
630 ++__len;
631 return __len;
632}
633
634inline constexpr
635int
636char_traits<char8_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
637{
638#if __has_feature(cxx_constexpr_string_builtins)
639 return __builtin_memcmp(__s1, __s2, __n);
640#else
641 for (; __n; --__n, ++__s1, ++__s2)
642 {
643 if (lt(*__s1, *__s2))
644 return -1;
645 if (lt(*__s2, *__s1))
646 return 1;
647 }
648 return 0;
649#endif
650}
651
652// TODO use '__builtin_char_memchr' if it ever supports char8_t ??
653inline constexpr
654const char8_t*
655char_traits<char8_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
656{
657 for (; __n; --__n)
658 {
659 if (eq(*__s, __a))
660 return __s;
661 ++__s;
662 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500663 return nullptr;
Marshall Clow8732fed2018-12-11 04:35:44 +0000664}
665
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400666#endif // #_LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +0000667
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000668#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
669
670template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000671struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000672{
673 typedef char16_t char_type;
674 typedef uint_least16_t int_type;
675 typedef streamoff off_type;
676 typedef u16streampos pos_type;
677 typedef mbstate_t state_type;
678
Marshall Clow951035b2017-01-12 04:37:14 +0000679 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
680 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000681 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
682 {return __c1 == __c2;}
683 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
684 {return __c1 < __c2;}
685
Marshall Clow951035b2017-01-12 04:37:14 +0000686 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
687 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
688 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
689 size_t length(const char_type* __s) _NOEXCEPT;
690 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
691 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
Michael Parkb5f44472019-11-08 13:42:26 +0000692 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow36097db2016-07-28 04:52:02 +0000693 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
Michael Parkb5f44472019-11-08 13:42:26 +0000694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow36097db2016-07-28 04:52:02 +0000695 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
Michael Parkb5f44472019-11-08 13:42:26 +0000696 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow36097db2016-07-28 04:52:02 +0000697 static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000698
699 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
700 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
701 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
702 {return char_type(__c);}
703 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
704 {return int_type(__c);}
705 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
706 {return __c1 == __c2;}
707 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
708 {return int_type(0xFFFF);}
709};
710
Marshall Clow951035b2017-01-12 04:37:14 +0000711inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000712int
Marshall Clow36097db2016-07-28 04:52:02 +0000713char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000714{
715 for (; __n; --__n, ++__s1, ++__s2)
716 {
717 if (lt(*__s1, *__s2))
718 return -1;
719 if (lt(*__s2, *__s1))
720 return 1;
721 }
722 return 0;
723}
724
Marshall Clow951035b2017-01-12 04:37:14 +0000725inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000726size_t
Marshall Clow36097db2016-07-28 04:52:02 +0000727char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000728{
729 size_t __len = 0;
730 for (; !eq(*__s, char_type(0)); ++__s)
731 ++__len;
732 return __len;
733}
734
Marshall Clow951035b2017-01-12 04:37:14 +0000735inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000736const char16_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000737char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000738{
739 for (; __n; --__n)
740 {
741 if (eq(*__s, __a))
742 return __s;
743 ++__s;
744 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500745 return nullptr;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000746}
747
Michael Parkb5f44472019-11-08 13:42:26 +0000748inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000749char16_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000750char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000751{
Michael Parkb5f44472019-11-08 13:42:26 +0000752 if (__n == 0) return __s1;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000753 char_type* __r = __s1;
754 if (__s1 < __s2)
755 {
756 for (; __n; --__n, ++__s1, ++__s2)
757 assign(*__s1, *__s2);
758 }
759 else if (__s2 < __s1)
760 {
761 __s1 += __n;
762 __s2 += __n;
763 for (; __n; --__n)
764 assign(*--__s1, *--__s2);
765 }
766 return __r;
767}
768
Michael Parkb5f44472019-11-08 13:42:26 +0000769inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000770char16_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000771char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000772{
Arthur O'Dwyere9a55122021-04-20 22:24:24 -0400773 if (!__libcpp_is_constant_evaluated()) {
774 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
775 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000776 char_type* __r = __s1;
777 for (; __n; --__n, ++__s1, ++__s2)
778 assign(*__s1, *__s2);
779 return __r;
780}
781
Michael Parkb5f44472019-11-08 13:42:26 +0000782inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000783char16_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000784char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000785{
786 char_type* __r = __s;
787 for (; __n; --__n, ++__s)
788 assign(*__s, __a);
789 return __r;
790}
791
792template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000793struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000794{
795 typedef char32_t char_type;
796 typedef uint_least32_t int_type;
797 typedef streamoff off_type;
798 typedef u32streampos pos_type;
799 typedef mbstate_t state_type;
800
Marshall Clow951035b2017-01-12 04:37:14 +0000801 static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
802 void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000803 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
804 {return __c1 == __c2;}
805 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
806 {return __c1 < __c2;}
807
Marshall Clow951035b2017-01-12 04:37:14 +0000808 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
809 int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
810 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
811 size_t length(const char_type* __s) _NOEXCEPT;
812 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
813 const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
Michael Parkb5f44472019-11-08 13:42:26 +0000814 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow36097db2016-07-28 04:52:02 +0000815 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
Michael Parkb5f44472019-11-08 13:42:26 +0000816 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow36097db2016-07-28 04:52:02 +0000817 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
Michael Parkb5f44472019-11-08 13:42:26 +0000818 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow36097db2016-07-28 04:52:02 +0000819 static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000820
821 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
822 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
823 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
824 {return char_type(__c);}
825 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
826 {return int_type(__c);}
827 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
828 {return __c1 == __c2;}
829 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
830 {return int_type(0xFFFFFFFF);}
831};
832
Marshall Clow951035b2017-01-12 04:37:14 +0000833inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000834int
Marshall Clow36097db2016-07-28 04:52:02 +0000835char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000836{
837 for (; __n; --__n, ++__s1, ++__s2)
838 {
839 if (lt(*__s1, *__s2))
840 return -1;
841 if (lt(*__s2, *__s1))
842 return 1;
843 }
844 return 0;
845}
846
Marshall Clow951035b2017-01-12 04:37:14 +0000847inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000848size_t
Marshall Clow36097db2016-07-28 04:52:02 +0000849char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000850{
851 size_t __len = 0;
852 for (; !eq(*__s, char_type(0)); ++__s)
853 ++__len;
854 return __len;
855}
856
Marshall Clow951035b2017-01-12 04:37:14 +0000857inline _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000858const char32_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000859char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000860{
861 for (; __n; --__n)
862 {
863 if (eq(*__s, __a))
864 return __s;
865 ++__s;
866 }
Bruce Mitchener170d8972020-11-24 12:53:53 -0500867 return nullptr;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000868}
869
Michael Parkb5f44472019-11-08 13:42:26 +0000870inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000871char32_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000872char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000873{
Michael Parkb5f44472019-11-08 13:42:26 +0000874 if (__n == 0) return __s1;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000875 char_type* __r = __s1;
876 if (__s1 < __s2)
877 {
878 for (; __n; --__n, ++__s1, ++__s2)
879 assign(*__s1, *__s2);
880 }
881 else if (__s2 < __s1)
882 {
883 __s1 += __n;
884 __s2 += __n;
885 for (; __n; --__n)
886 assign(*--__s1, *--__s2);
887 }
888 return __r;
889}
890
Michael Parkb5f44472019-11-08 13:42:26 +0000891inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000892char32_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000893char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000894{
Arthur O'Dwyere9a55122021-04-20 22:24:24 -0400895 if (!__libcpp_is_constant_evaluated()) {
896 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
897 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000898 char_type* __r = __s1;
899 for (; __n; --__n, ++__s1, ++__s2)
900 assign(*__s1, *__s2);
901 return __r;
902}
903
Michael Parkb5f44472019-11-08 13:42:26 +0000904inline _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000905char32_t*
Marshall Clow36097db2016-07-28 04:52:02 +0000906char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000907{
908 char_type* __r = __s;
909 for (; __n; --__n, ++__s)
910 assign(*__s, __a);
911 return __r;
912}
913
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400914#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000915
916// helper fns for basic_string and string_view
917
918// __str_find
919template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
920inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700921__str_find(const _CharT *__p, _SizeT __sz,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000922 _CharT __c, _SizeT __pos) _NOEXCEPT
923{
924 if (__pos >= __sz)
925 return __npos;
926 const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500927 if (__r == nullptr)
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000928 return __npos;
929 return static_cast<_SizeT>(__r - __p);
930}
931
Sebastian Pop65115202016-12-30 18:01:36 +0000932template <class _CharT, class _Traits>
933inline _LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT *
934__search_substring(const _CharT *__first1, const _CharT *__last1,
zoecarver1997e0a2021-02-05 11:54:47 -0800935 const _CharT *__first2, const _CharT *__last2) _NOEXCEPT {
Sebastian Pop65115202016-12-30 18:01:36 +0000936 // Take advantage of knowing source and pattern lengths.
937 // Stop short when source is smaller than pattern.
938 const ptrdiff_t __len2 = __last2 - __first2;
939 if (__len2 == 0)
940 return __first1;
941
942 ptrdiff_t __len1 = __last1 - __first1;
943 if (__len1 < __len2)
944 return __last1;
945
946 // First element of __first2 is loop invariant.
947 _CharT __f2 = *__first2;
948 while (true) {
949 __len1 = __last1 - __first1;
950 // Check whether __first1 still has at least __len2 bytes.
951 if (__len1 < __len2)
952 return __last1;
953
954 // Find __f2 the first byte matching in __first1.
955 __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500956 if (__first1 == nullptr)
Sebastian Pop65115202016-12-30 18:01:36 +0000957 return __last1;
958
959 // It is faster to compare from the first byte of __first1 even if we
960 // already know that it matches the first byte of __first2: this is because
961 // __first2 is most likely aligned, as it is user's "pattern" string, and
962 // __first1 + 1 is most likely not aligned, as the match is in the middle of
963 // the string.
964 if (_Traits::compare(__first1, __first2, __len2) == 0)
965 return __first1;
966
967 ++__first1;
968 }
969}
970
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000971template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
972inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700973__str_find(const _CharT *__p, _SizeT __sz,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000974 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
975{
Sebastian Pop65115202016-12-30 18:01:36 +0000976 if (__pos > __sz)
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000977 return __npos;
Sebastian Pop65115202016-12-30 18:01:36 +0000978
979 if (__n == 0) // There is nothing to search, just return __pos.
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000980 return __pos;
Sebastian Pop65115202016-12-30 18:01:36 +0000981
982 const _CharT *__r = __search_substring<_CharT, _Traits>(
983 __p + __pos, __p + __sz, __s, __s + __n);
984
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000985 if (__r == __p + __sz)
986 return __npos;
987 return static_cast<_SizeT>(__r - __p);
988}
989
990
991// __str_rfind
992
993template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
994inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700995__str_rfind(const _CharT *__p, _SizeT __sz,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000996 _CharT __c, _SizeT __pos) _NOEXCEPT
997{
998 if (__sz < 1)
999 return __npos;
1000 if (__pos < __sz)
1001 ++__pos;
1002 else
1003 __pos = __sz;
1004 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1005 {
1006 if (_Traits::eq(*--__ps, __c))
1007 return static_cast<_SizeT>(__ps - __p);
1008 }
1009 return __npos;
1010}
1011
1012template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1013inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionnefd77e4f2019-10-23 10:40:15 -07001014__str_rfind(const _CharT *__p, _SizeT __sz,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001015 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1016{
1017 __pos = _VSTD::min(__pos, __sz);
1018 if (__n < __sz - __pos)
1019 __pos += __n;
1020 else
1021 __pos = __sz;
1022 const _CharT* __r = _VSTD::__find_end(
Louis Dionnefd77e4f2019-10-23 10:40:15 -07001023 __p, __p + __pos, __s, __s + __n, _Traits::eq,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001024 random_access_iterator_tag(), random_access_iterator_tag());
1025 if (__n > 0 && __r == __p + __pos)
1026 return __npos;
1027 return static_cast<_SizeT>(__r - __p);
1028}
1029
1030// __str_find_first_of
1031template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1032inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1033__str_find_first_of(const _CharT *__p, _SizeT __sz,
1034 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1035{
1036 if (__pos >= __sz || __n == 0)
1037 return __npos;
1038 const _CharT* __r = _VSTD::__find_first_of_ce
1039 (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
1040 if (__r == __p + __sz)
1041 return __npos;
1042 return static_cast<_SizeT>(__r - __p);
1043}
1044
1045
1046// __str_find_last_of
1047template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1048inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1049__str_find_last_of(const _CharT *__p, _SizeT __sz,
1050 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1051 {
1052 if (__n != 0)
1053 {
1054 if (__pos < __sz)
1055 ++__pos;
1056 else
1057 __pos = __sz;
1058 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1059 {
1060 const _CharT* __r = _Traits::find(__s, __n, *--__ps);
1061 if (__r)
1062 return static_cast<_SizeT>(__ps - __p);
1063 }
1064 }
1065 return __npos;
1066}
1067
1068
1069// __str_find_first_not_of
1070template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1071inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1072__str_find_first_not_of(const _CharT *__p, _SizeT __sz,
1073 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1074{
1075 if (__pos < __sz)
1076 {
1077 const _CharT* __pe = __p + __sz;
1078 for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001079 if (_Traits::find(__s, __n, *__ps) == nullptr)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001080 return static_cast<_SizeT>(__ps - __p);
1081 }
1082 return __npos;
1083}
1084
1085
1086template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1087inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1088__str_find_first_not_of(const _CharT *__p, _SizeT __sz,
1089 _CharT __c, _SizeT __pos) _NOEXCEPT
1090{
1091 if (__pos < __sz)
1092 {
1093 const _CharT* __pe = __p + __sz;
1094 for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
1095 if (!_Traits::eq(*__ps, __c))
1096 return static_cast<_SizeT>(__ps - __p);
1097 }
1098 return __npos;
1099}
1100
1101
1102// __str_find_last_not_of
1103template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1104inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1105__str_find_last_not_of(const _CharT *__p, _SizeT __sz,
1106 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1107{
1108 if (__pos < __sz)
1109 ++__pos;
1110 else
1111 __pos = __sz;
1112 for (const _CharT* __ps = __p + __pos; __ps != __p;)
Bruce Mitchener170d8972020-11-24 12:53:53 -05001113 if (_Traits::find(__s, __n, *--__ps) == nullptr)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001114 return static_cast<_SizeT>(__ps - __p);
1115 return __npos;
1116}
1117
1118
1119template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1120inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1121__str_find_last_not_of(const _CharT *__p, _SizeT __sz,
1122 _CharT __c, _SizeT __pos) _NOEXCEPT
1123{
1124 if (__pos < __sz)
1125 ++__pos;
1126 else
1127 __pos = __sz;
1128 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1129 if (!_Traits::eq(*--__ps, __c))
1130 return static_cast<_SizeT>(__ps - __p);
1131 return __npos;
1132}
1133
1134template<class _Ptr>
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001135inline _LIBCPP_INLINE_VISIBILITY
1136size_t __do_string_hash(_Ptr __p, _Ptr __e)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001137{
1138 typedef typename iterator_traits<_Ptr>::value_type value_type;
1139 return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
1140}
1141
1142template <class _CharT, class _Iter, class _Traits=char_traits<_CharT> >
1143struct __quoted_output_proxy
1144{
1145 _Iter __first;
1146 _Iter __last;
1147 _CharT __delim;
1148 _CharT __escape;
1149
1150 __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
1151 : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
Louis Dionnefd77e4f2019-10-23 10:40:15 -07001152 // This would be a nice place for a string_ref
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001153};
1154
1155_LIBCPP_END_NAMESPACE_STD
1156
Eric Fiselierf4433a32017-05-31 22:07:49 +00001157_LIBCPP_POP_MACROS
1158
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001159#endif // _LIBCPP___STRING