__string 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. // Disable double inline warning.
  527. #ifdef _LIBCPP_COMPILER_MSVC
  528. #pragma warning ( push )
  529. #pragma warning ( disable : 4141 )
  530. #endif
  531. template <class _Traits>
  532. _LIBCPP_INLINE_VISIBILITY
  533. _LIBCPP_CONSTEXPR
  534. inline size_t __char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
  535. #if _LIBCPP_DEBUG_LEVEL >= 1
  536. 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);
  537. #else
  538. return _Traits::length(__s);
  539. #endif
  540. }
  541. #ifdef _LIBCPP_COMPILER_MSVC
  542. #pragma warning ( pop )
  543. #endif
  544. #ifndef _LIBCPP_HAS_NO_CHAR8_T
  545. template <>
  546. struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>
  547. {
  548. typedef char8_t char_type;
  549. typedef unsigned int int_type;
  550. typedef streamoff off_type;
  551. typedef u8streampos pos_type;
  552. typedef mbstate_t state_type;
  553. static inline constexpr void assign(char_type& __c1, const char_type& __c2) noexcept
  554. {__c1 = __c2;}
  555. static inline constexpr bool eq(char_type __c1, char_type __c2) noexcept
  556. {return __c1 == __c2;}
  557. static inline constexpr bool lt(char_type __c1, char_type __c2) noexcept
  558. {return __c1 < __c2;}
  559. static constexpr
  560. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  561. static constexpr
  562. size_t length(const char_type* __s) _NOEXCEPT;
  563. _LIBCPP_INLINE_VISIBILITY static constexpr
  564. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  565. static _LIBCPP_CONSTEXPR_AFTER_CXX17
  566. char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  567. {
  568. return __libcpp_is_constant_evaluated()
  569. ? _VSTD::__move_constexpr(__s1, __s2, __n)
  570. : __n == 0 ? __s1 : (char_type*)_VSTD::memmove(__s1, __s2, __n);
  571. }
  572. static _LIBCPP_CONSTEXPR_AFTER_CXX17
  573. char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  574. {
  575. if (!__libcpp_is_constant_evaluated()) {
  576. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  577. }
  578. return __libcpp_is_constant_evaluated()
  579. ? _VSTD::__copy_constexpr(__s1, __s2, __n)
  580. : __n == 0 ? __s1 : (char_type*)_VSTD::memcpy(__s1, __s2, __n);
  581. }
  582. static _LIBCPP_CONSTEXPR_AFTER_CXX17
  583. char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  584. {
  585. return __libcpp_is_constant_evaluated()
  586. ? _VSTD::__assign_constexpr(__s, __n, __a)
  587. : __n == 0 ? __s : (char_type*)_VSTD::memset(__s, to_int_type(__a), __n);
  588. }
  589. static inline constexpr int_type not_eof(int_type __c) noexcept
  590. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  591. static inline constexpr char_type to_char_type(int_type __c) noexcept
  592. {return char_type(__c);}
  593. static inline constexpr int_type to_int_type(char_type __c) noexcept
  594. {return int_type(__c);}
  595. static inline constexpr bool eq_int_type(int_type __c1, int_type __c2) noexcept
  596. {return __c1 == __c2;}
  597. static inline constexpr int_type eof() noexcept
  598. {return int_type(EOF);}
  599. };
  600. // TODO use '__builtin_strlen' if it ever supports char8_t ??
  601. inline constexpr
  602. size_t
  603. char_traits<char8_t>::length(const char_type* __s) _NOEXCEPT
  604. {
  605. size_t __len = 0;
  606. for (; !eq(*__s, char_type(0)); ++__s)
  607. ++__len;
  608. return __len;
  609. }
  610. inline constexpr
  611. int
  612. char_traits<char8_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  613. {
  614. #if __has_feature(cxx_constexpr_string_builtins)
  615. return __builtin_memcmp(__s1, __s2, __n);
  616. #else
  617. for (; __n; --__n, ++__s1, ++__s2)
  618. {
  619. if (lt(*__s1, *__s2))
  620. return -1;
  621. if (lt(*__s2, *__s1))
  622. return 1;
  623. }
  624. return 0;
  625. #endif
  626. }
  627. // TODO use '__builtin_char_memchr' if it ever supports char8_t ??
  628. inline constexpr
  629. const char8_t*
  630. char_traits<char8_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  631. {
  632. for (; __n; --__n)
  633. {
  634. if (eq(*__s, __a))
  635. return __s;
  636. ++__s;
  637. }
  638. return nullptr;
  639. }
  640. #endif // #_LIBCPP_HAS_NO_CHAR8_T
  641. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  642. template <>
  643. struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
  644. {
  645. typedef char16_t char_type;
  646. typedef uint_least16_t int_type;
  647. typedef streamoff off_type;
  648. typedef u16streampos pos_type;
  649. typedef mbstate_t state_type;
  650. static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  651. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  652. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  653. {return __c1 == __c2;}
  654. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  655. {return __c1 < __c2;}
  656. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  657. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  658. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  659. size_t length(const char_type* __s) _NOEXCEPT;
  660. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  661. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  662. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  663. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  664. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  665. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  666. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  667. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
  668. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  669. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  670. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  671. {return char_type(__c);}
  672. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  673. {return int_type(__c);}
  674. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  675. {return __c1 == __c2;}
  676. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  677. {return int_type(0xFFFF);}
  678. };
  679. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  680. int
  681. char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  682. {
  683. for (; __n; --__n, ++__s1, ++__s2)
  684. {
  685. if (lt(*__s1, *__s2))
  686. return -1;
  687. if (lt(*__s2, *__s1))
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  693. size_t
  694. char_traits<char16_t>::length(const char_type* __s) _NOEXCEPT
  695. {
  696. size_t __len = 0;
  697. for (; !eq(*__s, char_type(0)); ++__s)
  698. ++__len;
  699. return __len;
  700. }
  701. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  702. const char16_t*
  703. char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  704. {
  705. for (; __n; --__n)
  706. {
  707. if (eq(*__s, __a))
  708. return __s;
  709. ++__s;
  710. }
  711. return nullptr;
  712. }
  713. inline _LIBCPP_CONSTEXPR_AFTER_CXX17
  714. char16_t*
  715. char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  716. {
  717. if (__n == 0) return __s1;
  718. char_type* __r = __s1;
  719. if (__s1 < __s2)
  720. {
  721. for (; __n; --__n, ++__s1, ++__s2)
  722. assign(*__s1, *__s2);
  723. }
  724. else if (__s2 < __s1)
  725. {
  726. __s1 += __n;
  727. __s2 += __n;
  728. for (; __n; --__n)
  729. assign(*--__s1, *--__s2);
  730. }
  731. return __r;
  732. }
  733. inline _LIBCPP_CONSTEXPR_AFTER_CXX17
  734. char16_t*
  735. char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  736. {
  737. if (!__libcpp_is_constant_evaluated()) {
  738. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  739. }
  740. char_type* __r = __s1;
  741. for (; __n; --__n, ++__s1, ++__s2)
  742. assign(*__s1, *__s2);
  743. return __r;
  744. }
  745. inline _LIBCPP_CONSTEXPR_AFTER_CXX17
  746. char16_t*
  747. char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  748. {
  749. char_type* __r = __s;
  750. for (; __n; --__n, ++__s)
  751. assign(*__s, __a);
  752. return __r;
  753. }
  754. template <>
  755. struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
  756. {
  757. typedef char32_t char_type;
  758. typedef uint_least32_t int_type;
  759. typedef streamoff off_type;
  760. typedef u32streampos pos_type;
  761. typedef mbstate_t state_type;
  762. static inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  763. void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;}
  764. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  765. {return __c1 == __c2;}
  766. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  767. {return __c1 < __c2;}
  768. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  769. int compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  770. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  771. size_t length(const char_type* __s) _NOEXCEPT;
  772. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14
  773. const char_type* find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT;
  774. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  775. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  776. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  777. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT;
  778. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  779. static char_type* assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT;
  780. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  781. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  782. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  783. {return char_type(__c);}
  784. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  785. {return int_type(__c);}
  786. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  787. {return __c1 == __c2;}
  788. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  789. {return int_type(0xFFFFFFFF);}
  790. };
  791. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  792. int
  793. char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  794. {
  795. for (; __n; --__n, ++__s1, ++__s2)
  796. {
  797. if (lt(*__s1, *__s2))
  798. return -1;
  799. if (lt(*__s2, *__s1))
  800. return 1;
  801. }
  802. return 0;
  803. }
  804. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  805. size_t
  806. char_traits<char32_t>::length(const char_type* __s) _NOEXCEPT
  807. {
  808. size_t __len = 0;
  809. for (; !eq(*__s, char_type(0)); ++__s)
  810. ++__len;
  811. return __len;
  812. }
  813. inline _LIBCPP_CONSTEXPR_AFTER_CXX14
  814. const char32_t*
  815. char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a) _NOEXCEPT
  816. {
  817. for (; __n; --__n)
  818. {
  819. if (eq(*__s, __a))
  820. return __s;
  821. ++__s;
  822. }
  823. return nullptr;
  824. }
  825. inline _LIBCPP_CONSTEXPR_AFTER_CXX17
  826. char32_t*
  827. char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  828. {
  829. if (__n == 0) return __s1;
  830. char_type* __r = __s1;
  831. if (__s1 < __s2)
  832. {
  833. for (; __n; --__n, ++__s1, ++__s2)
  834. assign(*__s1, *__s2);
  835. }
  836. else if (__s2 < __s1)
  837. {
  838. __s1 += __n;
  839. __s2 += __n;
  840. for (; __n; --__n)
  841. assign(*--__s1, *--__s2);
  842. }
  843. return __r;
  844. }
  845. inline _LIBCPP_CONSTEXPR_AFTER_CXX17
  846. char32_t*
  847. char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT
  848. {
  849. if (!__libcpp_is_constant_evaluated()) {
  850. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  851. }
  852. char_type* __r = __s1;
  853. for (; __n; --__n, ++__s1, ++__s2)
  854. assign(*__s1, *__s2);
  855. return __r;
  856. }
  857. inline _LIBCPP_CONSTEXPR_AFTER_CXX17
  858. char32_t*
  859. char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a) _NOEXCEPT
  860. {
  861. char_type* __r = __s;
  862. for (; __n; --__n, ++__s)
  863. assign(*__s, __a);
  864. return __r;
  865. }
  866. #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
  867. // helper fns for basic_string and string_view
  868. // __str_find
  869. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  870. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  871. __str_find(const _CharT *__p, _SizeT __sz,
  872. _CharT __c, _SizeT __pos) _NOEXCEPT
  873. {
  874. if (__pos >= __sz)
  875. return __npos;
  876. const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
  877. if (__r == nullptr)
  878. return __npos;
  879. return static_cast<_SizeT>(__r - __p);
  880. }
  881. template <class _CharT, class _Traits>
  882. inline _LIBCPP_CONSTEXPR_AFTER_CXX11 const _CharT *
  883. __search_substring(const _CharT *__first1, const _CharT *__last1,
  884. const _CharT *__first2, const _CharT *__last2) _NOEXCEPT {
  885. // Take advantage of knowing source and pattern lengths.
  886. // Stop short when source is smaller than pattern.
  887. const ptrdiff_t __len2 = __last2 - __first2;
  888. if (__len2 == 0)
  889. return __first1;
  890. ptrdiff_t __len1 = __last1 - __first1;
  891. if (__len1 < __len2)
  892. return __last1;
  893. // First element of __first2 is loop invariant.
  894. _CharT __f2 = *__first2;
  895. while (true) {
  896. __len1 = __last1 - __first1;
  897. // Check whether __first1 still has at least __len2 bytes.
  898. if (__len1 < __len2)
  899. return __last1;
  900. // Find __f2 the first byte matching in __first1.
  901. __first1 = _Traits::find(__first1, __len1 - __len2 + 1, __f2);
  902. if (__first1 == nullptr)
  903. return __last1;
  904. // It is faster to compare from the first byte of __first1 even if we
  905. // already know that it matches the first byte of __first2: this is because
  906. // __first2 is most likely aligned, as it is user's "pattern" string, and
  907. // __first1 + 1 is most likely not aligned, as the match is in the middle of
  908. // the string.
  909. if (_Traits::compare(__first1, __first2, __len2) == 0)
  910. return __first1;
  911. ++__first1;
  912. }
  913. }
  914. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  915. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  916. __str_find(const _CharT *__p, _SizeT __sz,
  917. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  918. {
  919. if (__pos > __sz)
  920. return __npos;
  921. if (__n == 0) // There is nothing to search, just return __pos.
  922. return __pos;
  923. const _CharT *__r = __search_substring<_CharT, _Traits>(
  924. __p + __pos, __p + __sz, __s, __s + __n);
  925. if (__r == __p + __sz)
  926. return __npos;
  927. return static_cast<_SizeT>(__r - __p);
  928. }
  929. // __str_rfind
  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. _CharT __c, _SizeT __pos) _NOEXCEPT
  934. {
  935. if (__sz < 1)
  936. return __npos;
  937. if (__pos < __sz)
  938. ++__pos;
  939. else
  940. __pos = __sz;
  941. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  942. {
  943. if (_Traits::eq(*--__ps, __c))
  944. return static_cast<_SizeT>(__ps - __p);
  945. }
  946. return __npos;
  947. }
  948. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  949. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  950. __str_rfind(const _CharT *__p, _SizeT __sz,
  951. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  952. {
  953. __pos = _VSTD::min(__pos, __sz);
  954. if (__n < __sz - __pos)
  955. __pos += __n;
  956. else
  957. __pos = __sz;
  958. const _CharT* __r = _VSTD::__find_end(
  959. __p, __p + __pos, __s, __s + __n, _Traits::eq,
  960. random_access_iterator_tag(), random_access_iterator_tag());
  961. if (__n > 0 && __r == __p + __pos)
  962. return __npos;
  963. return static_cast<_SizeT>(__r - __p);
  964. }
  965. // __str_find_first_of
  966. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  967. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  968. __str_find_first_of(const _CharT *__p, _SizeT __sz,
  969. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  970. {
  971. if (__pos >= __sz || __n == 0)
  972. return __npos;
  973. const _CharT* __r = _VSTD::__find_first_of_ce
  974. (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
  975. if (__r == __p + __sz)
  976. return __npos;
  977. return static_cast<_SizeT>(__r - __p);
  978. }
  979. // __str_find_last_of
  980. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  981. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  982. __str_find_last_of(const _CharT *__p, _SizeT __sz,
  983. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  984. {
  985. if (__n != 0)
  986. {
  987. if (__pos < __sz)
  988. ++__pos;
  989. else
  990. __pos = __sz;
  991. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  992. {
  993. const _CharT* __r = _Traits::find(__s, __n, *--__ps);
  994. if (__r)
  995. return static_cast<_SizeT>(__ps - __p);
  996. }
  997. }
  998. return __npos;
  999. }
  1000. // __str_find_first_not_of
  1001. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  1002. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  1003. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  1004. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  1005. {
  1006. if (__pos < __sz)
  1007. {
  1008. const _CharT* __pe = __p + __sz;
  1009. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  1010. if (_Traits::find(__s, __n, *__ps) == nullptr)
  1011. return static_cast<_SizeT>(__ps - __p);
  1012. }
  1013. return __npos;
  1014. }
  1015. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  1016. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  1017. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  1018. _CharT __c, _SizeT __pos) _NOEXCEPT
  1019. {
  1020. if (__pos < __sz)
  1021. {
  1022. const _CharT* __pe = __p + __sz;
  1023. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  1024. if (!_Traits::eq(*__ps, __c))
  1025. return static_cast<_SizeT>(__ps - __p);
  1026. }
  1027. return __npos;
  1028. }
  1029. // __str_find_last_not_of
  1030. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  1031. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  1032. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  1033. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  1034. {
  1035. if (__pos < __sz)
  1036. ++__pos;
  1037. else
  1038. __pos = __sz;
  1039. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  1040. if (_Traits::find(__s, __n, *--__ps) == nullptr)
  1041. return static_cast<_SizeT>(__ps - __p);
  1042. return __npos;
  1043. }
  1044. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  1045. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  1046. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  1047. _CharT __c, _SizeT __pos) _NOEXCEPT
  1048. {
  1049. if (__pos < __sz)
  1050. ++__pos;
  1051. else
  1052. __pos = __sz;
  1053. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  1054. if (!_Traits::eq(*--__ps, __c))
  1055. return static_cast<_SizeT>(__ps - __p);
  1056. return __npos;
  1057. }
  1058. template<class _Ptr>
  1059. inline _LIBCPP_INLINE_VISIBILITY
  1060. size_t __do_string_hash(_Ptr __p, _Ptr __e)
  1061. {
  1062. typedef typename iterator_traits<_Ptr>::value_type value_type;
  1063. return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
  1064. }
  1065. template <class _CharT, class _Iter, class _Traits=char_traits<_CharT> >
  1066. struct __quoted_output_proxy
  1067. {
  1068. _Iter __first;
  1069. _Iter __last;
  1070. _CharT __delim;
  1071. _CharT __escape;
  1072. __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
  1073. : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
  1074. // This would be a nice place for a string_ref
  1075. };
  1076. _LIBCPP_END_NAMESPACE_STD
  1077. _LIBCPP_POP_MACROS
  1078. #endif // _LIBCPP___STRING