1193326Sed// SGI's rope class implementation -*- C++ -*-
2193326Sed
3193326Sed// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4193326Sed// Free Software Foundation, Inc.
5193326Sed//
6193326Sed// This file is part of the GNU ISO C++ Library.  This library is free
7193326Sed// software; you can redistribute it and/or modify it under the
8193326Sed// terms of the GNU General Public License as published by the
9193326Sed// Free Software Foundation; either version 2, or (at your option)
10193326Sed// any later version.
11193326Sed
12193326Sed// This library is distributed in the hope that it will be useful,
13193326Sed// but WITHOUT ANY WARRANTY; without even the implied warranty of
14193326Sed// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15193326Sed// GNU General Public License for more details.
16193326Sed
17193326Sed// You should have received a copy of the GNU General Public License along
18193326Sed// with this library; see the file COPYING.  If not, write to the Free
19198092Srdivacky// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20193326Sed// USA.
21193326Sed
22193326Sed// As a special exception, you may use this file as part of a free software
23193326Sed// library without restriction.  Specifically, if other files instantiate
24193326Sed// templates or use macros or inline functions from this file, or you compile
25193326Sed// this file and link it with other files to produce an executable, this
26193326Sed// file does not by itself cause the resulting executable to be covered by
27193326Sed// the GNU General Public License.  This exception does not however
28193326Sed// invalidate any other reasons why the executable file might be covered by
29193326Sed// the GNU General Public License.
30193326Sed
31193326Sed/*
32193326Sed * Copyright (c) 1997
33193326Sed * Silicon Graphics Computer Systems, Inc.
34193326Sed *
35193326Sed * Permission to use, copy, modify, distribute and sell this software
36193326Sed * and its documentation for any purpose is hereby granted without fee,
37193326Sed * provided that the above copyright notice appear in all copies and
38193326Sed * that both that copyright notice and this permission notice appear
39193326Sed * in supporting documentation.  Silicon Graphics makes no
40193326Sed * representations about the suitability of this software for any
41193326Sed * purpose.  It is provided "as is" without express or implied warranty.
42193326Sed */
43193326Sed
44200583Srdivacky/** @file ropeimpl.h
45193326Sed *  This is an internal header file, included by other library headers.
46198092Srdivacky *  You should not attempt to use it directly.
47193326Sed */
48193326Sed
49200583Srdivacky#include <cstdio>
50200583Srdivacky#include <ostream>
51200583Srdivacky#include <bits/functexcept.h>
52200583Srdivacky
53200583Srdivacky#include <ext/algorithm> // For copy_n and lexicographical_compare_3way
54200583Srdivacky#include <ext/memory> // For uninitialized_copy_n
55200583Srdivacky#include <ext/numeric> // For power
56200583Srdivacky
57193326Sed_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
58193326Sed
59200583Srdivacky  using std::size_t;
60193326Sed  using std::printf;
61200583Srdivacky  using std::basic_ostream;
62200583Srdivacky  using std::__throw_length_error;
63200583Srdivacky  using std::_Destroy;
64200583Srdivacky  using std::uninitialized_fill_n;
65200583Srdivacky
66200583Srdivacky  // Set buf_start, buf_end, and buf_ptr appropriately, filling tmp_buf
67200583Srdivacky  // if necessary.  Assumes _M_path_end[leaf_index] and leaf_pos are correct.
68200583Srdivacky  // Results in a valid buf_ptr if the iterator can be legitimately
69200583Srdivacky  // dereferenced.
70200583Srdivacky  template <class _CharT, class _Alloc>
71200583Srdivacky    void
72200583Srdivacky    _Rope_iterator_base<_CharT, _Alloc>::
73200583Srdivacky    _S_setbuf(_Rope_iterator_base<_CharT, _Alloc>& __x)
74200583Srdivacky    {
75200583Srdivacky      const _RopeRep* __leaf = __x._M_path_end[__x._M_leaf_index];
76200583Srdivacky      size_t __leaf_pos = __x._M_leaf_pos;
77198092Srdivacky      size_t __pos = __x._M_current_pos;
78198092Srdivacky
79200583Srdivacky      switch(__leaf->_M_tag)
80198092Srdivacky	{
81198092Srdivacky	case __detail::_S_leaf:
82193326Sed	  __x._M_buf_start = ((_Rope_RopeLeaf<_CharT, _Alloc>*)__leaf)->_M_data;
83193326Sed	  __x._M_buf_ptr = __x._M_buf_start + (__pos - __leaf_pos);
84193326Sed	  __x._M_buf_end = __x._M_buf_start + __leaf->_M_size;
85198092Srdivacky	  break;
86193326Sed	case __detail::_S_function:
87193326Sed	case __detail::_S_substringfn:
88193326Sed	  {
89198092Srdivacky	    size_t __len = _S_iterator_buf_len;
90193326Sed	    size_t __buf_start_pos = __leaf_pos;
91193326Sed	    size_t __leaf_end = __leaf_pos + __leaf->_M_size;
92193326Sed	    char_producer<_CharT>* __fn = ((_Rope_RopeFunction<_CharT,
93193326Sed					    _Alloc>*)__leaf)->_M_fn;
94193326Sed	    if (__buf_start_pos + __len <= __pos)
95193326Sed	      {
96193326Sed		__buf_start_pos = __pos - __len / 4;
97193326Sed		if (__buf_start_pos + __len > __leaf_end)
98193326Sed		  __buf_start_pos = __leaf_end - __len;
99193326Sed	      }
100193326Sed	    if (__buf_start_pos + __len > __leaf_end)
101193326Sed	      __len = __leaf_end - __buf_start_pos;
102198092Srdivacky	    (*__fn)(__buf_start_pos - __leaf_pos, __len, __x._M_tmp_buf);
103193326Sed	    __x._M_buf_ptr = __x._M_tmp_buf + (__pos - __buf_start_pos);
104193326Sed	    __x._M_buf_start = __x._M_tmp_buf;
105198092Srdivacky	    __x._M_buf_end = __x._M_tmp_buf + __len;
106193326Sed	  }
107193326Sed	  break;
108198092Srdivacky	default:
109193326Sed	  break;
110193326Sed	}
111193326Sed    }
112193326Sed
113193326Sed  // Set path and buffer inside a rope iterator.  We assume that
114198092Srdivacky  // pos and root are already set.
115193326Sed  template <class _CharT, class _Alloc>
116193326Sed    void
117193326Sed    _Rope_iterator_base<_CharT, _Alloc>::
118193326Sed    _S_setcache(_Rope_iterator_base<_CharT, _Alloc>& __x)
119193326Sed    {
120198092Srdivacky      const _RopeRep* __path[int(__detail::_S_max_rope_depth) + 1];
121193326Sed      const _RopeRep* __curr_rope;
122193326Sed      int __curr_depth = -1;  /* index into path    */
123193326Sed      size_t __curr_start_pos = 0;
124193326Sed      size_t __pos = __x._M_current_pos;
125193326Sed      unsigned char __dirns = 0; // Bit vector marking right turns in the path
126193326Sed
127193326Sed      if (__pos >= __x._M_root->_M_size)
128193326Sed	{
129193326Sed	  __x._M_buf_ptr = 0;
130193326Sed	  return;
131193326Sed	}
132193326Sed      __curr_rope = __x._M_root;
133193326Sed      if (0 != __curr_rope->_M_c_string)
134193326Sed	{
135198092Srdivacky	  /* Treat the root as a leaf. */
136193326Sed	  __x._M_buf_start = __curr_rope->_M_c_string;
137198092Srdivacky	  __x._M_buf_end = __curr_rope->_M_c_string + __curr_rope->_M_size;
138193326Sed	  __x._M_buf_ptr = __curr_rope->_M_c_string + __pos;
139193326Sed	  __x._M_path_end[0] = __curr_rope;
140193326Sed	  __x._M_leaf_index = 0;
141193326Sed	  __x._M_leaf_pos = 0;
142193326Sed	  return;
143193326Sed	}
144193326Sed      for(;;)
145193326Sed	{
146193326Sed	  ++__curr_depth;
147193326Sed	  __path[__curr_depth] = __curr_rope;
148193326Sed	  switch(__curr_rope->_M_tag)
149198092Srdivacky	    {
150193326Sed	    case __detail::_S_leaf:
151193326Sed	    case __detail::_S_function:
152193326Sed	    case __detail::_S_substringfn:
153193326Sed	      __x._M_leaf_pos = __curr_start_pos;
154193326Sed	      goto done;
155193326Sed	    case __detail::_S_concat:
156198092Srdivacky	      {
157193326Sed		_Rope_RopeConcatenation<_CharT, _Alloc>* __c =
158193326Sed		  (_Rope_RopeConcatenation<_CharT, _Alloc>*)__curr_rope;
159193326Sed		_RopeRep* __left = __c->_M_left;
160193326Sed		size_t __left_len = __left->_M_size;
161193326Sed
162193326Sed		__dirns <<= 1;
163193326Sed		if (__pos >= __curr_start_pos + __left_len)
164198092Srdivacky		  {
165193326Sed		    __dirns |= 1;
166193326Sed		    __curr_rope = __c->_M_right;
167193326Sed		    __curr_start_pos += __left_len;
168193326Sed		  }
169193326Sed		else
170193326Sed		  __curr_rope = __left;
171193326Sed	      }
172193326Sed	      break;
173193326Sed	    }
174193326Sed	}
175193326Sed    done:
176193326Sed      // Copy last section of path into _M_path_end.
177193326Sed      {
178193326Sed	int __i = -1;
179193326Sed	int __j = __curr_depth + 1 - int(_S_path_cache_len);
180193326Sed
181193326Sed	if (__j < 0) __j = 0;
182193326Sed	while (__j <= __curr_depth)
183198092Srdivacky	  __x._M_path_end[++__i] = __path[__j++];
184193326Sed	__x._M_leaf_index = __i;
185193326Sed      }
186193326Sed      __x._M_path_directions = __dirns;
187193326Sed      _S_setbuf(__x);
188193326Sed    }
189198092Srdivacky
190193326Sed  // Specialized version of the above.  Assumes that
191193326Sed  // the path cache is valid for the previous position.
192193326Sed  template <class _CharT, class _Alloc>
193193326Sed    void
194193326Sed    _Rope_iterator_base<_CharT, _Alloc>::
195193326Sed    _S_setcache_for_incr(_Rope_iterator_base<_CharT, _Alloc>& __x)
196193326Sed    {
197193326Sed      int __current_index = __x._M_leaf_index;
198193326Sed      const _RopeRep* __current_node = __x._M_path_end[__current_index];
199193326Sed      size_t __len = __current_node->_M_size;
200193326Sed      size_t __node_start_pos = __x._M_leaf_pos;
201193326Sed      unsigned char __dirns = __x._M_path_directions;
202193326Sed      _Rope_RopeConcatenation<_CharT, _Alloc>* __c;
203198092Srdivacky
204193326Sed      if (__x._M_current_pos - __node_start_pos < __len)
205193326Sed	{
206193326Sed	  /* More stuff in this leaf, we just didn't cache it. */
207193326Sed	  _S_setbuf(__x);
208198092Srdivacky	  return;
209193326Sed	}
210193326Sed      //  node_start_pos is starting position of last_node.
211193326Sed      while (--__current_index >= 0)
212193326Sed	{
213193326Sed	  if (!(__dirns & 1) /* Path turned left */)
214193326Sed	    break;
215193326Sed	  __current_node = __x._M_path_end[__current_index];
216193326Sed	  __c = (_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node;
217193326Sed	  // Otherwise we were in the right child.  Thus we should pop
218193326Sed	  // the concatenation node.
219193326Sed	  __node_start_pos -= __c->_M_left->_M_size;
220193326Sed	  __dirns >>= 1;
221193326Sed	}
222193326Sed      if (__current_index < 0)
223193326Sed	{
224193326Sed	  // We underflowed the cache. Punt.
225193326Sed	  _S_setcache(__x);
226198092Srdivacky	  return;
227193326Sed	}
228193326Sed      __current_node = __x._M_path_end[__current_index];
229198092Srdivacky      __c = (_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node;
230193326Sed      // current_node is a concatenation node.  We are positioned on the first
231193326Sed      // character in its right child.
232198092Srdivacky      // node_start_pos is starting position of current_node.
233193326Sed      __node_start_pos += __c->_M_left->_M_size;
234193326Sed      __current_node = __c->_M_right;
235198092Srdivacky      __x._M_path_end[++__current_index] = __current_node;
236193326Sed      __dirns |= 1;
237193326Sed      while (__detail::_S_concat == __current_node->_M_tag)
238193326Sed	{
239193326Sed	  ++__current_index;
240193326Sed	  if (int(_S_path_cache_len) == __current_index)
241193326Sed	    {
242193326Sed	      int __i;
243198092Srdivacky	      for (__i = 0; __i < int(_S_path_cache_len) - 1; __i++)
244193326Sed		__x._M_path_end[__i] = __x._M_path_end[__i+1];
245193326Sed	      --__current_index;
246193326Sed	    }
247193326Sed	  __current_node =
248193326Sed	    ((_Rope_RopeConcatenation<_CharT, _Alloc>*)__current_node)->_M_left;
249198092Srdivacky	  __x._M_path_end[__current_index] = __current_node;
250193326Sed	  __dirns <<= 1;
251193326Sed	  // node_start_pos is unchanged.
252193326Sed	}
253193326Sed      __x._M_leaf_index = __current_index;
254193326Sed      __x._M_leaf_pos = __node_start_pos;
255193326Sed      __x._M_path_directions = __dirns;
256193326Sed      _S_setbuf(__x);
257193326Sed    }
258193326Sed
259193326Sed  template <class _CharT, class _Alloc>
260193326Sed    void
261193326Sed    _Rope_iterator_base<_CharT, _Alloc>::
262193326Sed    _M_incr(size_t __n)
263193326Sed    {
264193326Sed      _M_current_pos += __n;
265193326Sed      if (0 != _M_buf_ptr)
266198092Srdivacky	{
267193326Sed	  size_t __chars_left = _M_buf_end - _M_buf_ptr;
268193326Sed	  if (__chars_left > __n)
269193326Sed	    _M_buf_ptr += __n;
270193326Sed	  else if (__chars_left == __n)
271193326Sed	    {
272193326Sed	      _M_buf_ptr += __n;
273193326Sed	      _S_setcache_for_incr(*this);
274193326Sed	    }
275193326Sed	  else
276193326Sed	    _M_buf_ptr = 0;
277193326Sed	}
278193326Sed    }
279193326Sed
280193326Sed  template <class _CharT, class _Alloc>
281193326Sed    void
282193326Sed    _Rope_iterator_base<_CharT, _Alloc>::
283193326Sed    _M_decr(size_t __n)
284193326Sed    {
285193326Sed      if (0 != _M_buf_ptr)
286193326Sed	{
287198092Srdivacky	  size_t __chars_left = _M_buf_ptr - _M_buf_start;
288193326Sed	  if (__chars_left >= __n)
289193326Sed	    _M_buf_ptr -= __n;
290198092Srdivacky	  else
291193326Sed	    _M_buf_ptr = 0;
292193326Sed	}
293193326Sed      _M_current_pos -= __n;
294193326Sed    }
295193326Sed
296193326Sed  template <class _CharT, class _Alloc>
297193326Sed    void
298193326Sed    _Rope_iterator<_CharT, _Alloc>::
299193326Sed    _M_check()
300193326Sed    {
301198092Srdivacky      if (_M_root_rope->_M_tree_ptr != this->_M_root)
302193326Sed	{
303193326Sed	  // _Rope was modified.  Get things fixed up.
304193326Sed	  _RopeRep::_S_unref(this->_M_root);
305198092Srdivacky	  this->_M_root = _M_root_rope->_M_tree_ptr;
306193326Sed	  _RopeRep::_S_ref(this->_M_root);
307193326Sed	  this->_M_buf_ptr = 0;
308193326Sed	}
309193326Sed    }
310193326Sed
311193326Sed  template <class _CharT, class _Alloc>
312193326Sed    inline
313193326Sed    _Rope_const_iterator<_CharT, _Alloc>::
314193326Sed    _Rope_const_iterator(const _Rope_iterator<_CharT, _Alloc>& __x)
315193326Sed    : _Rope_iterator_base<_CharT, _Alloc>(__x)
316193326Sed    { }
317193326Sed
318193326Sed  template <class _CharT, class _Alloc>
319193326Sed    inline
320193326Sed    _Rope_iterator<_CharT, _Alloc>::
321193326Sed    _Rope_iterator(rope<_CharT, _Alloc>& __r, size_t __pos)
322193326Sed    : _Rope_iterator_base<_CharT,_Alloc>(__r._M_tree_ptr, __pos),
323193326Sed      _M_root_rope(&__r)
324193326Sed    { _RopeRep::_S_ref(this->_M_root); }
325193326Sed
326193326Sed  template <class _CharT, class _Alloc>
327193326Sed    inline size_t
328193326Sed    rope<_CharT, _Alloc>::
329193326Sed    _S_char_ptr_len(const _CharT* __s)
330193326Sed    {
331193326Sed      const _CharT* __p = __s;
332193326Sed
333193326Sed      while (!_S_is0(*__p))
334193326Sed	++__p;
335193326Sed      return (__p - __s);
336193326Sed    }
337193326Sed
338193326Sed
339193326Sed#ifndef __GC
340193326Sed
341193326Sed  template <class _CharT, class _Alloc>
342193326Sed    inline void
343193326Sed    _Rope_RopeRep<_CharT, _Alloc>::
344193326Sed    _M_free_c_string()
345193326Sed    {
346193326Sed      _CharT* __cstr = _M_c_string;
347193326Sed      if (0 != __cstr)
348193326Sed	{
349193326Sed	  size_t __size = this->_M_size + 1;
350193326Sed	  _Destroy(__cstr, __cstr + __size, get_allocator());
351193326Sed	  this->_Data_deallocate(__cstr, __size);
352193326Sed	}
353193326Sed    }
354193326Sed
355193326Sed  template <class _CharT, class _Alloc>
356193326Sed    inline void
357193326Sed    _Rope_RopeRep<_CharT, _Alloc>::
358193326Sed    _S_free_string(_CharT* __s, size_t __n, allocator_type __a)
359193326Sed    {
360193326Sed      if (!_S_is_basic_char_type((_CharT*)0))
361193326Sed	_Destroy(__s, __s + __n, __a);
362193326Sed
363193326Sed      //  This has to be a static member, so this gets a bit messy
364193326Sed      __a.deallocate(__s,
365193326Sed		     _Rope_RopeLeaf<_CharT, _Alloc>::_S_rounded_up_size(__n));
366193326Sed    }
367193326Sed
368193326Sed  //  There are several reasons for not doing this with virtual destructors
369193326Sed  //  and a class specific delete operator:
370193326Sed  //  - A class specific delete operator can't easily get access to
371193326Sed  //    allocator instances if we need them.
372193326Sed  //  - Any virtual function would need a 4 or byte vtable pointer;
373193326Sed  //    this only requires a one byte tag per object.
374193326Sed  template <class _CharT, class _Alloc>
375198092Srdivacky    void
376193326Sed    _Rope_RopeRep<_CharT, _Alloc>::
377198092Srdivacky    _M_free_tree()
378193326Sed    {
379193326Sed      switch(_M_tag)
380198092Srdivacky	{
381193326Sed	case __detail::_S_leaf:
382193326Sed	  {
383194613Sed	    _Rope_RopeLeaf<_CharT, _Alloc>* __l
384194613Sed	      = (_Rope_RopeLeaf<_CharT, _Alloc>*)this;
385193326Sed	    __l->template _Rope_RopeLeaf<_CharT, _Alloc>::~_Rope_RopeLeaf();
386193326Sed	    _L_deallocate(__l, 1);
387198092Srdivacky	    break;
388193326Sed	  }
389193326Sed	case __detail::_S_concat:
390193326Sed	  {
391193326Sed	    _Rope_RopeConcatenation<_CharT,_Alloc>* __c
392193326Sed	      = (_Rope_RopeConcatenation<_CharT, _Alloc>*)this;
393198092Srdivacky	    __c->template _Rope_RopeConcatenation<_CharT, _Alloc>::
394193326Sed	      ~_Rope_RopeConcatenation();
395193326Sed	    _C_deallocate(__c, 1);
396194711Sed	    break;
397194711Sed	  }
398193326Sed	case __detail::_S_function:
399193326Sed	  {
400193326Sed	    _Rope_RopeFunction<_CharT, _Alloc>* __f
401193326Sed	      = (_Rope_RopeFunction<_CharT, _Alloc>*)this;
402193326Sed	    __f->template _Rope_RopeFunction<_CharT, _Alloc>::~_Rope_RopeFunction();
403193326Sed	    _F_deallocate(__f, 1);
404193326Sed	    break;
405193326Sed	  }
406193326Sed	case __detail::_S_substringfn:
407193326Sed	  {
408193326Sed	    _Rope_RopeSubstring<_CharT, _Alloc>* __ss =
409193326Sed	      (_Rope_RopeSubstring<_CharT, _Alloc>*)this;
410193326Sed	    __ss->template _Rope_RopeSubstring<_CharT, _Alloc>::
411193326Sed	      ~_Rope_RopeSubstring();
412193326Sed	    _S_deallocate(__ss, 1);
413198092Srdivacky	    break;
414193326Sed	  }
415198092Srdivacky	}
416193326Sed    }
417193326Sed#else
418193326Sed
419193326Sed  template <class _CharT, class _Alloc>
420193326Sed    inline void
421193326Sed    _Rope_RopeRep<_CharT, _Alloc>::
422193326Sed    _S_free_string(const _CharT*, size_t, allocator_type)
423193326Sed    { }
424193326Sed
425193326Sed#endif
426193326Sed
427193326Sed  // Concatenate a C string onto a leaf rope by copying the rope data.
428200583Srdivacky  // Used for short ropes.
429200583Srdivacky  template <class _CharT, class _Alloc>
430200583Srdivacky    typename rope<_CharT, _Alloc>::_RopeLeaf*
431200583Srdivacky    rope<_CharT, _Alloc>::
432200583Srdivacky    _S_leaf_concat_char_iter(_RopeLeaf* __r, const _CharT* __iter, size_t __len)
433200583Srdivacky    {
434200583Srdivacky      size_t __old_len = __r->_M_size;
435200583Srdivacky      _CharT* __new_data = (_CharT*)
436200583Srdivacky	_Rope_rep_base<_CharT, _Alloc>::_Data_allocate(_S_rounded_up_size(__old_len + __len));
437200583Srdivacky      _RopeLeaf* __result;
438200583Srdivacky
439200583Srdivacky      uninitialized_copy_n(__r->_M_data, __old_len, __new_data);
440200583Srdivacky      uninitialized_copy_n(__iter, __len, __new_data + __old_len);
441200583Srdivacky      _S_cond_store_eos(__new_data[__old_len + __len]);
442200583Srdivacky      try
443200583Srdivacky	{
444200583Srdivacky	  __result = _S_new_RopeLeaf(__new_data, __old_len + __len,
445200583Srdivacky				     __r->get_allocator());
446200583Srdivacky	}
447193326Sed      catch(...)
448193326Sed	{
449193326Sed	  _RopeRep::__STL_FREE_STRING(__new_data, __old_len + __len,
450193326Sed				      __r->get_allocator());
451193326Sed	  __throw_exception_again;
452193326Sed	}
453193326Sed      return __result;
454193326Sed    }
455193326Sed
456193326Sed#ifndef __GC
457193326Sed  // As above, but it's OK to clobber original if refcount is 1
458193326Sed  template <class _CharT, class _Alloc>
459193326Sed    typename rope<_CharT,_Alloc>::_RopeLeaf*
460193326Sed    rope<_CharT, _Alloc>::
461193326Sed    _S_destr_leaf_concat_char_iter(_RopeLeaf* __r, const _CharT* __iter,
462193326Sed				   size_t __len)
463193326Sed    {
464193326Sed      if (__r->_M_ref_count > 1)
465193326Sed	return _S_leaf_concat_char_iter(__r, __iter, __len);
466193326Sed      size_t __old_len = __r->_M_size;
467198092Srdivacky      if (_S_allocated_capacity(__old_len) >= __old_len + __len)
468193326Sed	{
469193326Sed	  // The space has been partially initialized for the standard
470193326Sed	  // character types.  But that doesn't matter for those types.
471193326Sed	  uninitialized_copy_n(__iter, __len, __r->_M_data + __old_len);
472193326Sed	  if (_S_is_basic_char_type((_CharT*)0))
473193326Sed	    _S_cond_store_eos(__r->_M_data[__old_len + __len]);
474193326Sed	  else if (__r->_M_c_string != __r->_M_data && 0 != __r->_M_c_string)
475193326Sed	    {
476198092Srdivacky	      __r->_M_free_c_string();
477193326Sed	      __r->_M_c_string = 0;
478193326Sed	    }
479193326Sed	  __r->_M_size = __old_len + __len;
480198092Srdivacky	  __r->_M_ref_count = 2;
481193326Sed	  return __r;
482193326Sed	}
483193326Sed      else
484193326Sed	{
485193326Sed	  _RopeLeaf* __result = _S_leaf_concat_char_iter(__r, __iter, __len);
486193326Sed	  return __result;
487193326Sed	}
488193326Sed    }
489193326Sed#endif
490193326Sed
491193326Sed  // Assumes left and right are not 0.
492193326Sed  // Does not increment (nor decrement on exception) child reference counts.
493193326Sed  // Result has ref count 1.
494193326Sed  template <class _CharT, class _Alloc>
495193326Sed    typename rope<_CharT, _Alloc>::_RopeRep*
496193326Sed    rope<_CharT, _Alloc>::
497193326Sed    _S_tree_concat(_RopeRep* __left, _RopeRep* __right)
498193326Sed    {
499193326Sed      _RopeConcatenation* __result = _S_new_RopeConcatenation(__left, __right,
500193326Sed							      __left->
501193326Sed							      get_allocator());
502193326Sed      size_t __depth = __result->_M_depth;
503193326Sed
504193326Sed      if (__depth > 20
505193326Sed	  && (__result->_M_size < 1000
506193326Sed	      || __depth > size_t(__detail::_S_max_rope_depth)))
507193326Sed	{
508193326Sed	  _RopeRep* __balanced;
509193326Sed
510193326Sed	  try
511193326Sed	    {
512193326Sed	      __balanced = _S_balance(__result);
513193326Sed	      __result->_M_unref_nonnil();
514193326Sed	    }
515198092Srdivacky	  catch(...)
516193326Sed	    {
517193326Sed	      _C_deallocate(__result,1);
518193326Sed	      __throw_exception_again;
519193326Sed	    }
520193326Sed	  // In case of exception, we need to deallocate
521193326Sed	  // otherwise dangling result node.  But caller
522193326Sed	  // still owns its children.  Thus unref is
523193326Sed	  // inappropriate.
524193326Sed	  return __balanced;
525193326Sed	}
526193326Sed      else
527198092Srdivacky	return __result;
528193326Sed    }
529198092Srdivacky
530193326Sed  template <class _CharT, class _Alloc>
531193326Sed    typename rope<_CharT, _Alloc>::_RopeRep*
532193326Sed    rope<_CharT, _Alloc>::
533193326Sed    _S_concat_char_iter(_RopeRep* __r, const _CharT*__s, size_t __slen)
534193326Sed    {
535193326Sed      _RopeRep* __result;
536198092Srdivacky      if (0 == __slen)
537193326Sed	{
538193326Sed	  _S_ref(__r);
539193326Sed	  return __r;
540193326Sed	}
541193326Sed      if (0 == __r)
542193326Sed	return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
543193326Sed						__r->get_allocator());
544193326Sed      if (__r->_M_tag == __detail::_S_leaf
545193326Sed	  && __r->_M_size + __slen <= size_t(_S_copy_max))
546193326Sed	{
547193326Sed	  __result = _S_leaf_concat_char_iter((_RopeLeaf*)__r, __s, __slen);
548193326Sed	  return __result;
549193326Sed	}
550193326Sed      if (__detail::_S_concat == __r->_M_tag
551193326Sed	  && __detail::_S_leaf == ((_RopeConcatenation*) __r)->_M_right->_M_tag)
552193326Sed	{
553193326Sed	  _RopeLeaf* __right =
554198092Srdivacky	    (_RopeLeaf* )(((_RopeConcatenation* )__r)->_M_right);
555193326Sed	  if (__right->_M_size + __slen <= size_t(_S_copy_max))
556193326Sed	    {
557193326Sed	      _RopeRep* __left = ((_RopeConcatenation*)__r)->_M_left;
558193326Sed	      _RopeRep* __nright =
559193326Sed		_S_leaf_concat_char_iter((_RopeLeaf*)__right, __s, __slen);
560193326Sed	      __left->_M_ref_nonnil();
561193326Sed	      try
562193326Sed		{ __result = _S_tree_concat(__left, __nright); }
563203955Srdivacky	      catch(...)
564203955Srdivacky		{
565203955Srdivacky		  _S_unref(__left);
566203955Srdivacky		  _S_unref(__nright);
567203955Srdivacky		  __throw_exception_again;
568203955Srdivacky		}
569203955Srdivacky	      return __result;
570193326Sed	    }
571193326Sed	}
572193326Sed      _RopeRep* __nright =
573193326Sed	__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
574193326Sed      try
575193326Sed	{
576193326Sed	  __r->_M_ref_nonnil();
577193326Sed	  __result = _S_tree_concat(__r, __nright);
578193326Sed	}
579193326Sed      catch(...)
580193326Sed	{
581193326Sed	  _S_unref(__r);
582193326Sed	  _S_unref(__nright);
583193326Sed	  __throw_exception_again;
584193326Sed	}
585193326Sed      return __result;
586193326Sed    }
587193326Sed
588193326Sed#ifndef __GC
589193326Sed  template <class _CharT, class _Alloc>
590193326Sed    typename rope<_CharT,_Alloc>::_RopeRep*
591193326Sed    rope<_CharT,_Alloc>::
592193326Sed    _S_destr_concat_char_iter(_RopeRep* __r, const _CharT* __s, size_t __slen)
593193326Sed    {
594193326Sed      _RopeRep* __result;
595198092Srdivacky      if (0 == __r)
596193326Sed	return __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen,
597193326Sed						__r->get_allocator());
598193326Sed      size_t __count = __r->_M_ref_count;
599193326Sed      size_t __orig_size = __r->_M_size;
600198092Srdivacky      if (__count > 1)
601193326Sed	return _S_concat_char_iter(__r, __s, __slen);
602193326Sed      if (0 == __slen)
603193326Sed	{
604193326Sed	  __r->_M_ref_count = 2;      // One more than before
605193326Sed	  return __r;
606193326Sed	}
607193326Sed      if (__orig_size + __slen <= size_t(_S_copy_max)
608193326Sed	  && __detail::_S_leaf == __r->_M_tag)
609193326Sed	{
610193326Sed	  __result = _S_destr_leaf_concat_char_iter((_RopeLeaf*)__r, __s,
611193326Sed						    __slen);
612193326Sed	  return __result;
613198092Srdivacky	}
614193326Sed      if (__detail::_S_concat == __r->_M_tag)
615193326Sed	{
616193326Sed	  _RopeLeaf* __right = (_RopeLeaf*)(((_RopeConcatenation*)
617193326Sed					     __r)->_M_right);
618198092Srdivacky	  if (__detail::_S_leaf == __right->_M_tag
619193326Sed	      && __right->_M_size + __slen <= size_t(_S_copy_max))
620193326Sed	    {
621193326Sed	      _RopeRep* __new_right =
622193326Sed		_S_destr_leaf_concat_char_iter(__right, __s, __slen);
623193326Sed	      if (__right == __new_right)
624193326Sed		__new_right->_M_ref_count = 1;
625193326Sed	      else
626193326Sed		__right->_M_unref_nonnil();
627193326Sed	      __r->_M_ref_count = 2;    // One more than before.
628193326Sed	      ((_RopeConcatenation*)__r)->_M_right = __new_right;
629193326Sed	      __r->_M_size = __orig_size + __slen;
630193326Sed	      if (0 != __r->_M_c_string)
631193326Sed		{
632193326Sed		  __r->_M_free_c_string();
633193326Sed		  __r->_M_c_string = 0;
634193326Sed		}
635193326Sed	      return __r;
636193326Sed	    }
637193326Sed	}
638193326Sed      _RopeRep* __right =
639193326Sed	__STL_ROPE_FROM_UNOWNED_CHAR_PTR(__s, __slen, __r->get_allocator());
640193326Sed      __r->_M_ref_nonnil();
641193326Sed      try
642193326Sed	{ __result = _S_tree_concat(__r, __right); }
643193326Sed      catch(...)
644193326Sed	{
645193326Sed	  _S_unref(__r);
646193326Sed	  _S_unref(__right);
647193326Sed	  __throw_exception_again;
648198092Srdivacky	}
649193326Sed      return __result;
650193326Sed    }
651198092Srdivacky#endif /* !__GC */
652193326Sed
653193326Sed  template <class _CharT, class _Alloc>
654193326Sed    typename rope<_CharT, _Alloc>::_RopeRep*
655193326Sed    rope<_CharT, _Alloc>::
656193326Sed    _S_concat(_RopeRep* __left, _RopeRep* __right)
657193326Sed    {
658193326Sed      if (0 == __left)
659193326Sed	{
660193326Sed	  _S_ref(__right);
661193326Sed	  return __right;
662193326Sed	}
663193326Sed      if (0 == __right)
664193326Sed	{
665193326Sed	  __left->_M_ref_nonnil();
666193326Sed	  return __left;
667193326Sed	}
668193326Sed      if (__detail::_S_leaf == __right->_M_tag)
669193326Sed	{
670193326Sed	  if (__detail::_S_leaf == __left->_M_tag)
671193326Sed	    {
672193326Sed	      if (__right->_M_size + __left->_M_size <= size_t(_S_copy_max))
673198092Srdivacky		return _S_leaf_concat_char_iter((_RopeLeaf*)__left,
674193326Sed						((_RopeLeaf*)__right)->_M_data,
675193326Sed						__right->_M_size);
676193326Sed	    }
677193326Sed	  else if (__detail::_S_concat == __left->_M_tag
678193326Sed		   && __detail::_S_leaf == ((_RopeConcatenation*)
679193326Sed						   __left)->_M_right->_M_tag)
680193326Sed	    {
681193326Sed	      _RopeLeaf* __leftright =
682193326Sed		(_RopeLeaf*)(((_RopeConcatenation*)__left)->_M_right);
683193326Sed	      if (__leftright->_M_size
684198092Srdivacky		  + __right->_M_size <= size_t(_S_copy_max))
685193326Sed		{
686193326Sed		  _RopeRep* __leftleft = ((_RopeConcatenation*)__left)->_M_left;
687193326Sed		  _RopeRep* __rest = _S_leaf_concat_char_iter(__leftright,
688193326Sed							      ((_RopeLeaf*)
689193326Sed							       __right)->
690193326Sed							      _M_data,
691193326Sed							      __right->_M_size);
692193326Sed		  __leftleft->_M_ref_nonnil();
693193326Sed		  try
694193326Sed		    { return(_S_tree_concat(__leftleft, __rest)); }
695193326Sed		  catch(...)
696193326Sed		    {
697193326Sed		      _S_unref(__leftleft);
698193326Sed		      _S_unref(__rest);
699193326Sed		      __throw_exception_again;
700193326Sed		    }
701193326Sed		}
702193326Sed	    }
703193326Sed	}
704193326Sed      __left->_M_ref_nonnil();
705199482Srdivacky      __right->_M_ref_nonnil();
706199482Srdivacky      try
707198092Srdivacky	{ return(_S_tree_concat(__left, __right)); }
708193326Sed      catch(...)
709193326Sed	{
710198092Srdivacky	  _S_unref(__left);
711193326Sed	  _S_unref(__right);
712193326Sed	  __throw_exception_again;
713193326Sed	}
714198092Srdivacky    }
715193326Sed
716193326Sed  template <class _CharT, class _Alloc>
717198092Srdivacky    typename rope<_CharT, _Alloc>::_RopeRep*
718193326Sed    rope<_CharT, _Alloc>::
719193326Sed    _S_substring(_RopeRep* __base, size_t __start, size_t __endp1)
720193326Sed    {
721193326Sed      if (0 == __base)
722193326Sed	return 0;
723193326Sed      size_t __len = __base->_M_size;
724193326Sed      size_t __adj_endp1;
725193326Sed      const size_t __lazy_threshold = 128;
726193326Sed
727193326Sed      if (__endp1 >= __len)
728193326Sed	{
729193326Sed	  if (0 == __start)
730198092Srdivacky	    {
731193326Sed	      __base->_M_ref_nonnil();
732193326Sed	      return __base;
733193326Sed	    }
734193326Sed	  else
735193326Sed	    __adj_endp1 = __len;
736193326Sed
737193326Sed	}
738193326Sed      else
739193326Sed	__adj_endp1 = __endp1;
740193326Sed
741193326Sed      switch(__base->_M_tag)
742193326Sed	{
743193326Sed	case __detail::_S_concat:
744198092Srdivacky	    {
745193326Sed	      _RopeConcatenation* __c = (_RopeConcatenation*)__base;
746193326Sed	      _RopeRep* __left = __c->_M_left;
747193326Sed	      _RopeRep* __right = __c->_M_right;
748193326Sed	      size_t __left_len = __left->_M_size;
749193326Sed	      _RopeRep* __result;
750193326Sed
751193326Sed	      if (__adj_endp1 <= __left_len)
752193326Sed		return _S_substring(__left, __start, __endp1);
753193326Sed	      else if (__start >= __left_len)
754193326Sed		return _S_substring(__right, __start - __left_len,
755193326Sed				    __adj_endp1 - __left_len);
756193326Sed	      _Self_destruct_ptr __left_result(_S_substring(__left,
757193326Sed							    __start,
758193326Sed							    __left_len));
759193326Sed	      _Self_destruct_ptr __right_result(_S_substring(__right, 0,
760193326Sed							     __endp1
761193326Sed							     - __left_len));
762198092Srdivacky	      __result = _S_concat(__left_result, __right_result);
763193326Sed	      return __result;
764193326Sed	    }
765193326Sed	case __detail::_S_leaf:
766193326Sed	  {
767193326Sed	    _RopeLeaf* __l = (_RopeLeaf*)__base;
768193326Sed	    _RopeLeaf* __result;
769193326Sed	    size_t __result_len;
770193326Sed	    if (__start >= __adj_endp1)
771193326Sed	      return 0;
772193326Sed	    __result_len = __adj_endp1 - __start;
773198092Srdivacky	    if (__result_len > __lazy_threshold)
774193326Sed	      goto lazy;
775193326Sed#ifdef __GC
776193326Sed	    const _CharT* __section = __l->_M_data + __start;
777198092Srdivacky	    __result = _S_new_RopeLeaf(__section, __result_len,
778193326Sed				       __base->get_allocator());
779193326Sed	    __result->_M_c_string = 0;  // Not eos terminated.
780193326Sed#else
781193326Sed	    // We should sometimes create substring node instead.
782193326Sed	    __result = __STL_ROPE_FROM_UNOWNED_CHAR_PTR(__l->_M_data + __start,
783193326Sed							__result_len,
784193326Sed							__base->
785193326Sed							get_allocator());
786193326Sed#endif
787193326Sed	    return __result;
788193326Sed	  }
789193326Sed	case __detail::_S_substringfn:
790193326Sed	  // Avoid introducing multiple layers of substring nodes.
791193326Sed	  {
792193326Sed	    _RopeSubstring* __old = (_RopeSubstring*)__base;
793198092Srdivacky	    size_t __result_len;
794193326Sed	    if (__start >= __adj_endp1)
795193326Sed	      return 0;
796193326Sed	    __result_len = __adj_endp1 - __start;
797193326Sed	    if (__result_len > __lazy_threshold)
798193326Sed	      {
799193326Sed		_RopeSubstring* __result =
800193326Sed		  _S_new_RopeSubstring(__old->_M_base,
801193326Sed				       __start + __old->_M_start,
802193326Sed				       __adj_endp1 - __start,
803193326Sed				       __base->get_allocator());
804193326Sed		return __result;
805193326Sed
806193326Sed	      } // *** else fall through: ***
807193326Sed	  }
808193326Sed	case __detail::_S_function:
809193326Sed	  {
810193326Sed	    _RopeFunction* __f = (_RopeFunction*)__base;
811193326Sed	    _CharT* __section;
812193326Sed	    size_t __result_len;
813193326Sed	    if (__start >= __adj_endp1)
814193326Sed	      return 0;
815198092Srdivacky	    __result_len = __adj_endp1 - __start;
816193326Sed
817193326Sed	    if (__result_len > __lazy_threshold)
818193326Sed	      goto lazy;
819193326Sed	    __section = (_CharT*)
820193326Sed	      _Rope_rep_base<_CharT, _Alloc>::_Data_allocate(_S_rounded_up_size(__result_len));
821193326Sed	    try
822198092Srdivacky	      {	(*(__f->_M_fn))(__start, __result_len, __section); }
823193326Sed	    catch(...)
824193326Sed	      {
825198092Srdivacky		_RopeRep::__STL_FREE_STRING(__section, __result_len,
826193326Sed					    __base->get_allocator());
827193326Sed		__throw_exception_again;
828193326Sed	      }
829193326Sed	    _S_cond_store_eos(__section[__result_len]);
830193326Sed	    return _S_new_RopeLeaf(__section, __result_len,
831193326Sed				   __base->get_allocator());
832193326Sed	  }
833193326Sed	}
834198092Srdivacky    lazy:
835193326Sed      {
836193326Sed	// Create substring node.
837193326Sed	return _S_new_RopeSubstring(__base, __start, __adj_endp1 - __start,
838193326Sed				    __base->get_allocator());
839193326Sed      }
840198092Srdivacky    }
841193326Sed
842193326Sed  template<class _CharT>
843193326Sed    class _Rope_flatten_char_consumer
844198092Srdivacky    : public _Rope_char_consumer<_CharT>
845193326Sed    {
846193326Sed    private:
847193326Sed      _CharT* _M_buf_ptr;
848193326Sed    public:
849193326Sed
850193326Sed      _Rope_flatten_char_consumer(_CharT* __buffer)
851193326Sed      { _M_buf_ptr = __buffer; };
852193326Sed
853193326Sed      ~_Rope_flatten_char_consumer() {}
854193326Sed
855193326Sed      bool
856193326Sed      operator()(const _CharT* __leaf, size_t __n)
857193326Sed      {
858193326Sed	uninitialized_copy_n(__leaf, __n, _M_buf_ptr);
859193326Sed	_M_buf_ptr += __n;
860193326Sed	return true;
861193326Sed      }
862193326Sed    };
863193326Sed
864198092Srdivacky  template<class _CharT>
865193326Sed    class _Rope_find_char_char_consumer
866193326Sed    : public _Rope_char_consumer<_CharT>
867193326Sed    {
868193326Sed    private:
869193326Sed      _CharT _M_pattern;
870193326Sed    public:
871198092Srdivacky      size_t _M_count;  // Number of nonmatching characters
872193326Sed
873193326Sed      _Rope_find_char_char_consumer(_CharT __p)
874193326Sed      : _M_pattern(__p), _M_count(0) {}
875193326Sed
876193326Sed      ~_Rope_find_char_char_consumer() {}
877193326Sed
878193326Sed      bool
879193326Sed      operator()(const _CharT* __leaf, size_t __n)
880193326Sed      {
881198092Srdivacky	size_t __i;
882193326Sed	for (__i = 0; __i < __n; __i++)
883193326Sed	  {
884193326Sed	    if (__leaf[__i] == _M_pattern)
885193326Sed	      {
886198092Srdivacky		_M_count += __i;
887193326Sed		return false;
888193326Sed	      }
889193326Sed	  }
890193326Sed	_M_count += __n; return true;
891193326Sed      }
892193326Sed    };
893193326Sed
894193326Sed  template<class _CharT, class _Traits>
895193326Sed  // Here _CharT is both the stream and rope character type.
896193326Sed    class _Rope_insert_char_consumer
897193326Sed    : public _Rope_char_consumer<_CharT>
898193326Sed    {
899198092Srdivacky    private:
900193326Sed      typedef basic_ostream<_CharT,_Traits> _Insert_ostream;
901193326Sed      _Insert_ostream& _M_o;
902193326Sed    public:
903193326Sed      _Rope_insert_char_consumer(_Insert_ostream& __writer)
904193326Sed	: _M_o(__writer) {};
905193326Sed      ~_Rope_insert_char_consumer() { };
906193326Sed      // Caller is presumed to own the ostream
907193326Sed      bool operator() (const _CharT* __leaf, size_t __n);
908193326Sed      // Returns true to continue traversal.
909193326Sed    };
910193326Sed
911193326Sed  template<class _CharT, class _Traits>
912193326Sed    bool
913198092Srdivacky    _Rope_insert_char_consumer<_CharT, _Traits>::
914193326Sed    operator()(const _CharT* __leaf, size_t __n)
915193326Sed    {
916198092Srdivacky      size_t __i;
917193326Sed      //  We assume that formatting is set up correctly for each element.
918193326Sed      for (__i = 0; __i < __n; __i++)
919198092Srdivacky	_M_o.put(__leaf[__i]);
920193326Sed      return true;
921193326Sed    }
922193326Sed
923198092Srdivacky  template <class _CharT, class _Alloc>
924193326Sed    bool
925193326Sed    rope<_CharT, _Alloc>::
926193326Sed    _S_apply_to_pieces(_Rope_char_consumer<_CharT>& __c,
927193326Sed		       const _RopeRep* __r, size_t __begin, size_t __end)
928198092Srdivacky    {
929193326Sed      if (0 == __r)
930193326Sed	return true;
931193326Sed      switch(__r->_M_tag)
932193326Sed	{
933193326Sed	case __detail::_S_concat:
934193326Sed	  {
935193326Sed	    _RopeConcatenation* __conc = (_RopeConcatenation*)__r;
936193326Sed	    _RopeRep* __left =  __conc->_M_left;
937193326Sed	    size_t __left_len = __left->_M_size;
938193326Sed	    if (__begin < __left_len)
939193326Sed	      {
940193326Sed		size_t __left_end = std::min(__left_len, __end);
941193326Sed		if (!_S_apply_to_pieces(__c, __left, __begin, __left_end))
942193326Sed		  return false;
943193326Sed	      }
944193326Sed	    if (__end > __left_len)
945193326Sed	      {
946198092Srdivacky		_RopeRep* __right =  __conc->_M_right;
947193326Sed		size_t __right_start = std::max(__left_len, __begin);
948198092Srdivacky		if (!_S_apply_to_pieces(__c, __right,
949193326Sed					__right_start - __left_len,
950193326Sed					__end - __left_len))
951193326Sed		  return false;
952193326Sed	      }
953193326Sed	  }
954193326Sed	  return true;
955193326Sed	case __detail::_S_leaf:
956193326Sed	  {
957193326Sed	    _RopeLeaf* __l = (_RopeLeaf*)__r;
958193326Sed	    return __c(__l->_M_data + __begin, __end - __begin);
959193326Sed	  }
960193326Sed	case __detail::_S_function:
961193326Sed	case __detail::_S_substringfn:
962193326Sed	    {
963193326Sed	      _RopeFunction* __f = (_RopeFunction*)__r;
964194613Sed	      size_t __len = __end - __begin;
965194613Sed	      bool __result;
966194613Sed	      _CharT* __buffer =
967194613Sed		(_CharT*)_Alloc().allocate(__len * sizeof(_CharT));
968194613Sed	      try
969194613Sed		{
970194613Sed		  (*(__f->_M_fn))(__begin, __len, __buffer);
971194613Sed		  __result = __c(__buffer, __len);
972193326Sed                  _Alloc().deallocate(__buffer, __len * sizeof(_CharT));
973194613Sed                }
974194613Sed	      catch(...)
975194613Sed		{
976194613Sed		  _Alloc().deallocate(__buffer, __len * sizeof(_CharT));
977198092Srdivacky		  __throw_exception_again;
978194613Sed		}
979194613Sed	      return __result;
980194613Sed	    }
981194613Sed	default:
982194613Sed	  return false;
983200583Srdivacky	}
984200583Srdivacky    }
985200583Srdivacky
986200583Srdivacky  template<class _CharT, class _Traits>
987200583Srdivacky    inline void
988200583Srdivacky    _Rope_fill(basic_ostream<_CharT, _Traits>& __o, size_t __n)
989200583Srdivacky    {
990200583Srdivacky      char __f = __o.fill();
991200583Srdivacky      size_t __i;
992200583Srdivacky
993200583Srdivacky      for (__i = 0; __i < __n; __i++)
994200583Srdivacky	__o.put(__f);
995200583Srdivacky    }
996200583Srdivacky
997200583Srdivacky
998200583Srdivacky  template <class _CharT>
999200583Srdivacky    inline bool
1000200583Srdivacky    _Rope_is_simple(_CharT*)
1001200583Srdivacky    { return false; }
1002200583Srdivacky
1003200583Srdivacky  inline bool
1004200583Srdivacky  _Rope_is_simple(char*)
1005200583Srdivacky  { return true; }
1006200583Srdivacky
1007200583Srdivacky  inline bool
1008200583Srdivacky  _Rope_is_simple(wchar_t*)
1009204643Srdivacky  { return true; }
1010204643Srdivacky
1011204643Srdivacky  template<class _CharT, class _Traits, class _Alloc>
1012204643Srdivacky    basic_ostream<_CharT, _Traits>&
1013204643Srdivacky    operator<<(basic_ostream<_CharT, _Traits>& __o,
1014204643Srdivacky	       const rope<_CharT, _Alloc>& __r)
1015204643Srdivacky    {
1016204643Srdivacky      size_t __w = __o.width();
1017204643Srdivacky      bool __left = bool(__o.flags() & std::ios::left);
1018204643Srdivacky      size_t __pad_len;
1019204643Srdivacky      size_t __rope_len = __r.size();
1020204643Srdivacky      _Rope_insert_char_consumer<_CharT, _Traits> __c(__o);
1021204643Srdivacky      bool __is_simple = _Rope_is_simple((_CharT*)0);
1022204643Srdivacky
1023204643Srdivacky      if (__rope_len < __w)
1024204643Srdivacky	__pad_len = __w - __rope_len;
1025204643Srdivacky      else
1026204643Srdivacky	__pad_len = 0;
1027200583Srdivacky
1028194613Sed      if (!__is_simple)
1029194613Sed	__o.width(__w / __rope_len);
1030195099Sed      try
1031195099Sed	{
1032195099Sed	  if (__is_simple && !__left && __pad_len > 0)
1033195099Sed	    _Rope_fill(__o, __pad_len);
1034195099Sed	  __r.apply_to_pieces(0, __r.size(), __c);
1035195099Sed	  if (__is_simple && __left && __pad_len > 0)
1036195099Sed	    _Rope_fill(__o, __pad_len);
1037195099Sed	  if (!__is_simple)
1038198092Srdivacky	    __o.width(__w);
1039195099Sed	}
1040195099Sed      catch(...)
1041198092Srdivacky	{
1042195099Sed	  if (!__is_simple)
1043195099Sed	    __o.width(__w);
1044195099Sed	  __throw_exception_again;
1045194613Sed	}
1046195099Sed      return __o;
1047195099Sed    }
1048198092Srdivacky
1049195099Sed  template <class _CharT, class _Alloc>
1050195099Sed    _CharT*
1051195099Sed    rope<_CharT, _Alloc>::
1052198092Srdivacky    _S_flatten(_RopeRep* __r, size_t __start, size_t __len,
1053195099Sed	       _CharT* __buffer)
1054195099Sed    {
1055198092Srdivacky      _Rope_flatten_char_consumer<_CharT> __c(__buffer);
1056195099Sed      _S_apply_to_pieces(__c, __r, __start, __start + __len);
1057195099Sed      return(__buffer + __len);
1058195099Sed    }
1059195099Sed
1060195099Sed  template <class _CharT, class _Alloc>
1061195099Sed    size_t
1062195099Sed    rope<_CharT, _Alloc>::
1063195099Sed    find(_CharT __pattern, size_t __start) const
1064195099Sed    {
1065195099Sed      _Rope_find_char_char_consumer<_CharT> __c(__pattern);
1066195099Sed      _S_apply_to_pieces(__c, this->_M_tree_ptr, __start, size());
1067195099Sed      size_type __result_pos = __start + __c._M_count;
1068195099Sed#ifndef __STL_OLD_ROPE_SEMANTICS
1069195099Sed      if (__result_pos == size())
1070195099Sed	__result_pos = npos;
1071195099Sed#endif
1072195099Sed      return __result_pos;
1073198092Srdivacky    }
1074195099Sed
1075195099Sed  template <class _CharT, class _Alloc>
1076198092Srdivacky    _CharT*
1077195099Sed    rope<_CharT, _Alloc>::
1078198092Srdivacky    _S_flatten(_RopeRep* __r, _CharT* __buffer)
1079195099Sed    {
1080195099Sed      if (0 == __r)
1081198092Srdivacky	return __buffer;
1082195099Sed      switch(__r->_M_tag)
1083195099Sed	{
1084195099Sed	case __detail::_S_concat:
1085195099Sed	  {
1086195099Sed	    _RopeConcatenation* __c = (_RopeConcatenation*)__r;
1087195099Sed	    _RopeRep* __left = __c->_M_left;
1088195099Sed	    _RopeRep* __right = __c->_M_right;
1089195099Sed	    _CharT* __rest = _S_flatten(__left, __buffer);
1090195099Sed	    return _S_flatten(__right, __rest);
1091195099Sed	  }
1092195099Sed	case __detail::_S_leaf:
1093195099Sed	  {
1094195099Sed	    _RopeLeaf* __l = (_RopeLeaf*)__r;
1095198092Srdivacky	    return copy_n(__l->_M_data, __l->_M_size, __buffer).second;
1096195099Sed	  }
1097195099Sed	case __detail::_S_function:
1098198092Srdivacky	case __detail::_S_substringfn:
1099195099Sed	  // We don't yet do anything with substring nodes.
1100198092Srdivacky	  // This needs to be fixed before ropefiles will work well.
1101195099Sed	  {
1102195099Sed	    _RopeFunction* __f = (_RopeFunction*)__r;
1103195099Sed	    (*(__f->_M_fn))(0, __f->_M_size, __buffer);
1104195099Sed	    return __buffer + __f->_M_size;
1105198092Srdivacky	  }
1106200583Srdivacky	default:
1107200583Srdivacky	  return 0;
1108200583Srdivacky	}
1109200583Srdivacky    }
1110200583Srdivacky
1111198092Srdivacky  // This needs work for _CharT != char
1112200583Srdivacky  template <class _CharT, class _Alloc>
1113200583Srdivacky    void
1114200583Srdivacky    rope<_CharT, _Alloc>::
1115200583Srdivacky    _S_dump(_RopeRep* __r, int __indent)
1116200583Srdivacky    {
1117198092Srdivacky      for (int __i = 0; __i < __indent; __i++)
1118200583Srdivacky	putchar(' ');
1119200583Srdivacky      if (0 == __r)
1120195099Sed	{
1121195099Sed	  printf("NULL\n");
1122193326Sed	  return;
1123193326Sed	}
1124193326Sed      if (_S_concat == __r->_M_tag)
1125198092Srdivacky	{
1126198092Srdivacky	  _RopeConcatenation* __c = (_RopeConcatenation*)__r;
1127198092Srdivacky	  _RopeRep* __left = __c->_M_left;
1128198092Srdivacky	  _RopeRep* __right = __c->_M_right;
1129198092Srdivacky
1130198092Srdivacky#ifdef __GC
1131193326Sed	  printf("Concatenation %p (depth = %d, len = %ld, %s balanced)\n",
1132193326Sed		 __r, __r->_M_depth, __r->_M_size,
1133193326Sed		 __r->_M_is_balanced? "" : "not");
1134193326Sed#else
1135193326Sed	  printf("Concatenation %p (rc = %ld, depth = %d, "
1136193326Sed		 "len = %ld, %s balanced)\n",
1137198092Srdivacky		 __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size,
1138198092Srdivacky		 __r->_M_is_balanced? "" : "not");
1139198092Srdivacky#endif
1140198092Srdivacky	  _S_dump(__left, __indent + 2);
1141198092Srdivacky	  _S_dump(__right, __indent + 2);
1142193326Sed	  return;
1143193326Sed	}
1144193326Sed      else
1145	{
1146	  const char* __kind;
1147
1148	  switch (__r->_M_tag)
1149	    {
1150	    case __detail::_S_leaf:
1151	      __kind = "Leaf";
1152	      break;
1153	    case __detail::_S_function:
1154	      __kind = "Function";
1155	      break;
1156	    case __detail::_S_substringfn:
1157	      __kind = "Function representing substring";
1158	      break;
1159	    default:
1160	      __kind = "(corrupted kind field!)";
1161	    }
1162#ifdef __GC
1163	  printf("%s %p (depth = %d, len = %ld) ",
1164		 __kind, __r, __r->_M_depth, __r->_M_size);
1165#else
1166	  printf("%s %p (rc = %ld, depth = %d, len = %ld) ",
1167		 __kind, __r, __r->_M_ref_count, __r->_M_depth, __r->_M_size);
1168#endif
1169	  if (_S_is_one_byte_char_type((_CharT*)0))
1170	    {
1171	      const int __max_len = 40;
1172	      _Self_destruct_ptr __prefix(_S_substring(__r, 0, __max_len));
1173	      _CharT __buffer[__max_len + 1];
1174	      bool __too_big = __r->_M_size > __prefix->_M_size;
1175
1176	      _S_flatten(__prefix, __buffer);
1177	      __buffer[__prefix->_M_size] = _S_eos((_CharT*)0);
1178	      printf("%s%s\n", (char*)__buffer,
1179		     __too_big? "...\n" : "\n");
1180	    }
1181	  else
1182	    printf("\n");
1183	}
1184    }
1185
1186  template <class _CharT, class _Alloc>
1187    const unsigned long
1188    rope<_CharT, _Alloc>::
1189    _S_min_len[int(__detail::_S_max_rope_depth) + 1] = {
1190      /* 0 */1, /* 1 */2, /* 2 */3, /* 3 */5, /* 4 */8, /* 5 */13, /* 6 */21,
1191      /* 7 */34, /* 8 */55, /* 9 */89, /* 10 */144, /* 11 */233, /* 12 */377,
1192      /* 13 */610, /* 14 */987, /* 15 */1597, /* 16 */2584, /* 17 */4181,
1193      /* 18 */6765, /* 19 */10946, /* 20 */17711, /* 21 */28657, /* 22 */46368,
1194      /* 23 */75025, /* 24 */121393, /* 25 */196418, /* 26 */317811,
1195      /* 27 */514229, /* 28 */832040, /* 29 */1346269, /* 30 */2178309,
1196      /* 31 */3524578, /* 32 */5702887, /* 33 */9227465, /* 34 */14930352,
1197      /* 35 */24157817, /* 36 */39088169, /* 37 */63245986, /* 38 */102334155,
1198      /* 39 */165580141, /* 40 */267914296, /* 41 */433494437,
1199      /* 42 */701408733, /* 43 */1134903170, /* 44 */1836311903,
1200      /* 45 */2971215073u };
1201  // These are Fibonacci numbers < 2**32.
1202
1203  template <class _CharT, class _Alloc>
1204    typename rope<_CharT, _Alloc>::_RopeRep*
1205    rope<_CharT, _Alloc>::
1206    _S_balance(_RopeRep* __r)
1207    {
1208      _RopeRep* __forest[int(__detail::_S_max_rope_depth) + 1];
1209      _RopeRep* __result = 0;
1210      int __i;
1211      // Invariant:
1212      // The concatenation of forest in descending order is equal to __r.
1213      // __forest[__i]._M_size >= _S_min_len[__i]
1214      // __forest[__i]._M_depth = __i
1215      // References from forest are included in refcount.
1216
1217      for (__i = 0; __i <= int(__detail::_S_max_rope_depth); ++__i)
1218	__forest[__i] = 0;
1219      try
1220	{
1221	  _S_add_to_forest(__r, __forest);
1222	  for (__i = 0; __i <= int(__detail::_S_max_rope_depth); ++__i)
1223	    if (0 != __forest[__i])
1224	      {
1225#ifndef __GC
1226		_Self_destruct_ptr __old(__result);
1227#endif
1228		__result = _S_concat(__forest[__i], __result);
1229		__forest[__i]->_M_unref_nonnil();
1230#if !defined(__GC) && defined(__EXCEPTIONS)
1231		__forest[__i] = 0;
1232#endif
1233	      }
1234	}
1235      catch(...)
1236	{
1237	  for(__i = 0; __i <= int(__detail::_S_max_rope_depth); __i++)
1238	    _S_unref(__forest[__i]);
1239	  __throw_exception_again;
1240	}
1241
1242      if (__result->_M_depth > int(__detail::_S_max_rope_depth))
1243	__throw_length_error(__N("rope::_S_balance"));
1244      return(__result);
1245    }
1246
1247  template <class _CharT, class _Alloc>
1248    void
1249    rope<_CharT, _Alloc>::
1250    _S_add_to_forest(_RopeRep* __r, _RopeRep** __forest)
1251    {
1252      if (__r->_M_is_balanced)
1253	{
1254	  _S_add_leaf_to_forest(__r, __forest);
1255	  return;
1256	}
1257
1258      {
1259	_RopeConcatenation* __c = (_RopeConcatenation*)__r;
1260
1261	_S_add_to_forest(__c->_M_left, __forest);
1262	_S_add_to_forest(__c->_M_right, __forest);
1263      }
1264    }
1265
1266
1267  template <class _CharT, class _Alloc>
1268    void
1269    rope<_CharT, _Alloc>::
1270    _S_add_leaf_to_forest(_RopeRep* __r, _RopeRep** __forest)
1271    {
1272      _RopeRep* __insertee;		// included in refcount
1273      _RopeRep* __too_tiny = 0;		// included in refcount
1274      int __i;				// forest[0..__i-1] is empty
1275      size_t __s = __r->_M_size;
1276
1277      for (__i = 0; __s >= _S_min_len[__i+1]/* not this bucket */; ++__i)
1278	{
1279	  if (0 != __forest[__i])
1280	    {
1281#ifndef __GC
1282	      _Self_destruct_ptr __old(__too_tiny);
1283#endif
1284	      __too_tiny = _S_concat_and_set_balanced(__forest[__i],
1285						      __too_tiny);
1286	      __forest[__i]->_M_unref_nonnil();
1287	      __forest[__i] = 0;
1288	    }
1289	}
1290      {
1291#ifndef __GC
1292	_Self_destruct_ptr __old(__too_tiny);
1293#endif
1294	__insertee = _S_concat_and_set_balanced(__too_tiny, __r);
1295      }
1296      // Too_tiny dead, and no longer included in refcount.
1297      // Insertee is live and included.
1298      for (;; ++__i)
1299	{
1300	  if (0 != __forest[__i])
1301	    {
1302#ifndef __GC
1303	      _Self_destruct_ptr __old(__insertee);
1304#endif
1305	      __insertee = _S_concat_and_set_balanced(__forest[__i],
1306						      __insertee);
1307	      __forest[__i]->_M_unref_nonnil();
1308	      __forest[__i] = 0;
1309	    }
1310	  if (__i == int(__detail::_S_max_rope_depth)
1311	      || __insertee->_M_size < _S_min_len[__i+1])
1312	    {
1313	      __forest[__i] = __insertee;
1314	      // refcount is OK since __insertee is now dead.
1315	      return;
1316	    }
1317	}
1318    }
1319
1320  template <class _CharT, class _Alloc>
1321    _CharT
1322    rope<_CharT, _Alloc>::
1323    _S_fetch(_RopeRep* __r, size_type __i)
1324    {
1325      __GC_CONST _CharT* __cstr = __r->_M_c_string;
1326
1327      if (0 != __cstr)
1328	return __cstr[__i];
1329      for(;;)
1330	{
1331	  switch(__r->_M_tag)
1332	    {
1333	    case __detail::_S_concat:
1334	      {
1335		_RopeConcatenation* __c = (_RopeConcatenation*)__r;
1336		_RopeRep* __left = __c->_M_left;
1337		size_t __left_len = __left->_M_size;
1338
1339		if (__i >= __left_len)
1340		  {
1341		    __i -= __left_len;
1342		    __r = __c->_M_right;
1343		  }
1344		else
1345		  __r = __left;
1346	      }
1347	      break;
1348	    case __detail::_S_leaf:
1349	      {
1350		_RopeLeaf* __l = (_RopeLeaf*)__r;
1351		return __l->_M_data[__i];
1352	      }
1353	    case __detail::_S_function:
1354	    case __detail::_S_substringfn:
1355	      {
1356		_RopeFunction* __f = (_RopeFunction*)__r;
1357		_CharT __result;
1358
1359		(*(__f->_M_fn))(__i, 1, &__result);
1360		return __result;
1361	      }
1362	    }
1363	}
1364    }
1365
1366#ifndef __GC
1367  // Return a uniquely referenced character slot for the given
1368  // position, or 0 if that's not possible.
1369  template <class _CharT, class _Alloc>
1370    _CharT*
1371    rope<_CharT, _Alloc>::
1372    _S_fetch_ptr(_RopeRep* __r, size_type __i)
1373    {
1374      _RopeRep* __clrstack[__detail::_S_max_rope_depth];
1375      size_t __csptr = 0;
1376
1377      for(;;)
1378	{
1379	  if (__r->_M_ref_count > 1)
1380	    return 0;
1381	  switch(__r->_M_tag)
1382	    {
1383	    case __detail::_S_concat:
1384	      {
1385		_RopeConcatenation* __c = (_RopeConcatenation*)__r;
1386		_RopeRep* __left = __c->_M_left;
1387		size_t __left_len = __left->_M_size;
1388
1389		if (__c->_M_c_string != 0)
1390		  __clrstack[__csptr++] = __c;
1391		if (__i >= __left_len)
1392		  {
1393		    __i -= __left_len;
1394		    __r = __c->_M_right;
1395		  }
1396		else
1397		  __r = __left;
1398	      }
1399	      break;
1400	    case __detail::_S_leaf:
1401	      {
1402		_RopeLeaf* __l = (_RopeLeaf*)__r;
1403		if (__l->_M_c_string != __l->_M_data && __l->_M_c_string != 0)
1404		  __clrstack[__csptr++] = __l;
1405		while (__csptr > 0)
1406		  {
1407		    -- __csptr;
1408		    _RopeRep* __d = __clrstack[__csptr];
1409		    __d->_M_free_c_string();
1410		    __d->_M_c_string = 0;
1411		  }
1412		return __l->_M_data + __i;
1413	      }
1414	    case __detail::_S_function:
1415	    case __detail::_S_substringfn:
1416	      return 0;
1417	    }
1418	}
1419    }
1420#endif /* __GC */
1421
1422  // The following could be implemented trivially using
1423  // lexicographical_compare_3way.
1424  // We do a little more work to avoid dealing with rope iterators for
1425  // flat strings.
1426  template <class _CharT, class _Alloc>
1427    int
1428    rope<_CharT, _Alloc>::
1429    _S_compare (const _RopeRep* __left, const _RopeRep* __right)
1430    {
1431      size_t __left_len;
1432      size_t __right_len;
1433
1434      if (0 == __right)
1435	return 0 != __left;
1436      if (0 == __left)
1437	return -1;
1438      __left_len = __left->_M_size;
1439      __right_len = __right->_M_size;
1440      if (__detail::_S_leaf == __left->_M_tag)
1441	{
1442	  _RopeLeaf* __l = (_RopeLeaf*) __left;
1443	  if (__detail::_S_leaf == __right->_M_tag)
1444	    {
1445	      _RopeLeaf* __r = (_RopeLeaf*) __right;
1446	      return lexicographical_compare_3way(__l->_M_data,
1447						  __l->_M_data + __left_len,
1448						  __r->_M_data, __r->_M_data
1449						  + __right_len);
1450	    }
1451	  else
1452	    {
1453	      const_iterator __rstart(__right, 0);
1454	      const_iterator __rend(__right, __right_len);
1455	      return lexicographical_compare_3way(__l->_M_data, __l->_M_data
1456						  + __left_len,
1457						  __rstart, __rend);
1458	    }
1459	}
1460      else
1461	{
1462	  const_iterator __lstart(__left, 0);
1463	  const_iterator __lend(__left, __left_len);
1464	  if (__detail::_S_leaf == __right->_M_tag)
1465	    {
1466	      _RopeLeaf* __r = (_RopeLeaf*) __right;
1467	      return lexicographical_compare_3way(__lstart, __lend,
1468						  __r->_M_data, __r->_M_data
1469						  + __right_len);
1470	    }
1471	  else
1472	    {
1473	      const_iterator __rstart(__right, 0);
1474	      const_iterator __rend(__right, __right_len);
1475	      return lexicographical_compare_3way(__lstart, __lend,
1476						  __rstart, __rend);
1477	    }
1478	}
1479    }
1480
1481  // Assignment to reference proxies.
1482  template <class _CharT, class _Alloc>
1483    _Rope_char_ref_proxy<_CharT, _Alloc>&
1484    _Rope_char_ref_proxy<_CharT, _Alloc>::
1485    operator=(_CharT __c)
1486    {
1487      _RopeRep* __old = _M_root->_M_tree_ptr;
1488#ifndef __GC
1489      // First check for the case in which everything is uniquely
1490      // referenced.  In that case we can do this destructively.
1491      _CharT* __ptr = _My_rope::_S_fetch_ptr(__old, _M_pos);
1492      if (0 != __ptr)
1493	{
1494	  *__ptr = __c;
1495	  return *this;
1496	}
1497#endif
1498      _Self_destruct_ptr __left(_My_rope::_S_substring(__old, 0, _M_pos));
1499      _Self_destruct_ptr __right(_My_rope::_S_substring(__old, _M_pos + 1,
1500							__old->_M_size));
1501      _Self_destruct_ptr __result_left(_My_rope::
1502				       _S_destr_concat_char_iter(__left,
1503								 &__c, 1));
1504
1505      _RopeRep* __result = _My_rope::_S_concat(__result_left, __right);
1506#ifndef __GC
1507      _RopeRep::_S_unref(__old);
1508#endif
1509      _M_root->_M_tree_ptr = __result;
1510      return *this;
1511    }
1512
1513  template <class _CharT, class _Alloc>
1514    inline _Rope_char_ref_proxy<_CharT, _Alloc>::
1515    operator _CharT() const
1516    {
1517      if (_M_current_valid)
1518	return _M_current;
1519      else
1520	return _My_rope::_S_fetch(_M_root->_M_tree_ptr, _M_pos);
1521    }
1522
1523  template <class _CharT, class _Alloc>
1524    _Rope_char_ptr_proxy<_CharT, _Alloc>
1525    _Rope_char_ref_proxy<_CharT, _Alloc>::
1526    operator&() const
1527    { return _Rope_char_ptr_proxy<_CharT, _Alloc>(*this); }
1528
1529  template <class _CharT, class _Alloc>
1530    rope<_CharT, _Alloc>::
1531    rope(size_t __n, _CharT __c, const allocator_type& __a)
1532    : _Base(__a)
1533    {
1534      rope<_CharT,_Alloc> __result;
1535      const size_t __exponentiate_threshold = 32;
1536      size_t __exponent;
1537      size_t __rest;
1538      _CharT* __rest_buffer;
1539      _RopeRep* __remainder;
1540      rope<_CharT, _Alloc> __remainder_rope;
1541
1542      if (0 == __n)
1543	return;
1544
1545      __exponent = __n / __exponentiate_threshold;
1546      __rest = __n % __exponentiate_threshold;
1547      if (0 == __rest)
1548	__remainder = 0;
1549      else
1550	{
1551	  __rest_buffer = this->_Data_allocate(_S_rounded_up_size(__rest));
1552	  __uninitialized_fill_n_a(__rest_buffer, __rest, __c,
1553				   get_allocator());
1554	  _S_cond_store_eos(__rest_buffer[__rest]);
1555	  try
1556	    { __remainder = _S_new_RopeLeaf(__rest_buffer, __rest, __a); }
1557	  catch(...)
1558	    {
1559	      _RopeRep::__STL_FREE_STRING(__rest_buffer, __rest, __a);
1560	      __throw_exception_again;
1561	    }
1562	}
1563      __remainder_rope._M_tree_ptr = __remainder;
1564      if (__exponent != 0)
1565	{
1566	  _CharT* __base_buffer =
1567	    this->_Data_allocate(_S_rounded_up_size(__exponentiate_threshold));
1568	  _RopeLeaf* __base_leaf;
1569	  rope __base_rope;
1570	  __uninitialized_fill_n_a(__base_buffer, __exponentiate_threshold, __c,
1571				   get_allocator());
1572	  _S_cond_store_eos(__base_buffer[__exponentiate_threshold]);
1573	  try
1574	    {
1575	      __base_leaf = _S_new_RopeLeaf(__base_buffer,
1576					    __exponentiate_threshold, __a);
1577	    }
1578	  catch(...)
1579	    {
1580	      _RopeRep::__STL_FREE_STRING(__base_buffer,
1581					  __exponentiate_threshold, __a);
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;
1593	}
1594      else
1595	__result = __remainder_rope;
1596
1597      this->_M_tree_ptr = __result._M_tree_ptr;
1598      this->_M_tree_ptr->_M_ref_nonnil();
1599    }
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);
1628    }
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;
1640      if (__detail::_S_leaf == this->_M_tree_ptr->_M_tag
1641	  && 0 != __old_c_string)
1642	return(__old_c_string);
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,
1649					  this->get_allocator());
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;
1676    }
1677
1678#if !defined(__GNUC__)
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); }
1685#endif
1686
1687# if 0
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); }
1700# endif
1701
1702_GLIBCXX_END_NAMESPACE
1703