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 size_t const xtra = static_cast<size_t>(8);
57 /// The total amount of data reserved for this representaion
59 /// Data. At least 1 char for trailing null.
60 lyxstring::value_type * s;
63 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
65 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
67 ~Srep() { delete[] s; }
71 if (ref == 1) return this;
73 return new Srep(sz, s);
77 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
79 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
81 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
83 void push_back(lyxstring::value_type c);
85 void insert(lyxstring::size_type pos,
86 const lyxstring::value_type * p,
87 lyxstring::size_type n);
89 void resize(lyxstring::size_type n, lyxstring::value_type c);
91 void reserve(lyxstring::size_type res_arg);
93 void replace(lyxstring::size_type i, lyxstring::size_type n,
94 lyxstring::value_type const * p, lyxstring::size_type n2);
97 Srep & operator=(const Srep &);
101 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
103 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
108 s = new value_type[res + 1]; // add space for terminator
110 // if sz = 0 nothing gets copied and we have an error
113 // possibly allows for large but empty string
114 sz = 0; // this line should be redundant
120 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
125 s = new value_type[res + 1]; // add space for terminator
128 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
129 // allows for large but empty string
135 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
137 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
143 s = new value_type[res + 1]; // add space for terminator
148 // if sz = 0 nothing gets copied and we have an error
152 sz = 0; // this line should be redundant
158 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
164 s = new value_type[res + 1]; // add space for terminator
168 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
169 // allows for a large empty string
175 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
177 register unsigned int const len = sz + asz;
180 value_type * tmp = new value_type[res + 1];
182 memcpy(tmp + sz, p, asz);
187 memcpy(s + sz, p, asz);
193 void lyxstring::Srep::push_back(value_type c)
195 s[sz] = c; // it is always room to put a value_type at the end
199 value_type * tmp = new value_type[res + 1];
207 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
208 lyxstring::size_type n)
213 value_type * tmp = new value_type[res + 1];
215 memcpy(tmp + pos, p, n);
216 memcpy(tmp + pos + n, & s[pos], sz - pos);
221 memmove(s + pos + n, & s[pos], sz - pos);
222 memcpy(s + pos, p, n);
228 void lyxstring::Srep::resize(size_type n, value_type c)
231 // This resets sz to res_arg
232 res = min(n, npos - 2); // We keep no xtra when we resize
233 value_type * tmp = new value_type[res + 1];
234 memcpy(tmp, s, min(sz, res));
236 memset(tmp + sz, c, res - sz);
243 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
245 // This keeps the old sz, but
246 // increases res with res_arg
248 value_type * tmp = new value_type[res + 1];
255 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
256 value_type const * p, size_type n2)
258 // can be called with p=0 and n2=0
259 Assert(i < sz && ((!p && !n2) || p));
262 if (res >= n2 + sz) {
263 memmove(s + i + n2, &s[i + n], sz - i);
264 memcpy(s + i, p, n2);
267 res = sz + n2 + xtra;
268 value_type * tmp = new value_type[res + 1];
270 memcpy(tmp + i, p, n2);
271 memcpy(tmp + i + n2, &s[i + n], sz - i);
279 ///////////////////////////////////////
280 // The lyxstring Invariant tester
281 ///////////////////////////////////////
284 /** Testing of the lyxstring invariant
285 * By creating an object that tests the lyxstring invariant during its
286 * construction *and* its deconstruction we greatly simplify our code.
287 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
288 * will test the invariant upon entry to the code. If the Asserts fail
289 * then we know from the stack trace that the corruption occurred *before*
290 * entry to this method. We can also be sure it didn't happen in any of
291 * the tested lyxstring methods. It is therefore likely to be due to some
292 * other external force.
293 * Several lyxstring methods have multiple exit points which would otherwise
294 * require us to insert a separate test before each return. But since we
295 * created an object its destructor will be called upon exit (any exit!).
296 * We thus get testing at both start and end of a method with one line of
297 * code at the head of a method. More importantly, we get good testing
298 * everytime we run the code.
299 * NOTE: just because we test the invariant doesn't mean we can forget
300 * about testing pre and post conditions specific to any given method.
301 * This test simply proves that the lyxstring/Srep is in a valid state it
302 * does *not* prove that the method did what it was supposed to.
304 class lyxstringInvariant {
306 lyxstringInvariant(lyxstring const *);
307 ~lyxstringInvariant();
310 lyxstring const * object;
314 // To test if this scheme works "as advertised" uncomment the printf's in
315 // the constructor and destructor below and then uncomment the printf and the
316 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
317 // The correct output when LyX has been recompiled and run is:
318 // lyxstringInvariant constructor
319 // lyxstring::operator=(char const *)
320 // lyxstringInvariant constructor
321 // lyxstringInvariant destructor completed
322 // lyxstringInvariant destructor completed
323 // NOTE: The easiest way to catch this snippet of the output is to wait for
324 // the splash screen to disappear and then open and close Help->Credits
326 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
328 // printf("lyxstringInvariant constructor\n");
332 lyxstringInvariant::~lyxstringInvariant()
335 // printf("lyxstringInvariant destructor completed\n");
338 void lyxstringInvariant::helper() const
340 // Some of these tests might look pointless but they are
341 // all part of the invariant and if we want to make sure
342 // we have a bullet proof implementation then we need to
343 // test every last little thing we *know* should be true.
344 // I may have missed a test or two, so feel free to fill
345 // in the gaps. ARRae.
348 Assert(object->rep->s); // s is never 0
349 Assert(object->rep->res); // always some space allocated
350 Assert(object->rep->sz <= object->rep->res);
351 Assert(object->rep->ref >= 1); // its in use so it must be referenced
352 Assert(object->rep->ref < static_cast<size_t>(1 << (8 * sizeof(object->rep->ref) - 1)));
353 // if it does ever == then we should be generating a new copy
354 // and starting again. (Is char always 8-bits?)
356 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
358 #define TestlyxstringInvariant(s)
359 #endif //DEVEL_VERSION
362 ///////////////////////////////////////
363 // Constructors and Deconstructors.
364 ///////////////////////////////////////
366 lyxstring::size_type const lyxstring::npos =
367 static_cast<lyxstring::size_type>(-1);
369 lyxstring::lyxstring()
371 static Srep empty_rep(0, "");
377 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
379 Assert(pos < x.rep->sz || pos == 0);
380 if (pos == 0 && n >= x.length()) { // this is the default
384 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
389 lyxstring::lyxstring(value_type const * s, size_type n)
391 Assert(s); // we don't allow null pointers
392 static Srep empty_rep(0, "");
393 if (*s && n) { // s is not empty string and n > 0
394 rep = new Srep(min(strlen(s), n), s);
402 lyxstring::lyxstring(value_type const * s)
404 Assert(s); // we don't allow null pointers
405 static Srep empty_rep(0, "");
406 if (*s) { // s is not empty string
407 rep = new Srep(strlen(s), s);
415 lyxstring::lyxstring(size_type n, value_type c)
417 rep = new Srep(n, c);
421 lyxstring::lyxstring(iterator first, iterator last)
423 rep = new Srep(last - first, first);
427 lyxstring::~lyxstring()
429 if (--rep->ref == 0) delete rep;
432 ///////////////////////
434 ///////////////////////
436 lyxstring::iterator lyxstring::begin()
442 lyxstring::const_iterator lyxstring::begin() const
448 lyxstring::iterator lyxstring::end()
450 return rep->s + rep->sz;
454 lyxstring::const_iterator lyxstring::end() const
456 return rep->s + rep->sz;
460 reverse_iterator lyxstring::rbegin()
462 return reverse_iterator( end() );
466 const_reverse_iterator lyxstring::rbegin() const
468 return const_reverse_iterator( end() );
472 reverse_iterator lyxstring::rend()
474 return reverse_iterator( begin() );
478 const_reverse_iterator lyxstring::rend() const
480 return const_reverse_iterator( begin() );
485 ///////////////////////
487 ///////////////////////
489 lyxstring::size_type lyxstring::size() const
495 void lyxstring::resize(size_type n, value_type c)
497 TestlyxstringInvariant(this);
499 // This resets sz to res_arg
500 rep = rep->get_own_copy();
505 lyxstring::size_type lyxstring::capacity() const
511 void lyxstring::reserve(size_type res_arg)
513 TestlyxstringInvariant(this);
515 rep = rep->get_own_copy();
516 rep->reserve(res_arg);
524 lyxstring & lyxstring::operator=(lyxstring const & x)
526 TestlyxstringInvariant(this);
532 lyxstring & lyxstring::operator=(value_type const * s)
535 TestlyxstringInvariant(this);
536 // printf("lyxstring::operator=(value_type const *)\n");
542 lyxstring & lyxstring::operator=(value_type c)
544 TestlyxstringInvariant(this);
548 if (rep->ref == 1) // recycle rep
552 rep = new Srep(1, s);
558 lyxstring & lyxstring::assign(lyxstring const & x)
560 TestlyxstringInvariant(this);
562 x.rep->ref++; // protect against ``st = st''
563 if (--rep->ref == 0) delete rep;
564 rep = x.rep; // share representation
569 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
571 TestlyxstringInvariant(this);
573 return assign(x.substr(pos, n));
577 lyxstring & lyxstring::assign(value_type const * s, size_type n)
580 TestlyxstringInvariant(this);
582 n = min(strlen(s), n);
583 if (rep->ref == 1) // recycle rep
587 rep = new Srep(n, s);
593 lyxstring & lyxstring::assign(value_type const * s)
596 TestlyxstringInvariant(this);
598 return assign(s, strlen(s));
602 lyxstring & lyxstring::assign(size_type n, value_type ch)
604 TestlyxstringInvariant(this);
606 rep = rep->get_own_copy();
612 lyxstring & lyxstring::assign(iterator first, iterator last)
614 TestlyxstringInvariant(this);
616 rep = rep->get_own_copy();
617 rep->assign(last - first, first);
626 lyxstring::const_reference lyxstring::operator[](size_type pos) const
628 Assert(pos < rep->sz);
633 lyxstring::reference lyxstring::operator[](size_type pos)
635 Assert(pos < rep->sz);
636 TestlyxstringInvariant(this);
638 rep = rep->get_own_copy();
643 lyxstring::const_reference lyxstring::at(size_type n) const
650 lyxstring::reference lyxstring::at(size_type n)
653 TestlyxstringInvariant(this);
655 rep = rep->get_own_copy();
664 lyxstring & lyxstring::operator+=(lyxstring const & x)
666 TestlyxstringInvariant(this);
672 lyxstring & lyxstring::operator+=(value_type const * x)
675 TestlyxstringInvariant(this);
681 lyxstring & lyxstring::operator+=(value_type c)
683 TestlyxstringInvariant(this);
690 void lyxstring::push_back(value_type c)
692 TestlyxstringInvariant(this);
694 rep = rep->get_own_copy();
699 lyxstring & lyxstring::append(lyxstring const & x)
701 TestlyxstringInvariant(this);
703 if (x.empty()) return *this;
704 rep = rep->get_own_copy();
705 rep->append(x.length(), x.rep->s);
710 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
712 TestlyxstringInvariant(this);
714 return append(x.substr(pos, n));
718 lyxstring & lyxstring::append(value_type const * p, size_type n)
721 TestlyxstringInvariant(this);
723 if (!*p || !n) return *this;
724 rep = rep->get_own_copy();
725 rep->append(min(n, strlen(p)), p);
730 lyxstring & lyxstring::append(value_type const * p)
733 TestlyxstringInvariant(this);
735 if (!*p) return *this;
736 rep = rep->get_own_copy();
737 rep->append(strlen(p), p);
742 lyxstring & lyxstring::append(size_type n, value_type c)
744 TestlyxstringInvariant(this);
746 value_type * tmp = new value_type[n];
748 rep = rep->get_own_copy();
755 lyxstring & lyxstring::append(iterator first, iterator last)
757 TestlyxstringInvariant(this);
759 rep = rep->get_own_copy();
760 rep->append(last - first, first);
764 // insert value_typeacters before (*this)[pos]
766 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
768 TestlyxstringInvariant(this);
770 return insert(pos, x, 0, x.rep->sz);
774 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
775 size_type pos2, size_type n)
777 TestlyxstringInvariant(this);
779 rep = rep->get_own_copy();
780 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
785 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
788 TestlyxstringInvariant(this);
791 // insert nothing and you change nothing
792 rep = rep->get_own_copy();
793 rep->insert(pos, p, min(n, strlen(p)));
799 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
802 TestlyxstringInvariant(this);
805 // insert nothing and you change nothing
806 rep = rep->get_own_copy();
807 rep->insert(pos, p, strlen(p));
813 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
815 TestlyxstringInvariant(this);
817 rep = rep->get_own_copy();
818 value_type * tmp = new value_type[n];
820 rep->insert(pos, tmp, n);
826 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
828 TestlyxstringInvariant(this);
830 // what iterator is this supposed to return??
831 size_type tmp = p - begin();
832 insert(p - begin(), 1, c);
833 return begin() + tmp + 1; // ??
837 void lyxstring::insert(iterator p, size_type n , value_type c)
839 TestlyxstringInvariant(this);
841 insert(p - begin(), n , c);
845 void lyxstring::insert(iterator p, iterator first, iterator last)
847 TestlyxstringInvariant(this);
849 insert(p - begin(), first, last - first);
857 // All the below find functions should be verified,
858 // it is very likely that I have mixed up or interpreted
859 // some of the parameters wrong, also some of the funcs can surely
860 // be written more effectively.
862 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
864 if (!rep->sz || i >= rep->sz) return npos;
866 TestlyxstringInvariant(this);
868 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
869 // search until (*this)[i] == a[0]
870 if (rep->s[t] == a[0]) {
871 // check if the rest of the value_types match
873 for (size_type j = 0; j < a.length(); ++j) {
874 if (rep->s[t + j] != a[j]) {
886 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
890 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
892 TestlyxstringInvariant(this);
894 // What is "n" here? is it the number of value_types to use in ptr
895 // or does "i" and "n" togeter form a substring to search
896 // for ptr in? For now I will assume that "n" tells the length
898 n = min(n, strlen(ptr));
899 for (size_type t = i; rep->sz - t >= n; ++t) {
900 // search until (*this)[i] == a[0]
901 if (rep->s[t] == ptr[0]) {
902 // check if the rest of the value_types match
904 for (size_type j = 0; j < n; ++j) {
905 if (rep->s[t + j] != ptr[j]) {
917 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
920 if (!rep->sz || i >= rep->sz) return npos;
922 TestlyxstringInvariant(this);
924 if (!s || !*s) return npos;
925 return find(s, i, strlen(s));
929 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
931 if (!rep->sz || i >= rep->sz) return npos;
933 TestlyxstringInvariant(this);
935 for (size_type t = 0; t + i < rep->sz; ++t) {
936 if (rep->s[t + i] == c) return t + i;
942 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
944 TestlyxstringInvariant(this);
946 size_type ii = min(rep->sz - 1, i);
948 if (a[a.length() - 1] == rep->s[ii]) {
950 size_type l = ii - 1;
951 for (; t >= 0; --t, --l) {
952 if (a[t] != rep->s[l]) break;
954 if (a[t] == rep->s[l]) return l;
961 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
965 TestlyxstringInvariant(this);
966 if (!*ptr) return npos;
968 size_type ii = min(rep->sz - 1, i);
970 if (ptr[n - 1] == rep->s[ii]) {
972 size_type l = ii - 1;
973 for (; t >= 0; --t, --l) {
974 if (ptr[t] != rep->s[l]) break;
976 if (ptr[t] == rep->s[l]) return l;
983 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
987 TestlyxstringInvariant(this);
988 if (!*ptr) return npos;
990 size_type ii = min(rep->sz - 1, i);
992 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
993 int t = strlen(ptr) - 2;
994 size_type l = ii - 1;
995 for (; t >= 0; --t, --l) {
996 if (ptr[t] != rep->s[l]) break;
998 if (ptr[t] == rep->s[l]) return l;
1005 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1007 TestlyxstringInvariant(this);
1009 size_type ii = min(rep->sz - 1, i);
1010 for (size_type t = ii; t != 0; --t) {
1011 if (rep->s[t] == c) return t;
1017 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1020 Assert(i < rep->sz);
1021 TestlyxstringInvariant(this);
1023 for (size_type t = i; t < rep->sz; ++t) {
1024 if (a.find(rep->s[t]) != npos) return t;
1030 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1033 Assert(ptr && i < rep->sz);
1034 TestlyxstringInvariant(this);
1035 if (!n) return npos;
1037 for (size_type t = i; t < rep->sz; ++t) {
1038 if(memchr(ptr, rep->s[t], n) != 0) return t;
1044 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1047 Assert(ptr && i < rep->sz);
1048 TestlyxstringInvariant(this);
1050 for (size_type t = i; t < rep->sz; ++t) {
1051 if (strchr(ptr, rep->s[t]) != 0) return t;
1057 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1059 Assert(i < rep->sz);
1060 TestlyxstringInvariant(this);
1062 for (size_type t = i; t < rep->sz; ++t) {
1063 if (rep->s[t] == c) return t;
1069 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1072 TestlyxstringInvariant(this);
1074 size_type ii = min(rep->sz - 1, i);
1075 for (int t = ii; t >= 0; --t) {
1076 if (a.find(rep->s[t]) != npos) return t;
1081 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1085 TestlyxstringInvariant(this);
1086 if (!n) return npos;
1088 size_type ii = min(rep->sz - 1, i);
1089 for (int t = ii; t >= 0; --t) {
1090 if(memchr(ptr, rep->s[t], n) != 0) return t;
1096 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1100 TestlyxstringInvariant(this);
1102 size_type ii = min(rep->sz - 1, i);
1103 for (int t = ii; t >= 0; --t) {
1104 if (strchr(ptr, rep->s[t]) != 0) return t;
1110 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1112 TestlyxstringInvariant(this);
1114 if (!rep->sz) return npos;
1115 size_type ii = min(rep->sz - 1, i);
1116 for (int t = ii; t >= 0; --t) {
1117 if (rep->s[t] == c) return t;
1123 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1126 TestlyxstringInvariant(this);
1128 if (!rep->sz) return npos;
1129 Assert(i < rep->sz);
1130 for (size_type t = i; t < rep->sz; ++t) {
1131 if (a.find(rep->s[t]) == npos) return t;
1137 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1141 Assert(ptr && i < rep->sz);
1142 TestlyxstringInvariant(this);
1144 if (!n) return (i < rep->sz) ? i : npos;
1145 for (size_type t = i; t < rep->sz; ++t) {
1146 if(memchr(ptr, rep->s[t], n) == 0) return t;
1152 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1155 Assert(ptr && i < rep->sz);
1156 TestlyxstringInvariant(this);
1158 for (size_type t = i; t < rep->sz; ++t) {
1159 if (strchr(ptr, rep->s[t]) == 0) return t;
1165 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1168 if (!rep->sz) return npos;
1169 Assert(i < rep->sz);
1170 TestlyxstringInvariant(this);
1172 for (size_type t = i; t < rep->sz; ++t) {
1173 if (rep->s[t] != c) return t;
1179 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1182 TestlyxstringInvariant(this);
1184 size_type ii = min(rep->sz - 1, i);
1185 for (int t = ii; t >= 0; --t) {
1186 if (a.find(rep->s[t]) == npos) return t;
1192 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1197 TestlyxstringInvariant(this);
1199 if (!n) return npos;
1200 size_type ii = min(rep->sz - 1, i);
1202 for (int t = ii; t >= 0; --t) {
1203 if(memchr(ptr, rep->s[t], n) == 0) return t;
1209 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1213 TestlyxstringInvariant(this);
1215 size_type ii = min(rep->sz - 1, i);
1216 for (int t = ii; t >= 0; --t) {
1217 if (strchr(ptr, rep->s[t]) == 0) return t;
1223 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1226 TestlyxstringInvariant(this);
1228 size_type ii = min(rep->sz - 1, i);
1229 for (int t = ii; t >= 0; --t) {
1230 if (rep->s[t] != c) return t;
1240 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1242 Assert(i < rep->sz || i == 0);
1243 TestlyxstringInvariant(this);
1245 return replace(i, n, x, 0, x.length());
1249 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1250 size_type i2, size_type n2)
1252 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1253 TestlyxstringInvariant(this);
1255 rep = rep->get_own_copy();
1256 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1261 lyxstring & lyxstring::replace(size_type i, size_type n,
1262 value_type const * p, size_type n2)
1264 Assert(p && i < rep->sz);
1265 TestlyxstringInvariant(this);
1267 rep = rep->get_own_copy();
1268 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1273 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1275 Assert(p && i < rep->sz);
1276 TestlyxstringInvariant(this);
1278 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1282 lyxstring & lyxstring::replace(size_type i, size_type n,
1283 size_type n2, value_type c)
1285 Assert(i < rep->sz);
1286 TestlyxstringInvariant(this);
1288 rep = rep->get_own_copy();
1289 value_type * tmp = new value_type[n2];
1291 rep->replace(i, min(n, rep->sz), tmp, n2);
1297 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1299 TestlyxstringInvariant(this);
1301 return replace(i - begin(), i2 - i, str);
1305 lyxstring & lyxstring::replace(iterator i, iterator i2,
1306 value_type const * p, size_type n)
1309 TestlyxstringInvariant(this);
1311 return replace(i - begin(), i2 - i, p, n);
1315 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1318 TestlyxstringInvariant(this);
1320 return replace(i - begin(), i2 - i, p);
1324 lyxstring & lyxstring::replace(iterator i, iterator i2,
1325 size_type n , value_type c)
1327 TestlyxstringInvariant(this);
1329 return replace(i - begin(), i2 - i, n, c);
1333 lyxstring & lyxstring::replace(iterator i, iterator i2,
1334 iterator j, iterator j2)
1336 TestlyxstringInvariant(this);
1338 return replace(i - begin(), i2 - i, j, j2 - j);
1342 lyxstring & lyxstring::erase(size_type i, size_type n)
1344 Assert(i < rep->sz || i == 0);
1345 TestlyxstringInvariant(this);
1347 rep = rep->get_own_copy();
1348 if (i == 0 && n >= rep->sz) {
1351 n = min(n, rep->sz - i);
1352 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1359 lyxstring::iterator lyxstring::erase(iterator i)
1361 TestlyxstringInvariant(this);
1363 // what iterator is this supposed to return?
1364 // the iterator after the one erased
1365 erase(i - begin(), 1);
1366 return begin(); // BUG
1370 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1372 TestlyxstringInvariant(this);
1374 erase(first - begin(), last - first);
1375 return begin(); // BUG
1379 /////////////////////////////////////
1380 // Conversion to C-style Strings
1381 /////////////////////////////////////
1383 lyxstring::value_type const * lyxstring::c_str() const
1385 rep->s[length()] = '\0';
1390 lyxstring::value_type const * lyxstring::data() const
1396 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1397 size_type pos) const
1400 TestlyxstringInvariant(this);
1402 register int nn = min(len, length() - pos);
1403 memcpy(buf, &(rep->s[pos]), nn);
1408 ////////////////////
1410 ////////////////////
1412 // Compare funcs should be verified.
1414 int lyxstring::internal_compare(size_type pos, size_type n,
1415 value_type const * s,
1416 size_type slen, size_type n2) const
1418 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1420 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1422 // remember that n can very well be a lot larger than rep->sz
1423 // so we have to ensure that n is no larger than rep->sz
1424 n = min(n, rep->sz);
1427 return memcmp(&(rep->s[pos]), s, n);
1428 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1437 int lyxstring::compare(lyxstring const & str) const
1439 TestlyxstringInvariant(this);
1440 return internal_compare(0, rep->sz, str.rep->s,
1441 str.rep->sz, str.rep->sz);
1445 int lyxstring::compare(value_type const * s) const
1448 TestlyxstringInvariant(this);
1449 int n = (!s) ? 0 : strlen(s);
1450 return internal_compare(0, rep->sz, s, n, n);
1454 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1456 Assert(pos < rep->sz || pos == 0);
1457 TestlyxstringInvariant(this);
1458 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1462 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1463 size_type pos2, size_type n2) const
1465 Assert(pos < rep->sz || pos == 0);
1466 Assert(pos2 < str.rep->sz || pos2 == 0);
1467 TestlyxstringInvariant(this);
1468 return internal_compare(pos, n,
1470 str.rep->sz - pos2, n2);
1474 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1477 Assert(s && (pos < rep->sz || pos == 0));
1478 TestlyxstringInvariant(this);
1479 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1487 // i = index, n = length
1488 lyxstring lyxstring::substr(size_type i, size_type n) const
1490 Assert(i < rep->sz || i == 0);
1491 TestlyxstringInvariant(this);
1493 return lyxstring(*this, i, n);
1497 /////////////////////////////////////////////
1498 // String operators, non member functions
1499 /////////////////////////////////////////////
1501 bool operator==(lyxstring const & a, lyxstring const & b)
1503 return a.compare(b) == 0;
1507 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1510 return b.compare(a) == 0;
1514 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1517 return a.compare(b) == 0;
1521 bool operator!=(lyxstring const & a, lyxstring const & b)
1523 return a.compare(b) != 0;
1527 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1530 return b.compare(a) != 0;
1534 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1537 return a.compare(b) != 0;
1541 bool operator>(lyxstring const & a, lyxstring const & b)
1543 return a.compare(b) > 0;
1547 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1550 return b.compare(a) < 0; // since we reverse the parameters
1554 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1557 return a.compare(b) > 0;
1561 bool operator<(lyxstring const & a, lyxstring const & b)
1563 return a.compare(b) < 0;
1567 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1570 return b.compare(a) > 0; // since we reverse the parameters
1574 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1577 return a.compare(b) < 0;
1581 bool operator>=(lyxstring const & a, lyxstring const & b)
1583 return a.compare(b) >= 0;
1587 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1590 return b.compare(a) <= 0; // since we reverse the parameters
1594 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1597 return a.compare(b) >= 0;
1601 bool operator<=(lyxstring const & a, lyxstring const & b)
1603 return a.compare(b) <= 0;
1607 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1610 return b.compare(a) >= 0; // since we reverse the parameters
1614 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1617 return a.compare(b) <= 0;
1621 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1629 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1638 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1647 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1656 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1665 istream & operator>>(istream & is, lyxstring & s)
1667 // very bad solution
1668 char * nome = new char[1024];
1670 lyxstring tmp(nome);
1672 if (!tmp.empty()) s = tmp;
1676 ostream & operator<<(ostream & o, lyxstring const & s)
1678 return o.write(s.data(), s.length());
1681 istream & getline(istream & is, lyxstring & s,
1682 lyxstring::value_type delim)
1684 // very bad solution