1169691Skan// Versatile string utility -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the
7169691Skan// terms of the GNU General Public License as published by the
8169691Skan// Free Software Foundation; either version 2, or (at your option)
9169691Skan// any later version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful,
12169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14169691Skan// GNU General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License along
17169691Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19169691Skan// USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free software
22169691Skan// library without restriction.  Specifically, if other files instantiate
23169691Skan// templates or use macros or inline functions from this file, or you compile
24169691Skan// this file and link it with other files to produce an executable, this
25169691Skan// file does not by itself cause the resulting executable to be covered by
26169691Skan// the GNU General Public License.  This exception does not however
27169691Skan// invalidate any other reasons why the executable file might be covered by
28169691Skan// the GNU General Public License.
29169691Skan
30169691Skan/** @file ext/vstring_util.h
31169691Skan *  This file is a GNU extension to the Standard C++ Library.
32169691Skan *  This is an internal header file, included by other library headers.
33169691Skan *  You should not attempt to use it directly.
34169691Skan */
35169691Skan
36169691Skan#ifndef _VSTRING_UTIL_H
37169691Skan#define _VSTRING_UTIL_H 1
38169691Skan
39169691Skan#pragma GCC system_header
40169691Skan
41169691Skan#include <ext/vstring_fwd.h>
42169691Skan#include <debug/debug.h>
43169691Skan#include <bits/stl_function.h>  // For less
44169691Skan#include <bits/functexcept.h>
45169691Skan#include <locale>
46169691Skan#include <algorithm> // For std::distance, srd::search.
47169691Skan#include <bits/ostream_insert.h>
48169691Skan
49169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
50169691Skan
51169691Skan  template<typename _CharT, typename _Traits, typename _Alloc>
52169691Skan    struct __vstring_utility
53169691Skan    {
54169691Skan      typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
55169691Skan
56169691Skan      typedef _Traits					    traits_type;
57169691Skan      typedef typename _Traits::char_type		    value_type;
58169691Skan      typedef typename _CharT_alloc_type::size_type	    size_type;
59169691Skan      typedef typename _CharT_alloc_type::pointer	    pointer;
60169691Skan      typedef typename _CharT_alloc_type::const_pointer	    const_pointer;
61169691Skan
62169691Skan      // For __sso_string.
63169691Skan      typedef __gnu_cxx::
64169691Skan      __normal_iterator<pointer, __gnu_cxx::
65169691Skan			__versa_string<_CharT, _Traits, _Alloc,
66169691Skan				       __sso_string_base> >
67169691Skan        __sso_iterator;
68169691Skan      typedef __gnu_cxx::
69169691Skan      __normal_iterator<const_pointer, __gnu_cxx::
70169691Skan			__versa_string<_CharT, _Traits, _Alloc,
71169691Skan				       __sso_string_base> >
72169691Skan        __const_sso_iterator;
73169691Skan
74169691Skan      // For __rc_string.
75169691Skan      typedef __gnu_cxx::
76169691Skan      __normal_iterator<pointer, __gnu_cxx::
77169691Skan			__versa_string<_CharT, _Traits, _Alloc,
78169691Skan				       __rc_string_base> >
79169691Skan        __rc_iterator;
80169691Skan      typedef __gnu_cxx::
81169691Skan      __normal_iterator<const_pointer, __gnu_cxx::
82169691Skan			__versa_string<_CharT, _Traits, _Alloc,
83169691Skan				       __rc_string_base> >
84169691Skan        __const_rc_iterator;
85169691Skan
86169691Skan      // NB:  When the allocator is empty, deriving from it saves space
87169691Skan      // (http://www.cantrip.org/emptyopt.html).
88169691Skan      template<typename _Alloc1>
89169691Skan        struct _Alloc_hider
90169691Skan	: public _Alloc1
91169691Skan	{
92169691Skan	  _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
93169691Skan	  : _Alloc1(__a), _M_p(__ptr) { }
94169691Skan
95169691Skan	  _CharT*  _M_p; // The actual data.
96169691Skan	};
97169691Skan
98169691Skan      // For use in _M_construct (_S_construct) forward_iterator_tag.
99169691Skan      template<typename _Type>
100169691Skan        static bool
101169691Skan        _S_is_null_pointer(_Type* __ptr)
102169691Skan        { return __ptr == 0; }
103169691Skan
104169691Skan      template<typename _Type>
105169691Skan        static bool
106169691Skan        _S_is_null_pointer(_Type)
107169691Skan        { return false; }
108169691Skan
109169691Skan      // When __n = 1 way faster than the general multichar
110169691Skan      // traits_type::copy/move/assign.
111169691Skan      static void
112169691Skan      _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
113169691Skan      {
114169691Skan	if (__n == 1)
115169691Skan	  traits_type::assign(*__d, *__s);
116169691Skan	else
117169691Skan	  traits_type::copy(__d, __s, __n);
118169691Skan      }
119169691Skan
120169691Skan      static void
121169691Skan      _S_move(_CharT* __d, const _CharT* __s, size_type __n)
122169691Skan      {
123169691Skan	if (__n == 1)
124169691Skan	  traits_type::assign(*__d, *__s);
125169691Skan	else
126169691Skan	  traits_type::move(__d, __s, __n);
127169691Skan      }
128169691Skan
129169691Skan      static void
130169691Skan      _S_assign(_CharT* __d, size_type __n, _CharT __c)
131169691Skan      {
132169691Skan	if (__n == 1)
133169691Skan	  traits_type::assign(*__d, __c);
134169691Skan	else
135169691Skan	  traits_type::assign(__d, __n, __c);
136169691Skan      }
137169691Skan
138169691Skan      // _S_copy_chars is a separate template to permit specialization
139169691Skan      // to optimize for the common case of pointers as iterators.
140169691Skan      template<typename _Iterator>
141169691Skan        static void
142169691Skan        _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
143169691Skan        {
144169691Skan	  for (; __k1 != __k2; ++__k1, ++__p)
145169691Skan	    traits_type::assign(*__p, *__k1); // These types are off.
146169691Skan	}
147169691Skan
148169691Skan      static void
149169691Skan      _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
150169691Skan      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
151169691Skan
152169691Skan      static void
153169691Skan      _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
154169691Skan		    __const_sso_iterator __k2)
155169691Skan      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
156169691Skan
157169691Skan      static void
158169691Skan      _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
159169691Skan      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
160169691Skan
161169691Skan      static void
162169691Skan      _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
163169691Skan		    __const_rc_iterator __k2)
164169691Skan      { _S_copy_chars(__p, __k1.base(), __k2.base()); }
165169691Skan
166169691Skan      static void
167169691Skan      _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
168169691Skan      { _S_copy(__p, __k1, __k2 - __k1); }
169169691Skan
170169691Skan      static void
171169691Skan      _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
172169691Skan      { _S_copy(__p, __k1, __k2 - __k1); }
173169691Skan    };
174169691Skan
175169691Skan_GLIBCXX_END_NAMESPACE
176169691Skan
177169691Skan#endif /* _VSTRING_UTIL_H */
178