]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // SGI's rope class implementation -*- C++ -*- |
2 | ||
aa118a03 | 3 | // Copyright (C) 2001-2014 Free Software Foundation, Inc. |
42526146 PE |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
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 | ||
748086b7 JJ |
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. | |
42526146 | 19 | |
748086b7 JJ |
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/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * Copyright (c) 1997 | |
27 | * Silicon Graphics Computer Systems, Inc. | |
28 | * | |
29 | * Permission to use, copy, modify, distribute and sell this software | |
30 | * and its documentation for any purpose is hereby granted without fee, | |
31 | * provided that the above copyright notice appear in all copies and | |
32 | * that both that copyright notice and this permission notice appear | |
33 | * in supporting documentation. Silicon Graphics makes no | |
34 | * representations about the suitability of this software for any | |
35 | * purpose. It is provided "as is" without express or implied warranty. | |
36 | */ | |
37 | ||
1b4a6975 PE |
38 | /** @file ropeimpl.h |
39 | * This is an internal header file, included by other library headers. | |
f910786b | 40 | * Do not attempt to use it directly. @headername{ext/rope} |
725dc051 BK |
41 | */ |
42 | ||
fa30fe72 | 43 | #include <cstdio> |
1b391ba9 | 44 | #include <ostream> |
1b4a6975 | 45 | #include <bits/functexcept.h> |
725dc051 | 46 | |
f53d0ff1 PC |
47 | #include <ext/algorithm> // For copy_n and lexicographical_compare_3way |
48 | #include <ext/memory> // For uninitialized_copy_n | |
01b0d4b8 | 49 | #include <ext/numeric> // For power |
f53d0ff1 | 50 | |
12ffa228 BK |
51 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
52 | { | |
53 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 54 | |
b2acb86f BK |
55 | using std::size_t; |
56 | using std::printf; | |
fa30fe72 | 57 | using std::basic_ostream; |
b2acb86f | 58 | using std::__throw_length_error; |
b2acb86f | 59 | using std::_Destroy; |
b4ca0e1a | 60 | using std::__uninitialized_fill_n_a; |
725dc051 | 61 | |
a6048f29 PC |
62 | // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf |
63 | // if necessary. Assumes _M_path_end[leaf_index] and leaf_pos are correct. | |
64 | // Results in a valid buf_ptr if the iterator can be legitimately | |
65 | // dereferenced. | |
66 | template <class _CharT, class _Alloc> | |
67 | void | |
68 | _Rope_iterator_base<_CharT, _Alloc>:: | |
69 | _S_setbuf(_Rope_iterator_base<_CharT, _Alloc>& __x) | |
70 | { | |
71 | const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index]; | |
72 | size_t __leaf_pos = __x._M_leaf_pos; | |
73 | size_t __pos = __x._M_current_pos; | |
725dc051 | 74 | |
a6048f29 PC |
75 | switch(__leaf->_M_tag) |
76 | { | |
46db4159 | 77 | case __detail::_S_leaf: |
a6048f29 PC |
78 | __x._M_buf_start = ((_Rope_RopeLeaf<_CharT, _Alloc>*)__leaf)->_M_data; |
79 | __x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos); | |
80 | __x._M_buf_end = __x._M_buf_start + __leaf->_M_size; | |
81 | break; | |
46db4159 BK |
82 | case __detail::_S_function: |
83 | case __detail::_S_substringfn: | |
a6048f29 PC |
84 | { |
85 | size_t __len = _S_iterator_buf_len; | |
86 | size_t __buf_start_pos = __leaf_pos; | |
87 | size_t __leaf_end = __leaf_pos + __leaf->_M_size; | |
88 | char_producer<_CharT>* __fn = ((_Rope_RopeFunction<_CharT, | |
89 | _Alloc>*)__leaf)->_M_fn; | |
90 | if (__buf_start_pos + __len <= __pos) | |
91 | { | |
92 | __buf_start_pos = __pos - __len / 4; | |
93 | if (__buf_start_pos + __len > __leaf_end) | |
94 | __buf_start_pos = __leaf_end - __len; | |
95 | } | |
96 | if (__buf_start_pos + __len > __leaf_end) | |
97 | __len = __leaf_end - __buf_start_pos; | |
98 | (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf); | |
99 | __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos); | |
100 | __x._M_buf_start = __x._M_tmp_buf; | |
101 | __x._M_buf_end = __x._M_tmp_buf + __len; | |
102 | } | |
103 | break; | |
725dc051 | 104 | default: |
322821b9 | 105 | break; |
a6048f29 | 106 | } |
725dc051 | 107 | } |
725dc051 | 108 | |
a6048f29 PC |
109 | // Set path and buffer inside a rope iterator. We assume that |
110 | // pos and root are already set. | |
111 | template <class _CharT, class _Alloc> | |
112 | void | |
113 | _Rope_iterator_base<_CharT, _Alloc>:: | |
114 | _S_setcache(_Rope_iterator_base<_CharT, _Alloc>& __x) | |
115 | { | |
46db4159 | 116 | const _RopeRep* __path[int(__detail::_S_max_rope_depth) + 1]; |
a6048f29 PC |
117 | const _RopeRep* __curr_rope; |
118 | int __curr_depth = -1; /* index into path */ | |
119 | size_t __curr_start_pos = 0; | |
120 | size_t __pos = __x._M_current_pos; | |
121 | unsigned char __dirns = 0; // Bit vector marking right turns in the path | |
122 | ||
123 | if (__pos >= __x._M_root->_M_size) | |
124 | { | |
125 | __x._M_buf_ptr = 0; | |
126 | return; | |
127 | } | |
128 | __curr_rope = __x._M_root; | |
129 | if (0 != __curr_rope->_M_c_string) | |
130 | { | |
131 | /* Treat the root as a leaf. */ | |
132 | __x._M_buf_start = __curr_rope->_M_c_string; | |
133 | __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size; | |
134 | __x._M_buf_ptr = __curr_rope->_M_c_string + __pos; | |
135 | __x._M_path_end[0] = __curr_rope; | |
136 | __x._M_leaf_index = 0; | |
137 | __x._M_leaf_pos = 0; | |
138 | return; | |
139 | } | |
140 | for(;;) | |
141 | { | |
142 | ++__curr_depth; | |
143 | __path[__curr_depth] = __curr_rope; | |
144 | switch(__curr_rope->_M_tag) | |
725dc051 | 145 | { |
46db4159 BK |
146 | case __detail::_S_leaf: |
147 | case __detail::_S_function: | |
148 | case __detail::_S_substringfn: | |
a6048f29 PC |
149 | __x._M_leaf_pos = __curr_start_pos; |
150 | goto done; | |
46db4159 | 151 | case __detail::_S_concat: |
a6048f29 PC |
152 | { |
153 | _Rope_RopeConcatenation<_CharT, _Alloc>* __c = | |
154 | (_Rope_RopeConcatenation<_CharT, _Alloc>*)__curr_rope; | |
725dc051 BK |
155 | _RopeRep* __left = __c->_M_left; |
156 | size_t __left_len = __left->_M_size; | |
fa30fe72 | 157 | |
725dc051 | 158 | __dirns <<= 1; |
a6048f29 PC |
159 | if (__pos >= __curr_start_pos + __left_len) |
160 | { | |
725dc051 BK |
161 | __dirns |= 1; |
162 | __curr_rope = __c->_M_right; | |
163 | __curr_start_pos += __left_len; | |
a6048f29 PC |
164 | } |
165 | else | |
166 | __curr_rope = __left; | |
167 | } | |
168 | break; | |
725dc051 | 169 | } |
725dc051 | 170 | } |
a6048f29 PC |
171 | done: |
172 | // Copy last section of path into _M_path_end. | |
725dc051 BK |
173 | { |
174 | int __i = -1; | |
a9dd5a46 | 175 | int __j = __curr_depth + 1 - int(_S_path_cache_len); |
725dc051 BK |
176 | |
177 | if (__j < 0) __j = 0; | |
a6048f29 PC |
178 | while (__j <= __curr_depth) |
179 | __x._M_path_end[++__i] = __path[__j++]; | |
725dc051 BK |
180 | __x._M_leaf_index = __i; |
181 | } | |
182 | __x._M_path_directions = __dirns; | |
183 | _S_setbuf(__x); | |
725dc051 | 184 | } |
a6048f29 PC |
185 | |
186 | // Specialized version of the above. Assumes that | |
187 | // the path cache is valid for the previous position. | |
188 | template <class _CharT, class _Alloc> | |
189 | void | |
190 | _Rope_iterator_base<_CharT, _Alloc>:: | |
191 | _S_setcache_for_incr(_Rope_iterator_base<_CharT, _Alloc>& __x) | |
192 | { | |
193 | int __current_index = __x._M_leaf_index; | |
194 | const _RopeRep* __current_node = __x._M_path_end[__current_index]; | |
195 | size_t __len = __current_node->_M_size; | |
196 | size_t __node_start_pos = __x._M_leaf_pos; | |
197 | unsigned char __dirns = __x._M_path_directions; | |
198 | _Rope_RopeConcatenation<_CharT, _Alloc>* __c; | |
199 | ||
200 | if (__x._M_current_pos - __node_start_pos < __len) | |
201 | { | |
202 | /* More stuff in this leaf, we just didn't cache it. */ | |
203 | _S_setbuf(__x); | |
204 | return; | |
205 | } | |
206 | // node_start_pos is starting position of last_node. | |
207 | while (--__current_index >= 0) | |
208 | { | |
209 | if (!(__dirns & 1) /* Path turned left */) | |
210 | break; | |
211 | __current_node = __x._M_path_end[__current_index]; | |
212 | __c = (_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node; | |
213 | // Otherwise we were in the right child. Thus we should pop | |
214 | // the concatenation node. | |
215 | __node_start_pos -= __c->_M_left->_M_size; | |
216 | __dirns >>= 1; | |
217 | } | |
218 | if (__current_index < 0) | |
219 | { | |
220 | // We underflowed the cache. Punt. | |
221 | _S_setcache(__x); | |
222 | return; | |
223 | } | |
224 | __current_node = __x._M_path_end[__current_index]; | |
225 | __c = (_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node; | |
226 | // current_node is a concatenation node. We are positioned on the first | |
227 | // character in its right child. | |
228 | // node_start_pos is starting position of current_node. | |
229 | __node_start_pos += __c->_M_left->_M_size; | |
230 | __current_node = __c->_M_right; | |
231 | __x._M_path_end[++__current_index] = __current_node; | |
232 | __dirns |= 1; | |
46db4159 | 233 | while (__detail::_S_concat == __current_node->_M_tag) |
a6048f29 PC |
234 | { |
235 | ++__current_index; | |
a9dd5a46 | 236 | if (int(_S_path_cache_len) == __current_index) |
a6048f29 PC |
237 | { |
238 | int __i; | |
a9dd5a46 | 239 | for (__i = 0; __i < int(_S_path_cache_len) - 1; __i++) |
725dc051 | 240 | __x._M_path_end[__i] = __x._M_path_end[__i+1]; |
a6048f29 | 241 | --__current_index; |
725dc051 | 242 | } |
a6048f29 PC |
243 | __current_node = |
244 | ((_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node)->_M_left; | |
245 | __x._M_path_end[__current_index] = __current_node; | |
246 | __dirns <<= 1; | |
247 | // node_start_pos is unchanged. | |
725dc051 | 248 | } |
a6048f29 PC |
249 | __x._M_leaf_index = __current_index; |
250 | __x._M_leaf_pos = __node_start_pos; | |
251 | __x._M_path_directions = __dirns; | |
252 | _S_setbuf(__x); | |
725dc051 | 253 | } |
a6048f29 PC |
254 | |
255 | template <class _CharT, class _Alloc> | |
256 | void | |
257 | _Rope_iterator_base<_CharT, _Alloc>:: | |
258 | _M_incr(size_t __n) | |
259 | { | |
260 | _M_current_pos += __n; | |
261 | if (0 != _M_buf_ptr) | |
262 | { | |
263 | size_t __chars_left = _M_buf_end - _M_buf_ptr; | |
264 | if (__chars_left > __n) | |
265 | _M_buf_ptr += __n; | |
266 | else if (__chars_left == __n) | |
267 | { | |
268 | _M_buf_ptr += __n; | |
269 | _S_setcache_for_incr(*this); | |
270 | } | |
271 | else | |
272 | _M_buf_ptr = 0; | |
273 | } | |
725dc051 | 274 | } |
a6048f29 PC |
275 | |
276 | template <class _CharT, class _Alloc> | |
277 | void | |
278 | _Rope_iterator_base<_CharT, _Alloc>:: | |
279 | _M_decr(size_t __n) | |
280 | { | |
281 | if (0 != _M_buf_ptr) | |
282 | { | |
283 | size_t __chars_left = _M_buf_ptr - _M_buf_start; | |
284 | if (__chars_left >= __n) | |
285 | _M_buf_ptr -= __n; | |
286 | else | |
287 | _M_buf_ptr = 0; | |
288 | } | |
289 | _M_current_pos -= __n; | |
725dc051 | 290 | } |
725dc051 | 291 | |
a6048f29 PC |
292 | template <class _CharT, class _Alloc> |
293 | void | |
294 | _Rope_iterator<_CharT, _Alloc>:: | |
295 | _M_check() | |
296 | { | |
297 | if (_M_root_rope->_M_tree_ptr != this->_M_root) | |
298 | { | |
299 | // _Rope was modified. Get things fixed up. | |
300 | _RopeRep::_S_unref(this->_M_root); | |
301 | this->_M_root = _M_root_rope->_M_tree_ptr; | |
302 | _RopeRep::_S_ref(this->_M_root); | |
303 | this->_M_buf_ptr = 0; | |
304 | } | |
305 | } | |
725dc051 | 306 | |
a6048f29 PC |
307 | template <class _CharT, class _Alloc> |
308 | inline | |
309 | _Rope_const_iterator<_CharT, _Alloc>:: | |
310 | _Rope_const_iterator(const _Rope_iterator<_CharT, _Alloc>& __x) | |
311 | : _Rope_iterator_base<_CharT, _Alloc>(__x) | |
312 | { } | |
313 | ||
314 | template <class _CharT, class _Alloc> | |
315 | inline | |
316 | _Rope_iterator<_CharT, _Alloc>:: | |
317 | _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos) | |
318 | : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos), | |
319 | _M_root_rope(&__r) | |
320 | { _RopeRep::_S_ref(this->_M_root); } | |
321 | ||
322 | template <class _CharT, class _Alloc> | |
323 | inline size_t | |
324 | rope<_CharT, _Alloc>:: | |
325 | _S_char_ptr_len(const _CharT* __s) | |
326 | { | |
327 | const _CharT* __p = __s; | |
328 | ||
329 | while (!_S_is0(*__p)) | |
330 | ++__p; | |
331 | return (__p - __s); | |
332 | } | |
725dc051 BK |
333 | |
334 | ||
335 | #ifndef __GC | |
336 | ||
a6048f29 PC |
337 | template <class _CharT, class _Alloc> |
338 | inline void | |
339 | _Rope_RopeRep<_CharT, _Alloc>:: | |
340 | _M_free_c_string() | |
341 | { | |
342 | _CharT* __cstr = _M_c_string; | |
343 | if (0 != __cstr) | |
344 | { | |
345 | size_t __size = this->_M_size + 1; | |
c531371e | 346 | _Destroy(__cstr, __cstr + __size, _M_get_allocator()); |
a6048f29 PC |
347 | this->_Data_deallocate(__cstr, __size); |
348 | } | |
725dc051 | 349 | } |
725dc051 | 350 | |
a6048f29 PC |
351 | template <class _CharT, class _Alloc> |
352 | inline void | |
353 | _Rope_RopeRep<_CharT, _Alloc>:: | |
c531371e | 354 | _S_free_string(_CharT* __s, size_t __n, allocator_type& __a) |
a6048f29 PC |
355 | { |
356 | if (!_S_is_basic_char_type((_CharT*)0)) | |
1985f1cd | 357 | _Destroy(__s, __s + __n, __a); |
a6048f29 PC |
358 | |
359 | // This has to be a static member, so this gets a bit messy | |
360 | __a.deallocate(__s, | |
361 | _Rope_RopeLeaf<_CharT, _Alloc>::_S_rounded_up_size(__n)); | |
725dc051 | 362 | } |
a6048f29 PC |
363 | |
364 | // There are several reasons for not doing this with virtual destructors | |
365 | // and a class specific delete operator: | |
366 | // - A class specific delete operator can't easily get access to | |
367 | // allocator instances if we need them. | |
368 | // - Any virtual function would need a 4 or byte vtable pointer; | |
369 | // this only requires a one byte tag per object. | |
370 | template <class _CharT, class _Alloc> | |
371 | void | |
372 | _Rope_RopeRep<_CharT, _Alloc>:: | |
373 | _M_free_tree() | |
374 | { | |
375 | switch(_M_tag) | |
376 | { | |
46db4159 | 377 | case __detail::_S_leaf: |
a6048f29 PC |
378 | { |
379 | _Rope_RopeLeaf<_CharT, _Alloc>* __l | |
380 | = (_Rope_RopeLeaf<_CharT, _Alloc>*)this; | |
381 | __l->_Rope_RopeLeaf<_CharT, _Alloc>::~_Rope_RopeLeaf(); | |
94df301f | 382 | this->_L_deallocate(__l, 1); |
a6048f29 PC |
383 | break; |
384 | } | |
46db4159 | 385 | case __detail::_S_concat: |
a6048f29 PC |
386 | { |
387 | _Rope_RopeConcatenation<_CharT,_Alloc>* __c | |
388 | = (_Rope_RopeConcatenation<_CharT, _Alloc>*)this; | |
389 | __c->_Rope_RopeConcatenation<_CharT, _Alloc>:: | |
390 | ~_Rope_RopeConcatenation(); | |
94df301f | 391 | this->_C_deallocate(__c, 1); |
a6048f29 PC |
392 | break; |
393 | } | |
46db4159 | 394 | case __detail::_S_function: |
a6048f29 PC |
395 | { |
396 | _Rope_RopeFunction<_CharT, _Alloc>* __f | |
397 | = (_Rope_RopeFunction<_CharT, _Alloc>*)this; | |
398 | __f->_Rope_RopeFunction<_CharT, _Alloc>::~_Rope_RopeFunction(); | |
94df301f | 399 | this->_F_deallocate(__f, 1); |
a6048f29 PC |
400 | break; |
401 | } | |
46db4159 | 402 | case __detail::_S_substringfn: |
a6048f29 PC |
403 | { |
404 | _Rope_RopeSubstring<_CharT, _Alloc>* __ss = | |
405 | (_Rope_RopeSubstring<_CharT, _Alloc>*)this; | |
406 | __ss->_Rope_RopeSubstring<_CharT, _Alloc>:: | |
407 | ~_Rope_RopeSubstring(); | |
94df301f | 408 | this->_S_deallocate(__ss, 1); |
a6048f29 PC |
409 | break; |
410 | } | |
411 | } | |
725dc051 | 412 | } |
725dc051 BK |
413 | #else |
414 | ||
a6048f29 PC |
415 | template <class _CharT, class _Alloc> |
416 | inline void | |
417 | _Rope_RopeRep<_CharT, _Alloc>:: | |
418 | _S_free_string(const _CharT*, size_t, allocator_type) | |
419 | { } | |
725dc051 BK |
420 | |
421 | #endif | |
422 | ||
a6048f29 PC |
423 | // Concatenate a C string onto a leaf rope by copying the rope data. |
424 | // Used for short ropes. | |
425 | template <class _CharT, class _Alloc> | |
426 | typename rope<_CharT, _Alloc>::_RopeLeaf* | |
427 | rope<_CharT, _Alloc>:: | |
428 | _S_leaf_concat_char_iter(_RopeLeaf* __r, const _CharT* __iter, size_t __len) | |
429 | { | |
430 | size_t __old_len = __r->_M_size; | |
431 | _CharT* __new_data = (_CharT*) | |
94df301f | 432 | rope::_Data_allocate(_S_rounded_up_size(__old_len + __len)); |
a6048f29 PC |
433 | _RopeLeaf* __result; |
434 | ||
435 | uninitialized_copy_n(__r->_M_data, __old_len, __new_data); | |
436 | uninitialized_copy_n(__iter, __len, __new_data + __old_len); | |
437 | _S_cond_store_eos(__new_data[__old_len + __len]); | |
bc2631e0 | 438 | __try |
a6048f29 PC |
439 | { |
440 | __result = _S_new_RopeLeaf(__new_data, __old_len + __len, | |
c531371e | 441 | __r->_M_get_allocator()); |
a6048f29 | 442 | } |
bc2631e0 | 443 | __catch(...) |
a6048f29 PC |
444 | { |
445 | _RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len, | |
c531371e | 446 | __r->_M_get_allocator()); |
a6048f29 PC |
447 | __throw_exception_again; |
448 | } | |
449 | return __result; | |
725dc051 | 450 | } |
725dc051 BK |
451 | |
452 | #ifndef __GC | |
a6048f29 PC |
453 | // As above, but it's OK to clobber original if refcount is 1 |
454 | template <class _CharT, class _Alloc> | |
455 | typename rope<_CharT,_Alloc>::_RopeLeaf* | |
456 | rope<_CharT, _Alloc>:: | |
457 | _S_destr_leaf_concat_char_iter(_RopeLeaf* __r, const _CharT* __iter, | |
458 | size_t __len) | |
459 | { | |
460 | if (__r->_M_ref_count > 1) | |
461 | return _S_leaf_concat_char_iter(__r, __iter, __len); | |
462 | size_t __old_len = __r->_M_size; | |
463 | if (_S_allocated_capacity(__old_len) >= __old_len + __len) | |
464 | { | |
465 | // The space has been partially initialized for the standard | |
466 | // character types. But that doesn't matter for those types. | |
467 | uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len); | |
468 | if (_S_is_basic_char_type((_CharT*)0)) | |
725dc051 | 469 | _S_cond_store_eos(__r->_M_data[__old_len + __len]); |
a6048f29 PC |
470 | else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string) |
471 | { | |
472 | __r->_M_free_c_string(); | |
473 | __r->_M_c_string = 0; | |
474 | } | |
475 | __r->_M_size = __old_len + __len; | |
476 | __r->_M_ref_count = 2; | |
477 | return __r; | |
478 | } | |
479 | else | |
480 | { | |
481 | _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len); | |
482 | return __result; | |
725dc051 | 483 | } |
725dc051 | 484 | } |
725dc051 BK |
485 | #endif |
486 | ||
a6048f29 PC |
487 | // Assumes left and right are not 0. |
488 | // Does not increment (nor decrement on exception) child reference counts. | |
489 | // Result has ref count 1. | |
490 | template <class _CharT, class _Alloc> | |
491 | typename rope<_CharT, _Alloc>::_RopeRep* | |
492 | rope<_CharT, _Alloc>:: | |
493 | _S_tree_concat(_RopeRep* __left, _RopeRep* __right) | |
322821b9 | 494 | { |
a6048f29 PC |
495 | _RopeConcatenation* __result = _S_new_RopeConcatenation(__left, __right, |
496 | __left-> | |
c531371e | 497 | _M_get_allocator()); |
a6048f29 PC |
498 | size_t __depth = __result->_M_depth; |
499 | ||
a9dd5a46 PC |
500 | if (__depth > 20 |
501 | && (__result->_M_size < 1000 | |
46db4159 | 502 | || __depth > size_t(__detail::_S_max_rope_depth))) |
a6048f29 PC |
503 | { |
504 | _RopeRep* __balanced; | |
505 | ||
bc2631e0 | 506 | __try |
a6048f29 PC |
507 | { |
508 | __balanced = _S_balance(__result); | |
509 | __result->_M_unref_nonnil(); | |
510 | } | |
bc2631e0 | 511 | __catch(...) |
a6048f29 | 512 | { |
94df301f | 513 | rope::_C_deallocate(__result,1); |
a6048f29 PC |
514 | __throw_exception_again; |
515 | } | |
516 | // In case of exception, we need to deallocate | |
517 | // otherwise dangling result node. But caller | |
518 | // still owns its children. Thus unref is | |
519 | // inappropriate. | |
520 | return __balanced; | |
521 | } | |
522 | else | |
523 | return __result; | |
524 | } | |
fa30fe72 | 525 | |
a6048f29 PC |
526 | template <class _CharT, class _Alloc> |
527 | typename rope<_CharT, _Alloc>::_RopeRep* | |
528 | rope<_CharT, _Alloc>:: | |
529 | _S_concat_char_iter(_RopeRep* __r, const _CharT*__s, size_t __slen) | |
530 | { | |
531 | _RopeRep* __result; | |
532 | if (0 == __slen) | |
533 | { | |
534 | _S_ref(__r); | |
535 | return __r; | |
536 | } | |
537 | if (0 == __r) | |
538 | return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, | |
c531371e | 539 | __r->_M_get_allocator()); |
46db4159 | 540 | if (__r->_M_tag == __detail::_S_leaf |
a9dd5a46 | 541 | && __r->_M_size + __slen <= size_t(_S_copy_max)) |
a6048f29 PC |
542 | { |
543 | __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen); | |
544 | return __result; | |
545 | } | |
46db4159 BK |
546 | if (__detail::_S_concat == __r->_M_tag |
547 | && __detail::_S_leaf == ((_RopeConcatenation*) __r)->_M_right->_M_tag) | |
a6048f29 PC |
548 | { |
549 | _RopeLeaf* __right = | |
550 | (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right); | |
a9dd5a46 | 551 | if (__right->_M_size + __slen <= size_t(_S_copy_max)) |
a6048f29 PC |
552 | { |
553 | _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left; | |
554 | _RopeRep* __nright = | |
555 | _S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen); | |
556 | __left->_M_ref_nonnil(); | |
bc2631e0 | 557 | __try |
a6048f29 | 558 | { __result = _S_tree_concat(__left, __nright); } |
bc2631e0 | 559 | __catch(...) |
a6048f29 PC |
560 | { |
561 | _S_unref(__left); | |
562 | _S_unref(__nright); | |
563 | __throw_exception_again; | |
564 | } | |
565 | return __result; | |
566 | } | |
567 | } | |
568 | _RopeRep* __nright = | |
c531371e | 569 | __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->_M_get_allocator()); |
bc2631e0 | 570 | __try |
322821b9 | 571 | { |
a6048f29 PC |
572 | __r->_M_ref_nonnil(); |
573 | __result = _S_tree_concat(__r, __nright); | |
574 | } | |
bc2631e0 | 575 | __catch(...) |
fa30fe72 | 576 | { |
a6048f29 PC |
577 | _S_unref(__r); |
578 | _S_unref(__nright); | |
322821b9 BK |
579 | __throw_exception_again; |
580 | } | |
a6048f29 | 581 | return __result; |
725dc051 | 582 | } |
a6048f29 PC |
583 | |
584 | #ifndef __GC | |
585 | template <class _CharT, class _Alloc> | |
586 | typename rope<_CharT,_Alloc>::_RopeRep* | |
587 | rope<_CharT,_Alloc>:: | |
588 | _S_destr_concat_char_iter(_RopeRep* __r, const _CharT* __s, size_t __slen) | |
589 | { | |
590 | _RopeRep* __result; | |
591 | if (0 == __r) | |
592 | return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, | |
c531371e | 593 | __r->_M_get_allocator()); |
a6048f29 PC |
594 | size_t __count = __r->_M_ref_count; |
595 | size_t __orig_size = __r->_M_size; | |
596 | if (__count > 1) | |
597 | return _S_concat_char_iter(__r, __s, __slen); | |
598 | if (0 == __slen) | |
599 | { | |
600 | __r->_M_ref_count = 2; // One more than before | |
601 | return __r; | |
602 | } | |
a9dd5a46 | 603 | if (__orig_size + __slen <= size_t(_S_copy_max) |
46db4159 | 604 | && __detail::_S_leaf == __r->_M_tag) |
a6048f29 PC |
605 | { |
606 | __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s, | |
607 | __slen); | |
608 | return __result; | |
609 | } | |
46db4159 | 610 | if (__detail::_S_concat == __r->_M_tag) |
a6048f29 PC |
611 | { |
612 | _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*) | |
613 | __r)->_M_right); | |
46db4159 | 614 | if (__detail::_S_leaf == __right->_M_tag |
a9dd5a46 | 615 | && __right->_M_size + __slen <= size_t(_S_copy_max)) |
322821b9 | 616 | { |
a6048f29 PC |
617 | _RopeRep* __new_right = |
618 | _S_destr_leaf_concat_char_iter(__right, __s, __slen); | |
619 | if (__right == __new_right) | |
620 | __new_right->_M_ref_count = 1; | |
621 | else | |
622 | __right->_M_unref_nonnil(); | |
623 | __r->_M_ref_count = 2; // One more than before. | |
624 | ((_RopeConcatenation*)__r)->_M_right = __new_right; | |
625 | __r->_M_size = __orig_size + __slen; | |
626 | if (0 != __r->_M_c_string) | |
627 | { | |
628 | __r->_M_free_c_string(); | |
629 | __r->_M_c_string = 0; | |
630 | } | |
631 | return __r; | |
322821b9 | 632 | } |
725dc051 | 633 | } |
a6048f29 | 634 | _RopeRep* __right = |
c531371e | 635 | __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->_M_get_allocator()); |
725dc051 | 636 | __r->_M_ref_nonnil(); |
bc2631e0 | 637 | __try |
a6048f29 | 638 | { __result = _S_tree_concat(__r, __right); } |
bc2631e0 | 639 | __catch(...) |
a6048f29 PC |
640 | { |
641 | _S_unref(__r); | |
642 | _S_unref(__right); | |
643 | __throw_exception_again; | |
725dc051 | 644 | } |
a6048f29 | 645 | return __result; |
725dc051 | 646 | } |
725dc051 | 647 | #endif /* !__GC */ |
a6048f29 PC |
648 | |
649 | template <class _CharT, class _Alloc> | |
650 | typename rope<_CharT, _Alloc>::_RopeRep* | |
651 | rope<_CharT, _Alloc>:: | |
652 | _S_concat(_RopeRep* __left, _RopeRep* __right) | |
653 | { | |
654 | if (0 == __left) | |
655 | { | |
656 | _S_ref(__right); | |
657 | return __right; | |
658 | } | |
659 | if (0 == __right) | |
660 | { | |
661 | __left->_M_ref_nonnil(); | |
662 | return __left; | |
663 | } | |
46db4159 | 664 | if (__detail::_S_leaf == __right->_M_tag) |
a6048f29 | 665 | { |
46db4159 | 666 | if (__detail::_S_leaf == __left->_M_tag) |
a6048f29 | 667 | { |
a9dd5a46 | 668 | if (__right->_M_size + __left->_M_size <= size_t(_S_copy_max)) |
a6048f29 PC |
669 | return _S_leaf_concat_char_iter((_RopeLeaf*)__left, |
670 | ((_RopeLeaf*)__right)->_M_data, | |
671 | __right->_M_size); | |
672 | } | |
46db4159 BK |
673 | else if (__detail::_S_concat == __left->_M_tag |
674 | && __detail::_S_leaf == ((_RopeConcatenation*) | |
a6048f29 PC |
675 | __left)->_M_right->_M_tag) |
676 | { | |
677 | _RopeLeaf* __leftright = | |
678 | (_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right); | |
a9dd5a46 PC |
679 | if (__leftright->_M_size |
680 | + __right->_M_size <= size_t(_S_copy_max)) | |
a6048f29 PC |
681 | { |
682 | _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left; | |
683 | _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright, | |
684 | ((_RopeLeaf*) | |
685 | __right)-> | |
686 | _M_data, | |
687 | __right->_M_size); | |
688 | __leftleft->_M_ref_nonnil(); | |
bc2631e0 | 689 | __try |
a6048f29 | 690 | { return(_S_tree_concat(__leftleft, __rest)); } |
bc2631e0 | 691 | __catch(...) |
a6048f29 PC |
692 | { |
693 | _S_unref(__leftleft); | |
694 | _S_unref(__rest); | |
695 | __throw_exception_again; | |
696 | } | |
697 | } | |
698 | } | |
699 | } | |
700 | __left->_M_ref_nonnil(); | |
701 | __right->_M_ref_nonnil(); | |
bc2631e0 | 702 | __try |
a6048f29 | 703 | { return(_S_tree_concat(__left, __right)); } |
bc2631e0 | 704 | __catch(...) |
a6048f29 PC |
705 | { |
706 | _S_unref(__left); | |
707 | _S_unref(__right); | |
708 | __throw_exception_again; | |
725dc051 BK |
709 | } |
710 | } | |
a6048f29 PC |
711 | |
712 | template <class _CharT, class _Alloc> | |
713 | typename rope<_CharT, _Alloc>::_RopeRep* | |
714 | rope<_CharT, _Alloc>:: | |
715 | _S_substring(_RopeRep* __base, size_t __start, size_t __endp1) | |
716 | { | |
717 | if (0 == __base) | |
718 | return 0; | |
719 | size_t __len = __base->_M_size; | |
720 | size_t __adj_endp1; | |
721 | const size_t __lazy_threshold = 128; | |
722 | ||
723 | if (__endp1 >= __len) | |
724 | { | |
725 | if (0 == __start) | |
726 | { | |
727 | __base->_M_ref_nonnil(); | |
728 | return __base; | |
729 | } | |
730 | else | |
725dc051 | 731 | __adj_endp1 = __len; |
a6048f29 | 732 | |
725dc051 | 733 | } |
a6048f29 | 734 | else |
725dc051 | 735 | __adj_endp1 = __endp1; |
a6048f29 PC |
736 | |
737 | switch(__base->_M_tag) | |
738 | { | |
46db4159 | 739 | case __detail::_S_concat: |
725dc051 | 740 | { |
a6048f29 PC |
741 | _RopeConcatenation* __c = (_RopeConcatenation*)__base; |
742 | _RopeRep* __left = __c->_M_left; | |
743 | _RopeRep* __right = __c->_M_right; | |
744 | size_t __left_len = __left->_M_size; | |
745 | _RopeRep* __result; | |
746 | ||
747 | if (__adj_endp1 <= __left_len) | |
748 | return _S_substring(__left, __start, __endp1); | |
749 | else if (__start >= __left_len) | |
750 | return _S_substring(__right, __start - __left_len, | |
751 | __adj_endp1 - __left_len); | |
752 | _Self_destruct_ptr __left_result(_S_substring(__left, | |
753 | __start, | |
754 | __left_len)); | |
755 | _Self_destruct_ptr __right_result(_S_substring(__right, 0, | |
756 | __endp1 | |
757 | - __left_len)); | |
758 | __result = _S_concat(__left_result, __right_result); | |
759 | return __result; | |
725dc051 | 760 | } |
46db4159 | 761 | case __detail::_S_leaf: |
a6048f29 PC |
762 | { |
763 | _RopeLeaf* __l = (_RopeLeaf*)__base; | |
764 | _RopeLeaf* __result; | |
765 | size_t __result_len; | |
766 | if (__start >= __adj_endp1) | |
767 | return 0; | |
768 | __result_len = __adj_endp1 - __start; | |
769 | if (__result_len > __lazy_threshold) | |
770 | goto lazy; | |
771 | #ifdef __GC | |
772 | const _CharT* __section = __l->_M_data + __start; | |
773 | __result = _S_new_RopeLeaf(__section, __result_len, | |
c531371e | 774 | __base->_M_get_allocator()); |
a6048f29 PC |
775 | __result->_M_c_string = 0; // Not eos terminated. |
776 | #else | |
777 | // We should sometimes create substring node instead. | |
778 | __result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__l->_M_data + __start, | |
779 | __result_len, | |
780 | __base-> | |
c531371e | 781 | _M_get_allocator()); |
a6048f29 PC |
782 | #endif |
783 | return __result; | |
784 | } | |
46db4159 | 785 | case __detail::_S_substringfn: |
a6048f29 PC |
786 | // Avoid introducing multiple layers of substring nodes. |
787 | { | |
788 | _RopeSubstring* __old = (_RopeSubstring*)__base; | |
789 | size_t __result_len; | |
790 | if (__start >= __adj_endp1) | |
791 | return 0; | |
792 | __result_len = __adj_endp1 - __start; | |
793 | if (__result_len > __lazy_threshold) | |
794 | { | |
795 | _RopeSubstring* __result = | |
796 | _S_new_RopeSubstring(__old->_M_base, | |
797 | __start + __old->_M_start, | |
798 | __adj_endp1 - __start, | |
c531371e | 799 | __base->_M_get_allocator()); |
a6048f29 PC |
800 | return __result; |
801 | ||
802 | } // *** else fall through: *** | |
803 | } | |
46db4159 | 804 | case __detail::_S_function: |
a6048f29 PC |
805 | { |
806 | _RopeFunction* __f = (_RopeFunction*)__base; | |
807 | _CharT* __section; | |
808 | size_t __result_len; | |
809 | if (__start >= __adj_endp1) | |
810 | return 0; | |
811 | __result_len = __adj_endp1 - __start; | |
812 | ||
813 | if (__result_len > __lazy_threshold) | |
814 | goto lazy; | |
815 | __section = (_CharT*) | |
94df301f | 816 | rope::_Data_allocate(_S_rounded_up_size(__result_len)); |
bc2631e0 | 817 | __try |
a6048f29 | 818 | { (*(__f->_M_fn))(__start, __result_len, __section); } |
bc2631e0 | 819 | __catch(...) |
a6048f29 PC |
820 | { |
821 | _RopeRep::__STL_FREE_STRING(__section, __result_len, | |
c531371e | 822 | __base->_M_get_allocator()); |
a6048f29 PC |
823 | __throw_exception_again; |
824 | } | |
825 | _S_cond_store_eos(__section[__result_len]); | |
826 | return _S_new_RopeLeaf(__section, __result_len, | |
c531371e | 827 | __base->_M_get_allocator()); |
a6048f29 PC |
828 | } |
829 | } | |
830 | lazy: | |
831 | { | |
725dc051 BK |
832 | // Create substring node. |
833 | return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start, | |
c531371e | 834 | __base->_M_get_allocator()); |
a6048f29 | 835 | } |
725dc051 | 836 | } |
725dc051 | 837 | |
a6048f29 PC |
838 | template<class _CharT> |
839 | class _Rope_flatten_char_consumer | |
840 | : public _Rope_char_consumer<_CharT> | |
841 | { | |
725dc051 | 842 | private: |
a6048f29 | 843 | _CharT* _M_buf_ptr; |
725dc051 | 844 | public: |
a6048f29 PC |
845 | |
846 | _Rope_flatten_char_consumer(_CharT* __buffer) | |
847 | { _M_buf_ptr = __buffer; }; | |
848 | ||
849 | ~_Rope_flatten_char_consumer() {} | |
850 | ||
851 | bool | |
852 | operator()(const _CharT* __leaf, size_t __n) | |
853 | { | |
854 | uninitialized_copy_n(__leaf, __n, _M_buf_ptr); | |
855 | _M_buf_ptr += __n; | |
856 | return true; | |
857 | } | |
858 | }; | |
725dc051 | 859 | |
a6048f29 PC |
860 | template<class _CharT> |
861 | class _Rope_find_char_char_consumer | |
862 | : public _Rope_char_consumer<_CharT> | |
863 | { | |
725dc051 | 864 | private: |
a6048f29 | 865 | _CharT _M_pattern; |
725dc051 | 866 | public: |
a6048f29 PC |
867 | size_t _M_count; // Number of nonmatching characters |
868 | ||
869 | _Rope_find_char_char_consumer(_CharT __p) | |
870 | : _M_pattern(__p), _M_count(0) {} | |
871 | ||
872 | ~_Rope_find_char_char_consumer() {} | |
873 | ||
874 | bool | |
875 | operator()(const _CharT* __leaf, size_t __n) | |
876 | { | |
877 | size_t __i; | |
878 | for (__i = 0; __i < __n; __i++) | |
879 | { | |
880 | if (__leaf[__i] == _M_pattern) | |
881 | { | |
882 | _M_count += __i; | |
883 | return false; | |
884 | } | |
885 | } | |
886 | _M_count += __n; return true; | |
887 | } | |
888 | }; | |
fa30fe72 | 889 | |
725dc051 BK |
890 | template<class _CharT, class _Traits> |
891 | // Here _CharT is both the stream and rope character type. | |
a6048f29 PC |
892 | class _Rope_insert_char_consumer |
893 | : public _Rope_char_consumer<_CharT> | |
894 | { | |
725dc051 | 895 | private: |
a6048f29 PC |
896 | typedef basic_ostream<_CharT,_Traits> _Insert_ostream; |
897 | _Insert_ostream& _M_o; | |
725dc051 | 898 | public: |
a6048f29 PC |
899 | _Rope_insert_char_consumer(_Insert_ostream& __writer) |
900 | : _M_o(__writer) {}; | |
901 | ~_Rope_insert_char_consumer() { }; | |
902 | // Caller is presumed to own the ostream | |
903 | bool operator() (const _CharT* __leaf, size_t __n); | |
904 | // Returns true to continue traversal. | |
905 | }; | |
906 | ||
907 | template<class _CharT, class _Traits> | |
908 | bool | |
909 | _Rope_insert_char_consumer<_CharT, _Traits>:: | |
910 | operator()(const _CharT* __leaf, size_t __n) | |
911 | { | |
912 | size_t __i; | |
913 | // We assume that formatting is set up correctly for each element. | |
914 | for (__i = 0; __i < __n; __i++) | |
915 | _M_o.put(__leaf[__i]); | |
916 | return true; | |
917 | } | |
918 | ||
919 | template <class _CharT, class _Alloc> | |
920 | bool | |
921 | rope<_CharT, _Alloc>:: | |
922 | _S_apply_to_pieces(_Rope_char_consumer<_CharT>& __c, | |
923 | const _RopeRep* __r, size_t __begin, size_t __end) | |
924 | { | |
925 | if (0 == __r) | |
926 | return true; | |
927 | switch(__r->_M_tag) | |
928 | { | |
46db4159 | 929 | case __detail::_S_concat: |
a6048f29 PC |
930 | { |
931 | _RopeConcatenation* __conc = (_RopeConcatenation*)__r; | |
932 | _RopeRep* __left = __conc->_M_left; | |
933 | size_t __left_len = __left->_M_size; | |
934 | if (__begin < __left_len) | |
935 | { | |
936 | size_t __left_end = std::min(__left_len, __end); | |
937 | if (!_S_apply_to_pieces(__c, __left, __begin, __left_end)) | |
938 | return false; | |
939 | } | |
940 | if (__end > __left_len) | |
941 | { | |
942 | _RopeRep* __right = __conc->_M_right; | |
943 | size_t __right_start = std::max(__left_len, __begin); | |
944 | if (!_S_apply_to_pieces(__c, __right, | |
945 | __right_start - __left_len, | |
946 | __end - __left_len)) | |
947 | return false; | |
948 | } | |
949 | } | |
950 | return true; | |
46db4159 | 951 | case __detail::_S_leaf: |
a6048f29 PC |
952 | { |
953 | _RopeLeaf* __l = (_RopeLeaf*)__r; | |
954 | return __c(__l->_M_data + __begin, __end - __begin); | |
955 | } | |
46db4159 BK |
956 | case __detail::_S_function: |
957 | case __detail::_S_substringfn: | |
725dc051 | 958 | { |
a6048f29 PC |
959 | _RopeFunction* __f = (_RopeFunction*)__r; |
960 | size_t __len = __end - __begin; | |
961 | bool __result; | |
962 | _CharT* __buffer = | |
963 | (_CharT*)_Alloc().allocate(__len * sizeof(_CharT)); | |
bc2631e0 | 964 | __try |
a6048f29 | 965 | { |
725dc051 BK |
966 | (*(__f->_M_fn))(__begin, __len, __buffer); |
967 | __result = __c(__buffer, __len); | |
59d35672 | 968 | _Alloc().deallocate(__buffer, __len * sizeof(_CharT)); |
725dc051 | 969 | } |
bc2631e0 | 970 | __catch(...) |
a6048f29 PC |
971 | { |
972 | _Alloc().deallocate(__buffer, __len * sizeof(_CharT)); | |
973 | __throw_exception_again; | |
974 | } | |
975 | return __result; | |
725dc051 BK |
976 | } |
977 | default: | |
322821b9 | 978 | return false; |
a6048f29 | 979 | } |
725dc051 | 980 | } |
725dc051 | 981 | |
725dc051 | 982 | template<class _CharT, class _Traits> |
a6048f29 PC |
983 | inline void |
984 | _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n) | |
985 | { | |
986 | char __f = __o.fill(); | |
987 | size_t __i; | |
988 | ||
989 | for (__i = 0; __i < __n; __i++) | |
990 | __o.put(__f); | |
991 | } | |
725dc051 | 992 | |
fa30fe72 | 993 | |
a6048f29 PC |
994 | template <class _CharT> |
995 | inline bool | |
996 | _Rope_is_simple(_CharT*) | |
997 | { return false; } | |
725dc051 | 998 | |
a6048f29 PC |
999 | inline bool |
1000 | _Rope_is_simple(char*) | |
1001 | { return true; } | |
725dc051 | 1002 | |
a6048f29 PC |
1003 | inline bool |
1004 | _Rope_is_simple(wchar_t*) | |
1005 | { return true; } | |
fa30fe72 | 1006 | |
a6048f29 PC |
1007 | template<class _CharT, class _Traits, class _Alloc> |
1008 | basic_ostream<_CharT, _Traits>& | |
1009 | operator<<(basic_ostream<_CharT, _Traits>& __o, | |
1010 | const rope<_CharT, _Alloc>& __r) | |
1011 | { | |
1012 | size_t __w = __o.width(); | |
1013 | bool __left = bool(__o.flags() & std::ios::left); | |
1014 | size_t __pad_len; | |
1015 | size_t __rope_len = __r.size(); | |
1016 | _Rope_insert_char_consumer<_CharT, _Traits> __c(__o); | |
1017 | bool __is_simple = _Rope_is_simple((_CharT*)0); | |
1018 | ||
1019 | if (__rope_len < __w) | |
725dc051 | 1020 | __pad_len = __w - __rope_len; |
a6048f29 | 1021 | else |
725dc051 | 1022 | __pad_len = 0; |
a6048f29 | 1023 | |
725dc051 | 1024 | if (!__is_simple) |
a6048f29 | 1025 | __o.width(__w / __rope_len); |
bc2631e0 | 1026 | __try |
a6048f29 PC |
1027 | { |
1028 | if (__is_simple && !__left && __pad_len > 0) | |
1029 | _Rope_fill(__o, __pad_len); | |
1030 | __r.apply_to_pieces(0, __r.size(), __c); | |
1031 | if (__is_simple && __left && __pad_len > 0) | |
1032 | _Rope_fill(__o, __pad_len); | |
1033 | if (!__is_simple) | |
1034 | __o.width(__w); | |
1035 | } | |
bc2631e0 | 1036 | __catch(...) |
a6048f29 PC |
1037 | { |
1038 | if (!__is_simple) | |
1039 | __o.width(__w); | |
1040 | __throw_exception_again; | |
1041 | } | |
1042 | return __o; | |
725dc051 | 1043 | } |
a6048f29 PC |
1044 | |
1045 | template <class _CharT, class _Alloc> | |
1046 | _CharT* | |
1047 | rope<_CharT, _Alloc>:: | |
1048 | _S_flatten(_RopeRep* __r, size_t __start, size_t __len, | |
1049 | _CharT* __buffer) | |
1050 | { | |
1051 | _Rope_flatten_char_consumer<_CharT> __c(__buffer); | |
1052 | _S_apply_to_pieces(__c, __r, __start, __start + __len); | |
1053 | return(__buffer + __len); | |
1054 | } | |
1055 | ||
1056 | template <class _CharT, class _Alloc> | |
1057 | size_t | |
1058 | rope<_CharT, _Alloc>:: | |
1059 | find(_CharT __pattern, size_t __start) const | |
1060 | { | |
1061 | _Rope_find_char_char_consumer<_CharT> __c(__pattern); | |
1062 | _S_apply_to_pieces(__c, this->_M_tree_ptr, __start, size()); | |
1063 | size_type __result_pos = __start + __c._M_count; | |
1064 | #ifndef __STL_OLD_ROPE_SEMANTICS | |
1065 | if (__result_pos == size()) | |
1066 | __result_pos = npos; | |
1067 | #endif | |
1068 | return __result_pos; | |
1069 | } | |
1070 | ||
1071 | template <class _CharT, class _Alloc> | |
1072 | _CharT* | |
1073 | rope<_CharT, _Alloc>:: | |
1074 | _S_flatten(_RopeRep* __r, _CharT* __buffer) | |
1075 | { | |
1076 | if (0 == __r) | |
1077 | return __buffer; | |
1078 | switch(__r->_M_tag) | |
1079 | { | |
46db4159 | 1080 | case __detail::_S_concat: |
a6048f29 PC |
1081 | { |
1082 | _RopeConcatenation* __c = (_RopeConcatenation*)__r; | |
1083 | _RopeRep* __left = __c->_M_left; | |
1084 | _RopeRep* __right = __c->_M_right; | |
1085 | _CharT* __rest = _S_flatten(__left, __buffer); | |
1086 | return _S_flatten(__right, __rest); | |
1087 | } | |
46db4159 | 1088 | case __detail::_S_leaf: |
a6048f29 PC |
1089 | { |
1090 | _RopeLeaf* __l = (_RopeLeaf*)__r; | |
1091 | return copy_n(__l->_M_data, __l->_M_size, __buffer).second; | |
1092 | } | |
46db4159 BK |
1093 | case __detail::_S_function: |
1094 | case __detail::_S_substringfn: | |
a6048f29 PC |
1095 | // We don't yet do anything with substring nodes. |
1096 | // This needs to be fixed before ropefiles will work well. | |
1097 | { | |
1098 | _RopeFunction* __f = (_RopeFunction*)__r; | |
1099 | (*(__f->_M_fn))(0, __f->_M_size, __buffer); | |
1100 | return __buffer + __f->_M_size; | |
1101 | } | |
725dc051 | 1102 | default: |
a6048f29 PC |
1103 | return 0; |
1104 | } | |
725dc051 | 1105 | } |
725dc051 | 1106 | |
a6048f29 PC |
1107 | // This needs work for _CharT != char |
1108 | template <class _CharT, class _Alloc> | |
1109 | void | |
1110 | rope<_CharT, _Alloc>:: | |
1111 | _S_dump(_RopeRep* __r, int __indent) | |
1112 | { | |
1113 | for (int __i = 0; __i < __indent; __i++) | |
1114 | putchar(' '); | |
1115 | if (0 == __r) | |
1116 | { | |
1117 | printf("NULL\n"); | |
1118 | return; | |
1119 | } | |
b82f782b | 1120 | if (_S_concat == __r->_M_tag) |
a6048f29 PC |
1121 | { |
1122 | _RopeConcatenation* __c = (_RopeConcatenation*)__r; | |
1123 | _RopeRep* __left = __c->_M_left; | |
1124 | _RopeRep* __right = __c->_M_right; | |
1125 | ||
1126 | #ifdef __GC | |
725dc051 | 1127 | printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n", |
a6048f29 PC |
1128 | __r, __r->_M_depth, __r->_M_size, |
1129 | __r->_M_is_balanced? "" : "not"); | |
1130 | #else | |
725dc051 | 1131 | printf("Concatenation %p (rc = %ld, depth = %d, " |
a6048f29 | 1132 | "len = %ld, %s balanced)\n", |
725dc051 BK |
1133 | __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size, |
1134 | __r->_M_is_balanced? "" : "not"); | |
a6048f29 PC |
1135 | #endif |
1136 | _S_dump(__left, __indent + 2); | |
1137 | _S_dump(__right, __indent + 2); | |
1138 | return; | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | char* __kind; | |
1143 | ||
1144 | switch (__r->_M_tag) | |
1145 | { | |
46db4159 | 1146 | case __detail::_S_leaf: |
a6048f29 PC |
1147 | __kind = "Leaf"; |
1148 | break; | |
46db4159 | 1149 | case __detail::_S_function: |
a6048f29 PC |
1150 | __kind = "Function"; |
1151 | break; | |
46db4159 | 1152 | case __detail::_S_substringfn: |
a6048f29 PC |
1153 | __kind = "Function representing substring"; |
1154 | break; | |
725dc051 | 1155 | default: |
a6048f29 PC |
1156 | __kind = "(corrupted kind field!)"; |
1157 | } | |
1158 | #ifdef __GC | |
725dc051 BK |
1159 | printf("%s %p (depth = %d, len = %ld) ", |
1160 | __kind, __r, __r->_M_depth, __r->_M_size); | |
a6048f29 | 1161 | #else |
725dc051 BK |
1162 | printf("%s %p (rc = %ld, depth = %d, len = %ld) ", |
1163 | __kind, __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size); | |
a6048f29 PC |
1164 | #endif |
1165 | if (_S_is_one_byte_char_type((_CharT*)0)) | |
1166 | { | |
1167 | const int __max_len = 40; | |
1168 | _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len)); | |
1169 | _CharT __buffer[__max_len + 1]; | |
1170 | bool __too_big = __r->_M_size > __prefix->_M_size; | |
1171 | ||
1172 | _S_flatten(__prefix, __buffer); | |
1173 | __buffer[__prefix->_M_size] = _S_eos((_CharT*)0); | |
1174 | printf("%s%s\n", (char*)__buffer, | |
1175 | __too_big? "...\n" : "\n"); | |
1176 | } | |
1177 | else | |
725dc051 BK |
1178 | printf("\n"); |
1179 | } | |
1180 | } | |
725dc051 | 1181 | |
a6048f29 PC |
1182 | template <class _CharT, class _Alloc> |
1183 | const unsigned long | |
1184 | rope<_CharT, _Alloc>:: | |
46db4159 | 1185 | _S_min_len[int(__detail::_S_max_rope_depth) + 1] = { |
a6048f29 PC |
1186 | /* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21, |
1187 | /* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377, | |
1188 | /* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181, | |
1189 | /* 18 */6765, /* 19 */10946, /* 20 */17711, /* 21 */28657, /* 22 */46368, | |
1190 | /* 23 */75025, /* 24 */121393, /* 25 */196418, /* 26 */317811, | |
1191 | /* 27 */514229, /* 28 */832040, /* 29 */1346269, /* 30 */2178309, | |
1192 | /* 31 */3524578, /* 32 */5702887, /* 33 */9227465, /* 34 */14930352, | |
1193 | /* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155, | |
1194 | /* 39 */165580141, /* 40 */267914296, /* 41 */433494437, | |
1195 | /* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903, | |
1196 | /* 45 */2971215073u }; | |
1197 | // These are Fibonacci numbers < 2**32. | |
1198 | ||
1199 | template <class _CharT, class _Alloc> | |
1200 | typename rope<_CharT, _Alloc>::_RopeRep* | |
1201 | rope<_CharT, _Alloc>:: | |
1202 | _S_balance(_RopeRep* __r) | |
1203 | { | |
46db4159 | 1204 | _RopeRep* __forest[int(__detail::_S_max_rope_depth) + 1]; |
a6048f29 PC |
1205 | _RopeRep* __result = 0; |
1206 | int __i; | |
1207 | // Invariant: | |
1208 | // The concatenation of forest in descending order is equal to __r. | |
1209 | // __forest[__i]._M_size >= _S_min_len[__i] | |
1210 | // __forest[__i]._M_depth = __i | |
1211 | // References from forest are included in refcount. | |
1212 | ||
46db4159 | 1213 | for (__i = 0; __i <= int(__detail::_S_max_rope_depth); ++__i) |
a6048f29 | 1214 | __forest[__i] = 0; |
bc2631e0 | 1215 | __try |
a6048f29 PC |
1216 | { |
1217 | _S_add_to_forest(__r, __forest); | |
46db4159 | 1218 | for (__i = 0; __i <= int(__detail::_S_max_rope_depth); ++__i) |
a6048f29 PC |
1219 | if (0 != __forest[__i]) |
1220 | { | |
1221 | #ifndef __GC | |
1222 | _Self_destruct_ptr __old(__result); | |
1223 | #endif | |
1224 | __result = _S_concat(__forest[__i], __result); | |
1225 | __forest[__i]->_M_unref_nonnil(); | |
1226 | #if !defined(__GC) && defined(__EXCEPTIONS) | |
1227 | __forest[__i] = 0; | |
1228 | #endif | |
1229 | } | |
1230 | } | |
bc2631e0 | 1231 | __catch(...) |
a6048f29 | 1232 | { |
46db4159 | 1233 | for(__i = 0; __i <= int(__detail::_S_max_rope_depth); __i++) |
a6048f29 PC |
1234 | _S_unref(__forest[__i]); |
1235 | __throw_exception_again; | |
1236 | } | |
1237 | ||
46db4159 | 1238 | if (__result->_M_depth > int(__detail::_S_max_rope_depth)) |
a6048f29 PC |
1239 | __throw_length_error(__N("rope::_S_balance")); |
1240 | return(__result); | |
725dc051 | 1241 | } |
322821b9 | 1242 | |
a6048f29 PC |
1243 | template <class _CharT, class _Alloc> |
1244 | void | |
1245 | rope<_CharT, _Alloc>:: | |
1246 | _S_add_to_forest(_RopeRep* __r, _RopeRep** __forest) | |
725dc051 | 1247 | { |
a6048f29 PC |
1248 | if (__r->_M_is_balanced) |
1249 | { | |
1250 | _S_add_leaf_to_forest(__r, __forest); | |
1251 | return; | |
1252 | } | |
725dc051 | 1253 | |
a6048f29 PC |
1254 | { |
1255 | _RopeConcatenation* __c = (_RopeConcatenation*)__r; | |
1256 | ||
725dc051 BK |
1257 | _S_add_to_forest(__c->_M_left, __forest); |
1258 | _S_add_to_forest(__c->_M_right, __forest); | |
a6048f29 | 1259 | } |
725dc051 | 1260 | } |
725dc051 BK |
1261 | |
1262 | ||
a6048f29 PC |
1263 | template <class _CharT, class _Alloc> |
1264 | void | |
1265 | rope<_CharT, _Alloc>:: | |
1266 | _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest) | |
1267 | { | |
1268 | _RopeRep* __insertee; // included in refcount | |
1269 | _RopeRep* __too_tiny = 0; // included in refcount | |
1270 | int __i; // forest[0..__i-1] is empty | |
1271 | size_t __s = __r->_M_size; | |
1272 | ||
1273 | for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i) | |
1274 | { | |
1275 | if (0 != __forest[__i]) | |
1276 | { | |
1277 | #ifndef __GC | |
725dc051 | 1278 | _Self_destruct_ptr __old(__too_tiny); |
a6048f29 PC |
1279 | #endif |
1280 | __too_tiny = _S_concat_and_set_balanced(__forest[__i], | |
1281 | __too_tiny); | |
1282 | __forest[__i]->_M_unref_nonnil(); | |
1283 | __forest[__i] = 0; | |
1284 | } | |
725dc051 | 1285 | } |
a6048f29 PC |
1286 | { |
1287 | #ifndef __GC | |
1288 | _Self_destruct_ptr __old(__too_tiny); | |
1289 | #endif | |
725dc051 | 1290 | __insertee = _S_concat_and_set_balanced(__too_tiny, __r); |
a6048f29 PC |
1291 | } |
1292 | // Too_tiny dead, and no longer included in refcount. | |
1293 | // Insertee is live and included. | |
1294 | for (;; ++__i) | |
1295 | { | |
1296 | if (0 != __forest[__i]) | |
1297 | { | |
1298 | #ifndef __GC | |
725dc051 | 1299 | _Self_destruct_ptr __old(__insertee); |
a6048f29 PC |
1300 | #endif |
1301 | __insertee = _S_concat_and_set_balanced(__forest[__i], | |
1302 | __insertee); | |
1303 | __forest[__i]->_M_unref_nonnil(); | |
1304 | __forest[__i] = 0; | |
1305 | } | |
46db4159 | 1306 | if (__i == int(__detail::_S_max_rope_depth) |
a6048f29 PC |
1307 | || __insertee->_M_size < _S_min_len[__i+1]) |
1308 | { | |
1309 | __forest[__i] = __insertee; | |
1310 | // refcount is OK since __insertee is now dead. | |
1311 | return; | |
1312 | } | |
725dc051 BK |
1313 | } |
1314 | } | |
725dc051 | 1315 | |
a6048f29 PC |
1316 | template <class _CharT, class _Alloc> |
1317 | _CharT | |
1318 | rope<_CharT, _Alloc>:: | |
1319 | _S_fetch(_RopeRep* __r, size_type __i) | |
1320 | { | |
1321 | __GC_CONST _CharT* __cstr = __r->_M_c_string; | |
1322 | ||
1323 | if (0 != __cstr) | |
1324 | return __cstr[__i]; | |
1325 | for(;;) | |
1326 | { | |
1327 | switch(__r->_M_tag) | |
725dc051 | 1328 | { |
46db4159 | 1329 | case __detail::_S_concat: |
a6048f29 | 1330 | { |
725dc051 BK |
1331 | _RopeConcatenation* __c = (_RopeConcatenation*)__r; |
1332 | _RopeRep* __left = __c->_M_left; | |
1333 | size_t __left_len = __left->_M_size; | |
a6048f29 PC |
1334 | |
1335 | if (__i >= __left_len) | |
1336 | { | |
725dc051 BK |
1337 | __i -= __left_len; |
1338 | __r = __c->_M_right; | |
a6048f29 PC |
1339 | } |
1340 | else | |
1341 | __r = __left; | |
1342 | } | |
1343 | break; | |
46db4159 | 1344 | case __detail::_S_leaf: |
a6048f29 | 1345 | { |
725dc051 BK |
1346 | _RopeLeaf* __l = (_RopeLeaf*)__r; |
1347 | return __l->_M_data[__i]; | |
a6048f29 | 1348 | } |
46db4159 BK |
1349 | case __detail::_S_function: |
1350 | case __detail::_S_substringfn: | |
a6048f29 | 1351 | { |
725dc051 BK |
1352 | _RopeFunction* __f = (_RopeFunction*)__r; |
1353 | _CharT __result; | |
a6048f29 | 1354 | |
725dc051 BK |
1355 | (*(__f->_M_fn))(__i, 1, &__result); |
1356 | return __result; | |
a6048f29 | 1357 | } |
725dc051 | 1358 | } |
a6048f29 | 1359 | } |
725dc051 | 1360 | } |
a6048f29 PC |
1361 | |
1362 | #ifndef __GC | |
1363 | // Return a uniquely referenced character slot for the given | |
1364 | // position, or 0 if that's not possible. | |
1365 | template <class _CharT, class _Alloc> | |
1366 | _CharT* | |
1367 | rope<_CharT, _Alloc>:: | |
1368 | _S_fetch_ptr(_RopeRep* __r, size_type __i) | |
1369 | { | |
46db4159 | 1370 | _RopeRep* __clrstack[__detail::_S_max_rope_depth]; |
a6048f29 PC |
1371 | size_t __csptr = 0; |
1372 | ||
1373 | for(;;) | |
1374 | { | |
1375 | if (__r->_M_ref_count > 1) | |
1376 | return 0; | |
1377 | switch(__r->_M_tag) | |
725dc051 | 1378 | { |
46db4159 | 1379 | case __detail::_S_concat: |
a6048f29 | 1380 | { |
725dc051 BK |
1381 | _RopeConcatenation* __c = (_RopeConcatenation*)__r; |
1382 | _RopeRep* __left = __c->_M_left; | |
1383 | size_t __left_len = __left->_M_size; | |
a6048f29 PC |
1384 | |
1385 | if (__c->_M_c_string != 0) | |
1386 | __clrstack[__csptr++] = __c; | |
1387 | if (__i >= __left_len) | |
1388 | { | |
725dc051 BK |
1389 | __i -= __left_len; |
1390 | __r = __c->_M_right; | |
a6048f29 PC |
1391 | } |
1392 | else | |
1393 | __r = __left; | |
1394 | } | |
1395 | break; | |
46db4159 | 1396 | case __detail::_S_leaf: |
a6048f29 | 1397 | { |
725dc051 BK |
1398 | _RopeLeaf* __l = (_RopeLeaf*)__r; |
1399 | if (__l->_M_c_string != __l->_M_data && __l->_M_c_string != 0) | |
a6048f29 PC |
1400 | __clrstack[__csptr++] = __l; |
1401 | while (__csptr > 0) | |
1402 | { | |
725dc051 BK |
1403 | -- __csptr; |
1404 | _RopeRep* __d = __clrstack[__csptr]; | |
1405 | __d->_M_free_c_string(); | |
1406 | __d->_M_c_string = 0; | |
a6048f29 | 1407 | } |
725dc051 | 1408 | return __l->_M_data + __i; |
a6048f29 | 1409 | } |
46db4159 BK |
1410 | case __detail::_S_function: |
1411 | case __detail::_S_substringfn: | |
a6048f29 | 1412 | return 0; |
725dc051 | 1413 | } |
a6048f29 | 1414 | } |
725dc051 | 1415 | } |
a6048f29 PC |
1416 | #endif /* __GC */ |
1417 | ||
1418 | // The following could be implemented trivially using | |
1419 | // lexicographical_compare_3way. | |
1420 | // We do a little more work to avoid dealing with rope iterators for | |
1421 | // flat strings. | |
1422 | template <class _CharT, class _Alloc> | |
1423 | int | |
1424 | rope<_CharT, _Alloc>:: | |
1425 | _S_compare (const _RopeRep* __left, const _RopeRep* __right) | |
1426 | { | |
1427 | size_t __left_len; | |
1428 | size_t __right_len; | |
1429 | ||
1430 | if (0 == __right) | |
1431 | return 0 != __left; | |
1432 | if (0 == __left) | |
1433 | return -1; | |
1434 | __left_len = __left->_M_size; | |
1435 | __right_len = __right->_M_size; | |
46db4159 | 1436 | if (__detail::_S_leaf == __left->_M_tag) |
a6048f29 PC |
1437 | { |
1438 | _RopeLeaf* __l = (_RopeLeaf*) __left; | |
46db4159 | 1439 | if (__detail::_S_leaf == __right->_M_tag) |
a6048f29 PC |
1440 | { |
1441 | _RopeLeaf* __r = (_RopeLeaf*) __right; | |
1442 | return lexicographical_compare_3way(__l->_M_data, | |
1443 | __l->_M_data + __left_len, | |
1444 | __r->_M_data, __r->_M_data | |
1445 | + __right_len); | |
1446 | } | |
1447 | else | |
1448 | { | |
1449 | const_iterator __rstart(__right, 0); | |
1450 | const_iterator __rend(__right, __right_len); | |
1451 | return lexicographical_compare_3way(__l->_M_data, __l->_M_data | |
1452 | + __left_len, | |
1453 | __rstart, __rend); | |
1454 | } | |
725dc051 | 1455 | } |
a6048f29 PC |
1456 | else |
1457 | { | |
1458 | const_iterator __lstart(__left, 0); | |
1459 | const_iterator __lend(__left, __left_len); | |
46db4159 | 1460 | if (__detail::_S_leaf == __right->_M_tag) |
a6048f29 PC |
1461 | { |
1462 | _RopeLeaf* __r = (_RopeLeaf*) __right; | |
1463 | return lexicographical_compare_3way(__lstart, __lend, | |
1464 | __r->_M_data, __r->_M_data | |
1465 | + __right_len); | |
1466 | } | |
1467 | else | |
1468 | { | |
1469 | const_iterator __rstart(__right, 0); | |
1470 | const_iterator __rend(__right, __right_len); | |
1471 | return lexicographical_compare_3way(__lstart, __lend, | |
1472 | __rstart, __rend); | |
1473 | } | |
725dc051 BK |
1474 | } |
1475 | } | |
a6048f29 PC |
1476 | |
1477 | // Assignment to reference proxies. | |
1478 | template <class _CharT, class _Alloc> | |
1479 | _Rope_char_ref_proxy<_CharT, _Alloc>& | |
1480 | _Rope_char_ref_proxy<_CharT, _Alloc>:: | |
1481 | operator=(_CharT __c) | |
1482 | { | |
1483 | _RopeRep* __old = _M_root->_M_tree_ptr; | |
1484 | #ifndef __GC | |
1485 | // First check for the case in which everything is uniquely | |
1486 | // referenced. In that case we can do this destructively. | |
1487 | _CharT* __ptr = _My_rope::_S_fetch_ptr(__old, _M_pos); | |
1488 | if (0 != __ptr) | |
1489 | { | |
1490 | *__ptr = __c; | |
1491 | return *this; | |
725dc051 | 1492 | } |
a6048f29 PC |
1493 | #endif |
1494 | _Self_destruct_ptr __left(_My_rope::_S_substring(__old, 0, _M_pos)); | |
1495 | _Self_destruct_ptr __right(_My_rope::_S_substring(__old, _M_pos + 1, | |
1496 | __old->_M_size)); | |
1497 | _Self_destruct_ptr __result_left(_My_rope:: | |
1498 | _S_destr_concat_char_iter(__left, | |
1499 | &__c, 1)); | |
1500 | ||
1501 | _RopeRep* __result = _My_rope::_S_concat(__result_left, __right); | |
1502 | #ifndef __GC | |
725dc051 | 1503 | _RopeRep::_S_unref(__old); |
a6048f29 PC |
1504 | #endif |
1505 | _M_root->_M_tree_ptr = __result; | |
1506 | return *this; | |
1507 | } | |
725dc051 | 1508 | |
a6048f29 PC |
1509 | template <class _CharT, class _Alloc> |
1510 | inline _Rope_char_ref_proxy<_CharT, _Alloc>:: | |
1511 | operator _CharT() const | |
1512 | { | |
1513 | if (_M_current_valid) | |
725dc051 | 1514 | return _M_current; |
a6048f29 PC |
1515 | else |
1516 | return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos); | |
725dc051 | 1517 | } |
a6048f29 PC |
1518 | |
1519 | template <class _CharT, class _Alloc> | |
1520 | _Rope_char_ptr_proxy<_CharT, _Alloc> | |
1521 | _Rope_char_ref_proxy<_CharT, _Alloc>:: | |
1522 | operator&() const | |
1523 | { return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this); } | |
1524 | ||
1525 | template <class _CharT, class _Alloc> | |
1526 | rope<_CharT, _Alloc>:: | |
1527 | rope(size_t __n, _CharT __c, const allocator_type& __a) | |
1528 | : _Base(__a) | |
1529 | { | |
1530 | rope<_CharT,_Alloc> __result; | |
1531 | const size_t __exponentiate_threshold = 32; | |
1532 | size_t __exponent; | |
1533 | size_t __rest; | |
1534 | _CharT* __rest_buffer; | |
1535 | _RopeRep* __remainder; | |
1536 | rope<_CharT, _Alloc> __remainder_rope; | |
1537 | ||
1538 | if (0 == __n) | |
1539 | return; | |
1540 | ||
1541 | __exponent = __n / __exponentiate_threshold; | |
1542 | __rest = __n % __exponentiate_threshold; | |
1543 | if (0 == __rest) | |
725dc051 | 1544 | __remainder = 0; |
a6048f29 PC |
1545 | else |
1546 | { | |
1547 | __rest_buffer = this->_Data_allocate(_S_rounded_up_size(__rest)); | |
1985f1cd | 1548 | __uninitialized_fill_n_a(__rest_buffer, __rest, __c, |
c531371e | 1549 | _M_get_allocator()); |
a6048f29 | 1550 | _S_cond_store_eos(__rest_buffer[__rest]); |
bc2631e0 | 1551 | __try |
c531371e PC |
1552 | { __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, |
1553 | _M_get_allocator()); } | |
bc2631e0 | 1554 | __catch(...) |
a6048f29 | 1555 | { |
c531371e PC |
1556 | _RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, |
1557 | _M_get_allocator()); | |
a6048f29 PC |
1558 | __throw_exception_again; |
1559 | } | |
725dc051 | 1560 | } |
a6048f29 PC |
1561 | __remainder_rope._M_tree_ptr = __remainder; |
1562 | if (__exponent != 0) | |
1563 | { | |
1564 | _CharT* __base_buffer = | |
1565 | this->_Data_allocate(_S_rounded_up_size(__exponentiate_threshold)); | |
1566 | _RopeLeaf* __base_leaf; | |
1567 | rope __base_rope; | |
1985f1cd | 1568 | __uninitialized_fill_n_a(__base_buffer, __exponentiate_threshold, __c, |
c531371e | 1569 | _M_get_allocator()); |
a6048f29 | 1570 | _S_cond_store_eos(__base_buffer[__exponentiate_threshold]); |
bc2631e0 | 1571 | __try |
a6048f29 PC |
1572 | { |
1573 | __base_leaf = _S_new_RopeLeaf(__base_buffer, | |
c531371e PC |
1574 | __exponentiate_threshold, |
1575 | _M_get_allocator()); | |
a6048f29 | 1576 | } |
bc2631e0 | 1577 | __catch(...) |
a6048f29 PC |
1578 | { |
1579 | _RopeRep::__STL_FREE_STRING(__base_buffer, | |
c531371e PC |
1580 | __exponentiate_threshold, |
1581 | _M_get_allocator()); | |
a6048f29 PC |
1582 | __throw_exception_again; |
1583 | } | |
1584 | __base_rope._M_tree_ptr = __base_leaf; | |
1585 | if (1 == __exponent) | |
1586 | __result = __base_rope; | |
1587 | else | |
1588 | __result = power(__base_rope, __exponent, | |
1589 | _Rope_Concat_fn<_CharT, _Alloc>()); | |
1590 | ||
1591 | if (0 != __remainder) | |
1592 | __result += __remainder_rope; | |
725dc051 | 1593 | } |
a6048f29 | 1594 | else |
725dc051 | 1595 | __result = __remainder_rope; |
a6048f29 PC |
1596 | |
1597 | this->_M_tree_ptr = __result._M_tree_ptr; | |
1598 | this->_M_tree_ptr->_M_ref_nonnil(); | |
725dc051 | 1599 | } |
a6048f29 PC |
1600 | |
1601 | template<class _CharT, class _Alloc> | |
1602 | _CharT | |
1603 | rope<_CharT, _Alloc>::_S_empty_c_str[1]; | |
1604 | ||
1605 | template<class _CharT, class _Alloc> | |
1606 | const _CharT* | |
1607 | rope<_CharT, _Alloc>:: | |
1608 | c_str() const | |
1609 | { | |
1610 | if (0 == this->_M_tree_ptr) | |
1611 | { | |
1612 | _S_empty_c_str[0] = _S_eos((_CharT*)0); // Possibly redundant, | |
1613 | // but probably fast. | |
1614 | return _S_empty_c_str; | |
1615 | } | |
1616 | __gthread_mutex_lock (&this->_M_tree_ptr->_M_c_string_lock); | |
1617 | __GC_CONST _CharT* __result = this->_M_tree_ptr->_M_c_string; | |
1618 | if (0 == __result) | |
1619 | { | |
1620 | size_t __s = size(); | |
1621 | __result = this->_Data_allocate(__s + 1); | |
1622 | _S_flatten(this->_M_tree_ptr, __result); | |
1623 | __result[__s] = _S_eos((_CharT*)0); | |
1624 | this->_M_tree_ptr->_M_c_string = __result; | |
1625 | } | |
1626 | __gthread_mutex_unlock (&this->_M_tree_ptr->_M_c_string_lock); | |
1627 | return(__result); | |
725dc051 | 1628 | } |
a6048f29 PC |
1629 | |
1630 | template<class _CharT, class _Alloc> | |
1631 | const _CharT* rope<_CharT, _Alloc>:: | |
1632 | replace_with_c_str() | |
1633 | { | |
1634 | if (0 == this->_M_tree_ptr) | |
1635 | { | |
1636 | _S_empty_c_str[0] = _S_eos((_CharT*)0); | |
1637 | return _S_empty_c_str; | |
1638 | } | |
1639 | __GC_CONST _CharT* __old_c_string = this->_M_tree_ptr->_M_c_string; | |
46db4159 | 1640 | if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag |
a6048f29 | 1641 | && 0 != __old_c_string) |
725dc051 | 1642 | return(__old_c_string); |
a6048f29 PC |
1643 | size_t __s = size(); |
1644 | _CharT* __result = this->_Data_allocate(_S_rounded_up_size(__s)); | |
1645 | _S_flatten(this->_M_tree_ptr, __result); | |
1646 | __result[__s] = _S_eos((_CharT*)0); | |
1647 | this->_M_tree_ptr->_M_unref_nonnil(); | |
1648 | this->_M_tree_ptr = _S_new_RopeLeaf(__result, __s, | |
c531371e | 1649 | this->_M_get_allocator()); |
a6048f29 PC |
1650 | return(__result); |
1651 | } | |
1652 | ||
1653 | // Algorithm specializations. More should be added. | |
1654 | ||
1655 | template<class _Rope_iterator> // was templated on CharT and Alloc | |
1656 | void // VC++ workaround | |
1657 | _Rope_rotate(_Rope_iterator __first, | |
1658 | _Rope_iterator __middle, | |
1659 | _Rope_iterator __last) | |
1660 | { | |
1661 | typedef typename _Rope_iterator::value_type _CharT; | |
1662 | typedef typename _Rope_iterator::_allocator_type _Alloc; | |
1663 | ||
1664 | rope<_CharT, _Alloc>& __r(__first.container()); | |
1665 | rope<_CharT, _Alloc> __prefix = __r.substr(0, __first.index()); | |
1666 | rope<_CharT, _Alloc> __suffix = | |
1667 | __r.substr(__last.index(), __r.size() - __last.index()); | |
1668 | rope<_CharT, _Alloc> __part1 = | |
1669 | __r.substr(__middle.index(), __last.index() - __middle.index()); | |
1670 | rope<_CharT, _Alloc> __part2 = | |
1671 | __r.substr(__first.index(), __middle.index() - __first.index()); | |
1672 | __r = __prefix; | |
1673 | __r += __part1; | |
1674 | __r += __part2; | |
1675 | __r += __suffix; | |
725dc051 | 1676 | } |
725dc051 BK |
1677 | |
1678 | #if !defined(__GNUC__) | |
a6048f29 PC |
1679 | // Appears to confuse g++ |
1680 | inline void | |
1681 | rotate(_Rope_iterator<char, __STL_DEFAULT_ALLOCATOR(char)> __first, | |
1682 | _Rope_iterator<char, __STL_DEFAULT_ALLOCATOR(char)> __middle, | |
1683 | _Rope_iterator<char, __STL_DEFAULT_ALLOCATOR(char)> __last) | |
1684 | { _Rope_rotate(__first, __middle, __last); } | |
725dc051 BK |
1685 | #endif |
1686 | ||
1687 | # if 0 | |
a6048f29 PC |
1688 | // Probably not useful for several reasons: |
1689 | // - for SGIs 7.1 compiler and probably some others, | |
1690 | // this forces lots of rope<wchar_t, ...> instantiations, creating a | |
1691 | // code bloat and compile time problem. (Fixed in 7.2.) | |
1692 | // - wchar_t is 4 bytes wide on most UNIX platforms, making it | |
1693 | // unattractive for unicode strings. Unsigned short may be a better | |
1694 | // character type. | |
1695 | inline void | |
1696 | rotate(_Rope_iterator<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __first, | |
1697 | _Rope_iterator<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __middle, | |
1698 | _Rope_iterator<wchar_t, __STL_DEFAULT_ALLOCATOR(char)> __last) | |
1699 | { _Rope_rotate(__first, __middle, __last); } | |
725dc051 BK |
1700 | # endif |
1701 | ||
12ffa228 BK |
1702 | _GLIBCXX_END_NAMESPACE_VERSION |
1703 | } // namespace |