1// Versatile string utility -*- C++ -*-
2
3// Copyright (C) 2005, 2006, 2007 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 2, 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// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING.  If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction.  Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License.  This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30/** @file ext/vstring_util.h
31 *  This file is a GNU extension to the Standard C++ Library.
32 *  This is an internal header file, included by other library headers.
33 *  You should not attempt to use it directly.
34 */
35
36#ifndef _VSTRING_UTIL_H
37#define _VSTRING_UTIL_H 1
38
39#pragma GCC system_header
40
41#include <ext/vstring_fwd.h>
42#include <debug/debug.h>
43#include <bits/stl_function.h>  // For less
44#include <bits/functexcept.h>
45#include <locale>
46#include <algorithm> // For std::distance, srd::search.
47#include <bits/ostream_insert.h>
48
49_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
50
51  template<typename _CharT, typename _Traits, typename _Alloc>
52    struct __vstring_utility
53    {
54      typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
55
56      typedef _Traits					    traits_type;
57      typedef typename _Traits::char_type		    value_type;
58      typedef typename _CharT_alloc_type::size_type	    size_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(const _Alloc1& __a, _CharT* __ptr)
93	  : _Alloc1(__a), _M_p(__ptr) { }
94
95	  _CharT*  _M_p; // The actual data.
96	};
97
98      // For use in _M_construct (_S_construct) forward_iterator_tag.
99      template<typename _Type>
100        static bool
101        _S_is_null_pointer(_Type* __ptr)
102        { return __ptr == 0; }
103
104      template<typename _Type>
105        static bool
106        _S_is_null_pointer(_Type)
107        { return false; }
108
109      // When __n = 1 way faster than the general multichar
110      // traits_type::copy/move/assign.
111      static void
112      _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
113      {
114	if (__n == 1)
115	  traits_type::assign(*__d, *__s);
116	else
117	  traits_type::copy(__d, __s, __n);
118      }
119
120      static void
121      _S_move(_CharT* __d, const _CharT* __s, size_type __n)
122      {
123	if (__n == 1)
124	  traits_type::assign(*__d, *__s);
125	else
126	  traits_type::move(__d, __s, __n);
127      }
128
129      static void
130      _S_assign(_CharT* __d, size_type __n, _CharT __c)
131      {
132	if (__n == 1)
133	  traits_type::assign(*__d, __c);
134	else
135	  traits_type::assign(__d, __n, __c);
136      }
137
138      // _S_copy_chars is a separate template to permit specialization
139      // to optimize for the common case of pointers as iterators.
140      template<typename _Iterator>
141        static void
142        _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
143        {
144	  for (; __k1 != __k2; ++__k1, ++__p)
145	    traits_type::assign(*__p, *__k1); // These types are off.
146	}
147
148      static void
149      _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
150      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
151
152      static void
153      _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
154		    __const_sso_iterator __k2)
155      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
156
157      static void
158      _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
159      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
160
161      static void
162      _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
163		    __const_rc_iterator __k2)
164      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
165
166      static void
167      _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
168      { _S_copy(__p, __k1, __k2 - __k1); }
169
170      static void
171      _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
172      { _S_copy(__p, __k1, __k2 - __k1); }
173    };
174
175_GLIBCXX_END_NAMESPACE
176
177#endif /* _VSTRING_UTIL_H */
178