31 #ifndef _SSO_STRING_BASE_H
32 #define _SSO_STRING_BASE_H 1
34 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
36 template<typename _CharT, typename _Traits, typename _Alloc>
37 class __sso_string_base
38 : protected __vstring_utility<_CharT, _Traits, _Alloc>
41 typedef _Traits traits_type;
42 typedef typename _Traits::char_type value_type;
44 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
45 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
46 typedef typename _CharT_alloc_type::size_type size_type;
50 typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
52 size_type _M_string_length;
54 enum { _S_local_capacity = 15 };
58 _CharT _M_local_data[_S_local_capacity + 1];
59 size_type _M_allocated_capacity;
64 { _M_dataplus._M_p = __p; }
67 _M_length(size_type __length)
68 { _M_string_length = __length; }
71 _M_capacity(size_type __capacity)
72 { _M_allocated_capacity = __capacity; }
76 {
return _M_data() == _M_local_data; }
80 _M_create(size_type&, size_type);
86 _M_destroy(_M_allocated_capacity);
90 _M_destroy(size_type __size)
throw()
91 { _M_get_allocator().deallocate(_M_data(), __size + 1); }
95 template<
typename _InIterator>
97 _M_construct_aux(_InIterator __beg, _InIterator __end,
100 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
101 _M_construct(__beg, __end, _Tag());
106 template<
typename _Integer>
108 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
109 { _M_construct(static_cast<size_type>(__beg), __end); }
111 template<
typename _InIterator>
113 _M_construct(_InIterator __beg, _InIterator __end)
115 typedef typename std::__is_integer<_InIterator>::__type _Integral;
116 _M_construct_aux(__beg, __end, _Integral());
120 template<
typename _InIterator>
122 _M_construct(_InIterator __beg, _InIterator __end,
127 template<
typename _FwdIterator>
129 _M_construct(_FwdIterator __beg, _FwdIterator __end,
133 _M_construct(size_type __req, _CharT __c);
138 {
return (_M_get_allocator().max_size() - 1) / 2; }
142 {
return _M_dataplus._M_p; }
146 {
return _M_string_length; }
151 return _M_is_local() ? size_type(_S_local_capacity)
152 : _M_allocated_capacity;
166 _M_set_length(size_type __n)
169 traits_type::assign(_M_data()[__n], _CharT());
173 : _M_dataplus(_M_local_data)
174 { _M_set_length(0); }
176 __sso_string_base(
const _Alloc& __a);
178 __sso_string_base(
const __sso_string_base& __rcs);
180 #ifdef __GXX_EXPERIMENTAL_CXX0X__
181 __sso_string_base(__sso_string_base&& __rcs);
184 __sso_string_base(size_type __n, _CharT __c,
const _Alloc& __a);
186 template<
typename _InputIterator>
187 __sso_string_base(_InputIterator __beg, _InputIterator __end,
195 {
return _M_dataplus; }
197 const _CharT_alloc_type&
198 _M_get_allocator()
const
199 {
return _M_dataplus; }
202 _M_swap(__sso_string_base& __rcs);
205 _M_assign(
const __sso_string_base& __rcs);
208 _M_reserve(size_type __res);
211 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
215 _M_erase(size_type __pos, size_type __n);
219 { _M_set_length(0); }
222 _M_compare(
const __sso_string_base&)
const
226 template<
typename _CharT,
typename _Traits,
typename _Alloc>
228 __sso_string_base<_CharT, _Traits, _Alloc>::
229 _M_swap(__sso_string_base& __rcs)
233 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
234 __rcs._M_get_allocator());
237 if (__rcs._M_is_local())
239 if (_M_length() && __rcs._M_length())
241 _CharT __tmp_data[_S_local_capacity + 1];
242 traits_type::copy(__tmp_data, __rcs._M_local_data,
243 _S_local_capacity + 1);
244 traits_type::copy(__rcs._M_local_data, _M_local_data,
245 _S_local_capacity + 1);
246 traits_type::copy(_M_local_data, __tmp_data,
247 _S_local_capacity + 1);
249 else if (__rcs._M_length())
251 traits_type::copy(_M_local_data, __rcs._M_local_data,
252 _S_local_capacity + 1);
253 _M_length(__rcs._M_length());
254 __rcs._M_set_length(0);
257 else if (_M_length())
259 traits_type::copy(__rcs._M_local_data, _M_local_data,
260 _S_local_capacity + 1);
261 __rcs._M_length(_M_length());
268 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
269 traits_type::copy(__rcs._M_local_data, _M_local_data,
270 _S_local_capacity + 1);
271 _M_data(__rcs._M_data());
272 __rcs._M_data(__rcs._M_local_data);
273 _M_capacity(__tmp_capacity);
277 const size_type __tmp_capacity = _M_allocated_capacity;
278 if (__rcs._M_is_local())
280 traits_type::copy(_M_local_data, __rcs._M_local_data,
281 _S_local_capacity + 1);
282 __rcs._M_data(_M_data());
283 _M_data(_M_local_data);
287 _CharT* __tmp_ptr = _M_data();
288 _M_data(__rcs._M_data());
289 __rcs._M_data(__tmp_ptr);
290 _M_capacity(__rcs._M_allocated_capacity);
292 __rcs._M_capacity(__tmp_capacity);
295 const size_type __tmp_length = _M_length();
296 _M_length(__rcs._M_length());
297 __rcs._M_length(__tmp_length);
300 template<
typename _CharT,
typename _Traits,
typename _Alloc>
302 __sso_string_base<_CharT, _Traits, _Alloc>::
303 _M_create(size_type& __capacity, size_type __old_capacity)
307 if (__capacity > _M_max_size())
308 std::__throw_length_error(__N(
"__sso_string_base::_M_create"));
313 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
315 __capacity = 2 * __old_capacity;
317 if (__capacity > _M_max_size())
318 __capacity = _M_max_size();
323 return _M_get_allocator().allocate(__capacity + 1);
326 template<
typename _CharT,
typename _Traits,
typename _Alloc>
327 __sso_string_base<_CharT, _Traits, _Alloc>::
328 __sso_string_base(
const _Alloc& __a)
329 : _M_dataplus(__a, _M_local_data)
330 { _M_set_length(0); }
332 template<
typename _CharT,
typename _Traits,
typename _Alloc>
333 __sso_string_base<_CharT, _Traits, _Alloc>::
334 __sso_string_base(
const __sso_string_base& __rcs)
335 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
336 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
338 #ifdef __GXX_EXPERIMENTAL_CXX0X__
339 template<
typename _CharT,
typename _Traits,
typename _Alloc>
340 __sso_string_base<_CharT, _Traits, _Alloc>::
341 __sso_string_base(__sso_string_base&& __rcs)
342 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
344 if (__rcs._M_is_local())
346 if (__rcs._M_length())
347 traits_type::copy(_M_local_data, __rcs._M_local_data,
348 _S_local_capacity + 1);
352 _M_data(__rcs._M_data());
353 _M_capacity(__rcs._M_allocated_capacity);
356 _M_length(__rcs._M_length());
358 __rcs._M_data(__rcs._M_local_data);
362 template<
typename _CharT,
typename _Traits,
typename _Alloc>
363 __sso_string_base<_CharT, _Traits, _Alloc>::
364 __sso_string_base(size_type __n, _CharT __c,
const _Alloc& __a)
365 : _M_dataplus(__a, _M_local_data)
366 { _M_construct(__n, __c); }
368 template<
typename _CharT,
typename _Traits,
typename _Alloc>
369 template<
typename _InputIterator>
370 __sso_string_base<_CharT, _Traits, _Alloc>::
371 __sso_string_base(_InputIterator __beg, _InputIterator __end,
373 : _M_dataplus(__a, _M_local_data)
374 { _M_construct(__beg, __end); }
380 template<
typename _CharT,
typename _Traits,
typename _Alloc>
381 template<
typename _InIterator>
383 __sso_string_base<_CharT, _Traits, _Alloc>::
384 _M_construct(_InIterator __beg, _InIterator __end,
388 size_type __capacity = size_type(_S_local_capacity);
390 while (__beg != __end && __len < __capacity)
392 _M_data()[__len++] = *__beg;
398 while (__beg != __end)
400 if (__len == __capacity)
403 __capacity = __len + 1;
404 _CharT* __another = _M_create(__capacity, __len);
405 _S_copy(__another, _M_data(), __len);
408 _M_capacity(__capacity);
410 _M_data()[__len++] = *__beg;
417 __throw_exception_again;
420 _M_set_length(__len);
423 template<
typename _CharT,
typename _Traits,
typename _Alloc>
424 template<
typename _InIterator>
426 __sso_string_base<_CharT, _Traits, _Alloc>::
427 _M_construct(_InIterator __beg, _InIterator __end,
431 if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
432 std::__throw_logic_error(__N(
"__sso_string_base::"
433 "_M_construct NULL not valid"));
435 size_type __dnew =
static_cast<size_type
>(
std::distance(__beg, __end));
437 if (__dnew > size_type(_S_local_capacity))
439 _M_data(_M_create(__dnew, size_type(0)));
445 { _S_copy_chars(_M_data(), __beg, __end); }
449 __throw_exception_again;
452 _M_set_length(__dnew);
455 template<
typename _CharT,
typename _Traits,
typename _Alloc>
457 __sso_string_base<_CharT, _Traits, _Alloc>::
458 _M_construct(size_type __n, _CharT __c)
460 if (__n > size_type(_S_local_capacity))
462 _M_data(_M_create(__n, size_type(0)));
467 _S_assign(_M_data(), __n, __c);
472 template<
typename _CharT,
typename _Traits,
typename _Alloc>
474 __sso_string_base<_CharT, _Traits, _Alloc>::
475 _M_assign(
const __sso_string_base& __rcs)
479 const size_type __rsize = __rcs._M_length();
480 const size_type __capacity = _M_capacity();
482 if (__rsize > __capacity)
484 size_type __new_capacity = __rsize;
485 _CharT* __tmp = _M_create(__new_capacity, __capacity);
488 _M_capacity(__new_capacity);
492 _S_copy(_M_data(), __rcs._M_data(), __rsize);
494 _M_set_length(__rsize);
498 template<
typename _CharT,
typename _Traits,
typename _Alloc>
500 __sso_string_base<_CharT, _Traits, _Alloc>::
501 _M_reserve(size_type __res)
504 if (__res < _M_length())
507 const size_type __capacity = _M_capacity();
508 if (__res != __capacity)
510 if (__res > __capacity
511 || __res > size_type(_S_local_capacity))
513 _CharT* __tmp = _M_create(__res, __capacity);
514 _S_copy(__tmp, _M_data(), _M_length() + 1);
519 else if (!_M_is_local())
521 _S_copy(_M_local_data, _M_data(), _M_length() + 1);
522 _M_destroy(__capacity);
523 _M_data(_M_local_data);
528 template<
typename _CharT,
typename _Traits,
typename _Alloc>
530 __sso_string_base<_CharT, _Traits, _Alloc>::
531 _M_mutate(size_type __pos, size_type __len1,
const _CharT* __s,
532 const size_type __len2)
534 const size_type __how_much = _M_length() - __pos - __len1;
536 size_type __new_capacity = _M_length() + __len2 - __len1;
537 _CharT* __r = _M_create(__new_capacity, _M_capacity());
540 _S_copy(__r, _M_data(), __pos);
542 _S_copy(__r + __pos, __s, __len2);
544 _S_copy(__r + __pos + __len2,
545 _M_data() + __pos + __len1, __how_much);
549 _M_capacity(__new_capacity);
552 template<
typename _CharT,
typename _Traits,
typename _Alloc>
554 __sso_string_base<_CharT, _Traits, _Alloc>::
555 _M_erase(size_type __pos, size_type __n)
557 const size_type __how_much = _M_length() - __pos - __n;
559 if (__how_much && __n)
560 _S_move(_M_data() + __pos, _M_data() + __pos + __n,
563 _M_set_length(_M_length() - __n);
566 _GLIBCXX_END_NAMESPACE