1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file ext/sso_string_base.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{ext/vstring.h}
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
)
238 // _GLIBCXX_RESOLVE_LIB_DEFECTS
239 // 431. Swapping containers with unequal allocators.
240 std::__alloc_swap
<_CharT_alloc_type
>::_S_do_it(_M_get_allocator(),
241 __rcs
._M_get_allocator());
244 if (__rcs
._M_is_local())
246 if (_M_length() && __rcs
._M_length())
248 _CharT __tmp_data
[_S_local_capacity
+ 1];
249 traits_type::copy(__tmp_data
, __rcs
._M_local_data
,
250 _S_local_capacity
+ 1);
251 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
252 _S_local_capacity
+ 1);
253 traits_type::copy(_M_local_data
, __tmp_data
,
254 _S_local_capacity
+ 1);
256 else if (__rcs
._M_length())
258 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
259 _S_local_capacity
+ 1);
260 _M_length(__rcs
._M_length());
261 __rcs
._M_set_length(0);
264 else if (_M_length())
266 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
267 _S_local_capacity
+ 1);
268 __rcs
._M_length(_M_length());
275 const size_type __tmp_capacity
= __rcs
._M_allocated_capacity
;
276 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
277 _S_local_capacity
+ 1);
278 _M_data(__rcs
._M_data());
279 __rcs
._M_data(__rcs
._M_local_data
);
280 _M_capacity(__tmp_capacity
);
284 const size_type __tmp_capacity
= _M_allocated_capacity
;
285 if (__rcs
._M_is_local())
287 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
288 _S_local_capacity
+ 1);
289 __rcs
._M_data(_M_data());
290 _M_data(_M_local_data
);
294 _CharT
* __tmp_ptr
= _M_data();
295 _M_data(__rcs
._M_data());
296 __rcs
._M_data(__tmp_ptr
);
297 _M_capacity(__rcs
._M_allocated_capacity
);
299 __rcs
._M_capacity(__tmp_capacity
);
302 const size_type __tmp_length
= _M_length();
303 _M_length(__rcs
._M_length());
304 __rcs
._M_length(__tmp_length
);
307 template<typename _CharT
, typename _Traits
, typename _Alloc
>
309 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
310 _M_create(size_type
& __capacity
, size_type __old_capacity
)
312 // _GLIBCXX_RESOLVE_LIB_DEFECTS
313 // 83. String::npos vs. string::max_size()
314 if (__capacity
> _M_max_size())
315 std::__throw_length_error(__N("__sso_string_base::_M_create"));
317 // The below implements an exponential growth policy, necessary to
318 // meet amortized linear time requirements of the library: see
319 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
320 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
322 __capacity
= 2 * __old_capacity
;
323 // Never allocate a string bigger than max_size.
324 if (__capacity
> _M_max_size())
325 __capacity
= _M_max_size();
328 // NB: Need an array of char_type[__capacity], plus a terminating
329 // null char_type() element.
330 return _M_get_allocator().allocate(__capacity
+ 1);
333 template<typename _CharT
, typename _Traits
, typename _Alloc
>
334 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
335 __sso_string_base(const _Alloc
& __a
)
336 : _M_dataplus(__a
, _M_local_data
)
337 { _M_set_length(0); }
339 template<typename _CharT
, typename _Traits
, typename _Alloc
>
340 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
341 __sso_string_base(const __sso_string_base
& __rcs
)
342 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
343 { _M_construct(__rcs
._M_data(), __rcs
._M_data() + __rcs
._M_length()); }
345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
346 template<typename _CharT
, typename _Traits
, typename _Alloc
>
347 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
348 __sso_string_base(__sso_string_base
&& __rcs
)
349 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
351 if (__rcs
._M_is_local())
353 if (__rcs
._M_length())
354 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
355 _S_local_capacity
+ 1);
359 _M_data(__rcs
._M_data());
360 _M_capacity(__rcs
._M_allocated_capacity
);
363 _M_length(__rcs
._M_length());
365 __rcs
._M_data(__rcs
._M_local_data
);
369 template<typename _CharT
, typename _Traits
, typename _Alloc
>
370 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
371 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
372 : _M_dataplus(__a
, _M_local_data
)
373 { _M_construct(__n
, __c
); }
375 template<typename _CharT
, typename _Traits
, typename _Alloc
>
376 template<typename _InputIterator
>
377 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
378 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
380 : _M_dataplus(__a
, _M_local_data
)
381 { _M_construct(__beg
, __end
); }
383 // NB: This is the special case for Input Iterators, used in
384 // istreambuf_iterators, etc.
385 // Input Iterators have a cost structure very different from
386 // pointers, calling for a different coding style.
387 template<typename _CharT
, typename _Traits
, typename _Alloc
>
388 template<typename _InIterator
>
390 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
391 _M_construct(_InIterator __beg
, _InIterator __end
,
392 std::input_iterator_tag
)
395 size_type __capacity
= size_type(_S_local_capacity
);
397 while (__beg
!= __end
&& __len
< __capacity
)
399 _M_data()[__len
++] = *__beg
;
405 while (__beg
!= __end
)
407 if (__len
== __capacity
)
409 // Allocate more space.
410 __capacity
= __len
+ 1;
411 _CharT
* __another
= _M_create(__capacity
, __len
);
412 _S_copy(__another
, _M_data(), __len
);
415 _M_capacity(__capacity
);
417 _M_data()[__len
++] = *__beg
;
424 __throw_exception_again
;
427 _M_set_length(__len
);
430 template<typename _CharT
, typename _Traits
, typename _Alloc
>
431 template<typename _InIterator
>
433 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
434 _M_construct(_InIterator __beg
, _InIterator __end
,
435 std::forward_iterator_tag
)
437 // NB: Not required, but considered best practice.
438 if (__is_null_pointer(__beg
) && __beg
!= __end
)
439 std::__throw_logic_error(__N("__sso_string_base::"
440 "_M_construct null not valid"));
442 size_type __dnew
= static_cast<size_type
>(std::distance(__beg
, __end
));
444 if (__dnew
> size_type(_S_local_capacity
))
446 _M_data(_M_create(__dnew
, size_type(0)));
450 // Check for out_of_range and length_error exceptions.
452 { _S_copy_chars(_M_data(), __beg
, __end
); }
456 __throw_exception_again
;
459 _M_set_length(__dnew
);
462 template<typename _CharT
, typename _Traits
, typename _Alloc
>
464 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
465 _M_construct(size_type __n
, _CharT __c
)
467 if (__n
> size_type(_S_local_capacity
))
469 _M_data(_M_create(__n
, size_type(0)));
474 _S_assign(_M_data(), __n
, __c
);
479 template<typename _CharT
, typename _Traits
, typename _Alloc
>
481 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
482 _M_assign(const __sso_string_base
& __rcs
)
486 const size_type __rsize
= __rcs
._M_length();
487 const size_type __capacity
= _M_capacity();
489 if (__rsize
> __capacity
)
491 size_type __new_capacity
= __rsize
;
492 _CharT
* __tmp
= _M_create(__new_capacity
, __capacity
);
495 _M_capacity(__new_capacity
);
499 _S_copy(_M_data(), __rcs
._M_data(), __rsize
);
501 _M_set_length(__rsize
);
505 template<typename _CharT
, typename _Traits
, typename _Alloc
>
507 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
508 _M_reserve(size_type __res
)
510 // Make sure we don't shrink below the current size.
511 if (__res
< _M_length())
514 const size_type __capacity
= _M_capacity();
515 if (__res
!= __capacity
)
517 if (__res
> __capacity
518 || __res
> size_type(_S_local_capacity
))
520 _CharT
* __tmp
= _M_create(__res
, __capacity
);
521 _S_copy(__tmp
, _M_data(), _M_length() + 1);
526 else if (!_M_is_local())
528 _S_copy(_M_local_data
, _M_data(), _M_length() + 1);
529 _M_destroy(__capacity
);
530 _M_data(_M_local_data
);
535 template<typename _CharT
, typename _Traits
, typename _Alloc
>
537 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
538 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
541 const size_type __how_much
= _M_length() - __pos
- __len1
;
543 size_type __new_capacity
= _M_length() + __len2
- __len1
;
544 _CharT
* __r
= _M_create(__new_capacity
, _M_capacity());
547 _S_copy(__r
, _M_data(), __pos
);
549 _S_copy(__r
+ __pos
, __s
, __len2
);
551 _S_copy(__r
+ __pos
+ __len2
,
552 _M_data() + __pos
+ __len1
, __how_much
);
556 _M_capacity(__new_capacity
);
559 template<typename _CharT
, typename _Traits
, typename _Alloc
>
561 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
562 _M_erase(size_type __pos
, size_type __n
)
564 const size_type __how_much
= _M_length() - __pos
- __n
;
566 if (__how_much
&& __n
)
567 _S_move(_M_data() + __pos
, _M_data() + __pos
+ __n
,
570 _M_set_length(_M_length() - __n
);
573 _GLIBCXX_END_NAMESPACE
575 #endif /* _SSO_STRING_BASE_H */