Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / ext / sso_string_base.h
blob4899e1fdcb70457deb3b7c730c473b5129baf76c
1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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 _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>
40 public:
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;
48 private:
49 // Data Members:
50 typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
51 _M_dataplus;
52 size_type _M_string_length;
54 enum { _S_local_capacity = 15 };
56 union
58 _CharT _M_local_data[_S_local_capacity + 1];
59 size_type _M_allocated_capacity;
62 void
63 _M_data(_CharT* __p)
64 { _M_dataplus._M_p = __p; }
66 void
67 _M_length(size_type __length)
68 { _M_string_length = __length; }
70 void
71 _M_capacity(size_type __capacity)
72 { _M_allocated_capacity = __capacity; }
74 bool
75 _M_is_local() const
76 { return _M_data() == _M_local_data; }
78 // Create & Destroy
79 _CharT*
80 _M_create(size_type&, size_type);
82 void
83 _M_dispose()
85 if (!_M_is_local())
86 _M_destroy(_M_allocated_capacity);
89 void
90 _M_destroy(size_type __size) throw()
91 { _M_get_allocator().deallocate(_M_data(), __size + 1); }
93 // _M_construct_aux is used to implement the 21.3.1 para 15 which
94 // requires special behaviour if _InIterator is an integral type
95 template<typename _InIterator>
96 void
97 _M_construct_aux(_InIterator __beg, _InIterator __end,
98 std::__false_type)
100 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
101 _M_construct(__beg, __end, _Tag());
104 // _GLIBCXX_RESOLVE_LIB_DEFECTS
105 // 438. Ambiguity in the "do the right thing" clause
106 template<typename _Integer>
107 void
108 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
109 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
111 void
112 _M_construct_aux_2(size_type __req, _CharT __c)
113 { _M_construct(__req, __c); }
115 template<typename _InIterator>
116 void
117 _M_construct(_InIterator __beg, _InIterator __end)
119 typedef typename std::__is_integer<_InIterator>::__type _Integral;
120 _M_construct_aux(__beg, __end, _Integral());
123 // For Input Iterators, used in istreambuf_iterators, etc.
124 template<typename _InIterator>
125 void
126 _M_construct(_InIterator __beg, _InIterator __end,
127 std::input_iterator_tag);
129 // For forward_iterators up to random_access_iterators, used for
130 // string::iterator, _CharT*, etc.
131 template<typename _FwdIterator>
132 void
133 _M_construct(_FwdIterator __beg, _FwdIterator __end,
134 std::forward_iterator_tag);
136 void
137 _M_construct(size_type __req, _CharT __c);
139 public:
140 size_type
141 _M_max_size() const
142 { return (_M_get_allocator().max_size() - 1) / 2; }
144 _CharT*
145 _M_data() const
146 { return _M_dataplus._M_p; }
148 size_type
149 _M_length() const
150 { return _M_string_length; }
152 size_type
153 _M_capacity() const
155 return _M_is_local() ? size_type(_S_local_capacity)
156 : _M_allocated_capacity;
159 bool
160 _M_is_shared() const
161 { return false; }
163 void
164 _M_set_leaked() { }
166 void
167 _M_leak() { }
169 void
170 _M_set_length(size_type __n)
172 _M_length(__n);
173 traits_type::assign(_M_data()[__n], _CharT());
176 __sso_string_base()
177 : _M_dataplus(_M_local_data)
178 { _M_set_length(0); }
180 __sso_string_base(const _Alloc& __a);
182 __sso_string_base(const __sso_string_base& __rcs);
184 #ifdef __GXX_EXPERIMENTAL_CXX0X__
185 __sso_string_base(__sso_string_base&& __rcs);
186 #endif
188 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
190 template<typename _InputIterator>
191 __sso_string_base(_InputIterator __beg, _InputIterator __end,
192 const _Alloc& __a);
194 ~__sso_string_base()
195 { _M_dispose(); }
197 _CharT_alloc_type&
198 _M_get_allocator()
199 { return _M_dataplus; }
201 const _CharT_alloc_type&
202 _M_get_allocator() const
203 { return _M_dataplus; }
205 void
206 _M_swap(__sso_string_base& __rcs);
208 void
209 _M_assign(const __sso_string_base& __rcs);
211 void
212 _M_reserve(size_type __res);
214 void
215 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
216 size_type __len2);
218 void
219 _M_erase(size_type __pos, size_type __n);
221 void
222 _M_clear()
223 { _M_set_length(0); }
225 bool
226 _M_compare(const __sso_string_base&) const
227 { return false; }
230 template<typename _CharT, typename _Traits, typename _Alloc>
231 void
232 __sso_string_base<_CharT, _Traits, _Alloc>::
233 _M_swap(__sso_string_base& __rcs)
235 if (this == &__rcs)
236 return;
238 // _GLIBCXX_RESOLVE_LIB_DEFECTS
239 // 431. Swapping containers with unequal allocators.
240 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
241 __rcs._M_get_allocator());
243 if (_M_is_local())
244 if (__rcs._M_is_local())
246 if (_M_length() && __rcs._M_length())
248 _CharT __tmp_data[_S_local_capacity + 1];
249 traits_type::copy(__tmp_data, __rcs._M_local_data,
250 _S_local_capacity + 1);
251 traits_type::copy(__rcs._M_local_data, _M_local_data,
252 _S_local_capacity + 1);
253 traits_type::copy(_M_local_data, __tmp_data,
254 _S_local_capacity + 1);
256 else if (__rcs._M_length())
258 traits_type::copy(_M_local_data, __rcs._M_local_data,
259 _S_local_capacity + 1);
260 _M_length(__rcs._M_length());
261 __rcs._M_set_length(0);
262 return;
264 else if (_M_length())
266 traits_type::copy(__rcs._M_local_data, _M_local_data,
267 _S_local_capacity + 1);
268 __rcs._M_length(_M_length());
269 _M_set_length(0);
270 return;
273 else
275 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
276 traits_type::copy(__rcs._M_local_data, _M_local_data,
277 _S_local_capacity + 1);
278 _M_data(__rcs._M_data());
279 __rcs._M_data(__rcs._M_local_data);
280 _M_capacity(__tmp_capacity);
282 else
284 const size_type __tmp_capacity = _M_allocated_capacity;
285 if (__rcs._M_is_local())
287 traits_type::copy(_M_local_data, __rcs._M_local_data,
288 _S_local_capacity + 1);
289 __rcs._M_data(_M_data());
290 _M_data(_M_local_data);
292 else
294 _CharT* __tmp_ptr = _M_data();
295 _M_data(__rcs._M_data());
296 __rcs._M_data(__tmp_ptr);
297 _M_capacity(__rcs._M_allocated_capacity);
299 __rcs._M_capacity(__tmp_capacity);
302 const size_type __tmp_length = _M_length();
303 _M_length(__rcs._M_length());
304 __rcs._M_length(__tmp_length);
307 template<typename _CharT, typename _Traits, typename _Alloc>
308 _CharT*
309 __sso_string_base<_CharT, _Traits, _Alloc>::
310 _M_create(size_type& __capacity, size_type __old_capacity)
312 // _GLIBCXX_RESOLVE_LIB_DEFECTS
313 // 83. String::npos vs. string::max_size()
314 if (__capacity > _M_max_size())
315 std::__throw_length_error(__N("__sso_string_base::_M_create"));
317 // The below implements an exponential growth policy, necessary to
318 // meet amortized linear time requirements of the library: see
319 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
320 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
322 __capacity = 2 * __old_capacity;
323 // Never allocate a string bigger than max_size.
324 if (__capacity > _M_max_size())
325 __capacity = _M_max_size();
328 // NB: Need an array of char_type[__capacity], plus a terminating
329 // null char_type() element.
330 return _M_get_allocator().allocate(__capacity + 1);
333 template<typename _CharT, typename _Traits, typename _Alloc>
334 __sso_string_base<_CharT, _Traits, _Alloc>::
335 __sso_string_base(const _Alloc& __a)
336 : _M_dataplus(__a, _M_local_data)
337 { _M_set_length(0); }
339 template<typename _CharT, typename _Traits, typename _Alloc>
340 __sso_string_base<_CharT, _Traits, _Alloc>::
341 __sso_string_base(const __sso_string_base& __rcs)
342 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
343 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
346 template<typename _CharT, typename _Traits, typename _Alloc>
347 __sso_string_base<_CharT, _Traits, _Alloc>::
348 __sso_string_base(__sso_string_base&& __rcs)
349 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
351 if (__rcs._M_is_local())
353 if (__rcs._M_length())
354 traits_type::copy(_M_local_data, __rcs._M_local_data,
355 _S_local_capacity + 1);
357 else
359 _M_data(__rcs._M_data());
360 _M_capacity(__rcs._M_allocated_capacity);
363 _M_length(__rcs._M_length());
364 __rcs._M_length(0);
365 __rcs._M_data(__rcs._M_local_data);
367 #endif
369 template<typename _CharT, typename _Traits, typename _Alloc>
370 __sso_string_base<_CharT, _Traits, _Alloc>::
371 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
372 : _M_dataplus(__a, _M_local_data)
373 { _M_construct(__n, __c); }
375 template<typename _CharT, typename _Traits, typename _Alloc>
376 template<typename _InputIterator>
377 __sso_string_base<_CharT, _Traits, _Alloc>::
378 __sso_string_base(_InputIterator __beg, _InputIterator __end,
379 const _Alloc& __a)
380 : _M_dataplus(__a, _M_local_data)
381 { _M_construct(__beg, __end); }
383 // NB: This is the special case for Input Iterators, used in
384 // istreambuf_iterators, etc.
385 // Input Iterators have a cost structure very different from
386 // pointers, calling for a different coding style.
387 template<typename _CharT, typename _Traits, typename _Alloc>
388 template<typename _InIterator>
389 void
390 __sso_string_base<_CharT, _Traits, _Alloc>::
391 _M_construct(_InIterator __beg, _InIterator __end,
392 std::input_iterator_tag)
394 size_type __len = 0;
395 size_type __capacity = size_type(_S_local_capacity);
397 while (__beg != __end && __len < __capacity)
399 _M_data()[__len++] = *__beg;
400 ++__beg;
403 __try
405 while (__beg != __end)
407 if (__len == __capacity)
409 // Allocate more space.
410 __capacity = __len + 1;
411 _CharT* __another = _M_create(__capacity, __len);
412 _S_copy(__another, _M_data(), __len);
413 _M_dispose();
414 _M_data(__another);
415 _M_capacity(__capacity);
417 _M_data()[__len++] = *__beg;
418 ++__beg;
421 __catch(...)
423 _M_dispose();
424 __throw_exception_again;
427 _M_set_length(__len);
430 template<typename _CharT, typename _Traits, typename _Alloc>
431 template<typename _InIterator>
432 void
433 __sso_string_base<_CharT, _Traits, _Alloc>::
434 _M_construct(_InIterator __beg, _InIterator __end,
435 std::forward_iterator_tag)
437 // NB: Not required, but considered best practice.
438 if (__is_null_pointer(__beg) && __beg != __end)
439 std::__throw_logic_error(__N("__sso_string_base::"
440 "_M_construct null not valid"));
442 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
444 if (__dnew > size_type(_S_local_capacity))
446 _M_data(_M_create(__dnew, size_type(0)));
447 _M_capacity(__dnew);
450 // Check for out_of_range and length_error exceptions.
451 __try
452 { _S_copy_chars(_M_data(), __beg, __end); }
453 __catch(...)
455 _M_dispose();
456 __throw_exception_again;
459 _M_set_length(__dnew);
462 template<typename _CharT, typename _Traits, typename _Alloc>
463 void
464 __sso_string_base<_CharT, _Traits, _Alloc>::
465 _M_construct(size_type __n, _CharT __c)
467 if (__n > size_type(_S_local_capacity))
469 _M_data(_M_create(__n, size_type(0)));
470 _M_capacity(__n);
473 if (__n)
474 _S_assign(_M_data(), __n, __c);
476 _M_set_length(__n);
479 template<typename _CharT, typename _Traits, typename _Alloc>
480 void
481 __sso_string_base<_CharT, _Traits, _Alloc>::
482 _M_assign(const __sso_string_base& __rcs)
484 if (this != &__rcs)
486 const size_type __rsize = __rcs._M_length();
487 const size_type __capacity = _M_capacity();
489 if (__rsize > __capacity)
491 size_type __new_capacity = __rsize;
492 _CharT* __tmp = _M_create(__new_capacity, __capacity);
493 _M_dispose();
494 _M_data(__tmp);
495 _M_capacity(__new_capacity);
498 if (__rsize)
499 _S_copy(_M_data(), __rcs._M_data(), __rsize);
501 _M_set_length(__rsize);
505 template<typename _CharT, typename _Traits, typename _Alloc>
506 void
507 __sso_string_base<_CharT, _Traits, _Alloc>::
508 _M_reserve(size_type __res)
510 // Make sure we don't shrink below the current size.
511 if (__res < _M_length())
512 __res = _M_length();
514 const size_type __capacity = _M_capacity();
515 if (__res != __capacity)
517 if (__res > __capacity
518 || __res > size_type(_S_local_capacity))
520 _CharT* __tmp = _M_create(__res, __capacity);
521 _S_copy(__tmp, _M_data(), _M_length() + 1);
522 _M_dispose();
523 _M_data(__tmp);
524 _M_capacity(__res);
526 else if (!_M_is_local())
528 _S_copy(_M_local_data, _M_data(), _M_length() + 1);
529 _M_destroy(__capacity);
530 _M_data(_M_local_data);
535 template<typename _CharT, typename _Traits, typename _Alloc>
536 void
537 __sso_string_base<_CharT, _Traits, _Alloc>::
538 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
539 size_type __len2)
541 const size_type __how_much = _M_length() - __pos - __len1;
543 size_type __new_capacity = _M_length() + __len2 - __len1;
544 _CharT* __r = _M_create(__new_capacity, _M_capacity());
546 if (__pos)
547 _S_copy(__r, _M_data(), __pos);
548 if (__s && __len2)
549 _S_copy(__r + __pos, __s, __len2);
550 if (__how_much)
551 _S_copy(__r + __pos + __len2,
552 _M_data() + __pos + __len1, __how_much);
554 _M_dispose();
555 _M_data(__r);
556 _M_capacity(__new_capacity);
559 template<typename _CharT, typename _Traits, typename _Alloc>
560 void
561 __sso_string_base<_CharT, _Traits, _Alloc>::
562 _M_erase(size_type __pos, size_type __n)
564 const size_type __how_much = _M_length() - __pos - __n;
566 if (__how_much && __n)
567 _S_move(_M_data() + __pos, _M_data() + __pos + __n,
568 __how_much);
570 _M_set_length(_M_length() - __n);
573 _GLIBCXX_END_NAMESPACE
575 #endif /* _SSO_STRING_BASE_H */