vstring_util.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Versatile string utility -*- C++ -*-
  2. // Copyright (C) 2005-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/vstring_util.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ext/vstring.h}
  23. */
  24. #ifndef _VSTRING_UTIL_H
  25. #define _VSTRING_UTIL_H 1
  26. #pragma GCC system_header
  27. #include <bits/requires_hosted.h> // GNU extensions are currently omitted
  28. #include <ext/vstring_fwd.h>
  29. #include <debug/debug.h>
  30. #include <bits/stl_function.h> // For less
  31. #include <bits/functexcept.h>
  32. #include <bits/localefwd.h>
  33. #include <bits/ostream_insert.h>
  34. #include <bits/stl_iterator.h>
  35. #include <ext/numeric_traits.h>
  36. #include <ext/alloc_traits.h>
  37. #include <bits/move.h>
  38. #include <bits/range_access.h>
  39. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. template<typename _CharT, typename _Traits, typename _Alloc>
  43. struct __vstring_utility
  44. {
  45. typedef typename __alloc_traits<_Alloc>::template rebind<_CharT>::other
  46. _CharT_alloc_type;
  47. typedef __alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  48. typedef _Traits traits_type;
  49. typedef typename _Traits::char_type value_type;
  50. typedef typename _CharT_alloc_type::size_type size_type;
  51. typedef typename _CharT_alloc_type::difference_type difference_type;
  52. typedef typename _CharT_alloc_traits::pointer pointer;
  53. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  54. // For __sso_string.
  55. typedef __gnu_cxx::
  56. __normal_iterator<pointer, __gnu_cxx::
  57. __versa_string<_CharT, _Traits, _Alloc,
  58. __sso_string_base> >
  59. __sso_iterator;
  60. typedef __gnu_cxx::
  61. __normal_iterator<const_pointer, __gnu_cxx::
  62. __versa_string<_CharT, _Traits, _Alloc,
  63. __sso_string_base> >
  64. __const_sso_iterator;
  65. // For __rc_string.
  66. typedef __gnu_cxx::
  67. __normal_iterator<pointer, __gnu_cxx::
  68. __versa_string<_CharT, _Traits, _Alloc,
  69. __rc_string_base> >
  70. __rc_iterator;
  71. typedef __gnu_cxx::
  72. __normal_iterator<const_pointer, __gnu_cxx::
  73. __versa_string<_CharT, _Traits, _Alloc,
  74. __rc_string_base> >
  75. __const_rc_iterator;
  76. // NB: When the allocator is empty, deriving from it saves space
  77. // (http://www.cantrip.org/emptyopt.html).
  78. template<typename _Alloc1>
  79. struct _Alloc_hider
  80. : public _Alloc1
  81. {
  82. _Alloc_hider(_CharT* __ptr)
  83. : _Alloc1(), _M_p(__ptr) { }
  84. _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
  85. : _Alloc1(__a), _M_p(__ptr) { }
  86. _CharT* _M_p; // The actual data.
  87. };
  88. // When __n = 1 way faster than the general multichar
  89. // traits_type::copy/move/assign.
  90. static void
  91. _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
  92. {
  93. if (__n == 1)
  94. traits_type::assign(*__d, *__s);
  95. else
  96. traits_type::copy(__d, __s, __n);
  97. }
  98. static void
  99. _S_move(_CharT* __d, const _CharT* __s, size_type __n)
  100. {
  101. if (__n == 1)
  102. traits_type::assign(*__d, *__s);
  103. else
  104. traits_type::move(__d, __s, __n);
  105. }
  106. static void
  107. _S_assign(_CharT* __d, size_type __n, _CharT __c)
  108. {
  109. if (__n == 1)
  110. traits_type::assign(*__d, __c);
  111. else
  112. traits_type::assign(__d, __n, __c);
  113. }
  114. // _S_copy_chars is a separate template to permit specialization
  115. // to optimize for the common case of pointers as iterators.
  116. template<typename _Iterator>
  117. static void
  118. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  119. {
  120. for (; __k1 != __k2; ++__k1, ++__p)
  121. traits_type::assign(*__p, *__k1); // These types are off.
  122. }
  123. static void
  124. _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
  125. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  126. static void
  127. _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
  128. __const_sso_iterator __k2)
  129. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  130. static void
  131. _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
  132. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  133. static void
  134. _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
  135. __const_rc_iterator __k2)
  136. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  137. static void
  138. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
  139. { _S_copy(__p, __k1, __k2 - __k1); }
  140. static void
  141. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  142. { _S_copy(__p, __k1, __k2 - __k1); }
  143. static int
  144. _S_compare(size_type __n1, size_type __n2)
  145. {
  146. const difference_type __d = difference_type(__n1 - __n2);
  147. if (__d > __numeric_traits_integer<int>::__max)
  148. return __numeric_traits_integer<int>::__max;
  149. else if (__d < __numeric_traits_integer<int>::__min)
  150. return __numeric_traits_integer<int>::__min;
  151. else
  152. return int(__d);
  153. }
  154. };
  155. _GLIBCXX_END_NAMESPACE_VERSION
  156. } // namespace
  157. #endif /* _VSTRING_UTIL_H */