vstring_util.h 5.6 KB

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