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 // Reference count has been checked, empty_rep removed and
35 // introduced again in a similar guise. Where is empty_rep _really_
38 // Insertion and replace is implemented, as far as I can see everything
39 // works, but could perhaps be done smarter.
41 // All the different find functions need a good look over.
42 // I have so far not tested them extensively and would be
43 // happy if others took the time to have a peek.
46 ///////////////////////////////////////
47 // The internal string representation
48 ///////////////////////////////////////
50 struct lyxstring::Srep {
52 static lyxstring::size_type const xtra =
53 static_cast<lyxstring::size_type>(8);
55 lyxstring::size_type sz;
58 /// The total amount of data reserved for this representaion
59 lyxstring::size_type res;
60 /// Data. At least 1 char for trailing null.
61 lyxstring::value_type * s;
64 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
66 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
68 ~Srep() { delete[] s; }
72 if (ref == 1) return this;
74 return new Srep(sz, s);
78 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
80 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
82 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
84 void push_back(lyxstring::value_type c);
86 void insert(lyxstring::size_type pos,
87 const lyxstring::value_type * p,
88 lyxstring::size_type n);
90 void resize(lyxstring::size_type n, lyxstring::value_type c);
92 void reserve(lyxstring::size_type res_arg);
94 void replace(lyxstring::size_type i, lyxstring::size_type n,
95 lyxstring::value_type const * p, lyxstring::size_type n2);
98 Srep & operator=(const Srep &);
102 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
104 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
109 s = new value_type[res + 1]; // add space for terminator
111 // if sz = 0 nothing gets copied and we have an error
114 // possibly allows for large but empty string
115 sz = 0; // this line should be redundant
121 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
126 s = new value_type[res + 1]; // add space for terminator
129 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
130 // allows for large but empty string
136 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
138 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
144 s = new value_type[res + 1]; // add space for terminator
149 // if sz = 0 nothing gets copied and we have an error
153 sz = 0; // this line should be redundant
159 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
165 s = new value_type[res + 1]; // add space for terminator
169 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
170 // allows for a large empty string
176 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
178 register unsigned int const len = sz + asz;
181 value_type * tmp = new value_type[res + 1];
183 memcpy(tmp + sz, p, asz);
188 memcpy(s + sz, p, asz);
194 void lyxstring::Srep::push_back(value_type c)
196 s[sz] = c; // it is always room to put a value_type at the end
200 value_type * tmp = new value_type[res + 1];
208 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
209 lyxstring::size_type n)
214 value_type * tmp = new value_type[res + 1];
216 memcpy(tmp + pos, p, n);
217 memcpy(tmp + pos + n, & s[pos], sz - pos);
222 memmove(s + pos + n, & s[pos], sz - pos);
223 memcpy(s + pos, p, n);
229 void lyxstring::Srep::resize(size_type n, value_type c)
232 // This resets sz to res_arg
233 res = min(n, npos - 2); // We keep no xtra when we resize
234 value_type * tmp = new value_type[res + 1];
235 memcpy(tmp, s, min(sz, res));
237 memset(tmp + sz, c, res - sz);
244 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
246 // This keeps the old sz, but
247 // increases res with res_arg
249 value_type * tmp = new value_type[res + 1];
256 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
257 value_type const * p, size_type n2)
259 // can be called with p=0 and n2=0
260 Assert(i < sz && ((!p && !n2) || p));
263 if (res >= n2 + sz) {
264 memmove(s + i + n2, &s[i + n], sz - i);
265 memcpy(s + i, p, n2);
268 res = sz + n2 + xtra;
269 value_type * tmp = new value_type[res + 1];
271 memcpy(tmp + i, p, n2);
272 memcpy(tmp + i + n2, &s[i + n], sz - i);
280 ///////////////////////////////////////
281 // The lyxstring Invariant tester
282 ///////////////////////////////////////
285 /** Testing of the lyxstring invariant
286 * By creating an object that tests the lyxstring invariant during its
287 * construction *and* its deconstruction we greatly simplify our code.
288 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
289 * will test the invariant upon entry to the code. If the Asserts fail
290 * then we know from the stack trace that the corruption occurred *before*
291 * entry to this method. We can also be sure it didn't happen in any of
292 * the tested lyxstring methods. It is therefore likely to be due to some
293 * other external force.
294 * Several lyxstring methods have multiple exit points which would otherwise
295 * require us to insert a separate test before each return. But since we
296 * created an object its destructor will be called upon exit (any exit!).
297 * We thus get testing at both start and end of a method with one line of
298 * code at the head of a method. More importantly, we get good testing
299 * everytime we run the code.
300 * NOTE: just because we test the invariant doesn't mean we can forget
301 * about testing pre and post conditions specific to any given method.
302 * This test simply proves that the lyxstring/Srep is in a valid state it
303 * does *not* prove that the method did what it was supposed to.
305 class lyxstringInvariant {
307 lyxstringInvariant(lyxstring const *);
308 ~lyxstringInvariant();
311 lyxstring const * object;
315 // To test if this scheme works "as advertised" uncomment the printf's in
316 // the constructor and destructor below and then uncomment the printf and the
317 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
318 // The correct output when LyX has been recompiled and run is:
319 // lyxstringInvariant constructor
320 // lyxstring::operator=(char const *)
321 // lyxstringInvariant constructor
322 // lyxstringInvariant destructor completed
323 // lyxstringInvariant destructor completed
324 // NOTE: The easiest way to catch this snippet of the output is to wait for
325 // the splash screen to disappear and then open and close Help->Credits
327 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
329 // printf("lyxstringInvariant constructor\n");
333 lyxstringInvariant::~lyxstringInvariant()
336 // printf("lyxstringInvariant destructor completed\n");
339 void lyxstringInvariant::helper() const
341 // Some of these tests might look pointless but they are
342 // all part of the invariant and if we want to make sure
343 // we have a bullet proof implementation then we need to
344 // test every last little thing we *know* should be true.
345 // I may have missed a test or two, so feel free to fill
346 // in the gaps. ARRae.
347 // NOTE: Don't put TestlyxstringInvariant() in any of the
348 // lyxstring methods used below otherwise you'll get an
349 // infinite recursion and a crash.
352 Assert(object->rep->s); // s is never 0
353 Assert(object->rep->res); // always some space allocated
354 Assert(object->size() <= object->rep->res);
355 Assert(object->rep->ref >= 1); // its in use so it must be referenced
356 Assert(object->rep->ref < (1 << 8*sizeof(object->rep->ref)) - 1);
357 // if it does ever == then we should be generating a new copy
358 // and starting again. (Is char always 8-bits?)
360 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
362 #define TestlyxstringInvariant(s)
363 #endif //DEVEL_VERSION
366 ///////////////////////////////////////
367 // Constructors and Deconstructors.
368 ///////////////////////////////////////
370 lyxstring::size_type const lyxstring::npos = static_cast<lyxstring::size_type>(-1);
372 lyxstring::lyxstring()
374 static Srep empty_rep(0, "");
380 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
382 Assert(pos < x.rep->sz || pos == 0);
383 if (pos == 0 && n >= x.length()) { // this is the default
387 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
392 lyxstring::lyxstring(value_type const * s, size_type n)
394 Assert(s); // we don't allow null pointers
395 static Srep empty_rep(0, "");
396 if (*s && n) { // s is not empty string and n > 0
397 rep = new Srep(min(strlen(s), n), s);
405 lyxstring::lyxstring(value_type const * s)
407 Assert(s); // we don't allow null pointers
408 static Srep empty_rep(0, "");
409 if (*s) { // s is not empty string
410 rep = new Srep(strlen(s), s);
418 lyxstring::lyxstring(size_type n, value_type c)
420 rep = new Srep(n, c);
424 lyxstring::lyxstring(iterator first, iterator last)
426 rep = new Srep(last - first, first);
430 lyxstring::~lyxstring()
432 if (--rep->ref == 0) delete rep;
435 ///////////////////////
437 ///////////////////////
439 lyxstring::iterator lyxstring::begin()
445 lyxstring::const_iterator lyxstring::begin() const
451 lyxstring::iterator lyxstring::end()
453 return rep->s + rep->sz;
457 lyxstring::const_iterator lyxstring::end() const
459 return rep->s + rep->sz;
463 reverse_iterator lyxstring::rbegin()
465 return reverse_iterator( end() );
469 const_reverse_iterator lyxstring::rbegin() const
471 return const_reverse_iterator( end() );
475 reverse_iterator lyxstring::rend()
477 return reverse_iterator( begin() );
481 const_reverse_iterator lyxstring::rend() const
483 return const_reverse_iterator( begin() );
488 ///////////////////////
490 ///////////////////////
492 lyxstring::size_type lyxstring::size() const
498 void lyxstring::resize(size_type n, value_type c)
500 TestlyxstringInvariant(this);
502 // This resets sz to res_arg
503 rep = rep->get_own_copy();
508 lyxstring::size_type lyxstring::capacity() const
514 void lyxstring::reserve(size_type res_arg)
516 TestlyxstringInvariant(this);
518 rep = rep->get_own_copy();
519 rep->reserve(res_arg);
527 lyxstring & lyxstring::operator=(lyxstring const & x)
529 TestlyxstringInvariant(this);
535 lyxstring & lyxstring::operator=(value_type const * s)
538 TestlyxstringInvariant(this);
539 // printf("lyxstring::operator=(value_type const *)\n");
545 lyxstring & lyxstring::operator=(value_type c)
547 TestlyxstringInvariant(this);
551 if (rep->ref == 1) // recycle rep
555 rep = new Srep(1, s);
561 lyxstring & lyxstring::assign(lyxstring const & x)
563 TestlyxstringInvariant(this);
565 x.rep->ref++; // protect against ``st = st''
566 if (--rep->ref == 0) delete rep;
567 rep = x.rep; // share representation
572 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
574 TestlyxstringInvariant(this);
576 return assign(x.substr(pos, n));
580 lyxstring & lyxstring::assign(value_type const * s, size_type n)
583 TestlyxstringInvariant(this);
585 n = min(strlen(s), n);
586 if (rep->ref == 1) // recycle rep
590 rep = new Srep(n, s);
596 lyxstring & lyxstring::assign(value_type const * s)
599 TestlyxstringInvariant(this);
601 return assign(s, strlen(s));
605 lyxstring & lyxstring::assign(size_type n, value_type ch)
607 TestlyxstringInvariant(this);
609 rep = rep->get_own_copy();
615 lyxstring & lyxstring::assign(iterator first, iterator last)
617 TestlyxstringInvariant(this);
619 rep = rep->get_own_copy();
620 rep->assign(last - first, first);
629 lyxstring::const_reference lyxstring::operator[](size_type pos) const
631 Assert(pos < rep->sz);
636 lyxstring::reference lyxstring::operator[](size_type pos)
638 Assert(pos < rep->sz);
639 TestlyxstringInvariant(this);
641 rep = rep->get_own_copy();
646 lyxstring::const_reference lyxstring::at(size_type n) const
653 lyxstring::reference lyxstring::at(size_type n)
656 TestlyxstringInvariant(this);
658 rep = rep->get_own_copy();
667 lyxstring & lyxstring::operator+=(lyxstring const & x)
669 TestlyxstringInvariant(this);
675 lyxstring & lyxstring::operator+=(value_type const * x)
678 TestlyxstringInvariant(this);
684 lyxstring & lyxstring::operator+=(value_type c)
686 TestlyxstringInvariant(this);
693 void lyxstring::push_back(value_type c)
695 TestlyxstringInvariant(this);
697 rep = rep->get_own_copy();
702 lyxstring & lyxstring::append(lyxstring const & x)
704 TestlyxstringInvariant(this);
706 if (x.empty()) return *this;
707 rep = rep->get_own_copy();
708 rep->append(x.length(), x.rep->s);
713 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
715 TestlyxstringInvariant(this);
717 return append(x.substr(pos, n));
721 lyxstring & lyxstring::append(value_type const * p, size_type n)
724 TestlyxstringInvariant(this);
726 if (!*p || !n) return *this;
727 rep = rep->get_own_copy();
728 rep->append(min(n, strlen(p)), p);
733 lyxstring & lyxstring::append(value_type const * p)
736 TestlyxstringInvariant(this);
738 if (!*p) return *this;
739 rep = rep->get_own_copy();
740 rep->append(strlen(p), p);
745 lyxstring & lyxstring::append(size_type n, value_type c)
747 TestlyxstringInvariant(this);
749 value_type * tmp = new value_type[n];
751 rep = rep->get_own_copy();
758 lyxstring & lyxstring::append(iterator first, iterator last)
760 TestlyxstringInvariant(this);
762 rep = rep->get_own_copy();
763 rep->append(last - first, first);
767 // insert value_typeacters before (*this)[pos]
769 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
771 TestlyxstringInvariant(this);
773 return insert(pos, x, 0, x.rep->sz);
777 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
778 size_type pos2, size_type n)
780 TestlyxstringInvariant(this);
782 rep = rep->get_own_copy();
783 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
788 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
791 TestlyxstringInvariant(this);
794 // insert nothing and you change nothing
795 rep = rep->get_own_copy();
796 rep->insert(pos, p, min(n, strlen(p)));
802 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
805 TestlyxstringInvariant(this);
808 // insert nothing and you change nothing
809 rep = rep->get_own_copy();
810 rep->insert(pos, p, strlen(p));
816 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
818 TestlyxstringInvariant(this);
820 rep = rep->get_own_copy();
821 value_type * tmp = new value_type[n];
823 rep->insert(pos, tmp, n);
829 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
831 TestlyxstringInvariant(this);
833 // what iterator is this supposed to return??
834 size_type tmp = p - begin();
835 insert(p - begin(), 1, c);
836 return begin() + tmp + 1; // ??
840 void lyxstring::insert(iterator p, size_type n , value_type c)
842 TestlyxstringInvariant(this);
844 insert(p - begin(), n , c);
848 void lyxstring::insert(iterator p, iterator first, iterator last)
850 TestlyxstringInvariant(this);
852 insert(p - begin(), first, last - first);
860 // All the below find functions should be verified,
861 // it is very likely that I have mixed up or interpreted
862 // some of the parameters wrong, also some of the funcs can surely
863 // be written more effectively.
865 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
867 if (!rep->sz) return npos;
870 TestlyxstringInvariant(this);
872 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
873 // search until (*this)[i] == a[0]
874 if (rep->s[t] == a[0]) {
875 // check if the rest of the value_types match
877 for (size_type j = 0; j < a.length(); ++j) {
878 if (rep->s[t + j] != a[j]) {
890 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
894 if (!rep->sz || !*ptr) return npos;
897 TestlyxstringInvariant(this);
899 // What is "n" here? is it the number of value_types to use in ptr
900 // or does "i" and "n" togeter form a substring to search
901 // for ptr in? For now I will assume that "n" tells the length
903 n = min(n, strlen(ptr));
904 for (size_type t = i; length() - t >= n; ++t) {
905 // search until (*this)[i] == a[0]
906 if (rep->s[t] == ptr[0]) {
907 // check if the rest of the value_types match
909 for (size_type j = 0; j < n; ++j) {
910 if (rep->s[t + j] != ptr[j]) {
922 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
925 if (!rep->sz) return npos;
928 TestlyxstringInvariant(this);
930 if (!s || !*s) return npos;
931 return find(s, i, strlen(s));
935 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
937 if (!rep->sz) return npos;
940 TestlyxstringInvariant(this);
942 for (size_type t = 0; t + i < length(); ++t) {
943 if (rep->s[t + i] == c) return t + i;
949 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
951 TestlyxstringInvariant(this);
953 size_type ii = min(length() - 1, i);
955 if (a[a.length() - 1] == rep->s[ii]) {
956 int t = length() - 2;
957 size_type l = ii - 1;
958 for (; t >= 0; --t, --l) {
959 if (a[t] != rep->s[l]) break;
961 if (a[t] == rep->s[l]) return l;
968 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
972 TestlyxstringInvariant(this);
973 if (!*ptr) return npos;
975 size_type ii = min(length() - 1, i);
977 if (ptr[n - 1] == rep->s[ii]) {
979 size_type l = ii - 1;
980 for (; t >= 0; --t, --l) {
981 if (ptr[t] != rep->s[l]) break;
983 if (ptr[t] == rep->s[l]) return l;
990 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
993 TestlyxstringInvariant(this);
994 if (!*ptr) return npos;
996 size_type ii = min(length() - 1, i);
998 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
999 int t = strlen(ptr) - 2;
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 c, size_type i) const
1013 TestlyxstringInvariant(this);
1015 size_type ii = min(length() - 1, i);
1016 for (size_type t = ii; t != 0; --t) {
1017 if (rep->s[t] == c) return t;
1023 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1026 Assert(i < rep->sz);
1027 TestlyxstringInvariant(this);
1029 for (size_type t = i; t < length(); ++t) {
1030 if (a.find(rep->s[t]) != npos) return t;
1036 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1039 Assert(ptr && i < rep->sz);
1040 TestlyxstringInvariant(this);
1041 if (!n) return npos;
1043 for (size_type t = i; t < length(); ++t) {
1044 if(memchr(ptr, rep->s[t], n) != 0) return t;
1050 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1053 Assert(ptr && i < rep->sz);
1054 TestlyxstringInvariant(this);
1056 for (size_type t = i; t < length(); ++t) {
1057 if (strchr(ptr, rep->s[t]) != 0) return t;
1063 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1065 Assert(i < rep->sz);
1066 TestlyxstringInvariant(this);
1068 for (size_type t = i; t < length(); ++t) {
1069 if (rep->s[t] == c) return t;
1075 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1078 TestlyxstringInvariant(this);
1080 size_type ii = min(length() - 1, i);
1081 for (int t = ii; t >= 0; --t) {
1082 if (a.find(rep->s[t]) != npos) return t;
1087 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1091 TestlyxstringInvariant(this);
1092 if (!n) return npos;
1094 size_type ii = min(length() - 1, i);
1095 for (int t = ii; t >= 0; --t) {
1096 if(memchr(ptr, rep->s[t], n) != 0) return t;
1102 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1106 TestlyxstringInvariant(this);
1108 size_type ii = min(length() - 1, i);
1109 for (int t = ii; t >= 0; --t) {
1110 if (strchr(ptr, rep->s[t]) != 0) return t;
1116 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1118 TestlyxstringInvariant(this);
1120 if (!rep->sz) return npos;
1121 size_type ii = min(length() - 1, i);
1122 for (int t = ii; t >= 0; --t) {
1123 if (rep->s[t] == c) return t;
1129 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1132 TestlyxstringInvariant(this);
1134 if (!rep->sz) return npos;
1135 Assert(i < rep->sz);
1136 for (size_type t = i; t < length(); ++t) {
1137 if (a.find(rep->s[t]) == npos) return t;
1143 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1146 Assert(ptr && i < rep->sz);
1147 TestlyxstringInvariant(this);
1149 if (!n) return (i < length()) ? i : npos;
1150 for (size_type t = i; t < length(); ++t) {
1151 if(memchr(ptr, rep->s[t], n) == 0) return t;
1157 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1160 Assert(ptr && i < rep->sz);
1161 TestlyxstringInvariant(this);
1163 for (size_type t = i; t < length(); ++t) {
1164 if (strchr(ptr, rep->s[t]) == 0) return t;
1170 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1173 if (!rep->sz) return npos;
1174 Assert(i < rep->sz);
1175 TestlyxstringInvariant(this);
1177 for (size_type t = i; t < length(); ++t) {
1178 if (rep->s[t] != c) return t;
1184 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1187 TestlyxstringInvariant(this);
1189 size_type ii = min(length() - 1, i);
1190 for (int t = ii; t >= 0; --t) {
1191 if (a.find(rep->s[t]) == npos) return t;
1197 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1202 TestlyxstringInvariant(this);
1204 if (!n) return npos;
1205 size_type ii = min(length() - 1, i);
1206 //if (!n) return (ii >= 0) ? ii : npos;
1207 for (int t = ii; t >= 0; --t) {
1208 if(memchr(ptr, rep->s[t], n) == 0) return t;
1214 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1218 TestlyxstringInvariant(this);
1220 size_type ii = min(length() - 1, i);
1221 for (int t = ii; t >= 0; --t) {
1222 if (strchr(ptr, rep->s[t]) == 0) return t;
1228 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1231 TestlyxstringInvariant(this);
1233 size_type ii = min(length() - 1, i);
1234 for (int t = ii; t >= 0; --t) {
1235 if (rep->s[t] != c) return t;
1245 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1247 Assert(i < rep->sz || i == 0);
1248 TestlyxstringInvariant(this);
1250 return replace(i, n, x, 0, x.length());
1254 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1255 size_type i2, size_type n2)
1257 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1258 TestlyxstringInvariant(this);
1260 rep = rep->get_own_copy();
1261 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1266 lyxstring & lyxstring::replace(size_type i, size_type n,
1267 value_type const * p, size_type n2)
1269 Assert(p && i < rep->sz);
1270 TestlyxstringInvariant(this);
1272 rep = rep->get_own_copy();
1273 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1278 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1280 Assert(p && i < rep->sz);
1281 TestlyxstringInvariant(this);
1283 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1287 lyxstring & lyxstring::replace(size_type i, size_type n,
1288 size_type n2, value_type c)
1290 Assert(i < rep->sz);
1291 TestlyxstringInvariant(this);
1293 rep = rep->get_own_copy();
1294 value_type * tmp = new value_type[n2];
1296 rep->replace(i, min(n, rep->sz), tmp, n2);
1302 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1304 TestlyxstringInvariant(this);
1306 return replace(i - begin(), i2 - i, str);
1310 lyxstring & lyxstring::replace(iterator i, iterator i2,
1311 value_type const * p, size_type n)
1314 TestlyxstringInvariant(this);
1316 return replace(i - begin(), i2 - i, p, n);
1320 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1323 TestlyxstringInvariant(this);
1325 return replace(i - begin(), i2 - i, p);
1329 lyxstring & lyxstring::replace(iterator i, iterator i2,
1330 size_type n , value_type c)
1332 TestlyxstringInvariant(this);
1334 return replace(i - begin(), i2 - i, n, c);
1338 lyxstring & lyxstring::replace(iterator i, iterator i2,
1339 iterator j, iterator j2)
1341 TestlyxstringInvariant(this);
1343 return replace(i - begin(), i2 - i, j, j2 - j);
1347 lyxstring & lyxstring::erase(size_type i, size_type n)
1349 Assert(i < rep->sz || i == 0);
1350 TestlyxstringInvariant(this);
1352 rep = rep->get_own_copy();
1353 if (i == 0 && n >= rep->sz) {
1356 n = min(n, rep->sz - i);
1357 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1364 lyxstring::iterator lyxstring::erase(iterator i)
1366 TestlyxstringInvariant(this);
1368 // what iterator is this supposed to return?
1369 // the iterator after the one erased
1370 erase(i - begin(), 1);
1371 return begin(); // BUG
1375 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1377 TestlyxstringInvariant(this);
1379 erase(first - begin(), last - first);
1380 return begin(); // BUG
1384 /////////////////////////////////////
1385 // Conversion to C-style Strings
1386 /////////////////////////////////////
1388 lyxstring::value_type const * lyxstring::c_str() const
1390 rep->s[length()] = '\0';
1395 lyxstring::value_type const * lyxstring::data() const
1401 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1402 size_type pos) const
1405 TestlyxstringInvariant(this);
1407 register int nn = min(len, length() - pos);
1408 memcpy(buf, &(rep->s[pos]), nn);
1413 ////////////////////
1415 ////////////////////
1417 // Compare funcs should be verified.
1418 // Should we try to make them work with '\0' value_types?
1419 // An STL string can usually contain '\0' value_types.
1421 int lyxstring::compare(lyxstring const & str) const
1423 TestlyxstringInvariant(this);
1425 return compare(0, rep->sz, str.rep->s, str.rep->sz);
1429 int lyxstring::compare(value_type const * s) const
1432 TestlyxstringInvariant(this);
1434 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1438 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1440 TestlyxstringInvariant(this);
1442 return compare(pos, n, str.rep->s, str.rep->sz);
1446 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1447 size_type pos2, size_type n2) const
1449 TestlyxstringInvariant(this);
1451 return compare(pos, n, str.rep->s + pos2, n2);
1455 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1458 Assert(s && (pos < rep->sz || pos == 0));
1459 TestlyxstringInvariant(this);
1461 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1463 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1465 // remember that n can very well be a lot larger than rep->sz
1466 // so we have to ensure that n is no larger than rep->sz
1467 n = min(n, rep->sz);
1468 n2 = min(n2, strlen(s));
1470 return memcmp(&(rep->s[pos]), s, n);
1471 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1484 // i = index, n = length
1485 lyxstring lyxstring::substr(size_type i, size_type n) const
1487 Assert(i < rep->sz || i == 0);
1488 TestlyxstringInvariant(this);
1490 return lyxstring(*this, i, n);
1494 /////////////////////////////////////////////
1495 // String operators, non member functions
1496 /////////////////////////////////////////////
1498 bool operator==(lyxstring const & a, lyxstring const & b)
1500 return a.compare(b) == 0;
1504 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1507 return b.compare(a) == 0;
1511 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1514 return a.compare(b) == 0;
1518 bool operator!=(lyxstring const & a, lyxstring const & b)
1520 return a.compare(b) != 0;
1524 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1527 return b.compare(a) != 0;
1531 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1534 return a.compare(b) != 0;
1538 bool operator>(lyxstring const & a, lyxstring const & b)
1540 return a.compare(b) > 0;
1544 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1547 return b.compare(a) < 0; // since we reverse the parameters
1551 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1554 return a.compare(b) > 0;
1558 bool operator<(lyxstring const & a, lyxstring const & b)
1560 return a.compare(b) < 0;
1564 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1567 return b.compare(a) > 0; // since we reverse the parameters
1571 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1574 return a.compare(b) < 0;
1578 bool operator>=(lyxstring const & a, lyxstring const & b)
1580 return a.compare(b) >= 0;
1584 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1587 return b.compare(a) <= 0; // since we reverse the parameters
1591 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1594 return a.compare(b) >= 0;
1598 bool operator<=(lyxstring const & a, lyxstring const & b)
1600 return a.compare(b) <= 0;
1604 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1607 return b.compare(a) >= 0; // since we reverse the parameters
1611 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1614 return a.compare(b) <= 0;
1618 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1626 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1635 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1644 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1653 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1662 istream & operator>>(istream & is, lyxstring & s)
1664 // very bad solution
1665 char * nome = new char[1024];
1667 lyxstring tmp(nome);
1669 if (!tmp.empty()) s = tmp;
1673 ostream & operator<<(ostream & o, lyxstring const & s)
1675 return o.write(s.data(), s.length());
1678 istream & getline(istream & is, lyxstring & s,
1679 lyxstring::value_type delim)
1681 // very bad solution