[multiple changes]
[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 #if _G_USE_EXCEPTIONS
39
40 #include <stdexcept>
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)
45
46 #else
47
48 #include <cassert>
49 #define OUTOFRANGE(cond) assert (!(cond))
50 #define LENGTHERROR(cond) assert (!(cond))
51
52 #endif
53
54 extern "C++" {
55 class istream; class ostream;
56
57 #include <iterator>
58
59 template <class charT, class traits = string_char_traits<charT> >
60 class basic_string
61 {
62 private:
63 struct Rep {
64 size_t len, res, ref;
65 bool selfish;
66
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; }
71
72 inline static void * operator new (size_t, size_t);
73 inline static Rep* create (size_t);
74 charT* clone ();
75
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);
79
80 #if _G_ALLOC_CONTROL
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.
84
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);
88
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);
92 #else
93 inline static bool excess_slop (size_t, size_t);
94 inline static size_t frob_size (size_t);
95 #endif
96
97 private:
98 Rep &operator= (const Rep &);
99 };
100
101 public:
102 // types:
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);
118
119 private:
120 Rep *rep () const { return reinterpret_cast<Rep *>(dat) - 1; }
121 void repup (Rep *p) { rep ()->release (); dat = p->data (); }
122
123 public:
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
134 bool empty () const
135 { return size () == 0; }
136
137 // _lib.string.cons_ construct/copy/destroy:
138 basic_string& operator= (const basic_string& str)
139 {
140 if (&str != this) { rep ()->release (); dat = str.rep ()->grab (); }
141 return *this;
142 }
143
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); }
154 #if 0
155 template<class InputIterator>
156 basic_string(InputIterator begin, InputIterator end,
157 Allocator& = Allocator());
158 #else
159 basic_string(const_iterator begin, const_iterator end)
160 : dat (nilRep.grab ()) { assign (begin, end); }
161 #endif
162
163 ~basic_string ()
164 { rep ()->release (); }
165
166 void swap (basic_string &s) { charT *d = dat; dat = s.dat; s.dat = d; }
167
168 basic_string& append (const basic_string& str, size_type pos = 0,
169 size_type n = npos)
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); }
177 #if 0
178 template<class InputIterator>
179 basic_string& append(InputIterator first, InputIterator last);
180 #else
181 basic_string& append(const_iterator first, const_iterator last)
182 { return replace (length (), 0, first, last - first); }
183 #endif
184
185 basic_string& assign (const basic_string& str, size_type pos = 0,
186 size_type n = npos)
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); }
194 #if 0
195 template<class InputIterator>
196 basic_string& assign(InputIterator first, InputIterator last);
197 #else
198 basic_string& assign(const_iterator first, const_iterator last)
199 { return replace (0, npos, first, last - first); }
200 #endif
201
202 basic_string& operator= (const charT* s)
203 { return assign (s); }
204 basic_string& operator= (charT c)
205 { return assign (1, c); }
206
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); }
213
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 (); }
227 #if 0
228 template<class InputIterator>
229 void insert(iterator p, InputIterator first, InputIterator last);
230 #else
231 void insert(iterator p, const_iterator first, const_iterator last)
232 { size_type pos = p - begin(); insert (pos, first, last - first); }
233 #endif
234
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);}
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 - 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); }
259 #if 0
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 { return replace (i1, i2, j1, j2 - j1); }
267 #endif
268
269 private:
270 static charT eos () { return traits::eos (); }
271 void unique () { if (rep ()->ref > 1) alloc (capacity (), true); }
272 void selfish () { unique (); rep ()->selfish = true; }
273
274 public:
275 charT operator[] (size_type pos) const
276 {
277 if (pos == length ())
278 return eos ();
279 return data ()[pos];
280 }
281
282 reference operator[] (size_type pos)
283 { unique (); return (*rep ())[pos]; }
284
285 reference at (size_type pos)
286 {
287 OUTOFRANGE (pos >= length ());
288 return (*this)[pos];
289 }
290 const_reference at (size_type pos) const
291 {
292 OUTOFRANGE (pos >= length ());
293 return data ()[pos];
294 }
295
296 private:
297 void terminate () const
298 { traits::assign ((*rep ())[length ()], eos ()); }
299
300 public:
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) { }
307
308 size_type copy (charT* s, size_type n, size_type pos = 0);
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 const_iterator begin () const { return &(*rep ())[0]; }
366 const_iterator end () const { return &(*rep ())[length ()]; }
367
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 ()); }
374
375 private:
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;
379
380 static Rep nilRep;
381 charT *dat;
382 };
383
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)
388 {
389 basic_string <charT, traits> str (lhs);
390 str.append (rhs);
391 return str;
392 }
393
394 template <class charT, class traits>
395 inline basic_string <charT, traits>
396 operator+ (const charT* lhs, const basic_string <charT, traits>& rhs)
397 {
398 basic_string <charT, traits> str (lhs);
399 str.append (rhs);
400 return str;
401 }
402
403 template <class charT, class traits>
404 inline basic_string <charT, traits>
405 operator+ (charT lhs, const basic_string <charT, traits>& rhs)
406 {
407 basic_string <charT, traits> str (1, lhs);
408 str.append (rhs);
409 return str;
410 }
411
412 template <class charT, class traits>
413 inline basic_string <charT, traits>
414 operator+ (const basic_string <charT, traits>& lhs, const charT* rhs)
415 {
416 basic_string <charT, traits> str (lhs);
417 str.append (rhs);
418 return str;
419 }
420
421 template <class charT, class traits>
422 inline basic_string <charT, traits>
423 operator+ (const basic_string <charT, traits>& lhs, charT rhs)
424 {
425 basic_string <charT, traits> str (lhs);
426 str.append (1, rhs);
427 return str;
428 }
429
430 template <class charT, class traits>
431 inline bool
432 operator== (const basic_string <charT, traits>& lhs,
433 const basic_string <charT, traits>& rhs)
434 {
435 return (lhs.compare (rhs) == 0);
436 }
437
438 template <class charT, class traits>
439 inline bool
440 operator== (const charT* lhs, const basic_string <charT, traits>& rhs)
441 {
442 return (rhs.compare (lhs) == 0);
443 }
444
445 template <class charT, class traits>
446 inline bool
447 operator== (const basic_string <charT, traits>& lhs, const charT* rhs)
448 {
449 return (lhs.compare (rhs) == 0);
450 }
451
452 template <class charT, class traits>
453 inline bool
454 operator!= (const charT* lhs, const basic_string <charT, traits>& rhs)
455 {
456 return (rhs.compare (lhs) != 0);
457 }
458
459 template <class charT, class traits>
460 inline bool
461 operator!= (const basic_string <charT, traits>& lhs, const charT* rhs)
462 {
463 return (lhs.compare (rhs) != 0);
464 }
465
466 template <class charT, class traits>
467 inline bool
468 operator< (const basic_string <charT, traits>& lhs,
469 const basic_string <charT, traits>& rhs)
470 {
471 return (lhs.compare (rhs) < 0);
472 }
473
474 template <class charT, class traits>
475 inline bool
476 operator< (const charT* lhs, const basic_string <charT, traits>& rhs)
477 {
478 return (rhs.compare (lhs) > 0);
479 }
480
481 template <class charT, class traits>
482 inline bool
483 operator< (const basic_string <charT, traits>& lhs, const charT* rhs)
484 {
485 return (lhs.compare (rhs) < 0);
486 }
487
488 template <class charT, class traits>
489 inline bool
490 operator> (const charT* lhs, const basic_string <charT, traits>& rhs)
491 {
492 return (rhs.compare (lhs) < 0);
493 }
494
495 template <class charT, class traits>
496 inline bool
497 operator> (const basic_string <charT, traits>& lhs, const charT* rhs)
498 {
499 return (lhs.compare (rhs) > 0);
500 }
501
502 template <class charT, class traits>
503 inline bool
504 operator<= (const charT* lhs, const basic_string <charT, traits>& rhs)
505 {
506 return (rhs.compare (lhs) >= 0);
507 }
508
509 template <class charT, class traits>
510 inline bool
511 operator<= (const basic_string <charT, traits>& lhs, const charT* rhs)
512 {
513 return (lhs.compare (rhs) <= 0);
514 }
515
516 template <class charT, class traits>
517 inline bool
518 operator>= (const charT* lhs, const basic_string <charT, traits>& rhs)
519 {
520 return (rhs.compare (lhs) <= 0);
521 }
522
523 template <class charT, class traits>
524 inline bool
525 operator>= (const basic_string <charT, traits>& lhs, const charT* rhs)
526 {
527 return (lhs.compare (rhs) >= 0);
528 }
529
530 template <class charT, class traits>
531 inline bool
532 operator!= (const basic_string <charT, traits>& lhs,
533 const basic_string <charT, traits>& rhs)
534 {
535 return (lhs.compare (rhs) != 0);
536 }
537
538 template <class charT, class traits>
539 inline bool
540 operator> (const basic_string <charT, traits>& lhs,
541 const basic_string <charT, traits>& rhs)
542 {
543 return (lhs.compare (rhs) > 0);
544 }
545
546 template <class charT, class traits>
547 inline bool
548 operator<= (const basic_string <charT, traits>& lhs,
549 const basic_string <charT, traits>& rhs)
550 {
551 return (lhs.compare (rhs) <= 0);
552 }
553
554 template <class charT, class traits>
555 inline bool
556 operator>= (const basic_string <charT, traits>& lhs,
557 const basic_string <charT, traits>& rhs)
558 {
559 return (lhs.compare (rhs) >= 0);
560 }
561
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');
569
570 } // extern "C++"
571
572 #endif