# vstring_util.h

Go to the documentation of this file.

    00001 // Versatile string utility -*- C++ -*-
    00002
    00003 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 3, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // Under Section 7 of GPL version 3, you are granted additional
    00017 // permissions described in the GCC Runtime Library Exception, version
    00018 // 3.1, as published by the Free Software Foundation.
    00019
    00020 // You should have received a copy of the GNU General Public License and
    00021 // a copy of the GCC Runtime Library Exception along with this program;
    00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00023 // <http://www.gnu.org/licenses/>.
    00024
    00025 /** @file ext/vstring_util.h
    00026  *  This file is a GNU extension to the Standard C++ Library.
    00027  *  This is an internal header file, included by other library headers.
    00028  *  You should not attempt to use it directly.
    00029  */
    00030
    00031 #ifndef _VSTRING_UTIL_H
    00032 #define _VSTRING_UTIL_H 1
    00033
    00034 #pragma GCC system_header
    00035
    00036 #include <ext/vstring_fwd.h>
    00037 #include <debug/debug.h>
    00038 #include <bits/stl_function.h>  // For less
    00039 #include <bits/functexcept.h>
    00040 #include <bits/localefwd.h>
    00041 #include <bits/ostream_insert.h>
    00042 #include <bits/stl_iterator.h>
    00043 #include <ext/numeric_traits.h>
    00044 #include <bits/move.h>
    00045
    00046 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00047
    00048   template<typename _CharT, typename _Traits, typename _Alloc>
    00049     struct __vstring_utility
    00050     {
    00051       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
    00052
    00053       typedef _Traits                       traits_type;
    00054       typedef typename _Traits::char_type           value_type;
    00055       typedef typename _CharT_alloc_type::size_type     size_type;
    00056       typedef typename _CharT_alloc_type::difference_type   difference_type;
    00057       typedef typename _CharT_alloc_type::pointer       pointer;
    00058       typedef typename _CharT_alloc_type::const_pointer     const_pointer;
    00059
    00060       // For __sso_string.
    00061       typedef __gnu_cxx::
    00062       __normal_iterator<pointer, __gnu_cxx::
    00063 			__versa_string<_CharT, _Traits, _Alloc,
    00064                        __sso_string_base> >
    00065         __sso_iterator;
    00066       typedef __gnu_cxx::
    00067       __normal_iterator<const_pointer, __gnu_cxx::
    00068 			__versa_string<_CharT, _Traits, _Alloc,
    00069                        __sso_string_base> >
    00070         __const_sso_iterator;
    00071
    00072       // For __rc_string.
    00073       typedef __gnu_cxx::
    00074       __normal_iterator<pointer, __gnu_cxx::
    00075 			__versa_string<_CharT, _Traits, _Alloc,
    00076                        __rc_string_base> >
    00077         __rc_iterator;
    00078       typedef __gnu_cxx::
    00079       __normal_iterator<const_pointer, __gnu_cxx::
    00080 			__versa_string<_CharT, _Traits, _Alloc,
    00081                        __rc_string_base> >
    00082         __const_rc_iterator;
    00083
    00084       // NB:  When the allocator is empty, deriving from it saves space
    00085       // (http://www.cantrip.org/emptyopt.html).
    00086       template<typename _Alloc1>
    00087         struct _Alloc_hider
    00088     : public _Alloc1
    00089     {
    00090       _Alloc_hider(_CharT* __ptr)
    00091       : _Alloc1(), _M_p(__ptr) { }
    00092
    00093       _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
    00094       : _Alloc1(__a), _M_p(__ptr) { }
    00095
    00096       _CharT*  _M_p; // The actual data.
    00097     };
    00098
    00099       // When __n = 1 way faster than the general multichar
    00100       // traits_type::copy/move/assign.
    00101       static void
    00102       _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
    00103       {
    00104     if (__n == 1)
    00105       traits_type::assign(*__d, *__s);
    00106     else
    00107       traits_type::copy(__d, __s, __n);
    00108       }
    00109
    00110       static void
    00111       _S_move(_CharT* __d, const _CharT* __s, size_type __n)
    00112       {
    00113     if (__n == 1)
    00114       traits_type::assign(*__d, *__s);
    00115     else
    00116       traits_type::move(__d, __s, __n);
    00117       }
    00118
    00119       static void
    00120       _S_assign(_CharT* __d, size_type __n, _CharT __c)
    00121       {
    00122     if (__n == 1)
    00123       traits_type::assign(*__d, __c);
    00124     else
    00125       traits_type::assign(__d, __n, __c);
    00126       }
    00127
    00128       // _S_copy_chars is a separate template to permit specialization
    00129       // to optimize for the common case of pointers as iterators.
    00130       template<typename _Iterator>
    00131         static void
    00132         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
    00133         {
    00134       for (; __k1 != __k2; ++__k1, ++__p)
    00135         traits_type::assign(*__p, *__k1); // These types are off.
    00136     }
    00137
    00138       static void
    00139       _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
    00140       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
    00141
    00142       static void
    00143       _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
    00144             __const_sso_iterator __k2)
    00145       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
    00146
    00147       static void
    00148       _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
    00149       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
    00150
    00151       static void
    00152       _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
    00153             __const_rc_iterator __k2)
    00154       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
    00155
    00156       static void
    00157       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
    00158       { _S_copy(__p, __k1, __k2 - __k1); }
    00159
    00160       static void
    00161       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
    00162       { _S_copy(__p, __k1, __k2 - __k1); }
    00163
    00164       static int
    00165       _S_compare(size_type __n1, size_type __n2)
    00166       {
    00167     const difference_type __d = difference_type(__n1 - __n2);
    00168
    00169     if (__d > __numeric_traits_integer<int>::__max)
    00170       return __numeric_traits_integer<int>::__max;
    00171     else if (__d < __numeric_traits_integer<int>::__min)
    00172       return __numeric_traits_integer<int>::__min;
    00173     else
    00174       return int(__d);
    00175       }
    00176     };
    00177
    00178 _GLIBCXX_END_NAMESPACE
    00179
    00180 #endif /* _VSTRING_UTIL_H */