1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file ext/sso_string_base.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{ext/vstring.h}
31 #ifndef _SSO_STRING_BASE_H
32 #define _SSO_STRING_BASE_H 1
34 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 template<typename _CharT, typename _Traits, typename _Alloc>
39 class __sso_string_base
40 : protected __vstring_utility<_CharT, _Traits, _Alloc>
43 typedef _Traits traits_type;
44 typedef typename _Traits::char_type value_type;
46 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
47 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
48 typedef typename _CharT_alloc_type::size_type size_type;
52 typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
54 size_type _M_string_length;
56 enum { _S_local_capacity = 15 };
60 _CharT _M_local_data[_S_local_capacity + 1];
61 size_type _M_allocated_capacity;
66 { _M_dataplus._M_p = __p; }
69 _M_length(size_type __length)
70 { _M_string_length = __length; }
73 _M_capacity(size_type __capacity)
74 { _M_allocated_capacity = __capacity; }
78 { return _M_data() == _M_local_data; }
82 _M_create(size_type&, size_type);
88 _M_destroy(_M_allocated_capacity);
92 _M_destroy(size_type __size) throw()
93 { _M_get_allocator().deallocate(_M_data(), __size + 1); }
95 // _M_construct_aux is used to implement the 21.3.1 para 15 which
96 // requires special behaviour if _InIterator is an integral type
97 template<typename _InIterator>
99 _M_construct_aux(_InIterator __beg, _InIterator __end,
102 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
103 _M_construct(__beg, __end, _Tag());
106 // _GLIBCXX_RESOLVE_LIB_DEFECTS
107 // 438. Ambiguity in the "do the right thing" clause
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());
125 // For Input Iterators, used in istreambuf_iterators, etc.
126 template<typename _InIterator>
128 _M_construct(_InIterator __beg, _InIterator __end,
129 std::input_iterator_tag);
131 // For forward_iterators up to random_access_iterators, used for
132 // string::iterator, _CharT*, etc.
133 template<typename _FwdIterator>
135 _M_construct(_FwdIterator __beg, _FwdIterator __end,
136 std::forward_iterator_tag);
139 _M_construct(size_type __req, _CharT __c);
144 { return (_M_get_allocator().max_size() - 1) / 2; }
148 { return _M_dataplus._M_p; }
152 { return _M_string_length; }
157 return _M_is_local() ? size_type(_S_local_capacity)
158 : _M_allocated_capacity;
172 _M_set_length(size_type __n)
175 traits_type::assign(_M_data()[__n], _CharT());
179 : _M_dataplus(_M_local_data)
180 { _M_set_length(0); }
182 __sso_string_base(const _Alloc& __a);
184 __sso_string_base(const __sso_string_base& __rcs);
186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
187 __sso_string_base(__sso_string_base&& __rcs);
190 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
192 template<typename _InputIterator>
193 __sso_string_base(_InputIterator __beg, _InputIterator __end,
201 { return _M_dataplus; }
203 const _CharT_alloc_type&
204 _M_get_allocator() const
205 { return _M_dataplus; }
208 _M_swap(__sso_string_base& __rcs);
211 _M_assign(const __sso_string_base& __rcs);
214 _M_reserve(size_type __res);
217 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
221 _M_erase(size_type __pos, size_type __n);
225 { _M_set_length(0); }
228 _M_compare(const __sso_string_base&) const
232 template<typename _CharT, typename _Traits, typename _Alloc>
234 __sso_string_base<_CharT, _Traits, _Alloc>::
235 _M_swap(__sso_string_base& __rcs)
240 // _GLIBCXX_RESOLVE_LIB_DEFECTS
241 // 431. Swapping containers with unequal allocators.
242 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
243 __rcs._M_get_allocator());
246 if (__rcs._M_is_local())
248 if (_M_length() && __rcs._M_length())
250 _CharT __tmp_data[_S_local_capacity + 1];
251 traits_type::copy(__tmp_data, __rcs._M_local_data,
252 _S_local_capacity + 1);
253 traits_type::copy(__rcs._M_local_data, _M_local_data,
254 _S_local_capacity + 1);
255 traits_type::copy(_M_local_data, __tmp_data,
256 _S_local_capacity + 1);
258 else if (__rcs._M_length())
260 traits_type::copy(_M_local_data, __rcs._M_local_data,
261 _S_local_capacity + 1);
262 _M_length(__rcs._M_length());
263 __rcs._M_set_length(0);
266 else if (_M_length())
268 traits_type::copy(__rcs._M_local_data, _M_local_data,
269 _S_local_capacity + 1);
270 __rcs._M_length(_M_length());
277 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
278 traits_type::copy(__rcs._M_local_data, _M_local_data,
279 _S_local_capacity + 1);
280 _M_data(__rcs._M_data());
281 __rcs._M_data(__rcs._M_local_data);
282 _M_capacity(__tmp_capacity);
286 const size_type __tmp_capacity = _M_allocated_capacity;
287 if (__rcs._M_is_local())
289 traits_type::copy(_M_local_data, __rcs._M_local_data,
290 _S_local_capacity + 1);
291 __rcs._M_data(_M_data());
292 _M_data(_M_local_data);
296 _CharT* __tmp_ptr = _M_data();
297 _M_data(__rcs._M_data());
298 __rcs._M_data(__tmp_ptr);
299 _M_capacity(__rcs._M_allocated_capacity);
301 __rcs._M_capacity(__tmp_capacity);
304 const size_type __tmp_length = _M_length();
305 _M_length(__rcs._M_length());
306 __rcs._M_length(__tmp_length);
309 template<typename _CharT, typename _Traits, typename _Alloc>
311 __sso_string_base<_CharT, _Traits, _Alloc>::
312 _M_create(size_type& __capacity, size_type __old_capacity)
314 // _GLIBCXX_RESOLVE_LIB_DEFECTS
315 // 83. String::npos vs. string::max_size()
316 if (__capacity > _M_max_size())
317 std::__throw_length_error(__N("__sso_string_base::_M_create"));
319 // The below implements an exponential growth policy, necessary to
320 // meet amortized linear time requirements of the library: see
321 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
322 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
324 __capacity = 2 * __old_capacity;
325 // Never allocate a string bigger than max_size.
326 if (__capacity > _M_max_size())
327 __capacity = _M_max_size();
330 // NB: Need an array of char_type[__capacity], plus a terminating
331 // null char_type() element.
332 return _M_get_allocator().allocate(__capacity + 1);
335 template<typename _CharT, typename _Traits, typename _Alloc>
336 __sso_string_base<_CharT, _Traits, _Alloc>::
337 __sso_string_base(const _Alloc& __a)
338 : _M_dataplus(__a, _M_local_data)
339 { _M_set_length(0); }
341 template<typename _CharT, typename _Traits, typename _Alloc>
342 __sso_string_base<_CharT, _Traits, _Alloc>::
343 __sso_string_base(const __sso_string_base& __rcs)
344 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
345 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
348 template<typename _CharT, typename _Traits, typename _Alloc>
349 __sso_string_base<_CharT, _Traits, _Alloc>::
350 __sso_string_base(__sso_string_base&& __rcs)
351 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
353 if (__rcs._M_is_local())
355 if (__rcs._M_length())
356 traits_type::copy(_M_local_data, __rcs._M_local_data,
357 _S_local_capacity + 1);
361 _M_data(__rcs._M_data());
362 _M_capacity(__rcs._M_allocated_capacity);
365 _M_length(__rcs._M_length());
367 __rcs._M_data(__rcs._M_local_data);
371 template<typename _CharT, typename _Traits, typename _Alloc>
372 __sso_string_base<_CharT, _Traits, _Alloc>::
373 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
374 : _M_dataplus(__a, _M_local_data)
375 { _M_construct(__n, __c); }
377 template<typename _CharT, typename _Traits, typename _Alloc>
378 template<typename _InputIterator>
379 __sso_string_base<_CharT, _Traits, _Alloc>::
380 __sso_string_base(_InputIterator __beg, _InputIterator __end,
382 : _M_dataplus(__a, _M_local_data)
383 { _M_construct(__beg, __end); }
385 // NB: This is the special case for Input Iterators, used in
386 // istreambuf_iterators, etc.
387 // Input Iterators have a cost structure very different from
388 // pointers, calling for a different coding style.
389 template<typename _CharT, typename _Traits, typename _Alloc>
390 template<typename _InIterator>
392 __sso_string_base<_CharT, _Traits, _Alloc>::
393 _M_construct(_InIterator __beg, _InIterator __end,
394 std::input_iterator_tag)
397 size_type __capacity = size_type(_S_local_capacity);
399 while (__beg != __end && __len < __capacity)
401 _M_data()[__len++] = *__beg;
407 while (__beg != __end)
409 if (__len == __capacity)
411 // Allocate more space.
412 __capacity = __len + 1;
413 _CharT* __another = _M_create(__capacity, __len);
414 this->_S_copy(__another, _M_data(), __len);
417 _M_capacity(__capacity);
419 _M_data()[__len++] = *__beg;
426 __throw_exception_again;
429 _M_set_length(__len);
432 template<typename _CharT, typename _Traits, typename _Alloc>
433 template<typename _InIterator>
435 __sso_string_base<_CharT, _Traits, _Alloc>::
436 _M_construct(_InIterator __beg, _InIterator __end,
437 std::forward_iterator_tag)
439 // NB: Not required, but considered best practice.
440 if (__is_null_pointer(__beg) && __beg != __end)
441 std::__throw_logic_error(__N("__sso_string_base::"
442 "_M_construct null not valid"));
444 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
446 if (__dnew > size_type(_S_local_capacity))
448 _M_data(_M_create(__dnew, size_type(0)));
452 // Check for out_of_range and length_error exceptions.
454 { this->_S_copy_chars(_M_data(), __beg, __end); }
458 __throw_exception_again;
461 _M_set_length(__dnew);
464 template<typename _CharT, typename _Traits, typename _Alloc>
466 __sso_string_base<_CharT, _Traits, _Alloc>::
467 _M_construct(size_type __n, _CharT __c)
469 if (__n > size_type(_S_local_capacity))
471 _M_data(_M_create(__n, size_type(0)));
476 this->_S_assign(_M_data(), __n, __c);
481 template<typename _CharT, typename _Traits, typename _Alloc>
483 __sso_string_base<_CharT, _Traits, _Alloc>::
484 _M_assign(const __sso_string_base& __rcs)
488 const size_type __rsize = __rcs._M_length();
489 const size_type __capacity = _M_capacity();
491 if (__rsize > __capacity)
493 size_type __new_capacity = __rsize;
494 _CharT* __tmp = _M_create(__new_capacity, __capacity);
497 _M_capacity(__new_capacity);
501 this->_S_copy(_M_data(), __rcs._M_data(), __rsize);
503 _M_set_length(__rsize);
507 template<typename _CharT, typename _Traits, typename _Alloc>
509 __sso_string_base<_CharT, _Traits, _Alloc>::
510 _M_reserve(size_type __res)
512 // Make sure we don't shrink below the current size.
513 if (__res < _M_length())
516 const size_type __capacity = _M_capacity();
517 if (__res != __capacity)
519 if (__res > __capacity
520 || __res > size_type(_S_local_capacity))
522 _CharT* __tmp = _M_create(__res, __capacity);
523 this->_S_copy(__tmp, _M_data(), _M_length() + 1);
528 else if (!_M_is_local())
530 this->_S_copy(_M_local_data, _M_data(), _M_length() + 1);
531 _M_destroy(__capacity);
532 _M_data(_M_local_data);
537 template<typename _CharT, typename _Traits, typename _Alloc>
539 __sso_string_base<_CharT, _Traits, _Alloc>::
540 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
543 const size_type __how_much = _M_length() - __pos - __len1;
545 size_type __new_capacity = _M_length() + __len2 - __len1;
546 _CharT* __r = _M_create(__new_capacity, _M_capacity());
549 this->_S_copy(__r, _M_data(), __pos);
551 this->_S_copy(__r + __pos, __s, __len2);
553 this->_S_copy(__r + __pos + __len2,
554 _M_data() + __pos + __len1, __how_much);
558 _M_capacity(__new_capacity);
561 template<typename _CharT, typename _Traits, typename _Alloc>
563 __sso_string_base<_CharT, _Traits, _Alloc>::
564 _M_erase(size_type __pos, size_type __n)
566 const size_type __how_much = _M_length() - __pos - __n;
568 if (__how_much && __n)
569 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
571 _M_set_length(_M_length() - __n);
574 _GLIBCXX_END_NAMESPACE_VERSION
577 #endif /* _SSO_STRING_BASE_H */