1 // Main templates for the -*- C++ -*- string classes.
2 // Copyright (C) 1994, 1995, 1999 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>
38 // NOTE : This does NOT conform to the draft standard and is likely to change
42 class istream
; class ostream
;
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)
59 #define OUTOFRANGE(cond) assert (!(cond))
60 #define LENGTHERROR(cond) assert (!(cond))
64 template <class charT
, class traits
= string_char_traits
<charT
>,
65 class Allocator
= alloc
>
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__
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
84 asm (".byte 0xf0, 0x0f, 0xc1, 0x02" // lock; xaddl %eax, (%edx)
86 : "0" (-1), "m" (ref
), "d" (&ref
)
92 #elif defined __sparc_v9__
95 size_t __newval
, __oldval
= ref
;
98 __newval
= __oldval
- 1;
105 : "=r" (__oldval
), "=m" (ref
)
106 : "r" (__oldval
), "m" (ref
), "r"(&(ref
)), "0" (__newval
));
108 while (__newval
!= __oldval
);
114 void release () { if (--ref
== 0) delete this; }
117 inline static void * operator new (size_t, size_t);
118 inline static void operator delete (void *);
119 inline static Rep
* create (size_t);
122 inline void copy (size_t, const charT
*, size_t);
123 inline void move (size_t, const charT
*, size_t);
124 inline void set (size_t, const charT
, size_t);
126 inline static bool excess_slop (size_t, size_t);
127 inline static size_t frob_size (size_t);
130 Rep
&operator= (const Rep
&);
135 typedef traits traits_type
;
136 typedef typename
traits::char_type value_type
;
137 typedef Allocator allocator_type
;
139 typedef size_t size_type
;
140 typedef ptrdiff_t difference_type
;
141 typedef charT
& reference
;
142 typedef const charT
& const_reference
;
143 typedef charT
* pointer
;
144 typedef const charT
* const_pointer
;
145 typedef pointer iterator
;
146 typedef const_pointer const_iterator
;
147 typedef ::reverse_iterator
<iterator
> reverse_iterator
;
148 typedef ::reverse_iterator
<const_iterator
> const_reverse_iterator
;
149 static const size_type npos
= static_cast<size_type
>(-1);
152 Rep
*rep () const { return reinterpret_cast<Rep
*>(dat
) - 1; }
153 void repup (Rep
*p
) { rep ()->release (); dat
= p
->data (); }
156 const charT
* data () const
157 { return rep ()->data(); }
158 size_type
length () const
159 { return rep ()->len
; }
160 size_type
size () const
161 { return rep ()->len
; }
162 size_type
capacity () const
163 { return rep ()->res
; }
164 size_type
max_size () const
165 { return (npos
- 1)/sizeof (charT
); } // XXX
167 { return size () == 0; }
169 // _lib.string.cons_ construct/copy/destroy:
170 basic_string
& operator= (const basic_string
& str
)
172 if (&str
!= this) { rep ()->release (); dat
= str
.rep ()->grab (); }
176 explicit basic_string (): dat (nilRep
.grab ()) { }
177 basic_string (const basic_string
& str
): dat (str
.rep ()->grab ()) { }
178 basic_string (const basic_string
& str
, size_type pos
, size_type n
= npos
)
179 : dat (nilRep
.grab ()) { assign (str
, pos
, n
); }
180 basic_string (const charT
* s
, size_type n
)
181 : dat (nilRep
.grab ()) { assign (s
, n
); }
182 basic_string (const charT
* s
)
183 : dat (nilRep
.grab ()) { assign (s
); }
184 basic_string (size_type n
, charT c
)
185 : dat (nilRep
.grab ()) { assign (n
, c
); }
186 #ifdef __STL_MEMBER_TEMPLATES
187 template<class InputIterator
>
188 basic_string(InputIterator __begin
, InputIterator __end
)
190 basic_string(const_iterator __begin
, const_iterator __end
)
192 : dat (nilRep
.grab ()) { assign (__begin
, __end
); }
195 { rep ()->release (); }
197 void swap (basic_string
&s
) { charT
*d
= dat
; dat
= s
.dat
; s
.dat
= d
; }
199 basic_string
& append (const basic_string
& str
, size_type pos
= 0,
201 { return replace (length (), 0, str
, pos
, n
); }
202 basic_string
& append (const charT
* s
, size_type n
)
203 { return replace (length (), 0, s
, n
); }
204 basic_string
& append (const charT
* s
)
205 { return append (s
, traits::length (s
)); }
206 basic_string
& append (size_type n
, charT c
)
207 { return replace (length (), 0, n
, c
); }
208 #ifdef __STL_MEMBER_TEMPLATES
209 template<class InputIterator
>
210 basic_string
& append(InputIterator first
, InputIterator last
)
212 basic_string
& append(const_iterator first
, const_iterator last
)
214 { return replace (iend (), iend (), first
, last
); }
216 void push_back(charT __c
)
219 basic_string
& assign (const basic_string
& str
, size_type pos
= 0,
221 { return replace (0, npos
, str
, pos
, n
); }
222 basic_string
& assign (const charT
* s
, size_type n
)
223 { return replace (0, npos
, s
, n
); }
224 basic_string
& assign (const charT
* s
)
225 { return assign (s
, traits::length (s
)); }
226 basic_string
& assign (size_type n
, charT c
)
227 { return replace (0, npos
, n
, c
); }
228 #ifdef __STL_MEMBER_TEMPLATES
229 template<class InputIterator
>
230 basic_string
& assign(InputIterator first
, InputIterator last
)
232 basic_string
& assign(const_iterator first
, const_iterator last
)
234 { return replace (ibegin (), iend (), first
, last
); }
236 basic_string
& operator= (const charT
* s
)
237 { return assign (s
); }
238 basic_string
& operator= (charT c
)
239 { return assign (1, c
); }
241 basic_string
& operator+= (const basic_string
& rhs
)
242 { return append (rhs
); }
243 basic_string
& operator+= (const charT
* s
)
244 { return append (s
); }
245 basic_string
& operator+= (charT c
)
246 { return append (1, c
); }
248 basic_string
& insert (size_type pos1
, const basic_string
& str
,
249 size_type pos2
= 0, size_type n
= npos
)
250 { return replace (pos1
, 0, str
, pos2
, n
); }
251 basic_string
& insert (size_type pos
, const charT
* s
, size_type n
)
252 { return replace (pos
, 0, s
, n
); }
253 basic_string
& insert (size_type pos
, const charT
* s
)
254 { return insert (pos
, s
, traits::length (s
)); }
255 basic_string
& insert (size_type pos
, size_type n
, charT c
)
256 { return replace (pos
, 0, n
, c
); }
257 iterator
insert(iterator p
, charT c
)
258 { size_type __o
= p
- ibegin ();
259 insert (p
- ibegin (), 1, c
); selfish ();
260 return ibegin () + __o
; }
261 iterator
insert(iterator p
, size_type n
, charT c
)
262 { size_type __o
= p
- ibegin ();
263 insert (p
- ibegin (), n
, c
); selfish ();
264 return ibegin () + __o
; }
265 #ifdef __STL_MEMBER_TEMPLATES
266 template<class InputIterator
>
267 void insert(iterator p
, InputIterator first
, InputIterator last
)
269 void insert(iterator p
, const_iterator first
, const_iterator last
)
271 { replace (p
, p
, first
, last
); }
273 basic_string
& erase (size_type pos
= 0, size_type n
= npos
)
274 { return replace (pos
, n
, (size_type
)0, (charT
)0); }
275 iterator
erase(iterator p
)
276 { size_type __o
= p
- begin();
277 replace (__o
, 1, (size_type
)0, (charT
)0); selfish ();
278 return ibegin() + __o
; }
279 iterator
erase(iterator f
, iterator l
)
280 { size_type __o
= f
- ibegin();
281 replace (__o
, l
-f
, (size_type
)0, (charT
)0);selfish ();
282 return ibegin() + __o
; }
284 basic_string
& replace (size_type pos1
, size_type n1
, const basic_string
& str
,
285 size_type pos2
= 0, size_type n2
= npos
);
286 basic_string
& replace (size_type pos
, size_type n1
, const charT
* s
,
288 basic_string
& replace (size_type pos
, size_type n1
, const charT
* s
)
289 { return replace (pos
, n1
, s
, traits::length (s
)); }
290 basic_string
& replace (size_type pos
, size_type n1
, size_type n2
, charT c
);
291 basic_string
& replace (size_type pos
, size_type n
, charT c
)
292 { return replace (pos
, n
, 1, c
); }
293 basic_string
& replace (iterator i1
, iterator i2
, const basic_string
& str
)
294 { return replace (i1
- ibegin (), i2
- i1
, str
); }
295 basic_string
& replace (iterator i1
, iterator i2
, const charT
* s
, size_type n
)
296 { return replace (i1
- ibegin (), i2
- i1
, s
, n
); }
297 basic_string
& replace (iterator i1
, iterator i2
, const charT
* s
)
298 { return replace (i1
- ibegin (), i2
- i1
, s
); }
299 basic_string
& replace (iterator i1
, iterator i2
, size_type n
, charT c
)
300 { return replace (i1
- ibegin (), i2
- i1
, n
, c
); }
301 #ifdef __STL_MEMBER_TEMPLATES
302 template<class InputIterator
>
303 basic_string
& replace(iterator i1
, iterator i2
,
304 InputIterator j1
, InputIterator j2
);
306 basic_string
& replace(iterator i1
, iterator i2
,
307 const_iterator j1
, const_iterator j2
);
311 static charT
eos () { return traits::eos (); }
312 void unique () { if (rep ()->ref
> 1) alloc (length (), true); }
313 void selfish () { unique (); rep ()->selfish
= true; }
316 charT
operator[] (size_type pos
) const
318 if (pos
== length ())
323 reference
operator[] (size_type pos
)
324 { selfish (); return (*rep ())[pos
]; }
326 reference
at (size_type pos
)
328 OUTOFRANGE (pos
>= length ());
331 const_reference
at (size_type pos
) const
333 OUTOFRANGE (pos
>= length ());
338 void terminate () const
339 { traits::assign ((*rep ())[length ()], eos ()); }
342 const charT
* c_str () const
343 { if (length () == 0) return ""; terminate (); return data (); }
344 void resize (size_type n
, charT c
);
345 void resize (size_type n
)
346 { resize (n
, eos ()); }
347 void reserve (size_type
) { }
349 void clear() { erase(begin(), end()); }
351 size_type
copy (charT
* s
, size_type n
, size_type pos
= 0) const;
353 size_type
find (const basic_string
& str
, size_type pos
= 0) const
354 { return find (str
.data(), pos
, str
.length()); }
355 size_type
find (const charT
* s
, size_type pos
, size_type n
) const;
356 size_type
find (const charT
* s
, size_type pos
= 0) const
357 { return find (s
, pos
, traits::length (s
)); }
358 size_type
find (charT c
, size_type pos
= 0) const;
360 size_type
rfind (const basic_string
& str
, size_type pos
= npos
) const
361 { return rfind (str
.data(), pos
, str
.length()); }
362 size_type
rfind (const charT
* s
, size_type pos
, size_type n
) const;
363 size_type
rfind (const charT
* s
, size_type pos
= npos
) const
364 { return rfind (s
, pos
, traits::length (s
)); }
365 size_type
rfind (charT c
, size_type pos
= npos
) const;
367 size_type
find_first_of (const basic_string
& str
, size_type pos
= 0) const
368 { return find_first_of (str
.data(), pos
, str
.length()); }
369 size_type
find_first_of (const charT
* s
, size_type pos
, size_type n
) const;
370 size_type
find_first_of (const charT
* s
, size_type pos
= 0) const
371 { return find_first_of (s
, pos
, traits::length (s
)); }
372 size_type
find_first_of (charT c
, size_type pos
= 0) const
373 { return find (c
, pos
); }
375 size_type
find_last_of (const basic_string
& str
, size_type pos
= npos
) const
376 { return find_last_of (str
.data(), pos
, str
.length()); }
377 size_type
find_last_of (const charT
* s
, size_type pos
, size_type n
) const;
378 size_type
find_last_of (const charT
* s
, size_type pos
= npos
) const
379 { return find_last_of (s
, pos
, traits::length (s
)); }
380 size_type
find_last_of (charT c
, size_type pos
= npos
) const
381 { return rfind (c
, pos
); }
383 size_type
find_first_not_of (const basic_string
& str
, size_type pos
= 0) const
384 { return find_first_not_of (str
.data(), pos
, str
.length()); }
385 size_type
find_first_not_of (const charT
* s
, size_type pos
, size_type n
) const;
386 size_type
find_first_not_of (const charT
* s
, size_type pos
= 0) const
387 { return find_first_not_of (s
, pos
, traits::length (s
)); }
388 size_type
find_first_not_of (charT c
, size_type pos
= 0) const;
390 size_type
find_last_not_of (const basic_string
& str
, size_type pos
= npos
) const
391 { return find_last_not_of (str
.data(), pos
, str
.length()); }
392 size_type
find_last_not_of (const charT
* s
, size_type pos
, size_type n
) const;
393 size_type
find_last_not_of (const charT
* s
, size_type pos
= npos
) const
394 { return find_last_not_of (s
, pos
, traits::length (s
)); }
395 size_type
find_last_not_of (charT c
, size_type pos
= npos
) const;
397 basic_string
substr (size_type pos
= 0, size_type n
= npos
) const
398 { return basic_string (*this, pos
, n
); }
400 int compare (const basic_string
& str
, size_type pos
= 0, size_type n
= npos
) const;
401 // There is no 'strncmp' equivalent for charT pointers.
402 int compare (const charT
* s
, size_type pos
, size_type n
) const;
403 int compare (const charT
* s
, size_type pos
= 0) const
404 { return compare (s
, pos
, traits::length (s
)); }
406 iterator
begin () { selfish (); return &(*this)[0]; }
407 iterator
end () { selfish (); return &(*this)[length ()]; }
410 iterator
ibegin () const { return &(*rep ())[0]; }
411 iterator
iend () const { return &(*rep ())[length ()]; }
414 const_iterator
begin () const { return ibegin (); }
415 const_iterator
end () const { return iend (); }
417 reverse_iterator
rbegin() { return reverse_iterator (end ()); }
418 const_reverse_iterator
rbegin() const
419 { return const_reverse_iterator (end ()); }
420 reverse_iterator
rend() { return reverse_iterator (begin ()); }
421 const_reverse_iterator
rend() const
422 { return const_reverse_iterator (begin ()); }
425 void alloc (size_type size
, bool save
);
426 static size_type
_find (const charT
* ptr
, charT c
, size_type xpos
, size_type len
);
427 inline bool check_realloc (size_type s
) const;
433 #ifdef __STL_MEMBER_TEMPLATES
434 template <class charT
, class traits
, class Allocator
> template <class InputIterator
>
435 basic_string
<charT
, traits
, Allocator
>& basic_string
<charT
, traits
, Allocator
>::
436 replace (iterator i1
, iterator i2
, InputIterator j1
, InputIterator j2
)
438 template <class charT
, class traits
, class Allocator
>
439 basic_string
<charT
, traits
, Allocator
>& basic_string
<charT
, traits
, Allocator
>::
440 replace (iterator i1
, iterator i2
, const_iterator j1
, const_iterator j2
)
443 const size_type len
= length ();
444 size_type pos
= i1
- ibegin ();
445 size_type n1
= i2
- i1
;
446 size_type n2
= j2
- j1
;
448 OUTOFRANGE (pos
> len
);
451 LENGTHERROR (len
- n1
> max_size () - n2
);
452 size_t newlen
= len
- n1
+ n2
;
454 if (check_realloc (newlen
))
456 Rep
*p
= Rep::create (newlen
);
457 p
->copy (0, data (), pos
);
458 p
->copy (pos
+ n2
, data () + pos
+ n1
, len
- (pos
+ n1
));
459 for (; j1
!= j2
; ++j1
, ++pos
)
460 traits::assign ((*p
)[pos
], *j1
);
465 rep ()->move (pos
+ n2
, data () + pos
+ n1
, len
- (pos
+ n1
));
466 for (; j1
!= j2
; ++j1
, ++pos
)
467 traits::assign ((*rep ())[pos
], *j1
);
469 rep ()->len
= newlen
;
474 template <class charT
, class traits
, class Allocator
>
475 inline basic_string
<charT
, traits
, Allocator
>
476 operator+ (const basic_string
<charT
, traits
, Allocator
>& lhs
,
477 const basic_string
<charT
, traits
, Allocator
>& rhs
)
479 basic_string
<charT
, traits
, Allocator
> str (lhs
);
484 template <class charT
, class traits
, class Allocator
>
485 inline basic_string
<charT
, traits
, Allocator
>
486 operator+ (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
488 basic_string
<charT
, traits
, Allocator
> str (lhs
);
493 template <class charT
, class traits
, class Allocator
>
494 inline basic_string
<charT
, traits
, Allocator
>
495 operator+ (charT lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
497 basic_string
<charT
, traits
, Allocator
> str (1, lhs
);
502 template <class charT
, class traits
, class Allocator
>
503 inline basic_string
<charT
, traits
, Allocator
>
504 operator+ (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
506 basic_string
<charT
, traits
, Allocator
> str (lhs
);
511 template <class charT
, class traits
, class Allocator
>
512 inline basic_string
<charT
, traits
, Allocator
>
513 operator+ (const basic_string
<charT
, traits
, Allocator
>& lhs
, charT rhs
)
515 basic_string
<charT
, traits
, Allocator
> str (lhs
);
520 template <class charT
, class traits
, class Allocator
>
522 operator== (const basic_string
<charT
, traits
, Allocator
>& lhs
,
523 const basic_string
<charT
, traits
, Allocator
>& rhs
)
525 return (lhs
.compare (rhs
) == 0);
528 template <class charT
, class traits
, class Allocator
>
530 operator== (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
532 return (rhs
.compare (lhs
) == 0);
535 template <class charT
, class traits
, class Allocator
>
537 operator== (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
539 return (lhs
.compare (rhs
) == 0);
542 template <class charT
, class traits
, class Allocator
>
544 operator!= (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
546 return (rhs
.compare (lhs
) != 0);
549 template <class charT
, class traits
, class Allocator
>
551 operator!= (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
553 return (lhs
.compare (rhs
) != 0);
556 template <class charT
, class traits
, class Allocator
>
558 operator< (const basic_string
<charT
, traits
, Allocator
>& lhs
,
559 const basic_string
<charT
, traits
, Allocator
>& rhs
)
561 return (lhs
.compare (rhs
) < 0);
564 template <class charT
, class traits
, class Allocator
>
566 operator< (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
568 return (rhs
.compare (lhs
) > 0);
571 template <class charT
, class traits
, class Allocator
>
573 operator< (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
575 return (lhs
.compare (rhs
) < 0);
578 template <class charT
, class traits
, class Allocator
>
580 operator> (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
582 return (rhs
.compare (lhs
) < 0);
585 template <class charT
, class traits
, class Allocator
>
587 operator> (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
589 return (lhs
.compare (rhs
) > 0);
592 template <class charT
, class traits
, class Allocator
>
594 operator<= (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
596 return (rhs
.compare (lhs
) >= 0);
599 template <class charT
, class traits
, class Allocator
>
601 operator<= (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
603 return (lhs
.compare (rhs
) <= 0);
606 template <class charT
, class traits
, class Allocator
>
608 operator>= (const charT
* lhs
, const basic_string
<charT
, traits
, Allocator
>& rhs
)
610 return (rhs
.compare (lhs
) <= 0);
613 template <class charT
, class traits
, class Allocator
>
615 operator>= (const basic_string
<charT
, traits
, Allocator
>& lhs
, const charT
* rhs
)
617 return (lhs
.compare (rhs
) >= 0);
620 template <class charT
, class traits
, class Allocator
>
622 operator!= (const basic_string
<charT
, traits
, Allocator
>& lhs
,
623 const basic_string
<charT
, traits
, Allocator
>& rhs
)
625 return (lhs
.compare (rhs
) != 0);
628 template <class charT
, class traits
, class Allocator
>
630 operator> (const basic_string
<charT
, traits
, Allocator
>& lhs
,
631 const basic_string
<charT
, traits
, Allocator
>& rhs
)
633 return (lhs
.compare (rhs
) > 0);
636 template <class charT
, class traits
, class Allocator
>
638 operator<= (const basic_string
<charT
, traits
, Allocator
>& lhs
,
639 const basic_string
<charT
, traits
, Allocator
>& rhs
)
641 return (lhs
.compare (rhs
) <= 0);
644 template <class charT
, class traits
, class Allocator
>
646 operator>= (const basic_string
<charT
, traits
, Allocator
>& lhs
,
647 const basic_string
<charT
, traits
, Allocator
>& rhs
)
649 return (lhs
.compare (rhs
) >= 0);
652 class istream
; class ostream
;
653 template <class charT
, class traits
, class Allocator
> istream
&
654 operator>> (istream
&, basic_string
<charT
, traits
, Allocator
>&);
655 template <class charT
, class traits
, class Allocator
> ostream
&
656 operator<< (ostream
&, const basic_string
<charT
, traits
, Allocator
>&);
657 template <class charT
, class traits
, class Allocator
> istream
&
658 getline (istream
&, basic_string
<charT
, traits
, Allocator
>&, charT delim
= '\n');
662 #include <std/bastring.cc>