__string 46 KB

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