(c_str): Change return of "" to return of traits::empty() call so that proper...
[gcc.git] / libstdc++ / std / bastring.h
1 // Main templates for the -*- C++ -*- string classes.
2 // Copyright (C) 1994, 1995 Free Software Foundation
3
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)
8 // any later version.
9
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.
14
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.
18
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.
24
25 // Written by Jason Merrill based upon the specification by Takanori Adachi
26 // in ANSI X3J16/94-0013R2.
27
28 #ifndef __BASTRING__
29 #define __BASTRING__
30
31 #ifdef __GNUG__
32 #pragma interface
33 #endif
34
35 #include <cstddef>
36 #include <std/straits.h>
37
38 // NOTE : This does NOT conform to the draft standard and is likely to change
39 #include <alloc.h>
40
41 extern "C++" {
42 class istream; class ostream;
43
44 #include <iterator>
45
46 #ifdef __STL_USE_EXCEPTIONS
47
48 extern void __out_of_range (const char *);
49 extern void __length_error (const char *);
50
51 #define OUTOFRANGE(cond) \
52 do { if (cond) __out_of_range (#cond); } while (0)
53 #define LENGTHERROR(cond) \
54 do { if (cond) __length_error (#cond); } while (0)
55
56 #else
57
58 #include <cassert>
59 #define OUTOFRANGE(cond) assert (!(cond))
60 #define LENGTHERROR(cond) assert (!(cond))
61
62 #endif
63
64 template <class charT, class traits = string_char_traits<charT>,
65 class Allocator = alloc >
66 class basic_string
67 {
68 private:
69 struct Rep {
70 size_t len, res, ref;
71 bool selfish;
72
73 charT* data () { return reinterpret_cast<charT *>(this + 1); }
74 charT& operator[] (size_t s) { return data () [s]; }
75 charT* grab () { if (selfish) return clone (); ++ref; return data (); }
76 void release () { if (--ref == 0) delete this; }
77
78 inline static void * operator new (size_t, size_t);
79 inline static void operator delete (void *);
80 inline static Rep* create (size_t);
81 charT* clone ();
82
83 inline void copy (size_t, const charT *, size_t);
84 inline void move (size_t, const charT *, size_t);
85 inline void set (size_t, const charT, size_t);
86
87 inline static bool excess_slop (size_t, size_t);
88 inline static size_t frob_size (size_t);
89
90 private:
91 Rep &operator= (const Rep &);
92 };
93
94 public:
95 // types:
96 typedef traits traits_type;
97 typedef typename traits::char_type value_type;
98 typedef Allocator allocator_type;
99
100 typedef size_t size_type;
101 typedef ptrdiff_t difference_type;
102 typedef charT& reference;
103 typedef const charT& const_reference;
104 typedef charT* pointer;
105 typedef const charT* const_pointer;
106 typedef pointer iterator;
107 typedef const_pointer const_iterator;
108 typedef ::reverse_iterator<iterator> reverse_iterator;
109 typedef ::reverse_iterator<const_iterator> const_reverse_iterator;
110 static const size_type npos = static_cast<size_type>(-1);
111
112 private:
113 Rep *rep () const { return reinterpret_cast<Rep *>(dat) - 1; }
114 void repup (Rep *p) { rep ()->release (); dat = p->data (); }
115
116 public:
117 const charT* data () const
118 { return rep ()->data(); }
119 size_type length () const
120 { return rep ()->len; }
121 size_type size () const
122 { return rep ()->len; }
123 size_type capacity () const
124 { return rep ()->res; }
125 size_type max_size () const
126 { return (npos - 1)/sizeof (charT); } // XXX
127 bool empty () const
128 { return size () == 0; }
129
130 // _lib.string.cons_ construct/copy/destroy:
131 basic_string& operator= (const basic_string& str)
132 {
133 if (&str != this) { rep ()->release (); dat = str.rep ()->grab (); }
134 return *this;
135 }
136
137 explicit basic_string (): dat (nilRep.grab ()) { }
138 basic_string (const basic_string& str): dat (str.rep ()->grab ()) { }
139 basic_string (const basic_string& str, size_type pos, size_type n = npos)
140 : dat (nilRep.grab ()) { assign (str, pos, n); }
141 basic_string (const charT* s, size_type n)
142 : dat (nilRep.grab ()) { assign (s, n); }
143 basic_string (const charT* s)
144 : dat (nilRep.grab ()) { assign (s); }
145 basic_string (size_type n, charT c)
146 : dat (nilRep.grab ()) { assign (n, c); }
147 #ifdef __STL_MEMBER_TEMPLATES
148 template<class InputIterator>
149 basic_string(InputIterator begin, InputIterator end)
150 #else
151 basic_string(const_iterator begin, const_iterator end)
152 #endif
153 : dat (nilRep.grab ()) { assign (begin, end); }
154
155 ~basic_string ()
156 { rep ()->release (); }
157
158 void swap (basic_string &s) { charT *d = dat; dat = s.dat; s.dat = d; }
159
160 basic_string& append (const basic_string& str, size_type pos = 0,
161 size_type n = npos)
162 { return replace (length (), 0, str, pos, n); }
163 basic_string& append (const charT* s, size_type n)
164 { return replace (length (), 0, s, n); }
165 basic_string& append (const charT* s)
166 { return append (s, traits::length (s)); }
167 basic_string& append (size_type n, charT c)
168 { return replace (length (), 0, n, c); }
169 #ifdef __STL_MEMBER_TEMPLATES
170 template<class InputIterator>
171 basic_string& append(InputIterator first, InputIterator last)
172 #else
173 basic_string& append(const_iterator first, const_iterator last)
174 #endif
175 { return replace (iend (), iend (), first, last); }
176
177 basic_string& assign (const basic_string& str, size_type pos = 0,
178 size_type n = npos)
179 { return replace (0, npos, str, pos, n); }
180 basic_string& assign (const charT* s, size_type n)
181 { return replace (0, npos, s, n); }
182 basic_string& assign (const charT* s)
183 { return assign (s, traits::length (s)); }
184 basic_string& assign (size_type n, charT c)
185 { return replace (0, npos, n, c); }
186 #ifdef __STL_MEMBER_TEMPLATES
187 template<class InputIterator>
188 basic_string& assign(InputIterator first, InputIterator last)
189 #else
190 basic_string& assign(const_iterator first, const_iterator last)
191 #endif
192 { return replace (ibegin (), iend (), first, last); }
193
194 basic_string& operator= (const charT* s)
195 { return assign (s); }
196 basic_string& operator= (charT c)
197 { return assign (1, c); }
198
199 basic_string& operator+= (const basic_string& rhs)
200 { return append (rhs); }
201 basic_string& operator+= (const charT* s)
202 { return append (s); }
203 basic_string& operator+= (charT c)
204 { return append (1, c); }
205
206 basic_string& insert (size_type pos1, const basic_string& str,
207 size_type pos2 = 0, size_type n = npos)
208 { return replace (pos1, 0, str, pos2, n); }
209 basic_string& insert (size_type pos, const charT* s, size_type n)
210 { return replace (pos, 0, s, n); }
211 basic_string& insert (size_type pos, const charT* s)
212 { return insert (pos, s, traits::length (s)); }
213 basic_string& insert (size_type pos, size_type n, charT c)
214 { return replace (pos, 0, n, c); }
215 iterator insert(iterator p, charT c)
216 { size_type __o = p - ibegin ();
217 insert (p - ibegin (), 1, c); selfish ();
218 return ibegin () + __o; }
219 iterator insert(iterator p, size_type n, charT c)
220 { size_type __o = p - ibegin ();
221 insert (p - ibegin (), n, c); selfish ();
222 return ibegin () + __o; }
223 #ifdef __STL_MEMBER_TEMPLATES
224 template<class InputIterator>
225 void insert(iterator p, InputIterator first, InputIterator last)
226 #else
227 void insert(iterator p, const_iterator first, const_iterator last)
228 #endif
229 { replace (p, p, first, last); }
230
231 basic_string& erase (size_type pos = 0, size_type n = npos)
232 { return replace (pos, n, (size_type)0, (charT)0); }
233 iterator erase(iterator p)
234 { size_type __o = p - begin();
235 replace (__o, 1, (size_type)0, (charT)0); selfish ();
236 return ibegin() + __o; }
237 iterator erase(iterator f, iterator l)
238 { size_type __o = f - ibegin();
239 replace (__o, l-f, (size_type)0, (charT)0);selfish ();
240 return ibegin() + __o; }
241
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,
245 size_type n2);
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 - ibegin (), i2 - i1, str); }
253 basic_string& replace (iterator i1, iterator i2, const charT* s, size_type n)
254 { return replace (i1 - ibegin (), i2 - i1, s, n); }
255 basic_string& replace (iterator i1, iterator i2, const charT* s)
256 { return replace (i1 - ibegin (), i2 - i1, s); }
257 basic_string& replace (iterator i1, iterator i2, size_type n, charT c)
258 { return replace (i1 - ibegin (), i2 - i1, n, c); }
259 #ifdef __STL_MEMBER_TEMPLATES
260 template<class InputIterator>
261 basic_string& replace(iterator i1, iterator i2,
262 InputIterator j1, InputIterator j2);
263 #else
264 basic_string& replace(iterator i1, iterator i2,
265 const_iterator j1, const_iterator j2);
266 #endif
267
268 private:
269 static charT eos () { return traits::eos (); }
270 void unique () { if (rep ()->ref > 1) alloc (length (), true); }
271 void selfish () { unique (); rep ()->selfish = true; }
272
273 public:
274 charT operator[] (size_type pos) const
275 {
276 if (pos == length ())
277 return eos ();
278 return data ()[pos];
279 }
280
281 reference operator[] (size_type pos)
282 { selfish (); return (*rep ())[pos]; }
283
284 reference at (size_type pos)
285 {
286 OUTOFRANGE (pos >= length ());
287 return (*this)[pos];
288 }
289 const_reference at (size_type pos) const
290 {
291 OUTOFRANGE (pos >= length ());
292 return data ()[pos];
293 }
294
295 private:
296 void terminate () const
297 { traits::assign ((*rep ())[length ()], eos ()); }
298
299 public:
300 const charT* c_str () const
301 { if (length () == 0) return traits::empty();
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) { }
307
308 size_type copy (charT* s, size_type n, size_type pos = 0) const;
309
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;
316
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;
323
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); }
331
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); }
339
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;
346
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;
353
354 basic_string substr (size_type pos = 0, size_type n = npos) const
355 { return basic_string (*this, pos, n); }
356
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)); }
362
363 iterator begin () { selfish (); return &(*this)[0]; }
364 iterator end () { selfish (); return &(*this)[length ()]; }
365
366 private:
367 iterator ibegin () const { return &(*rep ())[0]; }
368 iterator iend () const { return &(*rep ())[length ()]; }
369
370 public:
371 const_iterator begin () const { return ibegin (); }
372 const_iterator end () const { return iend (); }
373
374 reverse_iterator rbegin() { return reverse_iterator (end ()); }
375 const_reverse_iterator rbegin() const
376 { return const_reverse_iterator (end ()); }
377 reverse_iterator rend() { return reverse_iterator (begin ()); }
378 const_reverse_iterator rend() const
379 { return const_reverse_iterator (begin ()); }
380
381 private:
382 void alloc (size_type size, bool save);
383 static size_type _find (const charT* ptr, charT c, size_type xpos, size_type len);
384 inline bool check_realloc (size_type s) const;
385
386 static Rep nilRep;
387 charT *dat;
388 };
389
390 #ifdef __STL_MEMBER_TEMPLATES
391 template <class charT, class traits, class Allocator> template <class InputIterator>
392 basic_string <charT, traits, Allocator>& basic_string <charT, traits, Allocator>::
393 replace (iterator i1, iterator i2, InputIterator j1, InputIterator j2)
394 #else
395 template <class charT, class traits, class Allocator>
396 basic_string <charT, traits, Allocator>& basic_string <charT, traits, Allocator>::
397 replace (iterator i1, iterator i2, const_iterator j1, const_iterator j2)
398 #endif
399 {
400 const size_type len = length ();
401 size_type pos = i1 - ibegin ();
402 size_type n1 = i2 - i1;
403 size_type n2 = j2 - j1;
404
405 OUTOFRANGE (pos > len);
406 if (n1 > len - pos)
407 n1 = len - pos;
408 LENGTHERROR (len - n1 > max_size () - n2);
409 size_t newlen = len - n1 + n2;
410
411 if (check_realloc (newlen))
412 {
413 Rep *p = Rep::create (newlen);
414 p->copy (0, data (), pos);
415 p->copy (pos + n2, data () + pos + n1, len - (pos + n1));
416 for (; j1 != j2; ++j1, ++pos)
417 traits::assign ((*p)[pos], *j1);
418 repup (p);
419 }
420 else
421 {
422 rep ()->move (pos + n2, data () + pos + n1, len - (pos + n1));
423 for (; j1 != j2; ++j1, ++pos)
424 traits::assign ((*rep ())[pos], *j1);
425 }
426 rep ()->len = newlen;
427
428 return *this;
429 }
430
431 template <class charT, class traits, class Allocator>
432 inline basic_string <charT, traits, Allocator>
433 operator+ (const basic_string <charT, traits, Allocator>& lhs,
434 const basic_string <charT, traits, Allocator>& rhs)
435 {
436 basic_string <charT, traits, Allocator> str (lhs);
437 str.append (rhs);
438 return str;
439 }
440
441 template <class charT, class traits, class Allocator>
442 inline basic_string <charT, traits, Allocator>
443 operator+ (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
444 {
445 basic_string <charT, traits, Allocator> str (lhs);
446 str.append (rhs);
447 return str;
448 }
449
450 template <class charT, class traits, class Allocator>
451 inline basic_string <charT, traits, Allocator>
452 operator+ (charT lhs, const basic_string <charT, traits, Allocator>& rhs)
453 {
454 basic_string <charT, traits, Allocator> str (1, lhs);
455 str.append (rhs);
456 return str;
457 }
458
459 template <class charT, class traits, class Allocator>
460 inline basic_string <charT, traits, Allocator>
461 operator+ (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
462 {
463 basic_string <charT, traits, Allocator> str (lhs);
464 str.append (rhs);
465 return str;
466 }
467
468 template <class charT, class traits, class Allocator>
469 inline basic_string <charT, traits, Allocator>
470 operator+ (const basic_string <charT, traits, Allocator>& lhs, charT rhs)
471 {
472 basic_string <charT, traits, Allocator> str (lhs);
473 str.append (1, rhs);
474 return str;
475 }
476
477 template <class charT, class traits, class Allocator>
478 inline bool
479 operator== (const basic_string <charT, traits, Allocator>& lhs,
480 const basic_string <charT, traits, Allocator>& rhs)
481 {
482 return (lhs.compare (rhs) == 0);
483 }
484
485 template <class charT, class traits, class Allocator>
486 inline bool
487 operator== (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
488 {
489 return (rhs.compare (lhs) == 0);
490 }
491
492 template <class charT, class traits, class Allocator>
493 inline bool
494 operator== (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
495 {
496 return (lhs.compare (rhs) == 0);
497 }
498
499 template <class charT, class traits, class Allocator>
500 inline bool
501 operator!= (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
502 {
503 return (rhs.compare (lhs) != 0);
504 }
505
506 template <class charT, class traits, class Allocator>
507 inline bool
508 operator!= (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
509 {
510 return (lhs.compare (rhs) != 0);
511 }
512
513 template <class charT, class traits, class Allocator>
514 inline bool
515 operator< (const basic_string <charT, traits, Allocator>& lhs,
516 const basic_string <charT, traits, Allocator>& rhs)
517 {
518 return (lhs.compare (rhs) < 0);
519 }
520
521 template <class charT, class traits, class Allocator>
522 inline bool
523 operator< (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
524 {
525 return (rhs.compare (lhs) > 0);
526 }
527
528 template <class charT, class traits, class Allocator>
529 inline bool
530 operator< (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
531 {
532 return (lhs.compare (rhs) < 0);
533 }
534
535 template <class charT, class traits, class Allocator>
536 inline bool
537 operator> (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
538 {
539 return (rhs.compare (lhs) < 0);
540 }
541
542 template <class charT, class traits, class Allocator>
543 inline bool
544 operator> (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
545 {
546 return (lhs.compare (rhs) > 0);
547 }
548
549 template <class charT, class traits, class Allocator>
550 inline bool
551 operator<= (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
552 {
553 return (rhs.compare (lhs) >= 0);
554 }
555
556 template <class charT, class traits, class Allocator>
557 inline bool
558 operator<= (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
559 {
560 return (lhs.compare (rhs) <= 0);
561 }
562
563 template <class charT, class traits, class Allocator>
564 inline bool
565 operator>= (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
566 {
567 return (rhs.compare (lhs) <= 0);
568 }
569
570 template <class charT, class traits, class Allocator>
571 inline bool
572 operator>= (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
573 {
574 return (lhs.compare (rhs) >= 0);
575 }
576
577 template <class charT, class traits, class Allocator>
578 inline bool
579 operator!= (const basic_string <charT, traits, Allocator>& lhs,
580 const basic_string <charT, traits, Allocator>& rhs)
581 {
582 return (lhs.compare (rhs) != 0);
583 }
584
585 template <class charT, class traits, class Allocator>
586 inline bool
587 operator> (const basic_string <charT, traits, Allocator>& lhs,
588 const basic_string <charT, traits, Allocator>& rhs)
589 {
590 return (lhs.compare (rhs) > 0);
591 }
592
593 template <class charT, class traits, class Allocator>
594 inline bool
595 operator<= (const basic_string <charT, traits, Allocator>& lhs,
596 const basic_string <charT, traits, Allocator>& rhs)
597 {
598 return (lhs.compare (rhs) <= 0);
599 }
600
601 template <class charT, class traits, class Allocator>
602 inline bool
603 operator>= (const basic_string <charT, traits, Allocator>& lhs,
604 const basic_string <charT, traits, Allocator>& rhs)
605 {
606 return (lhs.compare (rhs) >= 0);
607 }
608
609 class istream; class ostream;
610 template <class charT, class traits, class Allocator> istream&
611 operator>> (istream&, basic_string <charT, traits, Allocator>&);
612 template <class charT, class traits, class Allocator> ostream&
613 operator<< (ostream&, const basic_string <charT, traits, Allocator>&);
614 template <class charT, class traits, class Allocator> istream&
615 getline (istream&, basic_string <charT, traits, Allocator>&, charT delim = '\n');
616
617 } // extern "C++"
618
619 #include <std/bastring.cc>
620
621 #endif