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) return npos;
896 TestlyxstringInvariant(this);
898 // What is "n" here? is it the number of value_types to use in ptr
899 // or does "i" and "n" togeter form a substring to search
900 // for ptr in? For now I will assume that "n" tells the length
902 n = min(n, strlen(ptr));
903 for (size_type t = i; length() - t >= n; ++t) {
904 // search until (*this)[i] == a[0]
905 if (rep->s[t] == ptr[0]) {
906 // check if the rest of the value_types match
908 for (size_type j = 0; j < n; ++j) {
909 if (rep->s[t + j] != ptr[j]) {
921 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
924 if (!rep->sz || i >= rep->sz) return npos;
926 TestlyxstringInvariant(this);
928 if (!s || !*s) return npos;
929 return find(s, i, strlen(s));
933 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
935 if (!rep->sz) return npos;
938 TestlyxstringInvariant(this);
940 for (size_type t = 0; t + i < length(); ++t) {
941 if (rep->s[t + i] == c) return t + i;
947 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
949 TestlyxstringInvariant(this);
951 size_type ii = min(length() - 1, i);
953 if (a[a.length() - 1] == rep->s[ii]) {
954 int t = length() - 2;
955 size_type l = ii - 1;
956 for (; t >= 0; --t, --l) {
957 if (a[t] != rep->s[l]) break;
959 if (a[t] == rep->s[l]) return l;
966 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
970 TestlyxstringInvariant(this);
971 if (!*ptr) return npos;
973 size_type ii = min(length() - 1, i);
975 if (ptr[n - 1] == rep->s[ii]) {
977 size_type l = ii - 1;
978 for (; t >= 0; --t, --l) {
979 if (ptr[t] != rep->s[l]) break;
981 if (ptr[t] == rep->s[l]) return l;
988 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
991 TestlyxstringInvariant(this);
992 if (!*ptr) return npos;
994 size_type ii = min(length() - 1, i);
996 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
997 int t = strlen(ptr) - 2;
998 size_type l = ii - 1;
999 for (; t >= 0; --t, --l) {
1000 if (ptr[t] != rep->s[l]) break;
1002 if (ptr[t] == rep->s[l]) return l;
1009 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1011 TestlyxstringInvariant(this);
1013 size_type ii = min(length() - 1, i);
1014 for (size_type t = ii; t != 0; --t) {
1015 if (rep->s[t] == c) return t;
1021 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1024 Assert(i < rep->sz);
1025 TestlyxstringInvariant(this);
1027 for (size_type t = i; t < length(); ++t) {
1028 if (a.find(rep->s[t]) != npos) return t;
1034 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1037 Assert(ptr && i < rep->sz);
1038 TestlyxstringInvariant(this);
1039 if (!n) return npos;
1041 for (size_type t = i; t < length(); ++t) {
1042 if(memchr(ptr, rep->s[t], n) != 0) return t;
1048 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1051 Assert(ptr && i < rep->sz);
1052 TestlyxstringInvariant(this);
1054 for (size_type t = i; t < length(); ++t) {
1055 if (strchr(ptr, rep->s[t]) != 0) return t;
1061 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1063 Assert(i < rep->sz);
1064 TestlyxstringInvariant(this);
1066 for (size_type t = i; t < length(); ++t) {
1067 if (rep->s[t] == c) return t;
1073 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1076 TestlyxstringInvariant(this);
1078 size_type ii = min(length() - 1, i);
1079 for (int t = ii; t >= 0; --t) {
1080 if (a.find(rep->s[t]) != npos) return t;
1085 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1089 TestlyxstringInvariant(this);
1090 if (!n) return npos;
1092 size_type ii = min(length() - 1, i);
1093 for (int t = ii; t >= 0; --t) {
1094 if(memchr(ptr, rep->s[t], n) != 0) return t;
1100 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1104 TestlyxstringInvariant(this);
1106 size_type ii = min(length() - 1, i);
1107 for (int t = ii; t >= 0; --t) {
1108 if (strchr(ptr, rep->s[t]) != 0) return t;
1114 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1116 TestlyxstringInvariant(this);
1118 if (!rep->sz) return npos;
1119 size_type ii = min(length() - 1, i);
1120 for (int t = ii; t >= 0; --t) {
1121 if (rep->s[t] == c) return t;
1127 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1130 TestlyxstringInvariant(this);
1132 if (!rep->sz) return npos;
1133 Assert(i < rep->sz);
1134 for (size_type t = i; t < length(); ++t) {
1135 if (a.find(rep->s[t]) == npos) return t;
1141 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1144 Assert(ptr && i < rep->sz);
1145 TestlyxstringInvariant(this);
1147 if (!n) return (i < length()) ? i : npos;
1148 for (size_type t = i; t < length(); ++t) {
1149 if(memchr(ptr, rep->s[t], n) == 0) return t;
1155 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1158 Assert(ptr && i < rep->sz);
1159 TestlyxstringInvariant(this);
1161 for (size_type t = i; t < length(); ++t) {
1162 if (strchr(ptr, rep->s[t]) == 0) return t;
1168 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1171 if (!rep->sz) return npos;
1172 Assert(i < rep->sz);
1173 TestlyxstringInvariant(this);
1175 for (size_type t = i; t < length(); ++t) {
1176 if (rep->s[t] != c) return t;
1182 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1185 TestlyxstringInvariant(this);
1187 size_type ii = min(length() - 1, i);
1188 for (int t = ii; t >= 0; --t) {
1189 if (a.find(rep->s[t]) == npos) return t;
1195 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1200 TestlyxstringInvariant(this);
1202 if (!n) return npos;
1203 size_type ii = min(length() - 1, i);
1204 //if (!n) return (ii >= 0) ? ii : npos;
1205 for (int t = ii; t >= 0; --t) {
1206 if(memchr(ptr, rep->s[t], n) == 0) return t;
1212 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1216 TestlyxstringInvariant(this);
1218 size_type ii = min(length() - 1, i);
1219 for (int t = ii; t >= 0; --t) {
1220 if (strchr(ptr, rep->s[t]) == 0) return t;
1226 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1229 TestlyxstringInvariant(this);
1231 size_type ii = min(length() - 1, i);
1232 for (int t = ii; t >= 0; --t) {
1233 if (rep->s[t] != c) return t;
1243 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1245 Assert(i < rep->sz || i == 0);
1246 TestlyxstringInvariant(this);
1248 return replace(i, n, x, 0, x.length());
1252 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1253 size_type i2, size_type n2)
1255 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1256 TestlyxstringInvariant(this);
1258 rep = rep->get_own_copy();
1259 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1264 lyxstring & lyxstring::replace(size_type i, size_type n,
1265 value_type const * p, size_type n2)
1267 Assert(p && i < rep->sz);
1268 TestlyxstringInvariant(this);
1270 rep = rep->get_own_copy();
1271 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1276 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1278 Assert(p && i < rep->sz);
1279 TestlyxstringInvariant(this);
1281 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1285 lyxstring & lyxstring::replace(size_type i, size_type n,
1286 size_type n2, value_type c)
1288 Assert(i < rep->sz);
1289 TestlyxstringInvariant(this);
1291 rep = rep->get_own_copy();
1292 value_type * tmp = new value_type[n2];
1294 rep->replace(i, min(n, rep->sz), tmp, n2);
1300 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1302 TestlyxstringInvariant(this);
1304 return replace(i - begin(), i2 - i, str);
1308 lyxstring & lyxstring::replace(iterator i, iterator i2,
1309 value_type const * p, size_type n)
1312 TestlyxstringInvariant(this);
1314 return replace(i - begin(), i2 - i, p, n);
1318 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1321 TestlyxstringInvariant(this);
1323 return replace(i - begin(), i2 - i, p);
1327 lyxstring & lyxstring::replace(iterator i, iterator i2,
1328 size_type n , value_type c)
1330 TestlyxstringInvariant(this);
1332 return replace(i - begin(), i2 - i, n, c);
1336 lyxstring & lyxstring::replace(iterator i, iterator i2,
1337 iterator j, iterator j2)
1339 TestlyxstringInvariant(this);
1341 return replace(i - begin(), i2 - i, j, j2 - j);
1345 lyxstring & lyxstring::erase(size_type i, size_type n)
1347 Assert(i < rep->sz || i == 0);
1348 TestlyxstringInvariant(this);
1350 rep = rep->get_own_copy();
1351 if (i == 0 && n >= rep->sz) {
1354 n = min(n, rep->sz - i);
1355 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1362 lyxstring::iterator lyxstring::erase(iterator i)
1364 TestlyxstringInvariant(this);
1366 // what iterator is this supposed to return?
1367 // the iterator after the one erased
1368 erase(i - begin(), 1);
1369 return begin(); // BUG
1373 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1375 TestlyxstringInvariant(this);
1377 erase(first - begin(), last - first);
1378 return begin(); // BUG
1382 /////////////////////////////////////
1383 // Conversion to C-style Strings
1384 /////////////////////////////////////
1386 lyxstring::value_type const * lyxstring::c_str() const
1388 rep->s[length()] = '\0';
1393 lyxstring::value_type const * lyxstring::data() const
1399 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1400 size_type pos) const
1403 TestlyxstringInvariant(this);
1405 register int nn = min(len, length() - pos);
1406 memcpy(buf, &(rep->s[pos]), nn);
1411 ////////////////////
1413 ////////////////////
1415 // Compare funcs should be verified.
1416 // Should we try to make them work with '\0' value_types?
1417 // An STL string can usually contain '\0' value_types.
1419 int lyxstring::compare(lyxstring const & str) const
1421 TestlyxstringInvariant(this);
1423 return compare(0, rep->sz, str.rep->s, str.rep->sz);
1427 int lyxstring::compare(value_type const * s) const
1430 TestlyxstringInvariant(this);
1432 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1436 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1438 TestlyxstringInvariant(this);
1440 return compare(pos, n, str.rep->s, str.rep->sz);
1444 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1445 size_type pos2, size_type n2) const
1447 TestlyxstringInvariant(this);
1449 return compare(pos, n, str.rep->s + pos2, n2);
1453 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1456 Assert(s && (pos < rep->sz || pos == 0));
1457 TestlyxstringInvariant(this);
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);
1466 n2 = min(n2, strlen(s));
1468 return memcmp(&(rep->s[pos]), s, n);
1469 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1482 // i = index, n = length
1483 lyxstring lyxstring::substr(size_type i, size_type n) const
1485 Assert(i < rep->sz || i == 0);
1486 TestlyxstringInvariant(this);
1488 return lyxstring(*this, i, n);
1492 /////////////////////////////////////////////
1493 // String operators, non member functions
1494 /////////////////////////////////////////////
1496 bool operator==(lyxstring const & a, lyxstring const & b)
1498 return a.compare(b) == 0;
1502 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1505 return b.compare(a) == 0;
1509 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1512 return a.compare(b) == 0;
1516 bool operator!=(lyxstring const & a, lyxstring const & b)
1518 return a.compare(b) != 0;
1522 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1525 return b.compare(a) != 0;
1529 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1532 return a.compare(b) != 0;
1536 bool operator>(lyxstring const & a, lyxstring const & b)
1538 return a.compare(b) > 0;
1542 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1545 return b.compare(a) < 0; // since we reverse the parameters
1549 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1552 return a.compare(b) > 0;
1556 bool operator<(lyxstring const & a, lyxstring const & b)
1558 return a.compare(b) < 0;
1562 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1565 return b.compare(a) > 0; // since we reverse the parameters
1569 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1572 return a.compare(b) < 0;
1576 bool operator>=(lyxstring const & a, lyxstring const & b)
1578 return a.compare(b) >= 0;
1582 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1585 return b.compare(a) <= 0; // since we reverse the parameters
1589 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1592 return a.compare(b) >= 0;
1596 bool operator<=(lyxstring const & a, lyxstring const & b)
1598 return a.compare(b) <= 0;
1602 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1605 return b.compare(a) >= 0; // since we reverse the parameters
1609 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1612 return a.compare(b) <= 0;
1616 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1624 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1633 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1642 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1651 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1660 istream & operator>>(istream & is, lyxstring & s)
1662 // very bad solution
1663 char * nome = new char[1024];
1665 lyxstring tmp(nome);
1667 if (!tmp.empty()) s = tmp;
1671 ostream & operator<<(ostream & o, lyxstring const & s)
1673 return o.write(s.data(), s.length());
1676 istream & getline(istream & is, lyxstring & s,
1677 lyxstring::value_type delim)
1679 // very bad solution