1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005-2024 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 #include <bits/requires_hosted.h> // GNU extensions are currently omitted
35 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39 template<typename _CharT
, typename _Traits
, typename _Alloc
>
40 class __sso_string_base
41 : protected __vstring_utility
<_CharT
, _Traits
, _Alloc
>
44 typedef _Traits traits_type
;
45 typedef typename
_Traits::char_type value_type
;
47 typedef __vstring_utility
<_CharT
, _Traits
, _Alloc
> _Util_Base
;
48 typedef typename
_Util_Base::_CharT_alloc_type _CharT_alloc_type
;
49 typedef typename
_CharT_alloc_type::size_type size_type
;
53 typename
_Util_Base::template _Alloc_hider
<_CharT_alloc_type
>
55 size_type _M_string_length
;
57 enum { _S_local_capacity
= 15 };
61 _CharT _M_local_data
[_S_local_capacity
+ 1];
62 size_type _M_allocated_capacity
;
67 { _M_dataplus
._M_p
= __p
; }
70 _M_length(size_type __length
)
71 { _M_string_length
= __length
; }
74 _M_capacity(size_type __capacity
)
75 { _M_allocated_capacity
= __capacity
; }
79 { return _M_data() == _M_local_data
; }
83 _M_create(size_type
&, size_type
);
89 _M_destroy(_M_allocated_capacity
);
93 _M_destroy(size_type __size
) throw()
94 { _M_get_allocator().deallocate(_M_data(), __size
+ 1); }
96 // _M_construct_aux is used to implement the 21.3.1 para 15 which
97 // requires special behaviour if _InIterator is an integral type
98 template<typename _InIterator
>
100 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
103 typedef typename
std::iterator_traits
<_InIterator
>::iterator_category
105 _M_construct(__beg
, __end
, _Tag());
108 // _GLIBCXX_RESOLVE_LIB_DEFECTS
109 // 438. Ambiguity in the "do the right thing" clause
110 template<typename _Integer
>
112 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
113 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
116 _M_construct_aux_2(size_type __req
, _CharT __c
)
117 { _M_construct(__req
, __c
); }
119 template<typename _InIterator
>
121 _M_construct(_InIterator __beg
, _InIterator __end
)
123 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
124 _M_construct_aux(__beg
, __end
, _Integral());
127 // For Input Iterators, used in istreambuf_iterators, etc.
128 template<typename _InIterator
>
130 _M_construct(_InIterator __beg
, _InIterator __end
,
131 std::input_iterator_tag
);
133 // For forward_iterators up to random_access_iterators, used for
134 // string::iterator, _CharT*, etc.
135 template<typename _FwdIterator
>
137 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
138 std::forward_iterator_tag
);
141 _M_construct(size_type __req
, _CharT __c
);
147 typedef __alloc_traits
<_CharT_alloc_type
> _ATraits
;
148 return (_ATraits::max_size(_M_get_allocator()) - 1) / 2;
153 { return _M_dataplus
._M_p
; }
157 { return _M_string_length
; }
162 return _M_is_local() ? size_type(_S_local_capacity
)
163 : _M_allocated_capacity
;
177 _M_set_length(size_type __n
)
180 traits_type::assign(_M_data()[__n
], _CharT());
184 : _M_dataplus(_M_local_data
)
185 { _M_set_length(0); }
187 __sso_string_base(const _Alloc
& __a
);
189 __sso_string_base(const __sso_string_base
& __rcs
);
191 #if __cplusplus >= 201103L
192 __sso_string_base(__sso_string_base
&& __rcs
);
195 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
);
197 template<typename _InputIterator
>
198 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
206 { return _M_dataplus
; }
208 const _CharT_alloc_type
&
209 _M_get_allocator() const
210 { return _M_dataplus
; }
213 _M_swap(__sso_string_base
& __rcs
);
216 _M_assign(const __sso_string_base
& __rcs
);
219 _M_reserve(size_type __res
);
222 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
226 _M_erase(size_type __pos
, size_type __n
);
230 { _M_set_length(0); }
233 _M_compare(const __sso_string_base
&) const
237 template<typename _CharT
, typename _Traits
, typename _Alloc
>
239 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
240 _M_swap(__sso_string_base
& __rcs
)
245 // _GLIBCXX_RESOLVE_LIB_DEFECTS
246 // 431. Swapping containers with unequal allocators.
247 std::__alloc_swap
<_CharT_alloc_type
>::_S_do_it(_M_get_allocator(),
248 __rcs
._M_get_allocator());
251 if (__rcs
._M_is_local())
253 if (_M_length() && __rcs
._M_length())
255 _CharT __tmp_data
[_S_local_capacity
+ 1];
256 traits_type::copy(__tmp_data
, __rcs
._M_local_data
,
257 _S_local_capacity
+ 1);
258 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
259 _S_local_capacity
+ 1);
260 traits_type::copy(_M_local_data
, __tmp_data
,
261 _S_local_capacity
+ 1);
263 else if (__rcs
._M_length())
265 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
266 _S_local_capacity
+ 1);
267 _M_length(__rcs
._M_length());
268 __rcs
._M_set_length(0);
271 else if (_M_length())
273 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
274 _S_local_capacity
+ 1);
275 __rcs
._M_length(_M_length());
282 const size_type __tmp_capacity
= __rcs
._M_allocated_capacity
;
283 traits_type::copy(__rcs
._M_local_data
, _M_local_data
,
284 _S_local_capacity
+ 1);
285 _M_data(__rcs
._M_data());
286 __rcs
._M_data(__rcs
._M_local_data
);
287 _M_capacity(__tmp_capacity
);
291 const size_type __tmp_capacity
= _M_allocated_capacity
;
292 if (__rcs
._M_is_local())
294 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
295 _S_local_capacity
+ 1);
296 __rcs
._M_data(_M_data());
297 _M_data(_M_local_data
);
301 _CharT
* __tmp_ptr
= _M_data();
302 _M_data(__rcs
._M_data());
303 __rcs
._M_data(__tmp_ptr
);
304 _M_capacity(__rcs
._M_allocated_capacity
);
306 __rcs
._M_capacity(__tmp_capacity
);
309 const size_type __tmp_length
= _M_length();
310 _M_length(__rcs
._M_length());
311 __rcs
._M_length(__tmp_length
);
314 template<typename _CharT
, typename _Traits
, typename _Alloc
>
316 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
317 _M_create(size_type
& __capacity
, size_type __old_capacity
)
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // 83. String::npos vs. string::max_size()
321 if (__capacity
> _M_max_size())
322 std::__throw_length_error(__N("__sso_string_base::_M_create"));
324 // The below implements an exponential growth policy, necessary to
325 // meet amortized linear time requirements of the library: see
326 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
327 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
329 __capacity
= 2 * __old_capacity
;
330 // Never allocate a string bigger than max_size.
331 if (__capacity
> _M_max_size())
332 __capacity
= _M_max_size();
335 // NB: Need an array of char_type[__capacity], plus a terminating
336 // null char_type() element.
337 return _M_get_allocator().allocate(__capacity
+ 1);
340 template<typename _CharT
, typename _Traits
, typename _Alloc
>
341 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
342 __sso_string_base(const _Alloc
& __a
)
343 : _M_dataplus(__a
, _M_local_data
)
344 { _M_set_length(0); }
346 template<typename _CharT
, typename _Traits
, typename _Alloc
>
347 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
348 __sso_string_base(const __sso_string_base
& __rcs
)
349 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
350 { _M_construct(__rcs
._M_data(), __rcs
._M_data() + __rcs
._M_length()); }
352 #if __cplusplus >= 201103L
353 template<typename _CharT
, typename _Traits
, typename _Alloc
>
354 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
355 __sso_string_base(__sso_string_base
&& __rcs
)
356 : _M_dataplus(__rcs
._M_get_allocator(), _M_local_data
)
358 if (__rcs
._M_is_local())
360 if (__rcs
._M_length())
361 traits_type::copy(_M_local_data
, __rcs
._M_local_data
,
362 _S_local_capacity
+ 1);
366 _M_data(__rcs
._M_data());
367 _M_capacity(__rcs
._M_allocated_capacity
);
370 _M_set_length(__rcs
._M_length());
371 __rcs
._M_data(__rcs
._M_local_data
);
372 __rcs
._M_set_length(0);
376 template<typename _CharT
, typename _Traits
, typename _Alloc
>
377 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
378 __sso_string_base(size_type __n
, _CharT __c
, const _Alloc
& __a
)
379 : _M_dataplus(__a
, _M_local_data
)
380 { _M_construct(__n
, __c
); }
382 template<typename _CharT
, typename _Traits
, typename _Alloc
>
383 template<typename _InputIterator
>
384 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
385 __sso_string_base(_InputIterator __beg
, _InputIterator __end
,
387 : _M_dataplus(__a
, _M_local_data
)
388 { _M_construct(__beg
, __end
); }
390 // NB: This is the special case for Input Iterators, used in
391 // istreambuf_iterators, etc.
392 // Input Iterators have a cost structure very different from
393 // pointers, calling for a different coding style.
394 template<typename _CharT
, typename _Traits
, typename _Alloc
>
395 template<typename _InIterator
>
397 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
398 _M_construct(_InIterator __beg
, _InIterator __end
,
399 std::input_iterator_tag
)
402 size_type __capacity
= size_type(_S_local_capacity
);
404 while (__beg
!= __end
&& __len
< __capacity
)
406 _M_data()[__len
++] = *__beg
;
412 while (__beg
!= __end
)
414 if (__len
== __capacity
)
416 // Allocate more space.
417 __capacity
= __len
+ 1;
418 _CharT
* __another
= _M_create(__capacity
, __len
);
419 this->_S_copy(__another
, _M_data(), __len
);
422 _M_capacity(__capacity
);
424 _M_data()[__len
++] = *__beg
;
431 __throw_exception_again
;
434 _M_set_length(__len
);
437 template<typename _CharT
, typename _Traits
, typename _Alloc
>
438 template<typename _InIterator
>
440 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
441 _M_construct(_InIterator __beg
, _InIterator __end
,
442 std::forward_iterator_tag
)
444 // NB: Not required, but considered best practice.
445 if (__is_null_pointer(__beg
) && __beg
!= __end
)
446 std::__throw_logic_error(__N("__sso_string_base::"
447 "_M_construct null not valid"));
449 size_type __dnew
= static_cast<size_type
>(std::distance(__beg
, __end
));
451 if (__dnew
> size_type(_S_local_capacity
))
453 _M_data(_M_create(__dnew
, size_type(0)));
457 // Check for out_of_range and length_error exceptions.
459 { this->_S_copy_chars(_M_data(), __beg
, __end
); }
463 __throw_exception_again
;
466 _M_set_length(__dnew
);
469 template<typename _CharT
, typename _Traits
, typename _Alloc
>
471 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
472 _M_construct(size_type __n
, _CharT __c
)
474 if (__n
> size_type(_S_local_capacity
))
476 _M_data(_M_create(__n
, size_type(0)));
481 this->_S_assign(_M_data(), __n
, __c
);
486 template<typename _CharT
, typename _Traits
, typename _Alloc
>
488 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
489 _M_assign(const __sso_string_base
& __rcs
)
493 const size_type __rsize
= __rcs
._M_length();
494 const size_type __capacity
= _M_capacity();
496 if (__rsize
> __capacity
)
498 size_type __new_capacity
= __rsize
;
499 _CharT
* __tmp
= _M_create(__new_capacity
, __capacity
);
502 _M_capacity(__new_capacity
);
506 this->_S_copy(_M_data(), __rcs
._M_data(), __rsize
);
508 _M_set_length(__rsize
);
512 template<typename _CharT
, typename _Traits
, typename _Alloc
>
514 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
515 _M_reserve(size_type __res
)
517 // Make sure we don't shrink below the current size.
518 if (__res
< _M_length())
521 const size_type __capacity
= _M_capacity();
522 if (__res
!= __capacity
)
524 if (__res
> __capacity
525 || __res
> size_type(_S_local_capacity
))
527 _CharT
* __tmp
= _M_create(__res
, __capacity
);
528 this->_S_copy(__tmp
, _M_data(), _M_length() + 1);
533 else if (!_M_is_local())
535 this->_S_copy(_M_local_data
, _M_data(), _M_length() + 1);
536 _M_destroy(__capacity
);
537 _M_data(_M_local_data
);
542 template<typename _CharT
, typename _Traits
, typename _Alloc
>
544 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
545 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
548 const size_type __how_much
= _M_length() - __pos
- __len1
;
550 size_type __new_capacity
= _M_length() + __len2
- __len1
;
551 _CharT
* __r
= _M_create(__new_capacity
, _M_capacity());
554 this->_S_copy(__r
, _M_data(), __pos
);
556 this->_S_copy(__r
+ __pos
, __s
, __len2
);
558 this->_S_copy(__r
+ __pos
+ __len2
,
559 _M_data() + __pos
+ __len1
, __how_much
);
563 _M_capacity(__new_capacity
);
566 template<typename _CharT
, typename _Traits
, typename _Alloc
>
568 __sso_string_base
<_CharT
, _Traits
, _Alloc
>::
569 _M_erase(size_type __pos
, size_type __n
)
571 const size_type __how_much
= _M_length() - __pos
- __n
;
573 if (__how_much
&& __n
)
574 this->_S_move(_M_data() + __pos
, _M_data() + __pos
+ __n
, __how_much
);
576 _M_set_length(_M_length() - __n
);
579 _GLIBCXX_END_NAMESPACE_VERSION
582 #endif /* _SSO_STRING_BASE_H */