* decl2.c (start_static_storage_duration_function): Push the
[official-gcc.git] / libstdc++ / std / bastring.h
blob70891262e9650192dc4322954bfbac80a73ee1ed
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)
8 // any later version.
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.
28 #ifndef __BASTRING__
29 #define __BASTRING__
31 #ifdef __GNUG__
32 #pragma interface
33 #endif
35 #include <cstddef>
36 #include <std/straits.h>
38 // NOTE : This does NOT conform to the draft standard and is likely to change
39 #include <alloc.h>
41 extern "C++" {
42 class istream; class ostream;
44 #include <iterator>
46 #ifdef __STL_USE_EXCEPTIONS
48 extern void __out_of_range (const char *);
49 extern void __length_error (const char *);
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)
56 #else
58 #include <cassert>
59 #define OUTOFRANGE(cond) assert (!(cond))
60 #define LENGTHERROR(cond) assert (!(cond))
62 #endif
64 template <class charT, class traits = string_char_traits<charT>,
65 class Allocator = alloc >
66 class basic_string
68 private:
69 struct Rep {
70 size_t len, res, ref;
71 bool selfish;
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 #if defined __i486__ || defined __i586__ || defined __i686__
77 void release ()
79 size_t __val;
80 // This opcode exists as a .byte instead of as a mnemonic for the
81 // benefit of SCO OpenServer 5. The system assembler (which is
82 // essentially required on this target) can't assemble xaddl in
83 //COFF mode.
84 asm (".byte 0xf0, 0x0f, 0xc1, 0x02" // lock; xaddl %eax, (%edx)
85 : "=a" (__val)
86 : "0" (-1), "m" (ref), "d" (&ref)
87 : "memory");
89 if (__val == 1)
90 delete this;
92 #elif defined __sparcv9__
93 void release ()
95 size_t __newval, __oldval = ref;
98 __newval = __oldval - 1;
99 __asm__ ("cas [%4], %2, %0"
100 : "=r" (__oldval), "=m" (ref)
101 : "r" (__oldval), "m" (ref), "r"(&(ref)), "0" (__newval));
103 while (__newval != __oldval);
105 if (__oldval == 0)
106 delete this;
108 #else
109 void release () { if (--ref == 0) delete this; }
110 #endif
112 inline static void * operator new (size_t, size_t);
113 inline static void operator delete (void *);
114 inline static Rep* create (size_t);
115 charT* clone ();
117 inline void copy (size_t, const charT *, size_t);
118 inline void move (size_t, const charT *, size_t);
119 inline void set (size_t, const charT, size_t);
121 inline static bool excess_slop (size_t, size_t);
122 inline static size_t frob_size (size_t);
124 private:
125 Rep &operator= (const Rep &);
128 public:
129 // types:
130 typedef traits traits_type;
131 typedef typename traits::char_type value_type;
132 typedef Allocator allocator_type;
134 typedef size_t size_type;
135 typedef ptrdiff_t difference_type;
136 typedef charT& reference;
137 typedef const charT& const_reference;
138 typedef charT* pointer;
139 typedef const charT* const_pointer;
140 typedef pointer iterator;
141 typedef const_pointer const_iterator;
142 typedef ::reverse_iterator<iterator> reverse_iterator;
143 typedef ::reverse_iterator<const_iterator> const_reverse_iterator;
144 static const size_type npos = static_cast<size_type>(-1);
146 private:
147 Rep *rep () const { return reinterpret_cast<Rep *>(dat) - 1; }
148 void repup (Rep *p) { rep ()->release (); dat = p->data (); }
150 public:
151 const charT* data () const
152 { return rep ()->data(); }
153 size_type length () const
154 { return rep ()->len; }
155 size_type size () const
156 { return rep ()->len; }
157 size_type capacity () const
158 { return rep ()->res; }
159 size_type max_size () const
160 { return (npos - 1)/sizeof (charT); } // XXX
161 bool empty () const
162 { return size () == 0; }
164 // _lib.string.cons_ construct/copy/destroy:
165 basic_string& operator= (const basic_string& str)
167 if (&str != this) { rep ()->release (); dat = str.rep ()->grab (); }
168 return *this;
171 explicit basic_string (): dat (nilRep.grab ()) { }
172 basic_string (const basic_string& str): dat (str.rep ()->grab ()) { }
173 basic_string (const basic_string& str, size_type pos, size_type n = npos)
174 : dat (nilRep.grab ()) { assign (str, pos, n); }
175 basic_string (const charT* s, size_type n)
176 : dat (nilRep.grab ()) { assign (s, n); }
177 basic_string (const charT* s)
178 : dat (nilRep.grab ()) { assign (s); }
179 basic_string (size_type n, charT c)
180 : dat (nilRep.grab ()) { assign (n, c); }
181 #ifdef __STL_MEMBER_TEMPLATES
182 template<class InputIterator>
183 basic_string(InputIterator begin, InputIterator end)
184 #else
185 basic_string(const_iterator begin, const_iterator end)
186 #endif
187 : dat (nilRep.grab ()) { assign (begin, end); }
189 ~basic_string ()
190 { rep ()->release (); }
192 void swap (basic_string &s) { charT *d = dat; dat = s.dat; s.dat = d; }
194 basic_string& append (const basic_string& str, size_type pos = 0,
195 size_type n = npos)
196 { return replace (length (), 0, str, pos, n); }
197 basic_string& append (const charT* s, size_type n)
198 { return replace (length (), 0, s, n); }
199 basic_string& append (const charT* s)
200 { return append (s, traits::length (s)); }
201 basic_string& append (size_type n, charT c)
202 { return replace (length (), 0, n, c); }
203 #ifdef __STL_MEMBER_TEMPLATES
204 template<class InputIterator>
205 basic_string& append(InputIterator first, InputIterator last)
206 #else
207 basic_string& append(const_iterator first, const_iterator last)
208 #endif
209 { return replace (iend (), iend (), first, last); }
211 basic_string& assign (const basic_string& str, size_type pos = 0,
212 size_type n = npos)
213 { return replace (0, npos, str, pos, n); }
214 basic_string& assign (const charT* s, size_type n)
215 { return replace (0, npos, s, n); }
216 basic_string& assign (const charT* s)
217 { return assign (s, traits::length (s)); }
218 basic_string& assign (size_type n, charT c)
219 { return replace (0, npos, n, c); }
220 #ifdef __STL_MEMBER_TEMPLATES
221 template<class InputIterator>
222 basic_string& assign(InputIterator first, InputIterator last)
223 #else
224 basic_string& assign(const_iterator first, const_iterator last)
225 #endif
226 { return replace (ibegin (), iend (), first, last); }
228 basic_string& operator= (const charT* s)
229 { return assign (s); }
230 basic_string& operator= (charT c)
231 { return assign (1, c); }
233 basic_string& operator+= (const basic_string& rhs)
234 { return append (rhs); }
235 basic_string& operator+= (const charT* s)
236 { return append (s); }
237 basic_string& operator+= (charT c)
238 { return append (1, c); }
240 basic_string& insert (size_type pos1, const basic_string& str,
241 size_type pos2 = 0, size_type n = npos)
242 { return replace (pos1, 0, str, pos2, n); }
243 basic_string& insert (size_type pos, const charT* s, size_type n)
244 { return replace (pos, 0, s, n); }
245 basic_string& insert (size_type pos, const charT* s)
246 { return insert (pos, s, traits::length (s)); }
247 basic_string& insert (size_type pos, size_type n, charT c)
248 { return replace (pos, 0, n, c); }
249 iterator insert(iterator p, charT c)
250 { size_type __o = p - ibegin ();
251 insert (p - ibegin (), 1, c); selfish ();
252 return ibegin () + __o; }
253 iterator insert(iterator p, size_type n, charT c)
254 { size_type __o = p - ibegin ();
255 insert (p - ibegin (), n, c); selfish ();
256 return ibegin () + __o; }
257 #ifdef __STL_MEMBER_TEMPLATES
258 template<class InputIterator>
259 void insert(iterator p, InputIterator first, InputIterator last)
260 #else
261 void insert(iterator p, const_iterator first, const_iterator last)
262 #endif
263 { replace (p, p, first, last); }
265 basic_string& erase (size_type pos = 0, size_type n = npos)
266 { return replace (pos, n, (size_type)0, (charT)0); }
267 iterator erase(iterator p)
268 { size_type __o = p - begin();
269 replace (__o, 1, (size_type)0, (charT)0); selfish ();
270 return ibegin() + __o; }
271 iterator erase(iterator f, iterator l)
272 { size_type __o = f - ibegin();
273 replace (__o, l-f, (size_type)0, (charT)0);selfish ();
274 return ibegin() + __o; }
276 basic_string& replace (size_type pos1, size_type n1, const basic_string& str,
277 size_type pos2 = 0, size_type n2 = npos);
278 basic_string& replace (size_type pos, size_type n1, const charT* s,
279 size_type n2);
280 basic_string& replace (size_type pos, size_type n1, const charT* s)
281 { return replace (pos, n1, s, traits::length (s)); }
282 basic_string& replace (size_type pos, size_type n1, size_type n2, charT c);
283 basic_string& replace (size_type pos, size_type n, charT c)
284 { return replace (pos, n, 1, c); }
285 basic_string& replace (iterator i1, iterator i2, const basic_string& str)
286 { return replace (i1 - ibegin (), i2 - i1, str); }
287 basic_string& replace (iterator i1, iterator i2, const charT* s, size_type n)
288 { return replace (i1 - ibegin (), i2 - i1, s, n); }
289 basic_string& replace (iterator i1, iterator i2, const charT* s)
290 { return replace (i1 - ibegin (), i2 - i1, s); }
291 basic_string& replace (iterator i1, iterator i2, size_type n, charT c)
292 { return replace (i1 - ibegin (), i2 - i1, n, c); }
293 #ifdef __STL_MEMBER_TEMPLATES
294 template<class InputIterator>
295 basic_string& replace(iterator i1, iterator i2,
296 InputIterator j1, InputIterator j2);
297 #else
298 basic_string& replace(iterator i1, iterator i2,
299 const_iterator j1, const_iterator j2);
300 #endif
302 private:
303 static charT eos () { return traits::eos (); }
304 void unique () { if (rep ()->ref > 1) alloc (length (), true); }
305 void selfish () { unique (); rep ()->selfish = true; }
307 public:
308 charT operator[] (size_type pos) const
310 if (pos == length ())
311 return eos ();
312 return data ()[pos];
315 reference operator[] (size_type pos)
316 { selfish (); return (*rep ())[pos]; }
318 reference at (size_type pos)
320 OUTOFRANGE (pos >= length ());
321 return (*this)[pos];
323 const_reference at (size_type pos) const
325 OUTOFRANGE (pos >= length ());
326 return data ()[pos];
329 private:
330 void terminate () const
331 { traits::assign ((*rep ())[length ()], eos ()); }
333 public:
334 const charT* c_str () const
335 { if (length () == 0) return ""; terminate (); return data (); }
336 void resize (size_type n, charT c);
337 void resize (size_type n)
338 { resize (n, eos ()); }
339 void reserve (size_type) { }
341 size_type copy (charT* s, size_type n, size_type pos = 0) const;
343 size_type find (const basic_string& str, size_type pos = 0) const
344 { return find (str.data(), pos, str.length()); }
345 size_type find (const charT* s, size_type pos, size_type n) const;
346 size_type find (const charT* s, size_type pos = 0) const
347 { return find (s, pos, traits::length (s)); }
348 size_type find (charT c, size_type pos = 0) const;
350 size_type rfind (const basic_string& str, size_type pos = npos) const
351 { return rfind (str.data(), pos, str.length()); }
352 size_type rfind (const charT* s, size_type pos, size_type n) const;
353 size_type rfind (const charT* s, size_type pos = npos) const
354 { return rfind (s, pos, traits::length (s)); }
355 size_type rfind (charT c, size_type pos = npos) const;
357 size_type find_first_of (const basic_string& str, size_type pos = 0) const
358 { return find_first_of (str.data(), pos, str.length()); }
359 size_type find_first_of (const charT* s, size_type pos, size_type n) const;
360 size_type find_first_of (const charT* s, size_type pos = 0) const
361 { return find_first_of (s, pos, traits::length (s)); }
362 size_type find_first_of (charT c, size_type pos = 0) const
363 { return find (c, pos); }
365 size_type find_last_of (const basic_string& str, size_type pos = npos) const
366 { return find_last_of (str.data(), pos, str.length()); }
367 size_type find_last_of (const charT* s, size_type pos, size_type n) const;
368 size_type find_last_of (const charT* s, size_type pos = npos) const
369 { return find_last_of (s, pos, traits::length (s)); }
370 size_type find_last_of (charT c, size_type pos = npos) const
371 { return rfind (c, pos); }
373 size_type find_first_not_of (const basic_string& str, size_type pos = 0) const
374 { return find_first_not_of (str.data(), pos, str.length()); }
375 size_type find_first_not_of (const charT* s, size_type pos, size_type n) const;
376 size_type find_first_not_of (const charT* s, size_type pos = 0) const
377 { return find_first_not_of (s, pos, traits::length (s)); }
378 size_type find_first_not_of (charT c, size_type pos = 0) const;
380 size_type find_last_not_of (const basic_string& str, size_type pos = npos) const
381 { return find_last_not_of (str.data(), pos, str.length()); }
382 size_type find_last_not_of (const charT* s, size_type pos, size_type n) const;
383 size_type find_last_not_of (const charT* s, size_type pos = npos) const
384 { return find_last_not_of (s, pos, traits::length (s)); }
385 size_type find_last_not_of (charT c, size_type pos = npos) const;
387 basic_string substr (size_type pos = 0, size_type n = npos) const
388 { return basic_string (*this, pos, n); }
390 int compare (const basic_string& str, size_type pos = 0, size_type n = npos) const;
391 // There is no 'strncmp' equivalent for charT pointers.
392 int compare (const charT* s, size_type pos, size_type n) const;
393 int compare (const charT* s, size_type pos = 0) const
394 { return compare (s, pos, traits::length (s)); }
396 iterator begin () { selfish (); return &(*this)[0]; }
397 iterator end () { selfish (); return &(*this)[length ()]; }
399 private:
400 iterator ibegin () const { return &(*rep ())[0]; }
401 iterator iend () const { return &(*rep ())[length ()]; }
403 public:
404 const_iterator begin () const { return ibegin (); }
405 const_iterator end () const { return iend (); }
407 reverse_iterator rbegin() { return reverse_iterator (end ()); }
408 const_reverse_iterator rbegin() const
409 { return const_reverse_iterator (end ()); }
410 reverse_iterator rend() { return reverse_iterator (begin ()); }
411 const_reverse_iterator rend() const
412 { return const_reverse_iterator (begin ()); }
414 private:
415 void alloc (size_type size, bool save);
416 static size_type _find (const charT* ptr, charT c, size_type xpos, size_type len);
417 inline bool check_realloc (size_type s) const;
419 static Rep nilRep;
420 charT *dat;
423 #ifdef __STL_MEMBER_TEMPLATES
424 template <class charT, class traits, class Allocator> template <class InputIterator>
425 basic_string <charT, traits, Allocator>& basic_string <charT, traits, Allocator>::
426 replace (iterator i1, iterator i2, InputIterator j1, InputIterator j2)
427 #else
428 template <class charT, class traits, class Allocator>
429 basic_string <charT, traits, Allocator>& basic_string <charT, traits, Allocator>::
430 replace (iterator i1, iterator i2, const_iterator j1, const_iterator j2)
431 #endif
433 const size_type len = length ();
434 size_type pos = i1 - ibegin ();
435 size_type n1 = i2 - i1;
436 size_type n2 = j2 - j1;
438 OUTOFRANGE (pos > len);
439 if (n1 > len - pos)
440 n1 = len - pos;
441 LENGTHERROR (len - n1 > max_size () - n2);
442 size_t newlen = len - n1 + n2;
444 if (check_realloc (newlen))
446 Rep *p = Rep::create (newlen);
447 p->copy (0, data (), pos);
448 p->copy (pos + n2, data () + pos + n1, len - (pos + n1));
449 for (; j1 != j2; ++j1, ++pos)
450 traits::assign ((*p)[pos], *j1);
451 repup (p);
453 else
455 rep ()->move (pos + n2, data () + pos + n1, len - (pos + n1));
456 for (; j1 != j2; ++j1, ++pos)
457 traits::assign ((*rep ())[pos], *j1);
459 rep ()->len = newlen;
461 return *this;
464 template <class charT, class traits, class Allocator>
465 inline basic_string <charT, traits, Allocator>
466 operator+ (const basic_string <charT, traits, Allocator>& lhs,
467 const basic_string <charT, traits, Allocator>& rhs)
469 basic_string <charT, traits, Allocator> str (lhs);
470 str.append (rhs);
471 return str;
474 template <class charT, class traits, class Allocator>
475 inline basic_string <charT, traits, Allocator>
476 operator+ (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
478 basic_string <charT, traits, Allocator> str (lhs);
479 str.append (rhs);
480 return str;
483 template <class charT, class traits, class Allocator>
484 inline basic_string <charT, traits, Allocator>
485 operator+ (charT lhs, const basic_string <charT, traits, Allocator>& rhs)
487 basic_string <charT, traits, Allocator> str (1, lhs);
488 str.append (rhs);
489 return str;
492 template <class charT, class traits, class Allocator>
493 inline basic_string <charT, traits, Allocator>
494 operator+ (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
496 basic_string <charT, traits, Allocator> str (lhs);
497 str.append (rhs);
498 return str;
501 template <class charT, class traits, class Allocator>
502 inline basic_string <charT, traits, Allocator>
503 operator+ (const basic_string <charT, traits, Allocator>& lhs, charT rhs)
505 basic_string <charT, traits, Allocator> str (lhs);
506 str.append (1, rhs);
507 return str;
510 template <class charT, class traits, class Allocator>
511 inline bool
512 operator== (const basic_string <charT, traits, Allocator>& lhs,
513 const basic_string <charT, traits, Allocator>& rhs)
515 return (lhs.compare (rhs) == 0);
518 template <class charT, class traits, class Allocator>
519 inline bool
520 operator== (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
522 return (rhs.compare (lhs) == 0);
525 template <class charT, class traits, class Allocator>
526 inline bool
527 operator== (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
529 return (lhs.compare (rhs) == 0);
532 template <class charT, class traits, class Allocator>
533 inline bool
534 operator!= (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
536 return (rhs.compare (lhs) != 0);
539 template <class charT, class traits, class Allocator>
540 inline bool
541 operator!= (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
543 return (lhs.compare (rhs) != 0);
546 template <class charT, class traits, class Allocator>
547 inline bool
548 operator< (const basic_string <charT, traits, Allocator>& lhs,
549 const basic_string <charT, traits, Allocator>& rhs)
551 return (lhs.compare (rhs) < 0);
554 template <class charT, class traits, class Allocator>
555 inline bool
556 operator< (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
558 return (rhs.compare (lhs) > 0);
561 template <class charT, class traits, class Allocator>
562 inline bool
563 operator< (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
565 return (lhs.compare (rhs) < 0);
568 template <class charT, class traits, class Allocator>
569 inline bool
570 operator> (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
572 return (rhs.compare (lhs) < 0);
575 template <class charT, class traits, class Allocator>
576 inline bool
577 operator> (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
579 return (lhs.compare (rhs) > 0);
582 template <class charT, class traits, class Allocator>
583 inline bool
584 operator<= (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
586 return (rhs.compare (lhs) >= 0);
589 template <class charT, class traits, class Allocator>
590 inline bool
591 operator<= (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
593 return (lhs.compare (rhs) <= 0);
596 template <class charT, class traits, class Allocator>
597 inline bool
598 operator>= (const charT* lhs, const basic_string <charT, traits, Allocator>& rhs)
600 return (rhs.compare (lhs) <= 0);
603 template <class charT, class traits, class Allocator>
604 inline bool
605 operator>= (const basic_string <charT, traits, Allocator>& lhs, const charT* rhs)
607 return (lhs.compare (rhs) >= 0);
610 template <class charT, class traits, class Allocator>
611 inline bool
612 operator!= (const basic_string <charT, traits, Allocator>& lhs,
613 const basic_string <charT, traits, Allocator>& rhs)
615 return (lhs.compare (rhs) != 0);
618 template <class charT, class traits, class Allocator>
619 inline bool
620 operator> (const basic_string <charT, traits, Allocator>& lhs,
621 const basic_string <charT, traits, Allocator>& rhs)
623 return (lhs.compare (rhs) > 0);
626 template <class charT, class traits, class Allocator>
627 inline bool
628 operator<= (const basic_string <charT, traits, Allocator>& lhs,
629 const basic_string <charT, traits, Allocator>& rhs)
631 return (lhs.compare (rhs) <= 0);
634 template <class charT, class traits, class Allocator>
635 inline bool
636 operator>= (const basic_string <charT, traits, Allocator>& lhs,
637 const basic_string <charT, traits, Allocator>& rhs)
639 return (lhs.compare (rhs) >= 0);
642 class istream; class ostream;
643 template <class charT, class traits, class Allocator> istream&
644 operator>> (istream&, basic_string <charT, traits, Allocator>&);
645 template <class charT, class traits, class Allocator> ostream&
646 operator<< (ostream&, const basic_string <charT, traits, Allocator>&);
647 template <class charT, class traits, class Allocator> istream&
648 getline (istream&, basic_string <charT, traits, Allocator>&, charT delim = '\n');
650 } // extern "C++"
652 #include <std/bastring.cc>
654 #endif