30#ifndef _SSO_STRING_BASE_H
31#define _SSO_STRING_BASE_H 1
33namespace __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); }
96 template<
typename _InIterator>
98 _M_construct_aux(_InIterator __beg, _InIterator __end,
103 _M_construct(__beg, __end, _Tag());
108 template<
typename _Integer>
110 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
111 { _M_construct_aux_2(
static_cast<size_type
>(__beg), __end); }
114 _M_construct_aux_2(size_type __req, _CharT __c)
115 { _M_construct(__req, __c); }
117 template<
typename _InIterator>
119 _M_construct(_InIterator __beg, _InIterator __end)
121 typedef typename std::__is_integer<_InIterator>::__type _Integral;
122 _M_construct_aux(__beg, __end, _Integral());
126 template<
typename _InIterator>
128 _M_construct(_InIterator __beg, _InIterator __end,
133 template<
typename _FwdIterator>
135 _M_construct(_FwdIterator __beg, _FwdIterator __end,
139 _M_construct(size_type __req, _CharT __c);
145 typedef __alloc_traits<_CharT_alloc_type> _ATraits;
146 return (_ATraits::max_size(_M_get_allocator()) - 1) / 2;
151 {
return _M_dataplus._M_p; }
155 {
return _M_string_length; }
160 return _M_is_local() ? size_type(_S_local_capacity)
161 : _M_allocated_capacity;
175 _M_set_length(size_type __n)
178 traits_type::assign(_M_data()[__n], _CharT());
182 : _M_dataplus(_M_local_data)
183 { _M_set_length(0); }
185 __sso_string_base(
const _Alloc& __a);
187 __sso_string_base(
const __sso_string_base& __rcs);
189#if __cplusplus >= 201103L
190 __sso_string_base(__sso_string_base&& __rcs);
193 __sso_string_base(size_type __n, _CharT __c,
const _Alloc& __a);
195 template<
typename _InputIterator>
196 __sso_string_base(_InputIterator __beg, _InputIterator __end,
204 {
return _M_dataplus; }
206 const _CharT_alloc_type&
207 _M_get_allocator()
const
208 {
return _M_dataplus; }
211 _M_swap(__sso_string_base& __rcs);
214 _M_assign(
const __sso_string_base& __rcs);
217 _M_reserve(size_type __res);
220 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
224 _M_erase(size_type __pos, size_type __n);
228 { _M_set_length(0); }
231 _M_compare(
const __sso_string_base&)
const
235 template<
typename _CharT,
typename _Traits,
typename _Alloc>
237 __sso_string_base<_CharT, _Traits, _Alloc>::
238 _M_swap(__sso_string_base& __rcs)
245 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
246 __rcs._M_get_allocator());
249 if (__rcs._M_is_local())
251 if (_M_length() && __rcs._M_length())
253 _CharT __tmp_data[_S_local_capacity + 1];
254 traits_type::copy(__tmp_data, __rcs._M_local_data,
255 _S_local_capacity + 1);
256 traits_type::copy(__rcs._M_local_data, _M_local_data,
257 _S_local_capacity + 1);
258 traits_type::copy(_M_local_data, __tmp_data,
259 _S_local_capacity + 1);
261 else if (__rcs._M_length())
263 traits_type::copy(_M_local_data, __rcs._M_local_data,
264 _S_local_capacity + 1);
265 _M_length(__rcs._M_length());
266 __rcs._M_set_length(0);
269 else if (_M_length())
271 traits_type::copy(__rcs._M_local_data, _M_local_data,
272 _S_local_capacity + 1);
273 __rcs._M_length(_M_length());
280 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
281 traits_type::copy(__rcs._M_local_data, _M_local_data,
282 _S_local_capacity + 1);
283 _M_data(__rcs._M_data());
284 __rcs._M_data(__rcs._M_local_data);
285 _M_capacity(__tmp_capacity);
289 const size_type __tmp_capacity = _M_allocated_capacity;
290 if (__rcs._M_is_local())
292 traits_type::copy(_M_local_data, __rcs._M_local_data,
293 _S_local_capacity + 1);
294 __rcs._M_data(_M_data());
295 _M_data(_M_local_data);
299 _CharT* __tmp_ptr = _M_data();
300 _M_data(__rcs._M_data());
301 __rcs._M_data(__tmp_ptr);
302 _M_capacity(__rcs._M_allocated_capacity);
304 __rcs._M_capacity(__tmp_capacity);
307 const size_type __tmp_length = _M_length();
308 _M_length(__rcs._M_length());
309 __rcs._M_length(__tmp_length);
312 template<
typename _CharT,
typename _Traits,
typename _Alloc>
314 __sso_string_base<_CharT, _Traits, _Alloc>::
315 _M_create(size_type& __capacity, size_type __old_capacity)
319 if (__capacity > _M_max_size())
320 std::__throw_length_error(__N(
"__sso_string_base::_M_create"));
325 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
327 __capacity = 2 * __old_capacity;
329 if (__capacity > _M_max_size())
330 __capacity = _M_max_size();
335 return _M_get_allocator().allocate(__capacity + 1);
338 template<
typename _CharT,
typename _Traits,
typename _Alloc>
339 __sso_string_base<_CharT, _Traits, _Alloc>::
340 __sso_string_base(
const _Alloc& __a)
341 : _M_dataplus(__a, _M_local_data)
342 { _M_set_length(0); }
344 template<
typename _CharT,
typename _Traits,
typename _Alloc>
345 __sso_string_base<_CharT, _Traits, _Alloc>::
346 __sso_string_base(
const __sso_string_base& __rcs)
347 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
348 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
350#if __cplusplus >= 201103L
351 template<
typename _CharT,
typename _Traits,
typename _Alloc>
352 __sso_string_base<_CharT, _Traits, _Alloc>::
353 __sso_string_base(__sso_string_base&& __rcs)
354 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
356 if (__rcs._M_is_local())
358 if (__rcs._M_length())
359 traits_type::copy(_M_local_data, __rcs._M_local_data,
360 _S_local_capacity + 1);
364 _M_data(__rcs._M_data());
365 _M_capacity(__rcs._M_allocated_capacity);
368 _M_set_length(__rcs._M_length());
369 __rcs._M_data(__rcs._M_local_data);
370 __rcs._M_set_length(0);
374 template<
typename _CharT,
typename _Traits,
typename _Alloc>
375 __sso_string_base<_CharT, _Traits, _Alloc>::
376 __sso_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
377 : _M_dataplus(__a, _M_local_data)
378 { _M_construct(__n, __c); }
380 template<
typename _CharT,
typename _Traits,
typename _Alloc>
381 template<
typename _InputIterator>
382 __sso_string_base<_CharT, _Traits, _Alloc>::
383 __sso_string_base(_InputIterator __beg, _InputIterator __end,
385 : _M_dataplus(__a, _M_local_data)
386 { _M_construct(__beg, __end); }
392 template<
typename _CharT,
typename _Traits,
typename _Alloc>
393 template<
typename _InIterator>
395 __sso_string_base<_CharT, _Traits, _Alloc>::
396 _M_construct(_InIterator __beg, _InIterator __end,
400 size_type __capacity = size_type(_S_local_capacity);
402 while (__beg != __end && __len < __capacity)
404 _M_data()[__len++] = *__beg;
410 while (__beg != __end)
412 if (__len == __capacity)
415 __capacity = __len + 1;
416 _CharT* __another = _M_create(__capacity, __len);
417 this->_S_copy(__another, _M_data(), __len);
420 _M_capacity(__capacity);
422 _M_data()[__len++] = *__beg;
429 __throw_exception_again;
432 _M_set_length(__len);
435 template<
typename _CharT,
typename _Traits,
typename _Alloc>
436 template<
typename _InIterator>
438 __sso_string_base<_CharT, _Traits, _Alloc>::
439 _M_construct(_InIterator __beg, _InIterator __end,
443 if (__is_null_pointer(__beg) && __beg != __end)
444 std::__throw_logic_error(__N(
"__sso_string_base::"
445 "_M_construct null not valid"));
447 size_type __dnew =
static_cast<size_type
>(
std::distance(__beg, __end));
449 if (__dnew > size_type(_S_local_capacity))
451 _M_data(_M_create(__dnew, size_type(0)));
457 { this->_S_copy_chars(_M_data(), __beg, __end); }
461 __throw_exception_again;
464 _M_set_length(__dnew);
467 template<
typename _CharT,
typename _Traits,
typename _Alloc>
469 __sso_string_base<_CharT, _Traits, _Alloc>::
470 _M_construct(size_type __n, _CharT __c)
472 if (__n > size_type(_S_local_capacity))
474 _M_data(_M_create(__n, size_type(0)));
479 this->_S_assign(_M_data(), __n, __c);
484 template<
typename _CharT,
typename _Traits,
typename _Alloc>
486 __sso_string_base<_CharT, _Traits, _Alloc>::
487 _M_assign(
const __sso_string_base& __rcs)
491 const size_type __rsize = __rcs._M_length();
492 const size_type __capacity = _M_capacity();
494 if (__rsize > __capacity)
496 size_type __new_capacity = __rsize;
497 _CharT* __tmp = _M_create(__new_capacity, __capacity);
500 _M_capacity(__new_capacity);
504 this->_S_copy(_M_data(), __rcs._M_data(), __rsize);
506 _M_set_length(__rsize);
510 template<
typename _CharT,
typename _Traits,
typename _Alloc>
512 __sso_string_base<_CharT, _Traits, _Alloc>::
513 _M_reserve(size_type __res)
516 if (__res < _M_length())
519 const size_type __capacity = _M_capacity();
520 if (__res != __capacity)
522 if (__res > __capacity
523 || __res > size_type(_S_local_capacity))
525 _CharT* __tmp = _M_create(__res, __capacity);
526 this->_S_copy(__tmp, _M_data(), _M_length() + 1);
531 else if (!_M_is_local())
533 this->_S_copy(_M_local_data, _M_data(), _M_length() + 1);
534 _M_destroy(__capacity);
535 _M_data(_M_local_data);
540 template<
typename _CharT,
typename _Traits,
typename _Alloc>
542 __sso_string_base<_CharT, _Traits, _Alloc>::
543 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
546 const size_type __how_much = _M_length() - __pos - __len1;
548 size_type __new_capacity = _M_length() + __len2 - __len1;
549 _CharT* __r = _M_create(__new_capacity, _M_capacity());
552 this->_S_copy(__r, _M_data(), __pos);
554 this->_S_copy(__r + __pos, __s, __len2);
556 this->_S_copy(__r + __pos + __len2,
557 _M_data() + __pos + __len1, __how_much);
561 _M_capacity(__new_capacity);
564 template<
typename _CharT,
typename _Traits,
typename _Alloc>
566 __sso_string_base<_CharT, _Traits, _Alloc>::
567 _M_erase(size_type __pos, size_type __n)
569 const size_type __how_much = _M_length() - __pos - __n;
571 if (__how_much && __n)
572 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
574 _M_set_length(_M_length() - __n);
577_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.