30#ifndef _SSO_STRING_BASE_H
31#define _SSO_STRING_BASE_H 1
35namespace __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); }
98 template<
typename _InIterator>
100 _M_construct_aux(_InIterator __beg, _InIterator __end,
105 _M_construct(__beg, __end, _Tag());
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());
128 template<
typename _InIterator>
130 _M_construct(_InIterator __beg, _InIterator __end,
135 template<
typename _FwdIterator>
137 _M_construct(_FwdIterator __beg, _FwdIterator __end,
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)
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)
321 if (__capacity > _M_max_size())
322 std::__throw_length_error(__N(
"__sso_string_base::_M_create"));
327 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
329 __capacity = 2 * __old_capacity;
331 if (__capacity > _M_max_size())
332 __capacity = _M_max_size();
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); }
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,
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)
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,
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)));
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)
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
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
GNU extensions for public use.
Traits class for iterators.
Forward iterators support a superset of input iterator operations.