1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005-2014 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 is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ext/vstring.h}
30 #ifndef _SSO_STRING_BASE_H
31 #define _SSO_STRING_BASE_H 1
33 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
37 template<typename _CharT
, typename _Traits
, typename _Alloc
>
38 class __sso_string_base
39 : protected __vstring_utility
<_CharT
, _Traits
, _Alloc
>
42 typedef _Traits traits_type
;
43 typedef typename
_Traits::char_type value_type
;
45 typedef __vstring_utility
<_CharT
, _Traits
, _Alloc
> _Util_Base
;
46 typedef typename
_Util_Base::_CharT_alloc_type _CharT_alloc_type
;
47 typedef typename
_CharT_alloc_type::size_type size_type
;
51 typename
_Util_Base::template _Alloc_hider
<_CharT_alloc_type
>
53 size_type _M_string_length
;
55 enum { _S_local_capacity
= 15 };
59 _CharT _M_local_data
[_S_local_capacity
+ 1];
60 size_type _M_allocated_capacity
;
65 { _M_dataplus
._M_p
= __p
; }
68 _M_length(size_type __length
)
69 { _M_string_length
= __length
; }
72 _M_capacity(size_type __capacity
)
73 { _M_allocated_capacity
= __capacity
; }
77 { return _M_data() == _M_local_data
; }
81 _M_create(size_type
&, size_type
);
87 _M_destroy(_M_allocated_capacity
);
91 _M_destroy(size_type __size
) throw()
92 { _M_get_allocator().deallocate(_M_data(), __size
+ 1); }
94 // _M_construct_aux is used to implement the 21.3.1 para 15 which
95 // requires special behaviour if _InIterator is an integral type
96 template<typename _InIterator
>
98 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
101 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
102 _M_construct(__beg
, __end
, _Tag());
105 // _GLIBCXX_RESOLVE_LIB_DEFECTS
106 // 438. Ambiguity in the "do the right thing" clause
107 template<typename _Integer
>
109 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
110 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
113 _M_construct_aux_2(size_type __req
, _CharT __c
)
114 { _M_construct(__req
, __c
); }
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(_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 #if __cplusplus >= 201103L
186 __sso_string_base(__sso_string_base
&& __rcs
);
189 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
);
191 template<typename _InputIterator
>
192 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
200 { return _M_dataplus
; }
202 const _CharT_alloc_type
&
203 _M_get_allocator() const
204 { return _M_dataplus
; }
207 _M_swap(__sso_string_base
& __rcs
);
210 _M_assign(const __sso_string_base
& __rcs
);
213 _M_reserve(size_type __res
);
216 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
220 _M_erase(size_type __pos
, size_type __n
);
224 { _M_set_length(0); }
227 _M_compare(const __sso_string_base
&) const
231 template<typename _CharT
, typename _Traits
, typename _Alloc
>
233 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
234 _M_swap(__sso_string_base
& __rcs
)
239 // _GLIBCXX_RESOLVE_LIB_DEFECTS
240 // 431. Swapping containers with unequal allocators.
241 std::__alloc_swap
<_CharT_alloc_type
>::_S_do_it(_M_get_allocator(),
242 __rcs
._M_get_allocator());
245 if (__rcs
._M_is_local())
247 if (_M_length() && __rcs
._M_length())
249 _CharT __tmp_data
[_S_local_capacity
+ 1];
250 traits_type::copy(__tmp_data
, __rcs
._M_local_data
,
251 _S_local_capacity
+ 1);
252 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
253 _S_local_capacity
+ 1);
254 traits_type::copy(_M_local_data
, __tmp_data
,
255 _S_local_capacity
+ 1);
257 else if (__rcs
._M_length())
259 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
260 _S_local_capacity
+ 1);
261 _M_length(__rcs
._M_length());
262 __rcs
._M_set_length(0);
265 else if (_M_length())
267 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
268 _S_local_capacity
+ 1);
269 __rcs
._M_length(_M_length());
276 const size_type __tmp_capacity
= __rcs
._M_allocated_capacity
;
277 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
278 _S_local_capacity
+ 1);
279 _M_data(__rcs
._M_data());
280 __rcs
._M_data(__rcs
._M_local_data
);
281 _M_capacity(__tmp_capacity
);
285 const size_type __tmp_capacity
= _M_allocated_capacity
;
286 if (__rcs
._M_is_local())
288 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
289 _S_local_capacity
+ 1);
290 __rcs
._M_data(_M_data());
291 _M_data(_M_local_data
);
295 _CharT
* __tmp_ptr
= _M_data();
296 _M_data(__rcs
._M_data());
297 __rcs
._M_data(__tmp_ptr
);
298 _M_capacity(__rcs
._M_allocated_capacity
);
300 __rcs
._M_capacity(__tmp_capacity
);
303 const size_type __tmp_length
= _M_length();
304 _M_length(__rcs
._M_length());
305 __rcs
._M_length(__tmp_length
);
308 template<typename _CharT
, typename _Traits
, typename _Alloc
>
310 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
311 _M_create(size_type
& __capacity
, size_type __old_capacity
)
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // 83. String::npos vs. string::max_size()
315 if (__capacity
> _M_max_size())
316 std::__throw_length_error(__N("__sso_string_base::_M_create"));
318 // The below implements an exponential growth policy, necessary to
319 // meet amortized linear time requirements of the library: see
320 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
321 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
323 __capacity
= 2 * __old_capacity
;
324 // Never allocate a string bigger than max_size.
325 if (__capacity
> _M_max_size())
326 __capacity
= _M_max_size();
329 // NB: Need an array of char_type[__capacity], plus a terminating
330 // null char_type() element.
331 return _M_get_allocator().allocate(__capacity
+ 1);
334 template<typename _CharT
, typename _Traits
, typename _Alloc
>
335 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
336 __sso_string_base(const _Alloc
& __a
)
337 : _M_dataplus(__a
, _M_local_data
)
338 { _M_set_length(0); }
340 template<typename _CharT
, typename _Traits
, typename _Alloc
>
341 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
342 __sso_string_base(const __sso_string_base
& __rcs
)
343 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
344 { _M_construct(__rcs
._M_data(), __rcs
._M_data() + __rcs
._M_length()); }
346 #if __cplusplus >= 201103L
347 template<typename _CharT
, typename _Traits
, typename _Alloc
>
348 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
349 __sso_string_base(__sso_string_base
&& __rcs
)
350 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
352 if (__rcs
._M_is_local())
354 if (__rcs
._M_length())
355 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
356 _S_local_capacity
+ 1);
360 _M_data(__rcs
._M_data());
361 _M_capacity(__rcs
._M_allocated_capacity
);
364 _M_set_length(__rcs
._M_length());
365 __rcs
._M_data(__rcs
._M_local_data
);
366 __rcs
._M_set_length(0);
370 template<typename _CharT
, typename _Traits
, typename _Alloc
>
371 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
372 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
373 : _M_dataplus(__a
, _M_local_data
)
374 { _M_construct(__n
, __c
); }
376 template<typename _CharT
, typename _Traits
, typename _Alloc
>
377 template<typename _InputIterator
>
378 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
379 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
381 : _M_dataplus(__a
, _M_local_data
)
382 { _M_construct(__beg
, __end
); }
384 // NB: This is the special case for Input Iterators, used in
385 // istreambuf_iterators, etc.
386 // Input Iterators have a cost structure very different from
387 // pointers, calling for a different coding style.
388 template<typename _CharT
, typename _Traits
, typename _Alloc
>
389 template<typename _InIterator
>
391 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
392 _M_construct(_InIterator __beg
, _InIterator __end
,
393 std::input_iterator_tag
)
396 size_type __capacity
= size_type(_S_local_capacity
);
398 while (__beg
!= __end
&& __len
< __capacity
)
400 _M_data()[__len
++] = *__beg
;
406 while (__beg
!= __end
)
408 if (__len
== __capacity
)
410 // Allocate more space.
411 __capacity
= __len
+ 1;
412 _CharT
* __another
= _M_create(__capacity
, __len
);
413 this->_S_copy(__another
, _M_data(), __len
);
416 _M_capacity(__capacity
);
418 _M_data()[__len
++] = *__beg
;
425 __throw_exception_again
;
428 _M_set_length(__len
);
431 template<typename _CharT
, typename _Traits
, typename _Alloc
>
432 template<typename _InIterator
>
434 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
435 _M_construct(_InIterator __beg
, _InIterator __end
,
436 std::forward_iterator_tag
)
438 // NB: Not required, but considered best practice.
439 if (__is_null_pointer(__beg
) && __beg
!= __end
)
440 std::__throw_logic_error(__N("__sso_string_base::"
441 "_M_construct null not valid"));
443 size_type __dnew
= static_cast<size_type
>(std::distance(__beg
, __end
));
445 if (__dnew
> size_type(_S_local_capacity
))
447 _M_data(_M_create(__dnew
, size_type(0)));
451 // Check for out_of_range and length_error exceptions.
453 { this->_S_copy_chars(_M_data(), __beg
, __end
); }
457 __throw_exception_again
;
460 _M_set_length(__dnew
);
463 template<typename _CharT
, typename _Traits
, typename _Alloc
>
465 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
466 _M_construct(size_type __n
, _CharT __c
)
468 if (__n
> size_type(_S_local_capacity
))
470 _M_data(_M_create(__n
, size_type(0)));
475 this->_S_assign(_M_data(), __n
, __c
);
480 template<typename _CharT
, typename _Traits
, typename _Alloc
>
482 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
483 _M_assign(const __sso_string_base
& __rcs
)
487 const size_type __rsize
= __rcs
._M_length();
488 const size_type __capacity
= _M_capacity();
490 if (__rsize
> __capacity
)
492 size_type __new_capacity
= __rsize
;
493 _CharT
* __tmp
= _M_create(__new_capacity
, __capacity
);
496 _M_capacity(__new_capacity
);
500 this->_S_copy(_M_data(), __rcs
._M_data(), __rsize
);
502 _M_set_length(__rsize
);
506 template<typename _CharT
, typename _Traits
, typename _Alloc
>
508 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
509 _M_reserve(size_type __res
)
511 // Make sure we don't shrink below the current size.
512 if (__res
< _M_length())
515 const size_type __capacity
= _M_capacity();
516 if (__res
!= __capacity
)
518 if (__res
> __capacity
519 || __res
> size_type(_S_local_capacity
))
521 _CharT
* __tmp
= _M_create(__res
, __capacity
);
522 this->_S_copy(__tmp
, _M_data(), _M_length() + 1);
527 else if (!_M_is_local())
529 this->_S_copy(_M_local_data
, _M_data(), _M_length() + 1);
530 _M_destroy(__capacity
);
531 _M_data(_M_local_data
);
536 template<typename _CharT
, typename _Traits
, typename _Alloc
>
538 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
539 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
542 const size_type __how_much
= _M_length() - __pos
- __len1
;
544 size_type __new_capacity
= _M_length() + __len2
- __len1
;
545 _CharT
* __r
= _M_create(__new_capacity
, _M_capacity());
548 this->_S_copy(__r
, _M_data(), __pos
);
550 this->_S_copy(__r
+ __pos
, __s
, __len2
);
552 this->_S_copy(__r
+ __pos
+ __len2
,
553 _M_data() + __pos
+ __len1
, __how_much
);
557 _M_capacity(__new_capacity
);
560 template<typename _CharT
, typename _Traits
, typename _Alloc
>
562 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
563 _M_erase(size_type __pos
, size_type __n
)
565 const size_type __how_much
= _M_length() - __pos
- __n
;
567 if (__how_much
&& __n
)
568 this->_S_move(_M_data() + __pos
, _M_data() + __pos
+ __n
, __how_much
);
570 _M_set_length(_M_length() - __n
);
573 _GLIBCXX_END_NAMESPACE_VERSION
576 #endif /* _SSO_STRING_BASE_H */