[multiple changes]
[gcc.git] / libstdc++-v3 / include / ext / sso_string_base.h
1 // Short-string-optimized versatile string base -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 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 2, or (at your option)
9 // any later version.
10
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.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file ext/sso_string_base.h
31 * This file is a GNU extension to the Standard C++ Library.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
34 */
35
36 #ifndef _SSO_STRING_BASE_H
37 #define _SSO_STRING_BASE_H 1
38
39 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
40
41 template<typename _CharT, typename _Traits, typename _Alloc>
42 class __sso_string_base
43 : protected __vstring_utility<_CharT, _Traits, _Alloc>
44 {
45 public:
46 typedef _Traits traits_type;
47 typedef typename _Traits::char_type value_type;
48
49 typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
50 typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
51 typedef typename _CharT_alloc_type::size_type size_type;
52
53 private:
54 // The maximum number of individual char_type elements of an
55 // individual string is determined by _S_max_size. This is the
56 // value that will be returned by max_size(). (Whereas npos
57 // is the maximum number of bytes the allocator can allocate.)
58 // If one was to divvy up the theoretical largest size string,
59 // with a terminating character and m _CharT elements, it'd
60 // look like this:
61 // npos = m * sizeof(_CharT) + sizeof(_CharT)
62 // Solving for m:
63 // m = npos / sizeof(_CharT) - 1
64 // In addition, this implementation halfs this amount.
65 enum { _S_max_size = (((static_cast<size_type>(-1)
66 / sizeof(_CharT)) - 1) / 2) };
67
68 // Data Members (private):
69 typename _Util_Base::template _Alloc_hider<_CharT_alloc_type>
70 _M_dataplus;
71 size_type _M_string_length;
72
73 enum { _S_local_capacity = 15 };
74
75 union
76 {
77 _CharT _M_local_data[_S_local_capacity + 1];
78 size_type _M_allocated_capacity;
79 };
80
81 void
82 _M_data(_CharT* __p)
83 { _M_dataplus._M_p = __p; }
84
85 void
86 _M_length(size_type __length)
87 { _M_string_length = __length; }
88
89 void
90 _M_capacity(size_type __capacity)
91 { _M_allocated_capacity = __capacity; }
92
93 bool
94 _M_is_local() const
95 { return _M_data() == _M_local_data; }
96
97 // Create & Destroy
98 _CharT*
99 _M_create(size_type&, size_type);
100
101 void
102 _M_dispose()
103 {
104 if (!_M_is_local())
105 _M_destroy(_M_allocated_capacity);
106 }
107
108 void
109 _M_destroy(size_type) throw();
110
111 // _M_construct_aux is used to implement the 21.3.1 para 15 which
112 // requires special behaviour if _InIterator is an integral type
113 template<typename _InIterator>
114 void
115 _M_construct_aux(_InIterator __beg, _InIterator __end, __false_type)
116 {
117 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
118 _M_construct(__beg, __end, _Tag());
119 }
120
121 template<typename _InIterator>
122 void
123 _M_construct_aux(_InIterator __beg, _InIterator __end, __true_type)
124 { _M_construct(static_cast<size_type>(__beg),
125 static_cast<value_type>(__end)); }
126
127 template<typename _InIterator>
128 void
129 _M_construct(_InIterator __beg, _InIterator __end)
130 {
131 typedef typename std::__is_integer<_InIterator>::__type _Integral;
132 _M_construct_aux(__beg, __end, _Integral());
133 }
134
135 // For Input Iterators, used in istreambuf_iterators, etc.
136 template<typename _InIterator>
137 void
138 _M_construct(_InIterator __beg, _InIterator __end,
139 std::input_iterator_tag);
140
141 // For forward_iterators up to random_access_iterators, used for
142 // string::iterator, _CharT*, etc.
143 template<typename _FwdIterator>
144 void
145 _M_construct(_FwdIterator __beg, _FwdIterator __end,
146 std::forward_iterator_tag);
147
148 void
149 _M_construct(size_type __req, _CharT __c);
150
151 public:
152 size_type
153 _M_max_size() const
154 { return size_type(_S_max_size); }
155
156 _CharT*
157 _M_data() const
158 { return _M_dataplus._M_p; }
159
160 size_type
161 _M_length() const
162 { return _M_string_length; }
163
164 size_type
165 _M_capacity() const
166 {
167 return _M_is_local() ? size_type(_S_local_capacity)
168 : _M_allocated_capacity;
169 }
170
171 bool
172 _M_is_shared() const
173 { return false; }
174
175 void
176 _M_set_leaked() { }
177
178 void
179 _M_leak() { }
180
181 void
182 _M_set_length(size_type __n)
183 {
184 _M_length(__n);
185 traits_type::assign(_M_data()[__n], _CharT());
186 }
187
188 __sso_string_base()
189 : _M_dataplus(_Alloc(), _M_local_data)
190 { _M_set_length(0); }
191
192 __sso_string_base(const _Alloc& __a);
193
194 __sso_string_base(const __sso_string_base& __rcs);
195
196 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a);
197
198 template<typename _InputIterator>
199 __sso_string_base(_InputIterator __beg, _InputIterator __end,
200 const _Alloc& __a);
201
202 ~__sso_string_base()
203 { _M_dispose(); }
204
205 _CharT_alloc_type&
206 _M_get_allocator()
207 { return _M_dataplus; }
208
209 const _CharT_alloc_type&
210 _M_get_allocator() const
211 { return _M_dataplus; }
212
213 void
214 _M_swap(__sso_string_base& __rcs);
215
216 void
217 _M_assign(const __sso_string_base& __rcs);
218
219 void
220 _M_reserve(size_type __res);
221
222 void
223 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
224 size_type __len2);
225
226 void
227 _M_erase(size_type __pos, size_type __n);
228
229 bool
230 _M_compare(const __sso_string_base&) const
231 { return false; }
232 };
233
234 template<typename _CharT, typename _Traits, typename _Alloc>
235 void
236 __sso_string_base<_CharT, _Traits, _Alloc>::
237 _M_destroy(size_type __size) throw()
238 { _M_dataplus._CharT_alloc_type::deallocate(_M_data(), __size + 1); }
239
240 template<typename _CharT, typename _Traits, typename _Alloc>
241 void
242 __sso_string_base<_CharT, _Traits, _Alloc>::
243 _M_swap(__sso_string_base& __rcs)
244 {
245 // _GLIBCXX_RESOLVE_LIB_DEFECTS
246 // 431. Swapping containers with unequal allocators.
247 std::__alloc_swap<_CharT_alloc_type>::_S_do_it(_M_get_allocator(),
248 __rcs._M_get_allocator());
249
250 if (_M_is_local())
251 if (__rcs._M_is_local())
252 {
253 if (_M_length() && __rcs._M_length())
254 {
255 _CharT __tmp_data[_S_local_capacity + 1];
256 traits_type::copy(__tmp_data, __rcs._M_local_data,
257 _S_local_capacity + 1);
258 traits_type::copy(__rcs._M_local_data, _M_local_data,
259 _S_local_capacity + 1);
260 traits_type::copy(_M_local_data, __tmp_data,
261 _S_local_capacity + 1);
262 }
263 else if (__rcs._M_length())
264 {
265 traits_type::copy(_M_local_data, __rcs._M_local_data,
266 _S_local_capacity + 1);
267 _M_length(__rcs._M_length());
268 __rcs._M_set_length(0);
269 return;
270 }
271 else if (_M_length())
272 {
273 traits_type::copy(__rcs._M_local_data, _M_local_data,
274 _S_local_capacity + 1);
275 __rcs._M_length(_M_length());
276 _M_set_length(0);
277 return;
278 }
279 }
280 else
281 {
282 const size_type __tmp_capacity = __rcs._M_allocated_capacity;
283 traits_type::copy(__rcs._M_local_data, _M_local_data,
284 _S_local_capacity + 1);
285 _M_data(__rcs._M_data());
286 __rcs._M_data(__rcs._M_local_data);
287 _M_capacity(__tmp_capacity);
288 }
289 else
290 {
291 const size_type __tmp_capacity = _M_allocated_capacity;
292 if (__rcs._M_is_local())
293 {
294 traits_type::copy(_M_local_data, __rcs._M_local_data,
295 _S_local_capacity + 1);
296 __rcs._M_data(_M_data());
297 _M_data(_M_local_data);
298 }
299 else
300 {
301 _CharT* __tmp_ptr = _M_data();
302 _M_data(__rcs._M_data());
303 __rcs._M_data(__tmp_ptr);
304 _M_capacity(__rcs._M_allocated_capacity);
305 }
306 __rcs._M_capacity(__tmp_capacity);
307 }
308
309 const size_type __tmp_length = _M_length();
310 _M_length(__rcs._M_length());
311 __rcs._M_length(__tmp_length);
312 }
313
314 template<typename _CharT, typename _Traits, typename _Alloc>
315 _CharT*
316 __sso_string_base<_CharT, _Traits, _Alloc>::
317 _M_create(size_type& __capacity, size_type __old_capacity)
318 {
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // 83. String::npos vs. string::max_size()
321 if (__capacity > size_type(_S_max_size))
322 std::__throw_length_error(__N("__sso_string_base::_M_create"));
323
324 // The below implements an exponential growth policy, necessary to
325 // meet amortized linear time requirements of the library: see
326 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
327 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
328 {
329 __capacity = 2 * __old_capacity;
330 // Never allocate a string bigger than _S_max_size.
331 if (__capacity > size_type(_S_max_size))
332 __capacity = size_type(_S_max_size);
333 }
334
335 // NB: Need an array of char_type[__capacity], plus a terminating
336 // null char_type() element.
337 return _M_dataplus._CharT_alloc_type::allocate(__capacity + 1);
338 }
339
340 template<typename _CharT, typename _Traits, typename _Alloc>
341 __sso_string_base<_CharT, _Traits, _Alloc>::
342 __sso_string_base(const _Alloc& __a)
343 : _M_dataplus(__a, _M_local_data)
344 { _M_set_length(0); }
345
346 template<typename _CharT, typename _Traits, typename _Alloc>
347 __sso_string_base<_CharT, _Traits, _Alloc>::
348 __sso_string_base(const __sso_string_base& __rcs)
349 : _M_dataplus(__rcs._M_get_allocator(), _M_local_data)
350 { _M_construct(__rcs._M_data(), __rcs._M_data() + __rcs._M_length()); }
351
352 template<typename _CharT, typename _Traits, typename _Alloc>
353 __sso_string_base<_CharT, _Traits, _Alloc>::
354 __sso_string_base(size_type __n, _CharT __c, const _Alloc& __a)
355 : _M_dataplus(__a, _M_local_data)
356 { _M_construct(__n, __c); }
357
358 template<typename _CharT, typename _Traits, typename _Alloc>
359 template<typename _InputIterator>
360 __sso_string_base<_CharT, _Traits, _Alloc>::
361 __sso_string_base(_InputIterator __beg, _InputIterator __end,
362 const _Alloc& __a)
363 : _M_dataplus(__a, _M_local_data)
364 { _M_construct(__beg, __end); }
365
366 // NB: This is the special case for Input Iterators, used in
367 // istreambuf_iterators, etc.
368 // Input Iterators have a cost structure very different from
369 // pointers, calling for a different coding style.
370 template<typename _CharT, typename _Traits, typename _Alloc>
371 template<typename _InIterator>
372 void
373 __sso_string_base<_CharT, _Traits, _Alloc>::
374 _M_construct(_InIterator __beg, _InIterator __end,
375 std::input_iterator_tag)
376 {
377 size_type __len = 0;
378 size_type __capacity = size_type(_S_local_capacity);
379
380 while (__beg != __end && __len < __capacity)
381 {
382 _M_data()[__len++] = *__beg;
383 ++__beg;
384 }
385
386 try
387 {
388 while (__beg != __end)
389 {
390 if (__len == __capacity)
391 {
392 // Allocate more space.
393 __capacity = __len + 1;
394 _CharT* __another = _M_create(__capacity, __len);
395 _S_copy(__another, _M_data(), __len);
396 _M_dispose();
397 _M_data(__another);
398 _M_capacity(__capacity);
399 }
400 _M_data()[__len++] = *__beg;
401 ++__beg;
402 }
403 }
404 catch(...)
405 {
406 _M_dispose();
407 __throw_exception_again;
408 }
409
410 _M_set_length(__len);
411 }
412
413 template<typename _CharT, typename _Traits, typename _Alloc>
414 template<typename _InIterator>
415 void
416 __sso_string_base<_CharT, _Traits, _Alloc>::
417 _M_construct(_InIterator __beg, _InIterator __end,
418 std::forward_iterator_tag)
419 {
420 // NB: Not required, but considered best practice.
421 if (__builtin_expect(_S_is_null_pointer(__beg) && __beg != __end, 0))
422 std::__throw_logic_error(__N("__sso_string_base::"
423 "_M_construct NULL not valid"));
424
425 size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
426
427 if (__dnew > size_type(_S_local_capacity))
428 {
429 _M_data(_M_create(__dnew, size_type(0)));
430 _M_capacity(__dnew);
431 }
432
433 // Check for out_of_range and length_error exceptions.
434 try
435 { _S_copy_chars(_M_data(), __beg, __end); }
436 catch(...)
437 {
438 _M_dispose();
439 __throw_exception_again;
440 }
441
442 _M_set_length(__dnew);
443 }
444
445 template<typename _CharT, typename _Traits, typename _Alloc>
446 void
447 __sso_string_base<_CharT, _Traits, _Alloc>::
448 _M_construct(size_type __n, _CharT __c)
449 {
450 if (__n > size_type(_S_local_capacity))
451 {
452 _M_data(_M_create(__n, size_type(0)));
453 _M_capacity(__n);
454 }
455
456 if (__n)
457 _S_assign(_M_data(), __n, __c);
458
459 _M_set_length(__n);
460 }
461
462 template<typename _CharT, typename _Traits, typename _Alloc>
463 void
464 __sso_string_base<_CharT, _Traits, _Alloc>::
465 _M_assign(const __sso_string_base& __rcs)
466 {
467 if (this != &__rcs)
468 {
469 const size_type __rsize = __rcs._M_length();
470 const size_type __capacity = _M_capacity();
471
472 if (__rsize > __capacity)
473 {
474 size_type __new_capacity = __rsize;
475 _CharT* __tmp = _M_create(__new_capacity, __capacity);
476 _M_dispose();
477 _M_data(__tmp);
478 _M_capacity(__new_capacity);
479 }
480
481 if (__rsize)
482 _S_copy(_M_data(), __rcs._M_data(), __rsize);
483
484 _M_set_length(__rsize);
485 }
486 }
487
488 template<typename _CharT, typename _Traits, typename _Alloc>
489 void
490 __sso_string_base<_CharT, _Traits, _Alloc>::
491 _M_reserve(size_type __res)
492 {
493 // Make sure we don't shrink below the current size.
494 if (__res < _M_length())
495 __res = _M_length();
496
497 const size_type __capacity = _M_capacity();
498 if (__res != __capacity)
499 {
500 if (__res > __capacity
501 || __res > size_type(_S_local_capacity))
502 {
503 _CharT* __tmp = _M_create(__res, __capacity);
504 _S_copy(__tmp, _M_data(), _M_length() + 1);
505 _M_dispose();
506 _M_data(__tmp);
507 _M_capacity(__res);
508 }
509 else if (!_M_is_local())
510 {
511 _S_copy(_M_local_data, _M_data(), _M_length() + 1);
512 _M_destroy(__capacity);
513 _M_data(_M_local_data);
514 }
515 }
516 }
517
518 template<typename _CharT, typename _Traits, typename _Alloc>
519 void
520 __sso_string_base<_CharT, _Traits, _Alloc>::
521 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
522 const size_type __len2)
523 {
524 const size_type __how_much = _M_length() - __pos - __len1;
525
526 size_type __new_capacity = _M_length() + __len2 - __len1;
527 _CharT* __r = _M_create(__new_capacity, _M_capacity());
528
529 if (__pos)
530 _S_copy(__r, _M_data(), __pos);
531 if (__s && __len2)
532 _S_copy(__r + __pos, __s, __len2);
533 if (__how_much)
534 _S_copy(__r + __pos + __len2,
535 _M_data() + __pos + __len1, __how_much);
536
537 _M_dispose();
538 _M_data(__r);
539 _M_capacity(__new_capacity);
540 }
541
542 template<typename _CharT, typename _Traits, typename _Alloc>
543 void
544 __sso_string_base<_CharT, _Traits, _Alloc>::
545 _M_erase(size_type __pos, size_type __n)
546 {
547 const size_type __how_much = _M_length() - __pos - __n;
548
549 if (__how_much && __n)
550 _S_move(_M_data() + __pos, _M_data() + __pos + __n,
551 __how_much);
552
553 _M_set_length(_M_length() - __n);
554 }
555
556 template<>
557 inline bool
558 __sso_string_base<char, std::char_traits<char>,
559 std::allocator<char> >::
560 _M_compare(const __sso_string_base& __rcs) const
561 {
562 if (this == &__rcs)
563 return true;
564 return false;
565 }
566
567 template<>
568 inline bool
569 __sso_string_base<wchar_t, std::char_traits<wchar_t>,
570 std::allocator<wchar_t> >::
571 _M_compare(const __sso_string_base& __rcs) const
572 {
573 if (this == &__rcs)
574 return true;
575 return false;
576 }
577
578 _GLIBCXX_END_NAMESPACE
579
580 #endif /* _SSO_STRING_BASE_H */