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