1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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)
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.
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,
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.
30 /** @file ext/sso_string_base.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.
36 #ifndef _SSO_STRING_BASE_H
37 #define _SSO_STRING_BASE_H 1
39 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
41 template<typename _CharT
, typename _Traits
, typename _Alloc
>
42 class __sso_string_base
43 : protected __vstring_utility
<_CharT
, _Traits
, _Alloc
>
46 typedef _Traits traits_type
;
47 typedef typename
_Traits::char_type value_type
;
49 typedef __vstring_utility
<_CharT
, _Traits
, _Alloc
> _Util_Base
;
50 typedef typename
_Util_Base::_CharT_alloc_type _CharT_alloc_type
;
51 typedef typename
_CharT_alloc_type::size_type size_type
;
55 typename
_Util_Base::template _Alloc_hider
<_CharT_alloc_type
>
57 size_type _M_string_length
;
59 enum { _S_local_capacity
= 15 };
63 _CharT _M_local_data
[_S_local_capacity
+ 1];
64 size_type _M_allocated_capacity
;
69 { _M_dataplus
._M_p
= __p
; }
72 _M_length(size_type __length
)
73 { _M_string_length
= __length
; }
76 _M_capacity(size_type __capacity
)
77 { _M_allocated_capacity
= __capacity
; }
81 { return _M_data() == _M_local_data
; }
85 _M_create(size_type
&, size_type
);
91 _M_destroy(_M_allocated_capacity
);
95 _M_destroy(size_type __size
) throw()
96 { _M_get_allocator().deallocate(_M_data(), __size
+ 1); }
98 // _M_construct_aux is used to implement the 21.3.1 para 15 which
99 // requires special behaviour if _InIterator is an integral type
100 template<typename _InIterator
>
102 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
105 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
106 _M_construct(__beg
, __end
, _Tag());
109 template<typename _InIterator
>
111 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
113 { _M_construct(static_cast<size_type
>(__beg
),
114 static_cast<value_type
>(__end
)); }
116 template<typename _InIterator
>
118 _M_construct(_InIterator __beg
, _InIterator __end
)
120 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
121 _M_construct_aux(__beg
, __end
, _Integral());
124 // For Input Iterators, used in istreambuf_iterators, etc.
125 template<typename _InIterator
>
127 _M_construct(_InIterator __beg
, _InIterator __end
,
128 std::input_iterator_tag
);
130 // For forward_iterators up to random_access_iterators, used for
131 // string::iterator, _CharT*, etc.
132 template<typename _FwdIterator
>
134 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
135 std::forward_iterator_tag
);
138 _M_construct(size_type __req
, _CharT __c
);
143 { return (_M_get_allocator().max_size() - 1) / 2; }
147 { return _M_dataplus
._M_p
; }
151 { return _M_string_length
; }
156 return _M_is_local() ? size_type(_S_local_capacity
)
157 : _M_allocated_capacity
;
171 _M_set_length(size_type __n
)
174 traits_type::assign(_M_data()[__n
], _CharT());
178 : _M_dataplus(_Alloc(), _M_local_data
)
179 { _M_set_length(0); }
181 __sso_string_base(const _Alloc
& __a
);
183 __sso_string_base(const __sso_string_base
& __rcs
);
185 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
);
187 template<typename _InputIterator
>
188 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
196 { return _M_dataplus
; }
198 const _CharT_alloc_type
&
199 _M_get_allocator() const
200 { return _M_dataplus
; }
203 _M_swap(__sso_string_base
& __rcs
);
206 _M_assign(const __sso_string_base
& __rcs
);
209 _M_reserve(size_type __res
);
212 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
216 _M_erase(size_type __pos
, size_type __n
);
220 { _M_set_length(0); }
223 _M_compare(const __sso_string_base
&) const
227 template<typename _CharT
, typename _Traits
, typename _Alloc
>
229 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
230 _M_swap(__sso_string_base
& __rcs
)
232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
233 // 431. Swapping containers with unequal allocators.
234 std::__alloc_swap
<_CharT_alloc_type
>::_S_do_it(_M_get_allocator(),
235 __rcs
._M_get_allocator());
238 if (__rcs
._M_is_local())
240 if (_M_length() && __rcs
._M_length())
242 _CharT __tmp_data
[_S_local_capacity
+ 1];
243 traits_type::copy(__tmp_data
, __rcs
._M_local_data
,
244 _S_local_capacity
+ 1);
245 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
246 _S_local_capacity
+ 1);
247 traits_type::copy(_M_local_data
, __tmp_data
,
248 _S_local_capacity
+ 1);
250 else if (__rcs
._M_length())
252 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
253 _S_local_capacity
+ 1);
254 _M_length(__rcs
._M_length());
255 __rcs
._M_set_length(0);
258 else if (_M_length())
260 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
261 _S_local_capacity
+ 1);
262 __rcs
._M_length(_M_length());
269 const size_type __tmp_capacity
= __rcs
._M_allocated_capacity
;
270 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
271 _S_local_capacity
+ 1);
272 _M_data(__rcs
._M_data());
273 __rcs
._M_data(__rcs
._M_local_data
);
274 _M_capacity(__tmp_capacity
);
278 const size_type __tmp_capacity
= _M_allocated_capacity
;
279 if (__rcs
._M_is_local())
281 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
282 _S_local_capacity
+ 1);
283 __rcs
._M_data(_M_data());
284 _M_data(_M_local_data
);
288 _CharT
* __tmp_ptr
= _M_data();
289 _M_data(__rcs
._M_data());
290 __rcs
._M_data(__tmp_ptr
);
291 _M_capacity(__rcs
._M_allocated_capacity
);
293 __rcs
._M_capacity(__tmp_capacity
);
296 const size_type __tmp_length
= _M_length();
297 _M_length(__rcs
._M_length());
298 __rcs
._M_length(__tmp_length
);
301 template<typename _CharT
, typename _Traits
, typename _Alloc
>
303 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
304 _M_create(size_type
& __capacity
, size_type __old_capacity
)
306 // _GLIBCXX_RESOLVE_LIB_DEFECTS
307 // 83. String::npos vs. string::max_size()
308 if (__capacity
> _M_max_size())
309 std::__throw_length_error(__N("__sso_string_base::_M_create"));
311 // The below implements an exponential growth policy, necessary to
312 // meet amortized linear time requirements of the library: see
313 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
314 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
316 __capacity
= 2 * __old_capacity
;
317 // Never allocate a string bigger than max_size.
318 if (__capacity
> _M_max_size())
319 __capacity
= _M_max_size();
322 // NB: Need an array of char_type[__capacity], plus a terminating
323 // null char_type() element.
324 return _M_get_allocator().allocate(__capacity
+ 1);
327 template<typename _CharT
, typename _Traits
, typename _Alloc
>
328 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
329 __sso_string_base(const _Alloc
& __a
)
330 : _M_dataplus(__a
, _M_local_data
)
331 { _M_set_length(0); }
333 template<typename _CharT
, typename _Traits
, typename _Alloc
>
334 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
335 __sso_string_base(const __sso_string_base
& __rcs
)
336 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
337 { _M_construct(__rcs
._M_data(), __rcs
._M_data() + __rcs
._M_length()); }
339 template<typename _CharT
, typename _Traits
, typename _Alloc
>
340 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
341 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
342 : _M_dataplus(__a
, _M_local_data
)
343 { _M_construct(__n
, __c
); }
345 template<typename _CharT
, typename _Traits
, typename _Alloc
>
346 template<typename _InputIterator
>
347 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
348 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
350 : _M_dataplus(__a
, _M_local_data
)
351 { _M_construct(__beg
, __end
); }
353 // NB: This is the special case for Input Iterators, used in
354 // istreambuf_iterators, etc.
355 // Input Iterators have a cost structure very different from
356 // pointers, calling for a different coding style.
357 template<typename _CharT
, typename _Traits
, typename _Alloc
>
358 template<typename _InIterator
>
360 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
361 _M_construct(_InIterator __beg
, _InIterator __end
,
362 std::input_iterator_tag
)
365 size_type __capacity
= size_type(_S_local_capacity
);
367 while (__beg
!= __end
&& __len
< __capacity
)
369 _M_data()[__len
++] = *__beg
;
375 while (__beg
!= __end
)
377 if (__len
== __capacity
)
379 // Allocate more space.
380 __capacity
= __len
+ 1;
381 _CharT
* __another
= _M_create(__capacity
, __len
);
382 _S_copy(__another
, _M_data(), __len
);
385 _M_capacity(__capacity
);
387 _M_data()[__len
++] = *__beg
;
394 __throw_exception_again
;
397 _M_set_length(__len
);
400 template<typename _CharT
, typename _Traits
, typename _Alloc
>
401 template<typename _InIterator
>
403 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
404 _M_construct(_InIterator __beg
, _InIterator __end
,
405 std::forward_iterator_tag
)
407 // NB: Not required, but considered best practice.
408 if (__builtin_expect(_S_is_null_pointer(__beg
) && __beg
!= __end
, 0))
409 std::__throw_logic_error(__N("__sso_string_base::"
410 "_M_construct NULL not valid"));
412 size_type __dnew
= static_cast<size_type
>(std::distance(__beg
, __end
));
414 if (__dnew
> size_type(_S_local_capacity
))
416 _M_data(_M_create(__dnew
, size_type(0)));
420 // Check for out_of_range and length_error exceptions.
422 { _S_copy_chars(_M_data(), __beg
, __end
); }
426 __throw_exception_again
;
429 _M_set_length(__dnew
);
432 template<typename _CharT
, typename _Traits
, typename _Alloc
>
434 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
435 _M_construct(size_type __n
, _CharT __c
)
437 if (__n
> size_type(_S_local_capacity
))
439 _M_data(_M_create(__n
, size_type(0)));
444 _S_assign(_M_data(), __n
, __c
);
449 template<typename _CharT
, typename _Traits
, typename _Alloc
>
451 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
452 _M_assign(const __sso_string_base
& __rcs
)
456 const size_type __rsize
= __rcs
._M_length();
457 const size_type __capacity
= _M_capacity();
459 if (__rsize
> __capacity
)
461 size_type __new_capacity
= __rsize
;
462 _CharT
* __tmp
= _M_create(__new_capacity
, __capacity
);
465 _M_capacity(__new_capacity
);
469 _S_copy(_M_data(), __rcs
._M_data(), __rsize
);
471 _M_set_length(__rsize
);
475 template<typename _CharT
, typename _Traits
, typename _Alloc
>
477 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
478 _M_reserve(size_type __res
)
480 // Make sure we don't shrink below the current size.
481 if (__res
< _M_length())
484 const size_type __capacity
= _M_capacity();
485 if (__res
!= __capacity
)
487 if (__res
> __capacity
488 || __res
> size_type(_S_local_capacity
))
490 _CharT
* __tmp
= _M_create(__res
, __capacity
);
491 _S_copy(__tmp
, _M_data(), _M_length() + 1);
496 else if (!_M_is_local())
498 _S_copy(_M_local_data
, _M_data(), _M_length() + 1);
499 _M_destroy(__capacity
);
500 _M_data(_M_local_data
);
505 template<typename _CharT
, typename _Traits
, typename _Alloc
>
507 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
508 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
509 const size_type __len2
)
511 const size_type __how_much
= _M_length() - __pos
- __len1
;
513 size_type __new_capacity
= _M_length() + __len2
- __len1
;
514 _CharT
* __r
= _M_create(__new_capacity
, _M_capacity());
517 _S_copy(__r
, _M_data(), __pos
);
519 _S_copy(__r
+ __pos
, __s
, __len2
);
521 _S_copy(__r
+ __pos
+ __len2
,
522 _M_data() + __pos
+ __len1
, __how_much
);
526 _M_capacity(__new_capacity
);
529 template<typename _CharT
, typename _Traits
, typename _Alloc
>
531 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
532 _M_erase(size_type __pos
, size_type __n
)
534 const size_type __how_much
= _M_length() - __pos
- __n
;
536 if (__how_much
&& __n
)
537 _S_move(_M_data() + __pos
, _M_data() + __pos
+ __n
,
540 _M_set_length(_M_length() - __n
);
545 __sso_string_base
<char, std::char_traits
<char>,
546 std::allocator
<char> >::
547 _M_compare(const __sso_string_base
& __rcs
) const
554 #ifdef _GLIBCXX_USE_WCHAR_T
557 __sso_string_base
<wchar_t, std::char_traits
<wchar_t>,
558 std::allocator
<wchar_t> >::
559 _M_compare(const __sso_string_base
& __rcs
) const
567 _GLIBCXX_END_NAMESPACE
569 #endif /* _SSO_STRING_BASE_H */