Make FDO more tolerant to source changes
[official-gcc.git] / libstdc++-v3 / include / ext / sso_string_base.h
blob80c88aeb58fc75c03305657e601015660ca10a1a
1 // Short-string-optimized versatile string base -*- C++ -*-
3 // Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/sso_string_base.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ext/vstring.h}
30 #ifndef _SSO_STRING_BASE_H
31 #define _SSO_STRING_BASE_H 1
33 namespace __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>
41 public:
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;
49 private:
50 // Data Members:
51 typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
52 _M_dataplus;
53 size_type _M_string_length;
55 enum { _S_local_capacity = 15 };
57 union
59 _CharT _M_local_data[_S_local_capacity + 1];
60 size_type _M_allocated_capacity;
63 void
64 _M_data(_CharT* __p)
65 { _M_dataplus._M_p = __p; }
67 void
68 _M_length(size_type __length)
69 { _M_string_length = __length; }
71 void
72 _M_capacity(size_type __capacity)
73 { _M_allocated_capacity = __capacity; }
75 bool
76 _M_is_local() const
77 { return _M_data() == _M_local_data; }
79 // Create & Destroy
80 _CharT*
81 _M_create(size_type&, size_type);
83 void
84 _M_dispose()
86 if (!_M_is_local())
87 _M_destroy(_M_allocated_capacity);
90 void
91 _M_destroy(size_type __size) throw()
92 { _M_get_allocator().deallocate(_M_data(), __size + 1); }
94 // _M_construct_aux is used to implement the 21.3.1 para 15 which
95 // requires special behaviour if _InIterator is an integral type
96 template<typename _InIterator>
97 void
98 _M_construct_aux(_InIterator __beg, _InIterator __end,
99 std::__false_type)
101 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
102 _M_construct(__beg, __end, _Tag());
105 // _GLIBCXX_RESOLVE_LIB_DEFECTS
106 // 438. Ambiguity in the "do the right thing" clause
107 template<typename _Integer>
108 void
109 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
110 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
112 void
113 _M_construct_aux_2(size_type __req, _CharT __c)
114 { _M_construct(__req, __c); }
116 template<typename _InIterator>
117 void
118 _M_construct(_InIterator __beg, _InIterator __end)
120 typedef typename std::__is_integer<_InIterator>::__type _Integral;
121 _M_construct_aux(__beg, __end, _Integral());
124 // For Input Iterators, used in istreambuf_iterators, etc.
125 template<typename _InIterator>
126 void
127 _M_construct(_InIterator __beg, _InIterator __end,
128 std::input_iterator_tag);
130 // For forward_iterators up to random_access_iterators, used for
131 // string::iterator, _CharT*, etc.
132 template<typename _FwdIterator>
133 void
134 _M_construct(_FwdIterator __beg, _FwdIterator __end,
135 std::forward_iterator_tag);
137 void
138 _M_construct(size_type __req, _CharT __c);
140 public:
141 size_type
142 _M_max_size() const
143 { return (_M_get_allocator().max_size() - 1) / 2; }
145 _CharT*
146 _M_data() const
147 { return _M_dataplus._M_p; }
149 size_type
150 _M_length() const
151 { return _M_string_length; }
153 size_type
154 _M_capacity() const
156 return _M_is_local() ? size_type(_S_local_capacity)
157 : _M_allocated_capacity;
160 bool
161 _M_is_shared() const
162 { return false; }
164 void
165 _M_set_leaked() { }
167 void
168 _M_leak() { }
170 void
171 _M_set_length(size_type __n)
173 _M_length(__n);
174 traits_type::assign(_M_data()[__n], _CharT());
177 __sso_string_base()
178 : _M_dataplus(_M_local_data)
179 { _M_set_length(0); }
181 __sso_string_base(const _Alloc& __a);
183 __sso_string_base(const __sso_string_base& __rcs);
185 #if __cplusplus >= 201103L
186 __sso_string_base(__sso_string_base&& __rcs);
187 #endif
189 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
191 template<typename _InputIterator>
192 __sso_string_base(_InputIterator __beg, _InputIterator __end,
193 const _Alloc& __a);
195 ~__sso_string_base()
196 { _M_dispose(); }
198 _CharT_alloc_type&
199 _M_get_allocator()
200 { return _M_dataplus; }
202 const _CharT_alloc_type&
203 _M_get_allocator() const
204 { return _M_dataplus; }
206 void
207 _M_swap(__sso_string_base& __rcs);
209 void
210 _M_assign(const __sso_string_base& __rcs);
212 void
213 _M_reserve(size_type __res);
215 void
216 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
217 size_type __len2);
219 void
220 _M_erase(size_type __pos, size_type __n);
222 void
223 _M_clear()
224 { _M_set_length(0); }
226 bool
227 _M_compare(const __sso_string_base&) const
228 { return false; }
231 template<typename _CharT, typename _Traits, typename _Alloc>
232 void
233 __sso_string_base<_CharT, _Traits, _Alloc>::
234 _M_swap(__sso_string_base& __rcs)
236 if (this == &__rcs)
237 return;
239 // _GLIBCXX_RESOLVE_LIB_DEFECTS
240 // 431. Swapping containers with unequal allocators.
241 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
242 __rcs._M_get_allocator());
244 if (_M_is_local())
245 if (__rcs._M_is_local())
247 if (_M_length() && __rcs._M_length())
249 _CharT __tmp_data[_S_local_capacity + 1];
250 traits_type::copy(__tmp_data, __rcs._M_local_data,
251 _S_local_capacity + 1);
252 traits_type::copy(__rcs._M_local_data, _M_local_data,
253 _S_local_capacity + 1);
254 traits_type::copy(_M_local_data, __tmp_data,
255 _S_local_capacity + 1);
257 else if (__rcs._M_length())
259 traits_type::copy(_M_local_data, __rcs._M_local_data,
260 _S_local_capacity + 1);
261 _M_length(__rcs._M_length());
262 __rcs._M_set_length(0);
263 return;
265 else if (_M_length())
267 traits_type::copy(__rcs._M_local_data, _M_local_data,
268 _S_local_capacity + 1);
269 __rcs._M_length(_M_length());
270 _M_set_length(0);
271 return;
274 else
276 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
277 traits_type::copy(__rcs._M_local_data, _M_local_data,
278 _S_local_capacity + 1);
279 _M_data(__rcs._M_data());
280 __rcs._M_data(__rcs._M_local_data);
281 _M_capacity(__tmp_capacity);
283 else
285 const size_type __tmp_capacity = _M_allocated_capacity;
286 if (__rcs._M_is_local())
288 traits_type::copy(_M_local_data, __rcs._M_local_data,
289 _S_local_capacity + 1);
290 __rcs._M_data(_M_data());
291 _M_data(_M_local_data);
293 else
295 _CharT* __tmp_ptr = _M_data();
296 _M_data(__rcs._M_data());
297 __rcs._M_data(__tmp_ptr);
298 _M_capacity(__rcs._M_allocated_capacity);
300 __rcs._M_capacity(__tmp_capacity);
303 const size_type __tmp_length = _M_length();
304 _M_length(__rcs._M_length());
305 __rcs._M_length(__tmp_length);
308 template<typename _CharT, typename _Traits, typename _Alloc>
309 _CharT*
310 __sso_string_base<_CharT, _Traits, _Alloc>::
311 _M_create(size_type& __capacity, size_type __old_capacity)
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // 83. String::npos vs. string::max_size()
315 if (__capacity > _M_max_size())
316 std::__throw_length_error(__N("__sso_string_base::_M_create"));
318 // The below implements an exponential growth policy, necessary to
319 // meet amortized linear time requirements of the library: see
320 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
321 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
323 __capacity = 2 * __old_capacity;
324 // Never allocate a string bigger than max_size.
325 if (__capacity > _M_max_size())
326 __capacity = _M_max_size();
329 // NB: Need an array of char_type[__capacity], plus a terminating
330 // null char_type() element.
331 return _M_get_allocator().allocate(__capacity + 1);
334 template<typename _CharT, typename _Traits, typename _Alloc>
335 __sso_string_base<_CharT, _Traits, _Alloc>::
336 __sso_string_base(const _Alloc& __a)
337 : _M_dataplus(__a, _M_local_data)
338 { _M_set_length(0); }
340 template<typename _CharT, typename _Traits, typename _Alloc>
341 __sso_string_base<_CharT, _Traits, _Alloc>::
342 __sso_string_base(const __sso_string_base& __rcs)
343 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
344 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
346 #if __cplusplus >= 201103L
347 template<typename _CharT, typename _Traits, typename _Alloc>
348 __sso_string_base<_CharT, _Traits, _Alloc>::
349 __sso_string_base(__sso_string_base&& __rcs)
350 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
352 if (__rcs._M_is_local())
354 if (__rcs._M_length())
355 traits_type::copy(_M_local_data, __rcs._M_local_data,
356 _S_local_capacity + 1);
358 else
360 _M_data(__rcs._M_data());
361 _M_capacity(__rcs._M_allocated_capacity);
364 _M_set_length(__rcs._M_length());
365 __rcs._M_data(__rcs._M_local_data);
366 __rcs._M_set_length(0);
368 #endif
370 template<typename _CharT, typename _Traits, typename _Alloc>
371 __sso_string_base<_CharT, _Traits, _Alloc>::
372 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
373 : _M_dataplus(__a, _M_local_data)
374 { _M_construct(__n, __c); }
376 template<typename _CharT, typename _Traits, typename _Alloc>
377 template<typename _InputIterator>
378 __sso_string_base<_CharT, _Traits, _Alloc>::
379 __sso_string_base(_InputIterator __beg, _InputIterator __end,
380 const _Alloc& __a)
381 : _M_dataplus(__a, _M_local_data)
382 { _M_construct(__beg, __end); }
384 // NB: This is the special case for Input Iterators, used in
385 // istreambuf_iterators, etc.
386 // Input Iterators have a cost structure very different from
387 // pointers, calling for a different coding style.
388 template<typename _CharT, typename _Traits, typename _Alloc>
389 template<typename _InIterator>
390 void
391 __sso_string_base<_CharT, _Traits, _Alloc>::
392 _M_construct(_InIterator __beg, _InIterator __end,
393 std::input_iterator_tag)
395 size_type __len = 0;
396 size_type __capacity = size_type(_S_local_capacity);
398 while (__beg != __end && __len < __capacity)
400 _M_data()[__len++] = *__beg;
401 ++__beg;
404 __try
406 while (__beg != __end)
408 if (__len == __capacity)
410 // Allocate more space.
411 __capacity = __len + 1;
412 _CharT* __another = _M_create(__capacity, __len);
413 this->_S_copy(__another, _M_data(), __len);
414 _M_dispose();
415 _M_data(__another);
416 _M_capacity(__capacity);
418 _M_data()[__len++] = *__beg;
419 ++__beg;
422 __catch(...)
424 _M_dispose();
425 __throw_exception_again;
428 _M_set_length(__len);
431 template<typename _CharT, typename _Traits, typename _Alloc>
432 template<typename _InIterator>
433 void
434 __sso_string_base<_CharT, _Traits, _Alloc>::
435 _M_construct(_InIterator __beg, _InIterator __end,
436 std::forward_iterator_tag)
438 // NB: Not required, but considered best practice.
439 if (__is_null_pointer(__beg) && __beg != __end)
440 std::__throw_logic_error(__N("__sso_string_base::"
441 "_M_construct null not valid"));
443 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
445 if (__dnew > size_type(_S_local_capacity))
447 _M_data(_M_create(__dnew, size_type(0)));
448 _M_capacity(__dnew);
451 // Check for out_of_range and length_error exceptions.
452 __try
453 { this->_S_copy_chars(_M_data(), __beg, __end); }
454 __catch(...)
456 _M_dispose();
457 __throw_exception_again;
460 _M_set_length(__dnew);
463 template<typename _CharT, typename _Traits, typename _Alloc>
464 void
465 __sso_string_base<_CharT, _Traits, _Alloc>::
466 _M_construct(size_type __n, _CharT __c)
468 if (__n > size_type(_S_local_capacity))
470 _M_data(_M_create(__n, size_type(0)));
471 _M_capacity(__n);
474 if (__n)
475 this->_S_assign(_M_data(), __n, __c);
477 _M_set_length(__n);
480 template<typename _CharT, typename _Traits, typename _Alloc>
481 void
482 __sso_string_base<_CharT, _Traits, _Alloc>::
483 _M_assign(const __sso_string_base& __rcs)
485 if (this != &__rcs)
487 const size_type __rsize = __rcs._M_length();
488 const size_type __capacity = _M_capacity();
490 if (__rsize > __capacity)
492 size_type __new_capacity = __rsize;
493 _CharT* __tmp = _M_create(__new_capacity, __capacity);
494 _M_dispose();
495 _M_data(__tmp);
496 _M_capacity(__new_capacity);
499 if (__rsize)
500 this->_S_copy(_M_data(), __rcs._M_data(), __rsize);
502 _M_set_length(__rsize);
506 template<typename _CharT, typename _Traits, typename _Alloc>
507 void
508 __sso_string_base<_CharT, _Traits, _Alloc>::
509 _M_reserve(size_type __res)
511 // Make sure we don't shrink below the current size.
512 if (__res < _M_length())
513 __res = _M_length();
515 const size_type __capacity = _M_capacity();
516 if (__res != __capacity)
518 if (__res > __capacity
519 || __res > size_type(_S_local_capacity))
521 _CharT* __tmp = _M_create(__res, __capacity);
522 this->_S_copy(__tmp, _M_data(), _M_length() + 1);
523 _M_dispose();
524 _M_data(__tmp);
525 _M_capacity(__res);
527 else if (!_M_is_local())
529 this->_S_copy(_M_local_data, _M_data(), _M_length() + 1);
530 _M_destroy(__capacity);
531 _M_data(_M_local_data);
536 template<typename _CharT, typename _Traits, typename _Alloc>
537 void
538 __sso_string_base<_CharT, _Traits, _Alloc>::
539 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
540 size_type __len2)
542 const size_type __how_much = _M_length() - __pos - __len1;
544 size_type __new_capacity = _M_length() + __len2 - __len1;
545 _CharT* __r = _M_create(__new_capacity, _M_capacity());
547 if (__pos)
548 this->_S_copy(__r, _M_data(), __pos);
549 if (__s && __len2)
550 this->_S_copy(__r + __pos, __s, __len2);
551 if (__how_much)
552 this->_S_copy(__r + __pos + __len2,
553 _M_data() + __pos + __len1, __how_much);
555 _M_dispose();
556 _M_data(__r);
557 _M_capacity(__new_capacity);
560 template<typename _CharT, typename _Traits, typename _Alloc>
561 void
562 __sso_string_base<_CharT, _Traits, _Alloc>::
563 _M_erase(size_type __pos, size_type __n)
565 const size_type __how_much = _M_length() - __pos - __n;
567 if (__how_much && __n)
568 this->_S_move(_M_data() + __pos, _M_data() + __pos + __n, __how_much);
570 _M_set_length(_M_length() - __n);
573 _GLIBCXX_END_NAMESPACE_VERSION
574 } // namespace
576 #endif /* _SSO_STRING_BASE_H */