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 || i >= rep->sz) return npos;
869 TestlyxstringInvariant(this);
871 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
872 // search until (*this)[i] == a[0]
873 if (rep->s[t] == a[0]) {
874 // check if the rest of the value_types match
876 for (size_type j = 0; j < a.length(); ++j) {
877 if (rep->s[t + j] != a[j]) {
889 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
893 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
895 TestlyxstringInvariant(this);
897 // What is "n" here? is it the number of value_types to use in ptr
898 // or does "i" and "n" togeter form a substring to search
899 // for ptr in? For now I will assume that "n" tells the length
901 n = min(n, strlen(ptr));
902 for (size_type t = i; rep->sz - t >= n; ++t) {
903 // search until (*this)[i] == a[0]
904 if (rep->s[t] == ptr[0]) {
905 // check if the rest of the value_types match
907 for (size_type j = 0; j < n; ++j) {
908 if (rep->s[t + j] != ptr[j]) {
920 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
923 if (!rep->sz || i >= rep->sz) return npos;
925 TestlyxstringInvariant(this);
927 if (!s || !*s) return npos;
928 return find(s, i, strlen(s));
932 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
934 if (!rep->sz || i >= rep->sz) return npos;
936 TestlyxstringInvariant(this);
938 for (size_type t = 0; t + i < rep->sz; ++t) {
939 if (rep->s[t + i] == c) return t + i;
945 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
947 TestlyxstringInvariant(this);
949 size_type ii = min(rep->sz - 1, i);
951 if (a[a.length() - 1] == rep->s[ii]) {
953 size_type l = ii - 1;
954 for (; t >= 0; --t, --l) {
955 if (a[t] != rep->s[l]) break;
957 if (a[t] == rep->s[l]) return l;
964 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
968 TestlyxstringInvariant(this);
969 if (!*ptr) return npos;
971 size_type ii = min(rep->sz - 1, i);
973 if (ptr[n - 1] == rep->s[ii]) {
975 size_type l = ii - 1;
976 for (; t >= 0; --t, --l) {
977 if (ptr[t] != rep->s[l]) break;
979 if (ptr[t] == rep->s[l]) return l;
986 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
989 TestlyxstringInvariant(this);
990 if (!*ptr) return npos;
992 size_type ii = min(rep->sz - 1, i);
994 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
995 int t = strlen(ptr) - 2;
996 size_type l = ii - 1;
997 for (; t >= 0; --t, --l) {
998 if (ptr[t] != rep->s[l]) break;
1000 if (ptr[t] == rep->s[l]) return l;
1007 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1009 TestlyxstringInvariant(this);
1011 size_type ii = min(rep->sz - 1, i);
1012 for (size_type t = ii; t != 0; --t) {
1013 if (rep->s[t] == c) return t;
1019 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1022 Assert(i < rep->sz);
1023 TestlyxstringInvariant(this);
1025 for (size_type t = i; t < rep->sz; ++t) {
1026 if (a.find(rep->s[t]) != npos) return t;
1032 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1035 Assert(ptr && i < rep->sz);
1036 TestlyxstringInvariant(this);
1037 if (!n) return npos;
1039 for (size_type t = i; t < rep->sz; ++t) {
1040 if(memchr(ptr, rep->s[t], n) != 0) return t;
1046 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1049 Assert(ptr && i < rep->sz);
1050 TestlyxstringInvariant(this);
1052 for (size_type t = i; t < rep->sz; ++t) {
1053 if (strchr(ptr, rep->s[t]) != 0) return t;
1059 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1061 Assert(i < rep->sz);
1062 TestlyxstringInvariant(this);
1064 for (size_type t = i; t < rep->sz; ++t) {
1065 if (rep->s[t] == c) return t;
1071 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1074 TestlyxstringInvariant(this);
1076 size_type ii = min(rep->sz - 1, i);
1077 for (int t = ii; t >= 0; --t) {
1078 if (a.find(rep->s[t]) != npos) return t;
1083 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1087 TestlyxstringInvariant(this);
1088 if (!n) return npos;
1090 size_type ii = min(rep->sz - 1, i);
1091 for (int t = ii; t >= 0; --t) {
1092 if(memchr(ptr, rep->s[t], n) != 0) return t;
1098 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1102 TestlyxstringInvariant(this);
1104 size_type ii = min(rep->sz - 1, i);
1105 for (int t = ii; t >= 0; --t) {
1106 if (strchr(ptr, rep->s[t]) != 0) return t;
1112 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1114 TestlyxstringInvariant(this);
1116 if (!rep->sz) return npos;
1117 size_type ii = min(rep->sz - 1, i);
1118 for (int t = ii; t >= 0; --t) {
1119 if (rep->s[t] == c) return t;
1125 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1128 TestlyxstringInvariant(this);
1130 if (!rep->sz) return npos;
1131 Assert(i < rep->sz);
1132 for (size_type t = i; t < rep->sz; ++t) {
1133 if (a.find(rep->s[t]) == npos) return t;
1139 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1143 Assert(ptr && i < rep->sz);
1144 TestlyxstringInvariant(this);
1146 if (!n) return (i < rep->sz) ? i : npos;
1147 for (size_type t = i; t < rep->sz; ++t) {
1148 if(memchr(ptr, rep->s[t], n) == 0) return t;
1154 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1157 Assert(ptr && i < rep->sz);
1158 TestlyxstringInvariant(this);
1160 for (size_type t = i; t < rep->sz; ++t) {
1161 if (strchr(ptr, rep->s[t]) == 0) return t;
1167 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1170 if (!rep->sz) return npos;
1171 Assert(i < rep->sz);
1172 TestlyxstringInvariant(this);
1174 for (size_type t = i; t < rep->sz; ++t) {
1175 if (rep->s[t] != c) return t;
1181 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1184 TestlyxstringInvariant(this);
1186 size_type ii = min(rep->sz - 1, i);
1187 for (int t = ii; t >= 0; --t) {
1188 if (a.find(rep->s[t]) == npos) return t;
1194 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1199 TestlyxstringInvariant(this);
1201 if (!n) return npos;
1202 size_type ii = min(rep->sz - 1, i);
1204 for (int t = ii; t >= 0; --t) {
1205 if(memchr(ptr, rep->s[t], n) == 0) return t;
1211 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1215 TestlyxstringInvariant(this);
1217 size_type ii = min(rep->sz - 1, i);
1218 for (int t = ii; t >= 0; --t) {
1219 if (strchr(ptr, rep->s[t]) == 0) return t;
1225 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1228 TestlyxstringInvariant(this);
1230 size_type ii = min(rep->sz - 1, i);
1231 for (int t = ii; t >= 0; --t) {
1232 if (rep->s[t] != c) return t;
1242 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1244 Assert(i < rep->sz || i == 0);
1245 TestlyxstringInvariant(this);
1247 return replace(i, n, x, 0, x.length());
1251 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1252 size_type i2, size_type n2)
1254 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1255 TestlyxstringInvariant(this);
1257 rep = rep->get_own_copy();
1258 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1263 lyxstring & lyxstring::replace(size_type i, size_type n,
1264 value_type const * p, size_type n2)
1266 Assert(p && i < rep->sz);
1267 TestlyxstringInvariant(this);
1269 rep = rep->get_own_copy();
1270 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1275 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1277 Assert(p && i < rep->sz);
1278 TestlyxstringInvariant(this);
1280 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1284 lyxstring & lyxstring::replace(size_type i, size_type n,
1285 size_type n2, value_type c)
1287 Assert(i < rep->sz);
1288 TestlyxstringInvariant(this);
1290 rep = rep->get_own_copy();
1291 value_type * tmp = new value_type[n2];
1293 rep->replace(i, min(n, rep->sz), tmp, n2);
1299 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1301 TestlyxstringInvariant(this);
1303 return replace(i - begin(), i2 - i, str);
1307 lyxstring & lyxstring::replace(iterator i, iterator i2,
1308 value_type const * p, size_type n)
1311 TestlyxstringInvariant(this);
1313 return replace(i - begin(), i2 - i, p, n);
1317 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1320 TestlyxstringInvariant(this);
1322 return replace(i - begin(), i2 - i, p);
1326 lyxstring & lyxstring::replace(iterator i, iterator i2,
1327 size_type n , value_type c)
1329 TestlyxstringInvariant(this);
1331 return replace(i - begin(), i2 - i, n, c);
1335 lyxstring & lyxstring::replace(iterator i, iterator i2,
1336 iterator j, iterator j2)
1338 TestlyxstringInvariant(this);
1340 return replace(i - begin(), i2 - i, j, j2 - j);
1344 lyxstring & lyxstring::erase(size_type i, size_type n)
1346 Assert(i < rep->sz || i == 0);
1347 TestlyxstringInvariant(this);
1349 rep = rep->get_own_copy();
1350 if (i == 0 && n >= rep->sz) {
1353 n = min(n, rep->sz - i);
1354 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1361 lyxstring::iterator lyxstring::erase(iterator i)
1363 TestlyxstringInvariant(this);
1365 // what iterator is this supposed to return?
1366 // the iterator after the one erased
1367 erase(i - begin(), 1);
1368 return begin(); // BUG
1372 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1374 TestlyxstringInvariant(this);
1376 erase(first - begin(), last - first);
1377 return begin(); // BUG
1381 /////////////////////////////////////
1382 // Conversion to C-style Strings
1383 /////////////////////////////////////
1385 lyxstring::value_type const * lyxstring::c_str() const
1387 rep->s[length()] = '\0';
1392 lyxstring::value_type const * lyxstring::data() const
1398 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1399 size_type pos) const
1402 TestlyxstringInvariant(this);
1404 register int nn = min(len, length() - pos);
1405 memcpy(buf, &(rep->s[pos]), nn);
1410 ////////////////////
1412 ////////////////////
1414 // Compare funcs should be verified.
1415 // Should we try to make them work with '\0' value_types?
1416 // An STL string can usually contain '\0' value_types.
1418 int lyxstring::compare(lyxstring const & str) const
1420 TestlyxstringInvariant(this);
1422 return compare(0, rep->sz, str.c_str(), str.rep->sz);
1426 int lyxstring::compare(value_type const * s) const
1429 TestlyxstringInvariant(this);
1431 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1435 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1437 TestlyxstringInvariant(this);
1439 return compare(pos, n, str.c_str(), str.rep->sz);
1443 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1444 size_type pos2, size_type n2) const
1446 TestlyxstringInvariant(this);
1448 return compare(pos, n, str.c_str() + pos2, n2);
1452 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1455 Assert(s && (pos < rep->sz || pos == 0));
1456 TestlyxstringInvariant(this);
1458 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1460 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1462 // remember that n can very well be a lot larger than rep->sz
1463 // so we have to ensure that n is no larger than rep->sz
1464 n = min(n, rep->sz);
1465 n2 = min(n2, strlen(s));
1467 return memcmp(&(rep->s[pos]), s, n);
1468 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1481 // i = index, n = length
1482 lyxstring lyxstring::substr(size_type i, size_type n) const
1484 Assert(i < rep->sz || i == 0);
1485 TestlyxstringInvariant(this);
1487 return lyxstring(*this, i, n);
1491 /////////////////////////////////////////////
1492 // String operators, non member functions
1493 /////////////////////////////////////////////
1495 bool operator==(lyxstring const & a, lyxstring const & b)
1497 return a.compare(b) == 0;
1501 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1504 return b.compare(a) == 0;
1508 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1511 return a.compare(b) == 0;
1515 bool operator!=(lyxstring const & a, lyxstring const & b)
1517 return a.compare(b) != 0;
1521 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1524 return b.compare(a) != 0;
1528 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1531 return a.compare(b) != 0;
1535 bool operator>(lyxstring const & a, lyxstring const & b)
1537 return a.compare(b) > 0;
1541 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1544 return b.compare(a) < 0; // since we reverse the parameters
1548 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1551 return a.compare(b) > 0;
1555 bool operator<(lyxstring const & a, lyxstring const & b)
1557 return a.compare(b) < 0;
1561 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1564 return b.compare(a) > 0; // since we reverse the parameters
1568 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1571 return a.compare(b) < 0;
1575 bool operator>=(lyxstring const & a, lyxstring const & b)
1577 return a.compare(b) >= 0;
1581 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1584 return b.compare(a) <= 0; // since we reverse the parameters
1588 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1591 return a.compare(b) >= 0;
1595 bool operator<=(lyxstring const & a, lyxstring const & b)
1597 return a.compare(b) <= 0;
1601 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1604 return b.compare(a) >= 0; // since we reverse the parameters
1608 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1611 return a.compare(b) <= 0;
1615 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1623 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1632 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1641 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1650 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1659 istream & operator>>(istream & is, lyxstring & s)
1661 // very bad solution
1662 char * nome = new char[1024];
1664 lyxstring tmp(nome);
1666 if (!tmp.empty()) s = tmp;
1670 ostream & operator<<(ostream & o, lyxstring const & s)
1672 return o.write(s.data(), s.length());
1675 istream & getline(istream & is, lyxstring & s,
1676 lyxstring::value_type delim)
1678 // very bad solution