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
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
;
54 // The maximum number of individual char_type elements of an
55 // individual string is determined by _S_max_size. This is the
56 // value that will be returned by max_size(). (Whereas npos
57 // is the maximum number of bytes the allocator can allocate.)
58 // If one was to divvy up the theoretical largest size string,
59 // with a terminating character and m _CharT elements, it'd
61 // npos = m * sizeof(_CharT) + sizeof(_CharT)
63 // m = npos / sizeof(_CharT) - 1
64 // In addition, this implementation halfs this amount.
65 enum { _S_max_size
= (((static_cast<size_type
>(-1)
66 / sizeof(_CharT
)) - 1) / 2) };
68 // Data Members (private):
69 typename
_Util_Base::template _Alloc_hider
<_CharT_alloc_type
>
71 size_type _M_string_length
;
73 enum { _S_local_capacity
= 15 };
77 _CharT _M_local_data
[_S_local_capacity
+ 1];
78 size_type _M_allocated_capacity
;
83 { _M_dataplus
._M_p
= __p
; }
86 _M_length(size_type __length
)
87 { _M_string_length
= __length
; }
90 _M_capacity(size_type __capacity
)
91 { _M_allocated_capacity
= __capacity
; }
95 { return _M_data() == _M_local_data
; }
99 _M_create(size_type
&, size_type
);
105 _M_destroy(_M_allocated_capacity
);
109 _M_destroy(size_type
) throw();
111 // _M_construct_aux is used to implement the 21.3.1 para 15 which
112 // requires special behaviour if _InIterator is an integral type
113 template<typename _InIterator
>
115 _M_construct_aux(_InIterator __beg
, _InIterator __end
, __false_type
)
117 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
118 _M_construct(__beg
, __end
, _Tag());
121 template<typename _InIterator
>
123 _M_construct_aux(_InIterator __beg
, _InIterator __end
, __true_type
)
124 { _M_construct(static_cast<size_type
>(__beg
),
125 static_cast<value_type
>(__end
)); }
127 template<typename _InIterator
>
129 _M_construct(_InIterator __beg
, _InIterator __end
)
131 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
132 _M_construct_aux(__beg
, __end
, _Integral());
135 // For Input Iterators, used in istreambuf_iterators, etc.
136 template<typename _InIterator
>
138 _M_construct(_InIterator __beg
, _InIterator __end
,
139 std::input_iterator_tag
);
141 // For forward_iterators up to random_access_iterators, used for
142 // string::iterator, _CharT*, etc.
143 template<typename _FwdIterator
>
145 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
146 std::forward_iterator_tag
);
149 _M_construct(size_type __req
, _CharT __c
);
154 { return size_type(_S_max_size
); }
158 { return _M_dataplus
._M_p
; }
162 { return _M_string_length
; }
167 return _M_is_local() ? size_type(_S_local_capacity
)
168 : _M_allocated_capacity
;
182 _M_set_length(size_type __n
)
185 traits_type::assign(_M_data()[__n
], _CharT());
189 : _M_dataplus(_Alloc(), _M_local_data
)
190 { _M_set_length(0); }
192 __sso_string_base(const _Alloc
& __a
);
194 __sso_string_base(const __sso_string_base
& __rcs
);
196 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
);
198 template<typename _InputIterator
>
199 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
207 { return _M_dataplus
; }
209 const _CharT_alloc_type
&
210 _M_get_allocator() const
211 { return _M_dataplus
; }
214 _M_swap(__sso_string_base
& __rcs
);
217 _M_assign(const __sso_string_base
& __rcs
);
220 _M_reserve(size_type __res
);
223 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
227 _M_erase(size_type __pos
, size_type __n
);
231 { _M_set_length(0); }
234 _M_compare(const __sso_string_base
&) const
238 template<typename _CharT
, typename _Traits
, typename _Alloc
>
240 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
241 _M_destroy(size_type __size
) throw()
242 { _M_dataplus
._CharT_alloc_type::deallocate(_M_data(), __size
+ 1); }
244 template<typename _CharT
, typename _Traits
, typename _Alloc
>
246 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
247 _M_swap(__sso_string_base
& __rcs
)
249 // NB: Implement Option 3 of DR 431 (see N1599).
250 std::__alloc_swap
<_CharT_alloc_type
>::_S_do_it(_M_get_allocator(),
251 __rcs
._M_get_allocator());
254 if (__rcs
._M_is_local())
256 if (_M_length() && __rcs
._M_length())
258 _CharT __tmp_data
[_S_local_capacity
+ 1];
259 traits_type::copy(__tmp_data
, __rcs
._M_local_data
,
260 _S_local_capacity
+ 1);
261 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
262 _S_local_capacity
+ 1);
263 traits_type::copy(_M_local_data
, __tmp_data
,
264 _S_local_capacity
+ 1);
266 else if (__rcs
._M_length())
268 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
269 _S_local_capacity
+ 1);
270 _M_length(__rcs
._M_length());
271 __rcs
._M_set_length(0);
274 else if (_M_length())
276 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
277 _S_local_capacity
+ 1);
278 __rcs
._M_length(_M_length());
285 const size_type __tmp_capacity
= __rcs
._M_allocated_capacity
;
286 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
287 _S_local_capacity
+ 1);
288 _M_data(__rcs
._M_data());
289 __rcs
._M_data(__rcs
._M_local_data
);
290 _M_capacity(__tmp_capacity
);
294 const size_type __tmp_capacity
= _M_allocated_capacity
;
295 if (__rcs
._M_is_local())
297 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
298 _S_local_capacity
+ 1);
299 __rcs
._M_data(_M_data());
300 _M_data(_M_local_data
);
304 _CharT
* __tmp_ptr
= _M_data();
305 _M_data(__rcs
._M_data());
306 __rcs
._M_data(__tmp_ptr
);
307 _M_capacity(__rcs
._M_allocated_capacity
);
309 __rcs
._M_capacity(__tmp_capacity
);
312 const size_type __tmp_length
= _M_length();
313 _M_length(__rcs
._M_length());
314 __rcs
._M_length(__tmp_length
);
317 template<typename _CharT
, typename _Traits
, typename _Alloc
>
319 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
320 _M_create(size_type
& __capacity
, size_type __old_capacity
)
322 // _GLIBCXX_RESOLVE_LIB_DEFECTS
323 // 83. String::npos vs. string::max_size()
324 if (__capacity
> size_type(_S_max_size
))
325 std::__throw_length_error(__N("__sso_string_base::_M_create"));
327 // The below implements an exponential growth policy, necessary to
328 // meet amortized linear time requirements of the library: see
329 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
330 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
332 __capacity
= 2 * __old_capacity
;
333 // Never allocate a string bigger than _S_max_size.
334 if (__capacity
> size_type(_S_max_size
))
335 __capacity
= size_type(_S_max_size
);
338 // NB: Need an array of char_type[__capacity], plus a terminating
339 // null char_type() element.
340 return _M_dataplus
._CharT_alloc_type::allocate(__capacity
+ 1);
343 template<typename _CharT
, typename _Traits
, typename _Alloc
>
344 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
345 __sso_string_base(const _Alloc
& __a
)
346 : _M_dataplus(__a
, _M_local_data
)
347 { _M_set_length(0); }
349 template<typename _CharT
, typename _Traits
, typename _Alloc
>
350 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
351 __sso_string_base(const __sso_string_base
& __rcs
)
352 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
353 { _M_construct(__rcs
._M_data(), __rcs
._M_data() + __rcs
._M_length()); }
355 template<typename _CharT
, typename _Traits
, typename _Alloc
>
356 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
357 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
358 : _M_dataplus(__a
, _M_local_data
)
359 { _M_construct(__n
, __c
); }
361 template<typename _CharT
, typename _Traits
, typename _Alloc
>
362 template<typename _InputIterator
>
363 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
364 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
366 : _M_dataplus(__a
, _M_local_data
)
367 { _M_construct(__beg
, __end
); }
369 // NB: This is the special case for Input Iterators, used in
370 // istreambuf_iterators, etc.
371 // Input Iterators have a cost structure very different from
372 // pointers, calling for a different coding style.
373 template<typename _CharT
, typename _Traits
, typename _Alloc
>
374 template<typename _InIterator
>
376 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
377 _M_construct(_InIterator __beg
, _InIterator __end
,
378 std::input_iterator_tag
)
381 size_type __capacity
= size_type(_S_local_capacity
);
383 while (__beg
!= __end
&& __len
< __capacity
)
385 _M_data()[__len
++] = *__beg
;
391 while (__beg
!= __end
)
393 if (__len
== __capacity
)
395 // Allocate more space.
396 __capacity
= __len
+ 1;
397 _CharT
* __another
= _M_create(__capacity
, __len
);
398 _S_copy(__another
, _M_data(), __len
);
401 _M_capacity(__capacity
);
403 _M_data()[__len
++] = *__beg
;
410 __throw_exception_again
;
413 _M_set_length(__len
);
416 template<typename _CharT
, typename _Traits
, typename _Alloc
>
417 template<typename _InIterator
>
419 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
420 _M_construct(_InIterator __beg
, _InIterator __end
,
421 std::forward_iterator_tag
)
423 // NB: Not required, but considered best practice.
424 if (__builtin_expect(_S_is_null_pointer(__beg
) && __beg
!= __end
, 0))
425 std::__throw_logic_error(__N("__sso_string_base::"
426 "_M_construct NULL not valid"));
428 size_type __dnew
= static_cast<size_type
>(std::distance(__beg
, __end
));
430 if (__dnew
> size_type(_S_local_capacity
))
432 _M_data(_M_create(__dnew
, size_type(0)));
436 // Check for out_of_range and length_error exceptions.
438 { _S_copy_chars(_M_data(), __beg
, __end
); }
442 __throw_exception_again
;
445 _M_set_length(__dnew
);
448 template<typename _CharT
, typename _Traits
, typename _Alloc
>
450 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
451 _M_construct(size_type __n
, _CharT __c
)
453 if (__n
> size_type(_S_local_capacity
))
455 _M_data(_M_create(__n
, size_type(0)));
460 _S_assign(_M_data(), __n
, __c
);
465 template<typename _CharT
, typename _Traits
, typename _Alloc
>
467 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
468 _M_assign(const __sso_string_base
& __rcs
)
472 const size_type __rsize
= __rcs
._M_length();
473 const size_type __capacity
= _M_capacity();
475 if (__rsize
> __capacity
)
477 size_type __new_capacity
= __rsize
;
478 _CharT
* __tmp
= _M_create(__new_capacity
, __capacity
);
481 _M_capacity(__new_capacity
);
485 _S_copy(_M_data(), __rcs
._M_data(), __rsize
);
487 _M_set_length(__rsize
);
491 template<typename _CharT
, typename _Traits
, typename _Alloc
>
493 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
494 _M_reserve(size_type __res
)
496 // Make sure we don't shrink below the current size.
497 if (__res
< _M_length())
500 const size_type __capacity
= _M_capacity();
501 if (__res
!= __capacity
)
503 if (__res
> __capacity
504 || __res
> size_type(_S_local_capacity
))
506 _CharT
* __tmp
= _M_create(__res
, __capacity
);
507 _S_copy(__tmp
, _M_data(), _M_length() + 1);
512 else if (!_M_is_local())
514 _S_copy(_M_local_data
, _M_data(), _M_length() + 1);
515 _M_destroy(__capacity
);
516 _M_data(_M_local_data
);
521 template<typename _CharT
, typename _Traits
, typename _Alloc
>
523 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
524 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
525 const size_type __len2
)
527 const size_type __how_much
= _M_length() - __pos
- __len1
;
529 size_type __new_capacity
= _M_length() + __len2
- __len1
;
530 _CharT
* __r
= _M_create(__new_capacity
, _M_capacity());
533 _S_copy(__r
, _M_data(), __pos
);
535 _S_copy(__r
+ __pos
, __s
, __len2
);
537 _S_copy(__r
+ __pos
+ __len2
,
538 _M_data() + __pos
+ __len1
, __how_much
);
542 _M_capacity(__new_capacity
);
545 template<typename _CharT
, typename _Traits
, typename _Alloc
>
547 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
548 _M_erase(size_type __pos
, size_type __n
)
550 const size_type __how_much
= _M_length() - __pos
- __n
;
552 if (__how_much
&& __n
)
553 _S_move(_M_data() + __pos
, _M_data() + __pos
+ __n
,
556 _M_set_length(_M_length() - __n
);
561 __sso_string_base
<char, std::char_traits
<char>,
562 std::allocator
<char> >::
563 _M_compare(const __sso_string_base
& __rcs
) const
572 __sso_string_base
<wchar_t, std::char_traits
<wchar_t>,
573 std::allocator
<wchar_t> >::
574 _M_compare(const __sso_string_base
& __rcs
) const
580 } // namespace __gnu_cxx
582 #endif /* _SSO_STRING_BASE_H */