libstdc++
vstring_util.h
Go to the documentation of this file.
00001 // Versatile string utility -*- C++ -*-
00002 
00003 // Copyright (C) 2005-2015 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 is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{ext/vstring.h}
00028  */
00029 
00030 #ifndef _VSTRING_UTIL_H
00031 #define _VSTRING_UTIL_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <ext/vstring_fwd.h>
00036 #include <debug/debug.h>
00037 #include <bits/stl_function.h>  // For less
00038 #include <bits/functexcept.h>
00039 #include <bits/localefwd.h>
00040 #include <bits/ostream_insert.h>
00041 #include <bits/stl_iterator.h>
00042 #include <ext/numeric_traits.h>
00043 #include <bits/move.h>
00044 #include <bits/range_access.h>
00045 
00046 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00047 {
00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00049 
00050   template<typename _CharT, typename _Traits, typename _Alloc>
00051     struct __vstring_utility
00052     {
00053       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
00054 
00055       typedef _Traits                                       traits_type;
00056       typedef typename _Traits::char_type                   value_type;
00057       typedef typename _CharT_alloc_type::size_type         size_type;
00058       typedef typename _CharT_alloc_type::difference_type   difference_type;
00059       typedef typename _CharT_alloc_type::pointer           pointer;
00060       typedef typename _CharT_alloc_type::const_pointer     const_pointer;
00061 
00062       // For __sso_string.
00063       typedef __gnu_cxx::
00064       __normal_iterator<pointer, __gnu_cxx::
00065 			__versa_string<_CharT, _Traits, _Alloc,
00066                                        __sso_string_base> >
00067         __sso_iterator;
00068       typedef __gnu_cxx::
00069       __normal_iterator<const_pointer, __gnu_cxx::
00070 			__versa_string<_CharT, _Traits, _Alloc,
00071                                        __sso_string_base> >
00072         __const_sso_iterator;
00073 
00074       // For __rc_string.
00075       typedef __gnu_cxx::
00076       __normal_iterator<pointer, __gnu_cxx::
00077 			__versa_string<_CharT, _Traits, _Alloc,
00078                                        __rc_string_base> >
00079         __rc_iterator;
00080       typedef __gnu_cxx::
00081       __normal_iterator<const_pointer, __gnu_cxx::
00082 			__versa_string<_CharT, _Traits, _Alloc,
00083                                        __rc_string_base> >
00084         __const_rc_iterator;
00085 
00086       // NB:  When the allocator is empty, deriving from it saves space
00087       // (http://www.cantrip.org/emptyopt.html).
00088       template<typename _Alloc1>
00089         struct _Alloc_hider
00090         : public _Alloc1
00091         {
00092           _Alloc_hider(_CharT* __ptr)
00093           : _Alloc1(), _M_p(__ptr) { }
00094 
00095           _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
00096           : _Alloc1(__a), _M_p(__ptr) { }
00097 
00098           _CharT*  _M_p; // The actual data.
00099         };
00100 
00101       // When __n = 1 way faster than the general multichar
00102       // traits_type::copy/move/assign.
00103       static void
00104       _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
00105       {
00106         if (__n == 1)
00107           traits_type::assign(*__d, *__s);
00108         else
00109           traits_type::copy(__d, __s, __n);
00110       }
00111 
00112       static void
00113       _S_move(_CharT* __d, const _CharT* __s, size_type __n)
00114       {
00115         if (__n == 1)
00116           traits_type::assign(*__d, *__s);
00117         else
00118           traits_type::move(__d, __s, __n);
00119       }
00120 
00121       static void
00122       _S_assign(_CharT* __d, size_type __n, _CharT __c)
00123       {
00124         if (__n == 1)
00125           traits_type::assign(*__d, __c);
00126         else
00127           traits_type::assign(__d, __n, __c);
00128       }
00129 
00130       // _S_copy_chars is a separate template to permit specialization
00131       // to optimize for the common case of pointers as iterators.
00132       template<typename _Iterator>
00133         static void
00134         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
00135         {
00136           for (; __k1 != __k2; ++__k1, ++__p)
00137             traits_type::assign(*__p, *__k1); // These types are off.
00138         }
00139 
00140       static void
00141       _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
00142       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00143 
00144       static void
00145       _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
00146                     __const_sso_iterator __k2)
00147       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00148 
00149       static void
00150       _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
00151       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00152 
00153       static void
00154       _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
00155                     __const_rc_iterator __k2)
00156       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00157 
00158       static void
00159       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
00160       { _S_copy(__p, __k1, __k2 - __k1); }
00161 
00162       static void
00163       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
00164       { _S_copy(__p, __k1, __k2 - __k1); }
00165 
00166       static int
00167       _S_compare(size_type __n1, size_type __n2)
00168       {
00169         const difference_type __d = difference_type(__n1 - __n2);
00170 
00171         if (__d > __numeric_traits_integer<int>::__max)
00172           return __numeric_traits_integer<int>::__max;
00173         else if (__d < __numeric_traits_integer<int>::__min)
00174           return __numeric_traits_integer<int>::__min;
00175         else
00176           return int(__d);
00177       }
00178     };
00179 
00180 _GLIBCXX_END_NAMESPACE_VERSION
00181 } // namespace
00182 
00183 #endif /* _VSTRING_UTIL_H */