1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1999 The LyX Team.
9 * ====================================================== */
16 #pragma implementation "lyxstring.h"
19 #include "lyxstring.h"
28 // This class is supposed to be functionaly equivalent to a
29 // standard conformant string. This mean among others that we
30 // are useing the same requirements. Before you change anything
31 // in this file consult me and/or the standard to discover the
34 // Asserts with a STD! are required by the standard.
35 // Asserts with a OURS! are added by me.
36 // Some asserts could still be missing and some of the existing
37 // ones might be wrong or not needed.
39 // Reference count has been checked, empty_rep removed and
40 // introduced again in a similar guise. Where is empty_rep _really_
43 // We are missing a couple of imporant things from the standard:
44 // reverse iterators and methods taking InputIterators as paramters.
45 // Also the methods returning iterators is returning the wrong value.
47 // All the different find functions need a good look over.
48 // I have so far not tested them extensively and would be
49 // happy if others took the time to have a peek.
51 // Space allocation of string.
52 // I have tried to do this very simple without using any special tricks.
53 // Earlier we used a fixed value to enlarge the string with this would
54 // cause a lot of reallocations with large strings (especially if
55 // push_back was used) and wasting space for very small strings.
56 // I have now changed the allocation to use a doubling of reserved
57 // space until it is large enough. So far tests show a small speed
58 // increase and a noticable memory saving.
63 ///////////////////////////////////////
64 // The internal string representation
65 ///////////////////////////////////////
67 struct lyxstring::Srep {
72 /// The total amount of data reserved for this representaion
74 /// Data. At least 1 char for trailing null.
75 lyxstring::value_type * s;
78 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
80 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
82 ~Srep() { delete[] s; }
84 Srep * get_own_copy() {
85 if (ref == 1) return this;
87 return new Srep(sz, s);
91 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
93 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
95 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
97 void push_back(lyxstring::value_type c);
99 void insert(lyxstring::size_type pos,
100 const lyxstring::value_type * p,
101 lyxstring::size_type n);
103 void resize(lyxstring::size_type n, lyxstring::value_type c);
105 void reserve(lyxstring::size_type res_arg);
107 void replace(lyxstring::size_type i, lyxstring::size_type n,
108 lyxstring::value_type const * p, lyxstring::size_type n2);
111 Srep & operator=(const Srep &);
115 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
117 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
122 s = new value_type[res + 1]; // add space for terminator
124 // if sz = 0 nothing gets copied and we have an error
127 // possibly allows for large but empty string
128 sz = 0; // this line should be redundant
134 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
139 s = new value_type[res + 1]; // add space for terminator
142 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
143 // allows for large but empty string
149 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
151 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
157 s = new value_type[res + 1]; // add space for terminator
162 // if sz = 0 nothing gets copied and we have an error
166 sz = 0; // this line should be redundant
172 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
178 s = new value_type[res + 1]; // add space for terminator
182 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
183 // allows for a large empty string
189 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
191 register unsigned int const len = sz + asz;
196 value_type * tmp = new value_type[res + 1];
198 memcpy(tmp + sz, p, asz);
203 memcpy(s + sz, p, asz);
209 void lyxstring::Srep::push_back(value_type c)
211 s[sz] = c; // it is always room to put a value_type at the end
217 value_type * tmp = new value_type[res + 1];
225 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
226 lyxstring::size_type n)
231 } while (res < n + sz);
232 value_type * tmp = new value_type[res + 1];
234 memcpy(tmp + pos, p, n);
235 memcpy(tmp + pos + n, &s[pos], sz - pos);
240 memmove(s + pos + n, &s[pos], sz - pos);
241 memcpy(s + pos, p, n);
247 void lyxstring::Srep::resize(size_type n, value_type c)
249 // This resets sz to res_arg
250 res = min(n, npos - 2); // We keep no xtra when we resize
251 value_type * tmp = new value_type[res + 1];
252 memcpy(tmp, s, min(sz, res));
254 memset(tmp + sz, c, res - sz);
261 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
263 // This keeps the old sz, but
264 // increases res with res_arg
266 value_type * tmp = new value_type[res + 1];
273 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
274 value_type const * p, size_type n2)
276 // can be called with p= 0 and n2= 0
279 if (res >= n2 + sz) {
280 memmove(s + i + n2, &s[i + n], sz - i);
281 memcpy(s + i, p, n2);
286 } while (res < n2 + sz);
287 value_type * tmp = new value_type[res + 1];
289 memcpy(tmp + i, p, n2);
290 memcpy(tmp + i + n2, &s[i + n], sz - i);
298 ///////////////////////////////////////
299 // The lyxstring Invariant tester
300 ///////////////////////////////////////
303 /** Testing of the lyxstring invariant
304 * By creating an object that tests the lyxstring invariant during its
305 * construction *and* its deconstruction we greatly simplify our code.
306 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
307 * will test the invariant upon entry to the code. If the Asserts fail
308 * then we know from the stack trace that the corruption occurred *before*
309 * entry to this method. We can also be sure it didn't happen in any of
310 * the tested lyxstring methods. It is therefore likely to be due to some
311 * other external force.
312 * Several lyxstring methods have multiple exit points which would otherwise
313 * require us to insert a separate test before each return. But since we
314 * created an object its destructor will be called upon exit (any exit!).
315 * We thus get testing at both start and end of a method with one line of
316 * code at the head of a method. More importantly, we get good testing
317 * everytime we run the code.
318 * NOTE: just because we test the invariant doesn't mean we can forget
319 * about testing pre and post conditions specific to any given method.
320 * This test simply proves that the lyxstring/Srep is in a valid state it
321 * does *not* prove that the method did what it was supposed to.
323 class lyxstringInvariant {
325 lyxstringInvariant(lyxstring const *);
326 ~lyxstringInvariant();
329 lyxstring const * object;
333 // To test if this scheme works "as advertised" uncomment the printf's in
334 // the constructor and destructor below and then uncomment the printf and the
335 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
336 // The correct output when LyX has been recompiled and run is:
337 // lyxstringInvariant constructor
338 // lyxstring::operator=(char const *)
339 // lyxstringInvariant constructor
340 // lyxstringInvariant destructor completed
341 // lyxstringInvariant destructor completed
342 // NOTE: The easiest way to catch this snippet of the output is to wait for
343 // the splash screen to disappear and then open and close Help->Credits
345 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
347 // printf("lyxstringInvariant constructor\n");
352 lyxstringInvariant::~lyxstringInvariant()
355 // printf("lyxstringInvariant destructor completed\n");
359 void lyxstringInvariant::helper() const
361 // Some of these tests might look pointless but they are
362 // all part of the invariant and if we want to make sure
363 // we have a bullet proof implementation then we need to
364 // test every last little thing we *know* should be true.
365 // I may have missed a test or two, so feel free to fill
366 // in the gaps. ARRae.
369 Assert(object->rep->s); // s is never 0
370 Assert(object->rep->res); // res cannot be 0
371 Assert(object->rep->sz <= object->rep->res);
372 Assert(object->rep->ref >= 1); // its in use so it must be referenced
373 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
374 // if it does ever == then we should be generating a new copy
375 // and starting again. (Is char always 8-bits?)
377 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
379 #define TestlyxstringInvariant(s)
380 #endif //DEVEL_VERSION
383 ///////////////////////////////////////
384 // Constructors and Deconstructors.
385 ///////////////////////////////////////
387 lyxstring::size_type const lyxstring::npos =
388 static_cast<lyxstring::size_type>(-1);
391 lyxstring::lyxstring()
393 static Srep empty_rep(0, "");
399 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
401 Assert(pos <= x.rep->sz); // STD!
402 if (pos == 0 && n >= x.length()) { // this is the default
406 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
411 lyxstring::lyxstring(value_type const * s, size_type n)
413 Assert(s && n < npos); // STD!
414 static Srep empty_rep(0, "");
415 if (*s && n) { // s is not empty string and n > 0
416 rep = new Srep(min(strlen(s), n), s);
424 lyxstring::lyxstring(value_type const * s)
427 static Srep empty_rep(0, "");
428 if (*s) { // s is not empty string
429 rep = new Srep(strlen(s), s);
437 lyxstring::lyxstring(size_type n, value_type c)
439 Assert(n < npos); // STD!
440 rep = new Srep(n, c);
444 lyxstring::lyxstring(const_iterator first, const_iterator last)
446 rep = new Srep(last - first, first);
450 lyxstring::~lyxstring()
452 if (--rep->ref == 0) delete rep;
455 ///////////////////////
457 ///////////////////////
459 lyxstring::iterator lyxstring::begin()
465 lyxstring::const_iterator lyxstring::begin() const
471 lyxstring::iterator lyxstring::end()
473 return rep->s + rep->sz;
477 lyxstring::const_iterator lyxstring::end() const
479 return rep->s + rep->sz;
483 reverse_iterator lyxstring::rbegin()
485 return reverse_iterator( end() );
489 const_reverse_iterator lyxstring::rbegin() const
491 return const_reverse_iterator( end() );
495 reverse_iterator lyxstring::rend()
497 return reverse_iterator( begin() );
501 const_reverse_iterator lyxstring::rend() const
503 return const_reverse_iterator( begin() );
508 ///////////////////////
510 ///////////////////////
512 lyxstring::size_type lyxstring::size() const
518 void lyxstring::resize(size_type n, value_type c)
520 Assert(n <= npos); // STD!
521 TestlyxstringInvariant(this);
523 // This resets sz to res_arg
524 rep = rep->get_own_copy();
529 lyxstring::size_type lyxstring::capacity() const
535 void lyxstring::reserve(size_type res_arg)
537 TestlyxstringInvariant(this);
539 rep = rep->get_own_copy();
540 rep->reserve(res_arg);
548 lyxstring & lyxstring::operator= (lyxstring const & x)
550 TestlyxstringInvariant(this);
556 lyxstring & lyxstring::operator= (value_type const * s)
559 TestlyxstringInvariant(this);
560 // printf("lyxstring::operator= (value_type const *)\n");
566 lyxstring & lyxstring::operator=(value_type c)
568 TestlyxstringInvariant(this);
572 if (rep->ref == 1) // recycle rep
576 rep = new Srep(1, s);
582 lyxstring & lyxstring::assign(lyxstring const & x)
584 TestlyxstringInvariant(this);
586 x.rep->ref++; // protect against ``st = st''
587 if (--rep->ref == 0) delete rep;
588 rep = x.rep; // share representation
593 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
595 Assert(pos <= x.rep->sz); // STD!
596 TestlyxstringInvariant(this);
598 return assign(x.substr(pos, n));
602 lyxstring & lyxstring::assign(value_type const * s, size_type n)
605 TestlyxstringInvariant(this);
607 n = min(strlen(s), n);
608 if (rep->ref == 1) // recycle rep
612 rep = new Srep(n, s);
618 lyxstring & lyxstring::assign(value_type const * s)
621 TestlyxstringInvariant(this);
623 return assign(s, strlen(s));
627 lyxstring & lyxstring::assign(size_type n, value_type ch)
629 TestlyxstringInvariant(this);
631 rep = rep->get_own_copy();
637 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
639 TestlyxstringInvariant(this);
641 rep = rep->get_own_copy();
642 rep->assign(last - first, first);
651 lyxstring::const_reference lyxstring::operator[](size_type pos) const
653 Assert(pos <= rep->sz); // OURS!
654 static char helper = '\0';
655 return pos == rep->sz ? helper : rep->s[pos];
659 lyxstring::reference lyxstring::operator[](size_type pos)
661 Assert(pos < rep->sz); // OURS!
662 TestlyxstringInvariant(this);
664 rep = rep->get_own_copy();
669 lyxstring::const_reference lyxstring::at(size_type n) const
671 Assert(n < rep->sz); // STD!
676 lyxstring::reference lyxstring::at(size_type n)
678 Assert(n < rep->sz); // STD!
679 TestlyxstringInvariant(this);
681 rep = rep->get_own_copy();
690 lyxstring & lyxstring::operator+=(lyxstring const & x)
692 TestlyxstringInvariant(this);
698 lyxstring & lyxstring::operator+=(value_type const * x)
701 TestlyxstringInvariant(this);
707 lyxstring & lyxstring::operator+=(value_type c)
709 TestlyxstringInvariant(this);
716 void lyxstring::push_back(value_type c)
718 TestlyxstringInvariant(this);
720 rep = rep->get_own_copy();
725 lyxstring & lyxstring::append(lyxstring const & x)
727 TestlyxstringInvariant(this);
729 if (x.empty()) return *this;
730 rep = rep->get_own_copy();
731 rep->append(x.length(), x.rep->s);
736 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
738 Assert(pos <= x.rep->sz); // STD!
739 TestlyxstringInvariant(this);
741 return append(x.substr(pos, n));
745 lyxstring & lyxstring::append(value_type const * p, size_type n)
748 TestlyxstringInvariant(this);
750 if (!*p || !n) return *this;
751 rep = rep->get_own_copy();
752 rep->append(min(n, strlen(p)), p);
757 lyxstring & lyxstring::append(value_type const * p)
760 TestlyxstringInvariant(this);
762 if (!*p) return *this;
763 rep = rep->get_own_copy();
764 rep->append(strlen(p), p);
769 lyxstring & lyxstring::append(size_type n, value_type c)
771 TestlyxstringInvariant(this);
773 value_type * tmp = new value_type[n];
775 rep = rep->get_own_copy();
782 lyxstring & lyxstring::append(iterator first, iterator last)
784 TestlyxstringInvariant(this);
786 rep = rep->get_own_copy();
787 rep->append(last - first, first);
791 // insert value_typeacters before (*this)[pos]
793 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
795 TestlyxstringInvariant(this);
797 return insert(pos, x, 0, x.rep->sz);
801 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
802 size_type pos2, size_type n)
804 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
805 TestlyxstringInvariant(this);
807 rep = rep->get_own_copy();
808 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
813 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
816 TestlyxstringInvariant(this);
819 // insert nothing and you change nothing
820 rep = rep->get_own_copy();
821 rep->insert(pos, p, min(n, strlen(p)));
827 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
830 TestlyxstringInvariant(this);
833 // insert nothing and you change nothing
834 rep = rep->get_own_copy();
835 rep->insert(pos, p, strlen(p));
841 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
843 TestlyxstringInvariant(this);
845 rep = rep->get_own_copy();
846 value_type * tmp = new value_type[n];
848 rep->insert(pos, tmp, n);
854 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
856 TestlyxstringInvariant(this);
858 // what iterator is this supposed to return??
859 size_type tmp = p - begin();
860 insert(p - begin(), 1, c);
861 return begin() + tmp + 1; // ??
865 void lyxstring::insert(iterator p, size_type n , value_type c)
867 TestlyxstringInvariant(this);
869 insert(p - begin(), n , c);
873 void lyxstring::insert(iterator p, iterator first, iterator last)
875 TestlyxstringInvariant(this);
877 insert(p - begin(), first, last - first);
885 // All the below find functions should be verified,
886 // it is very likely that I have mixed up or interpreted
887 // some of the parameters wrong, also some of the funcs can surely
888 // be written more effectively.
890 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
892 if (!rep->sz || i >= rep->sz) return npos;
894 TestlyxstringInvariant(this);
896 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
897 // search until (*this)[i] == a[0]
898 if (rep->s[t] == a[0]) {
899 // check if the rest of the value_types match
901 for (size_type j = 0; j < a.length(); ++j) {
902 if (rep->s[t + j] != a[j]) {
914 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
917 Assert(ptr); // OURS!
918 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
920 TestlyxstringInvariant(this);
922 // What is "n" here? is it the number of value_types to use in ptr
923 // or does "i" and "n" togeter form a substring to search
924 // for ptr in? For now I will assume that "n" tells the length
926 n = min(n, strlen(ptr));
927 for (size_type t = i; rep->sz - t >= n; ++t) {
928 // search until (*this)[i] == a[0]
929 if (rep->s[t] == ptr[0]) {
930 // check if the rest of the value_types match
932 for (size_type j = 0; j < n; ++j) {
933 if (rep->s[t + j] != ptr[j]) {
945 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
948 if (!rep->sz || i >= rep->sz) return npos;
950 TestlyxstringInvariant(this);
952 if (!s || !*s) return npos;
953 return find(s, i, strlen(s));
957 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
959 if (!rep->sz || i >= rep->sz) return npos;
961 TestlyxstringInvariant(this);
963 for (size_type t = 0; t + i < rep->sz; ++t) {
964 if (rep->s[t + i] == c) return t + i;
970 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
972 TestlyxstringInvariant(this);
974 size_type ii = min(rep->sz - 1, i);
976 if (a[a.length() - 1] == rep->s[ii]) {
978 size_type l = ii - 1;
979 for (; t >= 0; --t, --l) {
980 if (a[t] != rep->s[l]) break;
982 if (a[t] == rep->s[l]) return l;
989 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
992 Assert(ptr); // OURS!
993 TestlyxstringInvariant(this);
994 if (!*ptr) return npos;
996 size_type ii = min(rep->sz - 1, i);
998 if (ptr[n - 1] == rep->s[ii]) {
1000 size_type l = ii - 1;
1001 for (; t >= 0; --t, --l) {
1002 if (ptr[t] != rep->s[l]) break;
1004 if (ptr[t] == rep->s[l]) return l;
1011 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1014 Assert(ptr); // OURS!
1015 TestlyxstringInvariant(this);
1016 if (!*ptr) return npos;
1018 size_type ii = min(rep->sz - 1, i);
1020 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1021 int t = strlen(ptr) - 2;
1022 size_type l = ii - 1;
1023 for (; t >= 0; --t, --l) {
1024 if (ptr[t] != rep->s[l]) break;
1026 if (ptr[t] == rep->s[l]) return l;
1033 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1035 TestlyxstringInvariant(this);
1037 size_type ii = min(rep->sz - 1, i);
1038 for (size_type t = ii; t != 0; --t) {
1039 if (rep->s[t] == c) return t;
1045 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1048 Assert(i < rep->sz); // OURS!
1049 TestlyxstringInvariant(this);
1051 for (size_type t = i; t < rep->sz; ++t) {
1052 if (a.find(rep->s[t]) != npos) return t;
1058 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1062 Assert(ptr && i < rep->sz); // OURS!
1063 TestlyxstringInvariant(this);
1064 if (!n) return npos;
1066 for (size_type t = i; t < rep->sz; ++t) {
1067 if(memchr(ptr, rep->s[t], n) != 0) return t;
1073 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1076 Assert(ptr && i < rep->sz); // OURS!
1077 TestlyxstringInvariant(this);
1079 for (size_type t = i; t < rep->sz; ++t) {
1080 if (strchr(ptr, rep->s[t]) != 0) return t;
1086 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1088 Assert(i < rep->sz); // OURS!
1089 TestlyxstringInvariant(this);
1091 for (size_type t = i; t < rep->sz; ++t) {
1092 if (rep->s[t] == c) return t;
1098 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1101 TestlyxstringInvariant(this);
1103 size_type ii = min(rep->sz - 1, i);
1104 for (int t = ii; t >= 0; --t) {
1105 if (a.find(rep->s[t]) != npos) return t;
1111 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1115 Assert(ptr); // OURS!
1116 TestlyxstringInvariant(this);
1117 if (!n) return npos;
1119 size_type ii = min(rep->sz - 1, i);
1120 for (int t = ii; t >= 0; --t) {
1121 if(memchr(ptr, rep->s[t], n) != 0) return t;
1127 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1130 Assert(ptr); // OURS!
1131 TestlyxstringInvariant(this);
1133 size_type ii = min(rep->sz - 1, i);
1134 for (int t = ii; t >= 0; --t) {
1135 if (strchr(ptr, rep->s[t]) != 0) return t;
1141 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1143 TestlyxstringInvariant(this);
1145 if (!rep->sz) return npos;
1146 size_type ii = min(rep->sz - 1, i);
1147 for (int t = ii; t >= 0; --t) {
1148 if (rep->s[t] == c) return t;
1154 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1157 TestlyxstringInvariant(this);
1159 if (!rep->sz) return npos;
1160 Assert(i < rep->sz);
1161 for (size_type t = i; t < rep->sz; ++t) {
1162 if (a.find(rep->s[t]) == npos) return t;
1168 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1172 Assert(ptr && i < rep->sz); // OURS!
1173 TestlyxstringInvariant(this);
1175 if (!n) return (i < rep->sz) ? i : npos;
1176 for (size_type t = i; t < rep->sz; ++t) {
1177 if(memchr(ptr, rep->s[t], n) == 0) return t;
1183 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1186 Assert(ptr && i < rep->sz); // OURS!
1187 TestlyxstringInvariant(this);
1189 for (size_type t = i; t < rep->sz; ++t) {
1190 if (strchr(ptr, rep->s[t]) == 0) return t;
1196 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1199 if (!rep->sz) return npos;
1200 Assert(i < rep->sz); // OURS!
1201 TestlyxstringInvariant(this);
1203 for (size_type t = i; t < rep->sz; ++t) {
1204 if (rep->s[t] != c) return t;
1210 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1213 TestlyxstringInvariant(this);
1215 size_type ii = min(rep->sz - 1, i);
1216 for (int t = ii; t >= 0; --t) {
1217 if (a.find(rep->s[t]) == npos) return t;
1223 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1227 Assert(ptr); // OURS!
1228 TestlyxstringInvariant(this);
1230 if (!n) return npos;
1231 size_type ii = min(rep->sz - 1, i);
1233 for (int t = ii; t >= 0; --t) {
1234 if(memchr(ptr, rep->s[t], n) == 0) return t;
1240 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1243 Assert(ptr); // OURS!
1244 TestlyxstringInvariant(this);
1246 size_type ii = min(rep->sz - 1, i);
1247 for (int t = ii; t >= 0; --t) {
1248 if (strchr(ptr, rep->s[t]) == 0) return t;
1254 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1257 TestlyxstringInvariant(this);
1259 size_type ii = min(rep->sz - 1, i);
1260 for (int t = ii; t >= 0; --t) {
1261 if (rep->s[t] != c) return t;
1271 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1273 Assert(i <= rep->sz); // OURS!
1274 TestlyxstringInvariant(this);
1276 return replace(i, n, x, 0, x.rep->sz);
1280 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1281 size_type i2, size_type n2)
1283 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1284 TestlyxstringInvariant(this);
1286 rep = rep->get_own_copy();
1287 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1292 lyxstring & lyxstring::replace(size_type i, size_type n,
1293 value_type const * p, size_type n2)
1295 Assert(p && i < rep->sz); // OURS!
1296 TestlyxstringInvariant(this);
1298 rep = rep->get_own_copy();
1299 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1304 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1306 Assert(p && i < rep->sz); // OURS!
1307 TestlyxstringInvariant(this);
1309 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1313 lyxstring & lyxstring::replace(size_type i, size_type n,
1314 size_type n2, value_type c)
1316 Assert(i < rep->sz); // OURS!
1317 TestlyxstringInvariant(this);
1319 rep = rep->get_own_copy();
1320 value_type * tmp = new value_type[n2];
1322 rep->replace(i, min(n, rep->sz), tmp, n2);
1328 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1330 TestlyxstringInvariant(this);
1332 return replace(i - begin(), i2 - i, str);
1336 lyxstring & lyxstring::replace(iterator i, iterator i2,
1337 value_type const * p, size_type n)
1340 TestlyxstringInvariant(this);
1342 return replace(i - begin(), i2 - i, p, n);
1346 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1349 TestlyxstringInvariant(this);
1351 return replace(i - begin(), i2 - i, p);
1355 lyxstring & lyxstring::replace(iterator i, iterator i2,
1356 size_type n , value_type c)
1358 TestlyxstringInvariant(this);
1360 return replace(i - begin(), i2 - i, n, c);
1364 lyxstring & lyxstring::replace(iterator i, iterator i2,
1365 iterator j, iterator j2)
1367 TestlyxstringInvariant(this);
1369 return replace(i - begin(), i2 - i, j, j2 - j);
1373 void lyxstring::swap(lyxstring & str)
1375 if (rep == str.rep) return;
1376 Srep * tmp = str.rep;
1382 lyxstring & lyxstring::erase(size_type i, size_type n)
1384 Assert(i <= rep->sz); // STD!
1385 TestlyxstringInvariant(this);
1387 rep = rep->get_own_copy();
1388 if (i == 0 && n >= rep->sz) {
1391 n = min(n, rep->sz - i);
1392 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1399 lyxstring::iterator lyxstring::erase(iterator i)
1401 TestlyxstringInvariant(this);
1403 // what iterator is this supposed to return?
1404 // the iterator after the one erased
1405 erase(i - begin(), 1);
1406 return begin(); // BUG
1410 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1412 TestlyxstringInvariant(this);
1414 erase(first - begin(), last - first);
1415 return begin(); // BUG
1419 /////////////////////////////////////
1420 // Conversion to C-style Strings
1421 /////////////////////////////////////
1423 lyxstring::value_type const * lyxstring::c_str() const
1425 rep->s[length()] = '\0';
1430 lyxstring::value_type const * lyxstring::data() const
1436 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1437 size_type pos) const
1439 Assert(buf); // OURS!
1440 Assert(pos <= rep->sz); // STD!
1441 TestlyxstringInvariant(this);
1443 register int nn = min(len, length() - pos);
1444 memcpy(buf, &(rep->s[pos]), nn);
1449 ////////////////////
1451 ////////////////////
1453 // Compare funcs should be verified.
1455 int lyxstring::internal_compare(size_type pos, size_type n,
1456 value_type const * s,
1457 size_type slen, size_type n2) const
1459 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1461 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1463 // remember that n can very well be a lot larger than rep->sz
1464 // so we have to ensure that n is no larger than rep->sz
1465 n = min(n, rep->sz);
1468 return memcmp(&(rep->s[pos]), s, n);
1469 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1478 int lyxstring::compare(lyxstring const & str) const
1480 TestlyxstringInvariant(this);
1481 return internal_compare(0, rep->sz, str.rep->s,
1482 str.rep->sz, str.rep->sz);
1486 int lyxstring::compare(value_type const * s) const
1489 TestlyxstringInvariant(this);
1490 int n = (!s) ? 0 : strlen(s);
1491 return internal_compare(0, rep->sz, s, n, n);
1495 int lyxstring::compare(size_type pos, size_type n,
1496 lyxstring const & str) const
1498 Assert(pos <= rep->sz); // OURS!
1499 TestlyxstringInvariant(this);
1500 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1504 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1505 size_type pos2, size_type n2) const
1507 Assert(pos <= rep->sz); // OURS!
1508 Assert(pos2 <= str.rep->sz); // OURS!
1509 TestlyxstringInvariant(this);
1510 return internal_compare(pos, n,
1512 str.rep->sz - pos2, n2);
1516 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1519 Assert(s && pos <= rep->sz); // OURS!
1520 TestlyxstringInvariant(this);
1521 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1529 // i = index, n = length
1530 lyxstring lyxstring::substr(size_type i, size_type n) const
1532 Assert(i <= rep->sz); // STD!
1533 TestlyxstringInvariant(this);
1535 return lyxstring(*this, i, n);
1539 /////////////////////////////////////////////
1540 // String operators, non member functions
1541 /////////////////////////////////////////////
1543 bool operator==(lyxstring const & a, lyxstring const & b)
1545 return a.compare(b) == 0;
1549 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1552 return b.compare(a) == 0;
1556 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1559 return a.compare(b) == 0;
1563 bool operator!=(lyxstring const & a, lyxstring const & b)
1565 return a.compare(b) != 0;
1569 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1572 return b.compare(a) != 0;
1576 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1579 return a.compare(b) != 0;
1583 bool operator>(lyxstring const & a, lyxstring const & b)
1585 return a.compare(b) > 0;
1589 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1592 return b.compare(a) < 0; // since we reverse the parameters
1596 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1599 return a.compare(b) > 0;
1603 bool operator<(lyxstring const & a, lyxstring const & b)
1605 return a.compare(b) < 0;
1609 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1612 return b.compare(a) > 0; // since we reverse the parameters
1616 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1619 return a.compare(b) < 0;
1623 bool operator>=(lyxstring const & a, lyxstring const & b)
1625 return a.compare(b) >= 0;
1629 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1632 return b.compare(a) <= 0; // since we reverse the parameters
1636 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1639 return a.compare(b) >= 0;
1643 bool operator<=(lyxstring const & a, lyxstring const & b)
1645 return a.compare(b) <= 0;
1649 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1652 return b.compare(a) >= 0; // since we reverse the parameters
1656 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1659 return a.compare(b) <= 0;
1663 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1671 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1680 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1689 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1698 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1706 void swap(lyxstring & str1, lyxstring & str2)
1714 istream & operator>>(istream & is, lyxstring & s)
1717 // very bad solution
1718 char * nome = new char[1024];
1720 lyxstring tmp(nome);
1722 if (!tmp.empty()) s = tmp;
1725 int w = is.widdth(0);
1729 if (isspace(c)) { is.putback(c); break; }
1731 if (--w == 1) break;
1733 if (s.empty()) is.setstate(ios::failbit);
1739 ostream & operator<<(ostream & o, lyxstring const & s)
1741 return o.write(s.data(), s.length());
1745 istream & getline(istream & is, lyxstring & s,
1746 lyxstring::value_type delim)
1748 // very bad solution