You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.7KB

  1. // Versatile string utility -*- C++ -*-
  2. // Copyright (C) 2005-2020 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 <ext/vstring_fwd.h>
  28. #include <debug/debug.h>
  29. #include <bits/stl_function.h> // For less
  30. #include <bits/functexcept.h>
  31. #include <bits/localefwd.h>
  32. #include <bits/ostream_insert.h>
  33. #include <bits/stl_iterator.h>
  34. #include <ext/numeric_traits.h>
  35. #include <ext/alloc_traits.h>
  36. #include <bits/move.h>
  37. #include <bits/range_access.h>
  38. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  39. {
  40. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  41. template<typename _CharT, typename _Traits, typename _Alloc>
  42. struct __vstring_utility
  43. {
  44. typedef typename __alloc_traits<_Alloc>::template rebind<_CharT>::other
  45. _CharT_alloc_type;
  46. typedef __alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  47. typedef _Traits traits_type;
  48. typedef typename _Traits::char_type value_type;
  49. typedef typename _CharT_alloc_type::size_type size_type;
  50. typedef typename _CharT_alloc_type::difference_type difference_type;
  51. typedef typename _CharT_alloc_traits::pointer pointer;
  52. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  53. // For __sso_string.
  54. typedef __gnu_cxx::
  55. __normal_iterator<pointer, __gnu_cxx::
  56. __versa_string<_CharT, _Traits, _Alloc,
  57. __sso_string_base> >
  58. __sso_iterator;
  59. typedef __gnu_cxx::
  60. __normal_iterator<const_pointer, __gnu_cxx::
  61. __versa_string<_CharT, _Traits, _Alloc,
  62. __sso_string_base> >
  63. __const_sso_iterator;
  64. // For __rc_string.
  65. typedef __gnu_cxx::
  66. __normal_iterator<pointer, __gnu_cxx::
  67. __versa_string<_CharT, _Traits, _Alloc,
  68. __rc_string_base> >
  69. __rc_iterator;
  70. typedef __gnu_cxx::
  71. __normal_iterator<const_pointer, __gnu_cxx::
  72. __versa_string<_CharT, _Traits, _Alloc,
  73. __rc_string_base> >
  74. __const_rc_iterator;
  75. // NB: When the allocator is empty, deriving from it saves space
  76. // (http://www.cantrip.org/emptyopt.html).
  77. template<typename _Alloc1>
  78. struct _Alloc_hider
  79. : public _Alloc1
  80. {
  81. _Alloc_hider(_CharT* __ptr)
  82. : _Alloc1(), _M_p(__ptr) { }
  83. _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
  84. : _Alloc1(__a), _M_p(__ptr) { }
  85. _CharT* _M_p; // The actual data.
  86. };
  87. // When __n = 1 way faster than the general multichar
  88. // traits_type::copy/move/assign.
  89. static void
  90. _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
  91. {
  92. if (__n == 1)
  93. traits_type::assign(*__d, *__s);
  94. else
  95. traits_type::copy(__d, __s, __n);
  96. }
  97. static void
  98. _S_move(_CharT* __d, const _CharT* __s, size_type __n)
  99. {
  100. if (__n == 1)
  101. traits_type::assign(*__d, *__s);
  102. else
  103. traits_type::move(__d, __s, __n);
  104. }
  105. static void
  106. _S_assign(_CharT* __d, size_type __n, _CharT __c)
  107. {
  108. if (__n == 1)
  109. traits_type::assign(*__d, __c);
  110. else
  111. traits_type::assign(__d, __n, __c);
  112. }
  113. // _S_copy_chars is a separate template to permit specialization
  114. // to optimize for the common case of pointers as iterators.
  115. template<typename _Iterator>
  116. static void
  117. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  118. {
  119. for (; __k1 != __k2; ++__k1, ++__p)
  120. traits_type::assign(*__p, *__k1); // These types are off.
  121. }
  122. static void
  123. _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
  124. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  125. static void
  126. _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
  127. __const_sso_iterator __k2)
  128. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  129. static void
  130. _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
  131. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  132. static void
  133. _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
  134. __const_rc_iterator __k2)
  135. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  136. static void
  137. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
  138. { _S_copy(__p, __k1, __k2 - __k1); }
  139. static void
  140. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  141. { _S_copy(__p, __k1, __k2 - __k1); }
  142. static int
  143. _S_compare(size_type __n1, size_type __n2)
  144. {
  145. const difference_type __d = difference_type(__n1 - __n2);
  146. if (__d > __numeric_traits_integer<int>::__max)
  147. return __numeric_traits_integer<int>::__max;
  148. else if (__d < __numeric_traits_integer<int>::__min)
  149. return __numeric_traits_integer<int>::__min;
  150. else
  151. return int(__d);
  152. }
  153. };
  154. _GLIBCXX_END_NAMESPACE_VERSION
  155. } // namespace
  156. #endif /* _VSTRING_UTIL_H */