2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
12 // This one is heavily based on the string class in The C++
13 // Programming Language by Bjarne Stroustrup
15 // This class is supposed to be functionaly equivalent to a
16 // standard conformant string. This mean among others that we
17 // are useing the same requirements. Before you change anything
18 // in this file consult me and/or the standard to discover the
34 #include <cstring> // for size_t
36 /* A string class for LyX
38 This is a permanent String class. It is modeled closely after the C++ STL
39 string class. In comparison with STL string lyxstring lack support for
40 reverse iterators and allocators, also char_traits is not used. In most
41 other senses it is written to be a drop in replacement for STL string
42 (or as a transition tool). So documentation for STL string should be
43 valid for lyxstring too.
47 When you declare an lyxstring, it is initially empty. There is no need to
48 do things like #lyxstring a= "";#, especially not in constructors.
50 If you want to use a default empty lyxstring as a parameter, use
52 #void foo(lyxstring par = lyxstring()); // Correct#
56 #void foo(lyxstring par = ""); // WRONG!#
57 #void foo(lyxstring par = 0); // WRONG!#
59 (The last one is only wrong because some compilers can't handle it.)
61 Methods that take an index as parameter all follow this rule: Valid indexes
62 go from 0 to length()-1.
64 Correct: & #foo.substring(0, length()-1);# \\
65 Wrong: & #bar.substring(0, length());#
68 It is important that you declare lyxstring as const if possible, because
69 some methods are much more efficient in const versions.
71 If you want to check whether a string is empty, do
73 #if (foo.empty()) something right#
75 rather than something along the lines of
77 #if (!foo) completely wrong#
79 When you want to copy an lyxstring, just do
81 #lyxstring a, b = "String";#
82 #a = b; // That's it!#
86 #lyxstring a, b = "String";#
87 #a = b.copy(); // This leaks. // and does not work either. #
89 The class automatically handles deep copying when required.
96 typedef char value_type;
99 typedef value_type * pointer;
102 typedef value_type & reference;
105 typedef value_type const & const_reference;
108 typedef size_t size_type;
111 typedef int difference_type;
114 typedef value_type * iterator;
116 typedef const value_type * const_iterator;
119 typedef reverse_iterator<iterator, value_type, reference>
122 typedef reverse_iterator<const_iterator, const value_type,
123 const_reference> const_reverse_iterator;
130 const_iterator begin() const;
134 const_iterator end() const;
137 reverse_iterator rbegin();
139 const_reverse_iterator rbegin() const;
141 reverse_iterator rend();
143 const_reverse_iterator rend() const;
145 /* Constructors and Deconstructors. lyxstring has the same
146 constructors as STL, except the one using iterators, one other
147 difference is that lyxstring, do not allow the optional allocator
150 /// "all characters" marker
151 static const size_type npos;
156 /// #lyxstring x(lyxstring ...)#
157 lyxstring(lyxstring const &, size_type pos = 0, size_type n = npos);
159 /// #lyxstring x("abc", 2) -> "ab"#
160 lyxstring(value_type const *, size_type n);
162 /// #lyxstring x("abc")#
163 lyxstring(value_type const *);
165 /// lyxstring(5, 'n') -> "nnnnn"
166 lyxstring(size_type n, value_type c);
170 lyxstring(const_iterator first, const_iterator last);
173 template<class InputIterator>
174 lyxstring::lyxstring(InputIterator begin, InputIterator end) {
175 while (begin != end) {
184 /// number of characters
185 size_type size() const;
187 /// largest possible string
188 size_type max_size() const { return npos - 1; }
191 size_type length() const { return size(); }
194 bool empty() const { return size() == 0; }
197 void resize(size_type n, value_type c);
200 void resize(size_type n) { resize(n, ' '); }
202 /// size of the memory (in number of elements) allocated.
203 size_type capacity() const;
206 void reserve(size_type res_arg = 0);
209 lyxstring & operator=(lyxstring const &);
212 lyxstring & operator=(value_type const *);
215 lyxstring & operator=(value_type);
218 lyxstring & assign(lyxstring const &);
221 lyxstring & assign(lyxstring const &, size_type pos, size_type n);
224 lyxstring & assign(value_type const * p, size_type n);
227 lyxstring & assign(value_type const * p);
230 lyxstring & assign(size_type n, value_type c);
234 lyxstring & assign(const_iterator first, const_iterator last);
237 template<class InputIterator>
238 lyxstring & assign(InputIterator begin, InputIterator end) {
240 while (begin != end) {
248 const_reference operator[](size_type) const;
251 reference operator[](size_type);
254 const_reference at(size_type) const;
257 reference at(size_type);
259 // add characters after (*this)[length()-1]:
262 lyxstring & operator+=(lyxstring const &);
265 lyxstring & operator+=(value_type const *);
268 lyxstring & operator+=(value_type);
271 void push_back(value_type);
274 lyxstring & append(lyxstring const &);
277 lyxstring & append(lyxstring const &, size_type pos, size_type n);
280 lyxstring & append(value_type const *, size_type n);
283 lyxstring & append(value_type const *);
286 lyxstring & append(size_type n, value_type);
290 lyxstring & append(iterator first, iterator last);
293 template<class InputIterator>
294 lyxstring & append(InputIterator begin, InputIterator end) {
295 while (begin != end) {
302 // insert characters before (*this)[pos]:
305 lyxstring & insert(size_type pos, lyxstring const &);
308 lyxstring & insert(size_type pos, lyxstring const &,
309 size_type pos2, size_type n);
312 lyxstring & insert(size_type pos, value_type const * p,
316 lyxstring & insert(size_type pos, value_type const * p);
319 lyxstring & insert(size_type pos, size_type n, value_type c);
321 // insert characters before p
324 iterator insert(iterator p, value_type c);
327 void insert(iterator p, size_type n , value_type c);
331 void insert(iterator p, iterator first, iterator last);
334 template<class InputIterator>
335 void insert(iterator p, InputIterator begin, InputIterator end) {
337 while (begin != end) {
338 it = insert(p, (*begin));
345 size_type find(lyxstring const &, size_type i = 0) const;
348 size_type find(value_type const * p,
349 size_type i, size_type n) const;
352 size_type find(value_type const * p, size_type i = 0) const;
355 size_type find(value_type c, size_type i = 0) const;
358 size_type rfind(lyxstring const &, size_type i = npos) const;
361 size_type rfind(value_type const * p, size_type i, size_type n) const;
364 size_type rfind(value_type const * p, size_type i = npos) const;
367 size_type rfind(value_type c, size_type i = npos) const;
370 size_type find_first_of(lyxstring const &, size_type i = 0) const;
373 size_type find_first_of(value_type const * p, size_type i,
377 size_type find_first_of(value_type const * p, size_type i = 0) const;
380 size_type find_first_of(value_type c, size_type i = 0) const;
383 size_type find_last_of(lyxstring const &, size_type i = npos) const;
386 size_type find_last_of(value_type const * p, size_type i,
390 size_type find_last_of(value_type const * p, size_type i = npos) const;
393 size_type find_last_of(value_type c, size_type i = npos) const;
396 size_type find_first_not_of(lyxstring const &, size_type i = 0) const;
399 size_type find_first_not_of(value_type const * p, size_type i,
403 size_type find_first_not_of(value_type const * p,
404 size_type i = 0) const;
407 size_type find_first_not_of(value_type c, size_type i = 0) const;
410 size_type find_last_not_of(lyxstring const &,
411 size_type i = npos) const;
414 size_type find_last_not_of(value_type const * p, size_type i,
418 size_type find_last_not_of(value_type const * p,
419 size_type i = npos) const;
422 size_type find_last_not_of(value_type c, size_type i = npos) const;
424 // replace [(*this)[i], (*this)[i+n]] with other characters:
427 lyxstring & replace(size_type i, size_type n, lyxstring const & str);
430 lyxstring & replace(size_type i, size_type n, lyxstring const & s,
431 size_type i2, size_type n2);
434 lyxstring & replace(size_type i, size_type n, value_type const * p,
438 lyxstring & replace(size_type i, size_type n, value_type const * p);
441 lyxstring & replace(size_type i, size_type n,
442 size_type n2, value_type c);
445 lyxstring & replace(iterator i, iterator i2, const lyxstring & str);
448 lyxstring & replace(iterator i, iterator i2,
449 value_type const * p, size_type n);
452 lyxstring & replace(iterator i, iterator i2, value_type const * p);
455 lyxstring & replace(iterator i, iterator i2,
456 size_type n , value_type c);
459 lyxstring & replace(iterator i, iterator i2, iterator j, iterator j2);
462 void swap(lyxstring & str);
464 /// Erase n chars from position i.
465 lyxstring & erase(size_type i = 0, size_type n = npos);
468 lyxstring & clear() {
469 return erase(0, npos);
473 iterator erase(iterator i);
476 iterator erase(iterator first, iterator last);
479 value_type const * c_str() const;
481 /* Note that this is STL compilant, so you can not assume
482 that the returned array has a trailing '\0'. */
483 value_type const * data() const;
485 /* This one returns a verbatim copy. Not the trailing '\0'
486 The caller must provide a buffer with engough room.
488 size_type copy(value_type * buf, size_type len,
489 size_type pos = 0) const;
493 int compare(lyxstring const & str) const;
496 int compare(value_type const * p) const;
499 int compare(size_type pos, size_type n, lyxstring const & str) const;
502 int compare(size_type pos, size_type n, lyxstring const & str,
503 size_type pos2, size_type n2) const;
506 int compare(size_type pos, size_type n, value_type const * p,
507 size_type n2 = npos) const;
511 lyxstring substr(size_type i = 0, size_type n = npos) const;
514 // These three operators can be used to discover erronous use of
515 // ints and strings. However a conforming C++ compiler will flag
516 // a lot of char operations as ambigous when they are compiled
517 // in. Use them for debugging only (or perhaps not even then.)
520 //lyxstring & operator+(int);
522 //lyxstring & operator=(int);
524 //lyxstring & operator+=(int);
526 /// Compare this with s. works with embedded '\0' chars also.
527 int internal_compare(size_type pos, size_type n,
528 value_type const * s,
529 size_type slen, size_type n2) const;
531 /// Forward declaration of the string representation
533 // DEC cxx requires this.
536 /// A string is a pointer to it's representation
539 /* Note: The empty_rep is a local static in each function that
540 benefits from one. There is no "global" empty srep but lyxstring
541 doesn't need one (no code actually relies upon a single
543 This overcomes *all* "static initialization" problems,
544 at maximum speed, with a small overhead of a few local static
548 #ifdef ENABLE_ASSERTIONS
549 /// lyxstringInvariant is used to test the lyxstring Invariant
550 friend class lyxstringInvariant;
554 // The usual comparison operators ==, !=, >, <, >=, <= are
555 // provided for lyxstrings
557 bool operator==(lyxstring const &, lyxstring const &);
558 bool operator==(lyxstring::value_type const *, lyxstring const &);
559 bool operator==(lyxstring const &, lyxstring::value_type const *);
562 bool operator!=(lyxstring const &, lyxstring const &);
563 bool operator!=(lyxstring::value_type const *, lyxstring const &);
564 bool operator!=(lyxstring const &, lyxstring::value_type const *);
567 bool operator>(lyxstring const &, lyxstring const &);
568 bool operator>(lyxstring::value_type const *, lyxstring const &);
569 bool operator>(lyxstring const &, lyxstring::value_type const *);
572 bool operator<(lyxstring const &, lyxstring const &);
573 bool operator<(lyxstring::value_type const *, lyxstring const &);
574 bool operator<(lyxstring const &, lyxstring::value_type const *);
577 bool operator>=(lyxstring const &, lyxstring const &);
578 bool operator>=(lyxstring::value_type const *, lyxstring const &);
579 bool operator>=(lyxstring const &, lyxstring::value_type const *);
582 bool operator<=(lyxstring const &, lyxstring const &);
583 bool operator<=(lyxstring::value_type const *, lyxstring const &);
584 bool operator<=(lyxstring const &, lyxstring::value_type const *);
589 lyxstring operator+(lyxstring const & a, lyxstring const & b);
590 lyxstring operator+(char const * a, lyxstring const & b);
591 lyxstring operator+(lyxstring::value_type a, lyxstring const & b);
592 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b);
593 lyxstring operator+(lyxstring const & a, lyxstring::value_type b);
595 void swap(lyxstring & s1, lyxstring & s2);
597 std::istream & operator>>(std::istream &, lyxstring &);
598 std::ostream & operator<<(std::ostream &, lyxstring const &);
599 std::istream & getline(std::istream &, lyxstring &, lyxstring::value_type delim = '\n');