1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/sso_string_base.h
26 * This file is a GNU extension to the Standard C++ Library.
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
31 #ifndef _SSO_STRING_BASE_H
32 #define _SSO_STRING_BASE_H 1
34 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
36 template<typename _CharT
, typename _Traits
, typename _Alloc
>
37 class __sso_string_base
38 : protected __vstring_utility
<_CharT
, _Traits
, _Alloc
>
41 typedef _Traits traits_type
;
42 typedef typename
_Traits::char_type value_type
;
44 typedef __vstring_utility
<_CharT
, _Traits
, _Alloc
> _Util_Base
;
45 typedef typename
_Util_Base::_CharT_alloc_type _CharT_alloc_type
;
46 typedef typename
_CharT_alloc_type::size_type size_type
;
50 typename
_Util_Base::template _Alloc_hider
<_CharT_alloc_type
>
52 size_type _M_string_length
;
54 enum { _S_local_capacity
= 15 };
58 _CharT _M_local_data
[_S_local_capacity
+ 1];
59 size_type _M_allocated_capacity
;
64 { _M_dataplus
._M_p
= __p
; }
67 _M_length(size_type __length
)
68 { _M_string_length
= __length
; }
71 _M_capacity(size_type __capacity
)
72 { _M_allocated_capacity
= __capacity
; }
76 { return _M_data() == _M_local_data
; }
80 _M_create(size_type
&, size_type
);
86 _M_destroy(_M_allocated_capacity
);
90 _M_destroy(size_type __size
) throw()
91 { _M_get_allocator().deallocate(_M_data(), __size
+ 1); }
93 // _M_construct_aux is used to implement the 21.3.1 para 15 which
94 // requires special behaviour if _InIterator is an integral type
95 template<typename _InIterator
>
97 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
100 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
101 _M_construct(__beg
, __end
, _Tag());
104 // _GLIBCXX_RESOLVE_LIB_DEFECTS
105 // 438. Ambiguity in the "do the right thing" clause
106 template<typename _Integer
>
108 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
109 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
112 _M_construct_aux_2(size_type __req
, _CharT __c
)
113 { _M_construct(__req
, __c
); }
115 template<typename _InIterator
>
117 _M_construct(_InIterator __beg
, _InIterator __end
)
119 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
120 _M_construct_aux(__beg
, __end
, _Integral());
123 // For Input Iterators, used in istreambuf_iterators, etc.
124 template<typename _InIterator
>
126 _M_construct(_InIterator __beg
, _InIterator __end
,
127 std::input_iterator_tag
);
129 // For forward_iterators up to random_access_iterators, used for
130 // string::iterator, _CharT*, etc.
131 template<typename _FwdIterator
>
133 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
134 std::forward_iterator_tag
);
137 _M_construct(size_type __req
, _CharT __c
);
142 { return (_M_get_allocator().max_size() - 1) / 2; }
146 { return _M_dataplus
._M_p
; }
150 { return _M_string_length
; }
155 return _M_is_local() ? size_type(_S_local_capacity
)
156 : _M_allocated_capacity
;
170 _M_set_length(size_type __n
)
173 traits_type::assign(_M_data()[__n
], _CharT());
177 : _M_dataplus(_M_local_data
)
178 { _M_set_length(0); }
180 __sso_string_base(const _Alloc
& __a
);
182 __sso_string_base(const __sso_string_base
& __rcs
);
184 #ifdef __GXX_EXPERIMENTAL_CXX0X__
185 __sso_string_base(__sso_string_base
&& __rcs
);
188 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
);
190 template<typename _InputIterator
>
191 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
199 { return _M_dataplus
; }
201 const _CharT_alloc_type
&
202 _M_get_allocator() const
203 { return _M_dataplus
; }
206 _M_swap(__sso_string_base
& __rcs
);
209 _M_assign(const __sso_string_base
& __rcs
);
212 _M_reserve(size_type __res
);
215 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
219 _M_erase(size_type __pos
, size_type __n
);
223 { _M_set_length(0); }
226 _M_compare(const __sso_string_base
&) const
230 template<typename _CharT
, typename _Traits
, typename _Alloc
>
232 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
233 _M_swap(__sso_string_base
& __rcs
)
235 // _GLIBCXX_RESOLVE_LIB_DEFECTS
236 // 431. Swapping containers with unequal allocators.
237 std::__alloc_swap
<_CharT_alloc_type
>::_S_do_it(_M_get_allocator(),
238 __rcs
._M_get_allocator());
241 if (__rcs
._M_is_local())
243 if (_M_length() && __rcs
._M_length())
245 _CharT __tmp_data
[_S_local_capacity
+ 1];
246 traits_type::copy(__tmp_data
, __rcs
._M_local_data
,
247 _S_local_capacity
+ 1);
248 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
249 _S_local_capacity
+ 1);
250 traits_type::copy(_M_local_data
, __tmp_data
,
251 _S_local_capacity
+ 1);
253 else if (__rcs
._M_length())
255 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
256 _S_local_capacity
+ 1);
257 _M_length(__rcs
._M_length());
258 __rcs
._M_set_length(0);
261 else if (_M_length())
263 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
264 _S_local_capacity
+ 1);
265 __rcs
._M_length(_M_length());
272 const size_type __tmp_capacity
= __rcs
._M_allocated_capacity
;
273 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
274 _S_local_capacity
+ 1);
275 _M_data(__rcs
._M_data());
276 __rcs
._M_data(__rcs
._M_local_data
);
277 _M_capacity(__tmp_capacity
);
281 const size_type __tmp_capacity
= _M_allocated_capacity
;
282 if (__rcs
._M_is_local())
284 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
285 _S_local_capacity
+ 1);
286 __rcs
._M_data(_M_data());
287 _M_data(_M_local_data
);
291 _CharT
* __tmp_ptr
= _M_data();
292 _M_data(__rcs
._M_data());
293 __rcs
._M_data(__tmp_ptr
);
294 _M_capacity(__rcs
._M_allocated_capacity
);
296 __rcs
._M_capacity(__tmp_capacity
);
299 const size_type __tmp_length
= _M_length();
300 _M_length(__rcs
._M_length());
301 __rcs
._M_length(__tmp_length
);
304 template<typename _CharT
, typename _Traits
, typename _Alloc
>
306 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
307 _M_create(size_type
& __capacity
, size_type __old_capacity
)
309 // _GLIBCXX_RESOLVE_LIB_DEFECTS
310 // 83. String::npos vs. string::max_size()
311 if (__capacity
> _M_max_size())
312 std::__throw_length_error(__N("__sso_string_base::_M_create"));
314 // The below implements an exponential growth policy, necessary to
315 // meet amortized linear time requirements of the library: see
316 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
317 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
319 __capacity
= 2 * __old_capacity
;
320 // Never allocate a string bigger than max_size.
321 if (__capacity
> _M_max_size())
322 __capacity
= _M_max_size();
325 // NB: Need an array of char_type[__capacity], plus a terminating
326 // null char_type() element.
327 return _M_get_allocator().allocate(__capacity
+ 1);
330 template<typename _CharT
, typename _Traits
, typename _Alloc
>
331 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
332 __sso_string_base(const _Alloc
& __a
)
333 : _M_dataplus(__a
, _M_local_data
)
334 { _M_set_length(0); }
336 template<typename _CharT
, typename _Traits
, typename _Alloc
>
337 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
338 __sso_string_base(const __sso_string_base
& __rcs
)
339 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
340 { _M_construct(__rcs
._M_data(), __rcs
._M_data() + __rcs
._M_length()); }
342 #ifdef __GXX_EXPERIMENTAL_CXX0X__
343 template<typename _CharT
, typename _Traits
, typename _Alloc
>
344 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
345 __sso_string_base(__sso_string_base
&& __rcs
)
346 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
348 if (__rcs
._M_is_local())
350 if (__rcs
._M_length())
351 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
352 _S_local_capacity
+ 1);
356 _M_data(__rcs
._M_data());
357 _M_capacity(__rcs
._M_allocated_capacity
);
360 _M_length(__rcs
._M_length());
362 __rcs
._M_data(__rcs
._M_local_data
);
366 template<typename _CharT
, typename _Traits
, typename _Alloc
>
367 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
368 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
369 : _M_dataplus(__a
, _M_local_data
)
370 { _M_construct(__n
, __c
); }
372 template<typename _CharT
, typename _Traits
, typename _Alloc
>
373 template<typename _InputIterator
>
374 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
375 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
377 : _M_dataplus(__a
, _M_local_data
)
378 { _M_construct(__beg
, __end
); }
380 // NB: This is the special case for Input Iterators, used in
381 // istreambuf_iterators, etc.
382 // Input Iterators have a cost structure very different from
383 // pointers, calling for a different coding style.
384 template<typename _CharT
, typename _Traits
, typename _Alloc
>
385 template<typename _InIterator
>
387 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
388 _M_construct(_InIterator __beg
, _InIterator __end
,
389 std::input_iterator_tag
)
392 size_type __capacity
= size_type(_S_local_capacity
);
394 while (__beg
!= __end
&& __len
< __capacity
)
396 _M_data()[__len
++] = *__beg
;
402 while (__beg
!= __end
)
404 if (__len
== __capacity
)
406 // Allocate more space.
407 __capacity
= __len
+ 1;
408 _CharT
* __another
= _M_create(__capacity
, __len
);
409 _S_copy(__another
, _M_data(), __len
);
412 _M_capacity(__capacity
);
414 _M_data()[__len
++] = *__beg
;
421 __throw_exception_again
;
424 _M_set_length(__len
);
427 template<typename _CharT
, typename _Traits
, typename _Alloc
>
428 template<typename _InIterator
>
430 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
431 _M_construct(_InIterator __beg
, _InIterator __end
,
432 std::forward_iterator_tag
)
434 // NB: Not required, but considered best practice.
435 if (__is_null_pointer(__beg
) && __beg
!= __end
)
436 std::__throw_logic_error(__N("__sso_string_base::"
437 "_M_construct null not valid"));
439 size_type __dnew
= static_cast<size_type
>(std::distance(__beg
, __end
));
441 if (__dnew
> size_type(_S_local_capacity
))
443 _M_data(_M_create(__dnew
, size_type(0)));
447 // Check for out_of_range and length_error exceptions.
449 { _S_copy_chars(_M_data(), __beg
, __end
); }
453 __throw_exception_again
;
456 _M_set_length(__dnew
);
459 template<typename _CharT
, typename _Traits
, typename _Alloc
>
461 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
462 _M_construct(size_type __n
, _CharT __c
)
464 if (__n
> size_type(_S_local_capacity
))
466 _M_data(_M_create(__n
, size_type(0)));
471 _S_assign(_M_data(), __n
, __c
);
476 template<typename _CharT
, typename _Traits
, typename _Alloc
>
478 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
479 _M_assign(const __sso_string_base
& __rcs
)
483 const size_type __rsize
= __rcs
._M_length();
484 const size_type __capacity
= _M_capacity();
486 if (__rsize
> __capacity
)
488 size_type __new_capacity
= __rsize
;
489 _CharT
* __tmp
= _M_create(__new_capacity
, __capacity
);
492 _M_capacity(__new_capacity
);
496 _S_copy(_M_data(), __rcs
._M_data(), __rsize
);
498 _M_set_length(__rsize
);
502 template<typename _CharT
, typename _Traits
, typename _Alloc
>
504 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
505 _M_reserve(size_type __res
)
507 // Make sure we don't shrink below the current size.
508 if (__res
< _M_length())
511 const size_type __capacity
= _M_capacity();
512 if (__res
!= __capacity
)
514 if (__res
> __capacity
515 || __res
> size_type(_S_local_capacity
))
517 _CharT
* __tmp
= _M_create(__res
, __capacity
);
518 _S_copy(__tmp
, _M_data(), _M_length() + 1);
523 else if (!_M_is_local())
525 _S_copy(_M_local_data
, _M_data(), _M_length() + 1);
526 _M_destroy(__capacity
);
527 _M_data(_M_local_data
);
532 template<typename _CharT
, typename _Traits
, typename _Alloc
>
534 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
535 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
538 const size_type __how_much
= _M_length() - __pos
- __len1
;
540 size_type __new_capacity
= _M_length() + __len2
- __len1
;
541 _CharT
* __r
= _M_create(__new_capacity
, _M_capacity());
544 _S_copy(__r
, _M_data(), __pos
);
546 _S_copy(__r
+ __pos
, __s
, __len2
);
548 _S_copy(__r
+ __pos
+ __len2
,
549 _M_data() + __pos
+ __len1
, __how_much
);
553 _M_capacity(__new_capacity
);
556 template<typename _CharT
, typename _Traits
, typename _Alloc
>
558 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
559 _M_erase(size_type __pos
, size_type __n
)
561 const size_type __how_much
= _M_length() - __pos
- __n
;
563 if (__how_much
&& __n
)
564 _S_move(_M_data() + __pos
, _M_data() + __pos
+ __n
,
567 _M_set_length(_M_length() - __n
);
570 _GLIBCXX_END_NAMESPACE
572 #endif /* _SSO_STRING_BASE_H */