1 // Main templates for the -*- C++ -*- string classes.
2 // Copyright (C) 1994, 1995 Free Software Foundation
4 // This file is part of the GNU ANSI C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 2, or (at your option)
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING. If not, write to the Free
17 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // As a special exception, if you link this library with files
20 // compiled with a GNU compiler to produce an executable, this does not cause
21 // the resulting executable to be covered by the GNU General Public License.
22 // This exception does not however invalidate any other reasons why
23 // the executable file might be covered by the GNU General Public License.
25 // Written by Jason Merrill based upon the specification by Takanori Adachi
26 // in ANSI X3J16/94-0013R2.
36 #include <std/straits.h>
41 #define OUTOFRANGE(cond) \
42 do { if (!(cond)) throw out_of_range (#cond); } while (0)
43 #define LENGTHERROR(cond) \
44 do { if (!(cond)) throw length_error (#cond); } while (0)
49 #define OUTOFRANGE(cond) assert (!(cond))
50 #define LENGTHERROR(cond) assert (!(cond))
55 class istream
; class ostream
;
59 template <class charT
, class traits
= string_char_traits
<charT
> >
67 charT
* data () { return reinterpret_cast<charT
*>(this + 1); }
68 charT
& operator[] (size_t s
) { return data () [s
]; }
69 charT
* grab () { if (selfish
) return clone (); ++ref
; return data (); }
70 void release () { if (--ref
== 0) delete this; }
72 inline static void * operator new (size_t, size_t);
73 inline static Rep
* create (size_t);
76 inline void copy (size_t, const charT
*, size_t);
77 inline void move (size_t, const charT
*, size_t);
78 inline void set (size_t, const charT
, size_t);
81 // These function pointers allow you to modify the allocation policy used
82 // by the string classes. By default they expand by powers of two, but
83 // this may be excessive for space-critical applications.
85 // Returns true if ALLOCATED is too much larger than LENGTH
86 static bool (*excess_slop
) (size_t length
, size_t allocated
);
87 inline static bool default_excess (size_t, size_t);
89 // Returns a good amount of space to allocate for a string of length LENGTH
90 static size_t (*frob_size
) (size_t length
);
91 inline static size_t default_frob (size_t);
93 inline static bool excess_slop (size_t, size_t);
94 inline static size_t frob_size (size_t);
98 Rep
&operator= (const Rep
&);
103 typedef traits traits_type
;
104 typedef charT value_type
;
105 typedef size_t size_type
;
106 typedef ptrdiff_t difference_type
;
107 typedef charT
& reference
;
108 typedef const charT
& const_reference
;
109 typedef charT
* pointer
;
110 typedef const charT
* const_pointer
;
111 typedef pointer iterator
;
112 typedef const_pointer const_iterator
;
113 typedef reverse_iterator
<iterator
, value_type
,
114 reference
, difference_type
> reverse_iterator
;
115 typedef reverse_iterator
<const_iterator
, value_type
, const_reference
,
116 difference_type
> const_reverse_iterator
;
117 static const size_type npos
= static_cast<size_type
>(-1);
120 Rep
*rep () const { return reinterpret_cast<Rep
*>(dat
) - 1; }
121 void repup (Rep
*p
) { rep ()->release (); dat
= p
->data (); }
124 const charT
* data () const
125 { return rep ()->data(); }
126 size_type
length () const
127 { return rep ()->len
; }
128 size_type
size () const
129 { return rep ()->len
; }
130 size_type
capacity () const
131 { return rep ()->res
; }
132 size_type
max_size () const
133 { return (npos
- 1)/sizeof (charT
); } // XXX
135 { return size () == 0; }
137 // _lib.string.cons_ construct/copy/destroy:
138 basic_string
& operator= (const basic_string
& str
)
140 if (&str
!= this) { rep ()->release (); dat
= str
.rep ()->grab (); }
144 explicit basic_string (): dat (nilRep
.grab ()) { }
145 basic_string (const basic_string
& str
): dat (str
.rep ()->grab ()) { }
146 basic_string (const basic_string
& str
, size_type pos
, size_type n
= npos
)
147 : dat (nilRep
.grab ()) { assign (str
, pos
, n
); }
148 basic_string (const charT
* s
, size_type n
)
149 : dat (nilRep
.grab ()) { assign (s
, n
); }
150 basic_string (const charT
* s
)
151 : dat (nilRep
.grab ()) { assign (s
); }
152 basic_string (size_type n
, charT c
)
153 : dat (nilRep
.grab ()) { assign (n
, c
); }
155 template<class InputIterator
>
156 basic_string(InputIterator begin
, InputIterator end
,
157 Allocator
& = Allocator());
159 basic_string(const_iterator begin
, const_iterator end
)
160 : dat (nilRep
.grab ()) { assign (begin
, end
); }
164 { rep ()->release (); }
166 void swap (basic_string
&s
) { charT
*d
= dat
; dat
= s
.dat
; s
.dat
= d
; }
168 basic_string
& append (const basic_string
& str
, size_type pos
= 0,
170 { return replace (length (), 0, str
, pos
, n
); }
171 basic_string
& append (const charT
* s
, size_type n
)
172 { return replace (length (), 0, s
, n
); }
173 basic_string
& append (const charT
* s
)
174 { return append (s
, traits::length (s
)); }
175 basic_string
& append (size_type n
, charT c
)
176 { return replace (length (), 0, n
, c
); }
178 template<class InputIterator
>
179 basic_string
& append(InputIterator first
, InputIterator last
);
181 basic_string
& append(const_iterator first
, const_iterator last
)
182 { return replace (length (), 0, first
, last
- first
); }
185 basic_string
& assign (const basic_string
& str
, size_type pos
= 0,
187 { return replace (0, npos
, str
, pos
, n
); }
188 basic_string
& assign (const charT
* s
, size_type n
)
189 { return replace (0, npos
, s
, n
); }
190 basic_string
& assign (const charT
* s
)
191 { return assign (s
, traits::length (s
)); }
192 basic_string
& assign (size_type n
, charT c
)
193 { return replace (0, npos
, n
, c
); }
195 template<class InputIterator
>
196 basic_string
& assign(InputIterator first
, InputIterator last
);
198 basic_string
& assign(const_iterator first
, const_iterator last
)
199 { return replace (0, npos
, first
, last
- first
); }
202 basic_string
& operator= (const charT
* s
)
203 { return assign (s
); }
204 basic_string
& operator= (charT c
)
205 { return assign (1, c
); }
207 basic_string
& operator+= (const basic_string
& rhs
)
208 { return append (rhs
); }
209 basic_string
& operator+= (const charT
* s
)
210 { return append (s
); }
211 basic_string
& operator+= (charT c
)
212 { return append (1, c
); }
214 basic_string
& insert (size_type pos1
, const basic_string
& str
,
215 size_type pos2
= 0, size_type n
= npos
)
216 { return replace (pos1
, 0, str
, pos2
, n
); }
217 basic_string
& insert (size_type pos
, const charT
* s
, size_type n
)
218 { return replace (pos
, 0, s
, n
); }
219 basic_string
& insert (size_type pos
, const charT
* s
)
220 { return insert (pos
, s
, traits::length (s
)); }
221 basic_string
& insert (size_type pos
, size_type n
, charT c
)
222 { return replace (pos
, 0, n
, c
); }
223 iterator
insert(iterator p
, charT c
)
224 { size_type pos
= p
- begin (); insert (pos
, 1, c
); return pos
+begin (); }
225 iterator
insert(iterator p
, size_type n
, charT c
)
226 { size_type pos
= p
- begin (); insert (pos
, n
, c
); return pos
+begin (); }
228 template<class InputIterator
>
229 void insert(iterator p
, InputIterator first
, InputIterator last
);
231 void insert(iterator p
, const_iterator first
, const_iterator last
)
232 { size_type pos
= p
- begin(); insert (pos
, first
, last
- first
); }
235 basic_string
& remove (size_type pos
= 0, size_type n
= npos
)
236 { return replace (pos
, n
, (size_type
)0, (charT
)0); }
237 basic_string
& remove (iterator pos
)
238 { return replace (pos
- begin (), 1, (size_type
)0, (charT
)0); }
239 basic_string
& remove (iterator first
, iterator last
)
240 { return replace (first
- begin (), last
- first
, (size_type
)0, (charT
)0);}
242 basic_string
& replace (size_type pos1
, size_type n1
, const basic_string
& str
,
243 size_type pos2
= 0, size_type n2
= npos
);
244 basic_string
& replace (size_type pos
, size_type n1
, const charT
* s
,
246 basic_string
& replace (size_type pos
, size_type n1
, const charT
* s
)
247 { return replace (pos
, n1
, s
, traits::length (s
)); }
248 basic_string
& replace (size_type pos
, size_type n1
, size_type n2
, charT c
);
249 basic_string
& replace (size_type pos
, size_type n
, charT c
)
250 { return replace (pos
, n
, 1, c
); }
251 basic_string
& replace (iterator i1
, iterator i2
, const basic_string
& str
)
252 { return replace (i1
- begin (), i2
- i1
, str
); }
253 basic_string
& replace (iterator i1
, iterator i2
, const charT
* s
, size_type n
)
254 { return replace (i1
- begin (), i2
- i1
, s
, n
); }
255 basic_string
& replace (iterator i1
, iterator i2
, const charT
* s
)
256 { return replace (i1
- begin (), i2
- i1
, s
); }
257 basic_string
& replace (iterator i1
, iterator i2
, size_type n
, charT c
)
258 { return replace (i1
- begin (), i2
- i1
, n
, c
); }
260 template<class InputIterator
>
261 basic_string
& replace(iterator i1
, iterator i2
,
262 InputIterator j1
, InputIterator j2
);
264 basic_string
& replace(iterator i1
, iterator i2
,
265 const_iterator j1
, const_iterator j2
)
266 { return replace (i1
, i2
, j1
, j2
- j1
); }
270 static charT
eos () { return traits::eos (); }
271 void unique () { if (rep ()->ref
> 1) alloc (capacity (), true); }
272 void selfish () { unique (); rep ()->selfish
= true; }
275 charT
operator[] (size_type pos
) const
277 if (pos
== length ())
282 reference
operator[] (size_type pos
)
283 { unique (); return (*rep ())[pos
]; }
285 reference
at (size_type pos
)
287 OUTOFRANGE (pos
>= length ());
290 const_reference
at (size_type pos
) const
292 OUTOFRANGE (pos
>= length ());
297 void terminate () const
298 { traits::assign ((*rep ())[length ()], eos ()); }
301 const charT
* c_str () const
302 { terminate (); return data (); }
303 void resize (size_type n
, charT c
);
304 void resize (size_type n
)
305 { resize (n
, eos ()); }
306 void reserve (size_type
) { }
308 size_type
copy (charT
* s
, size_type n
, size_type pos
= 0);
310 size_type
find (const basic_string
& str
, size_type pos
= 0) const
311 { return find (str
.data(), pos
, str
.length()); }
312 size_type
find (const charT
* s
, size_type pos
, size_type n
) const;
313 size_type
find (const charT
* s
, size_type pos
= 0) const
314 { return find (s
, pos
, traits::length (s
)); }
315 size_type
find (charT c
, size_type pos
= 0) const;
317 size_type
rfind (const basic_string
& str
, size_type pos
= npos
) const
318 { return rfind (str
.data(), pos
, str
.length()); }
319 size_type
rfind (const charT
* s
, size_type pos
, size_type n
) const;
320 size_type
rfind (const charT
* s
, size_type pos
= npos
) const
321 { return rfind (s
, pos
, traits::length (s
)); }
322 size_type
rfind (charT c
, size_type pos
= npos
) const;
324 size_type
find_first_of (const basic_string
& str
, size_type pos
= 0) const
325 { return find_first_of (str
.data(), pos
, str
.length()); }
326 size_type
find_first_of (const charT
* s
, size_type pos
, size_type n
) const;
327 size_type
find_first_of (const charT
* s
, size_type pos
= 0) const
328 { return find_first_of (s
, pos
, traits::length (s
)); }
329 size_type
find_first_of (charT c
, size_type pos
= 0) const
330 { return find (c
, pos
); }
332 size_type
find_last_of (const basic_string
& str
, size_type pos
= npos
) const
333 { return find_last_of (str
.data(), pos
, str
.length()); }
334 size_type
find_last_of (const charT
* s
, size_type pos
, size_type n
) const;
335 size_type
find_last_of (const charT
* s
, size_type pos
= npos
) const
336 { return find_last_of (s
, pos
, traits::length (s
)); }
337 size_type
find_last_of (charT c
, size_type pos
= npos
) const
338 { return rfind (c
, pos
); }
340 size_type
find_first_not_of (const basic_string
& str
, size_type pos
= 0) const
341 { return find_first_not_of (str
.data(), pos
, str
.length()); }
342 size_type
find_first_not_of (const charT
* s
, size_type pos
, size_type n
) const;
343 size_type
find_first_not_of (const charT
* s
, size_type pos
= 0) const
344 { return find_first_not_of (s
, pos
, traits::length (s
)); }
345 size_type
find_first_not_of (charT c
, size_type pos
= 0) const;
347 size_type
find_last_not_of (const basic_string
& str
, size_type pos
= npos
) const
348 { return find_last_not_of (str
.data(), pos
, str
.length()); }
349 size_type
find_last_not_of (const charT
* s
, size_type pos
, size_type n
) const;
350 size_type
find_last_not_of (const charT
* s
, size_type pos
= npos
) const
351 { return find_last_not_of (s
, pos
, traits::length (s
)); }
352 size_type
find_last_not_of (charT c
, size_type pos
= npos
) const;
354 basic_string
substr (size_type pos
= 0, size_type n
= npos
) const
355 { return basic_string (*this, pos
, n
); }
357 int compare (const basic_string
& str
, size_type pos
= 0, size_type n
= npos
) const;
358 // There is no 'strncmp' equivalent for charT pointers.
359 int compare (const charT
* s
, size_type pos
, size_type n
) const;
360 int compare (const charT
* s
, size_type pos
= 0) const
361 { return compare (s
, pos
, traits::length (s
)); }
363 iterator
begin () { selfish (); return &(*this)[0]; }
364 iterator
end () { selfish (); return &(*this)[length ()]; }
365 const_iterator
begin () const { return &(*rep ())[0]; }
366 const_iterator
end () const { return &(*rep ())[length ()]; }
368 reverse_iterator
rbegin() { return reverse_iterator (end ()); }
369 const_reverse_iterator
rbegin() const
370 { return const_reverse_iterator (end ()); }
371 reverse_iterator
rend() { return reverse_iterator (begin ()); }
372 const_reverse_iterator
rend() const
373 { return const_reverse_iterator (begin ()); }
376 void alloc (size_type size
, bool save
);
377 static size_type
_find (const charT
* ptr
, charT c
, size_type xpos
, size_type len
);
378 inline bool check_realloc (size_type s
) const;
384 template <class charT
, class traits
>
385 inline basic_string
<charT
, traits
>
386 operator+ (const basic_string
<charT
, traits
>& lhs
,
387 const basic_string
<charT
, traits
>& rhs
)
389 basic_string
<charT
, traits
> str (lhs
);
394 template <class charT
, class traits
>
395 inline basic_string
<charT
, traits
>
396 operator+ (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
398 basic_string
<charT
, traits
> str (lhs
);
403 template <class charT
, class traits
>
404 inline basic_string
<charT
, traits
>
405 operator+ (charT lhs
, const basic_string
<charT
, traits
>& rhs
)
407 basic_string
<charT
, traits
> str (1, lhs
);
412 template <class charT
, class traits
>
413 inline basic_string
<charT
, traits
>
414 operator+ (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
416 basic_string
<charT
, traits
> str (lhs
);
421 template <class charT
, class traits
>
422 inline basic_string
<charT
, traits
>
423 operator+ (const basic_string
<charT
, traits
>& lhs
, charT rhs
)
425 basic_string
<charT
, traits
> str (lhs
);
430 template <class charT
, class traits
>
432 operator== (const basic_string
<charT
, traits
>& lhs
,
433 const basic_string
<charT
, traits
>& rhs
)
435 return (lhs
.compare (rhs
) == 0);
438 template <class charT
, class traits
>
440 operator== (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
442 return (rhs
.compare (lhs
) == 0);
445 template <class charT
, class traits
>
447 operator== (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
449 return (lhs
.compare (rhs
) == 0);
452 template <class charT
, class traits
>
454 operator!= (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
456 return (rhs
.compare (lhs
) != 0);
459 template <class charT
, class traits
>
461 operator!= (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
463 return (lhs
.compare (rhs
) != 0);
466 template <class charT
, class traits
>
468 operator< (const basic_string
<charT
, traits
>& lhs
,
469 const basic_string
<charT
, traits
>& rhs
)
471 return (lhs
.compare (rhs
) < 0);
474 template <class charT
, class traits
>
476 operator< (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
478 return (rhs
.compare (lhs
) > 0);
481 template <class charT
, class traits
>
483 operator< (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
485 return (lhs
.compare (rhs
) < 0);
488 template <class charT
, class traits
>
490 operator> (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
492 return (rhs
.compare (lhs
) < 0);
495 template <class charT
, class traits
>
497 operator> (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
499 return (lhs
.compare (rhs
) > 0);
502 template <class charT
, class traits
>
504 operator<= (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
506 return (rhs
.compare (lhs
) >= 0);
509 template <class charT
, class traits
>
511 operator<= (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
513 return (lhs
.compare (rhs
) <= 0);
516 template <class charT
, class traits
>
518 operator>= (const charT
* lhs
, const basic_string
<charT
, traits
>& rhs
)
520 return (rhs
.compare (lhs
) <= 0);
523 template <class charT
, class traits
>
525 operator>= (const basic_string
<charT
, traits
>& lhs
, const charT
* rhs
)
527 return (lhs
.compare (rhs
) >= 0);
530 template <class charT
, class traits
>
532 operator!= (const basic_string
<charT
, traits
>& lhs
,
533 const basic_string
<charT
, traits
>& rhs
)
535 return (lhs
.compare (rhs
) != 0);
538 template <class charT
, class traits
>
540 operator> (const basic_string
<charT
, traits
>& lhs
,
541 const basic_string
<charT
, traits
>& rhs
)
543 return (lhs
.compare (rhs
) > 0);
546 template <class charT
, class traits
>
548 operator<= (const basic_string
<charT
, traits
>& lhs
,
549 const basic_string
<charT
, traits
>& rhs
)
551 return (lhs
.compare (rhs
) <= 0);
554 template <class charT
, class traits
>
556 operator>= (const basic_string
<charT
, traits
>& lhs
,
557 const basic_string
<charT
, traits
>& rhs
)
559 return (lhs
.compare (rhs
) >= 0);
562 class istream
; class ostream
;
563 template <class charT
, class traits
> istream
&
564 operator>> (istream
&, basic_string
<charT
, traits
>&);
565 template <class charT
, class traits
> ostream
&
566 operator<< (ostream
&, const basic_string
<charT
, traits
>&);
567 template <class charT
, class traits
> istream
&
568 getline (istream
&, basic_string
<charT
, traits
>&, charT delim
= '\n');