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