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 // Reference count has been checked, empty_rep removed and
29 // introduced again in a similar guise. Where is empty_rep _really_
32 // Insertion and replace is implemented, as far as I can see everything
33 // works, but could perhaps be done smarter.
35 // All the different find functions need a good look over.
36 // I have so far not tested them extensively and would be
37 // happy if others took the time to have a peek.
40 #warning temporarily here for debugging purposes only
42 lyxstring::size_type lyxstring::size() const
48 //--------------------------------------------------------------------------
52 /** Testing of the lyxstring invariant
53 * By creating an object that tests the lyxstring invariant during its
54 * construction *and* its deconstruction we greatly simplify our code.
55 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
56 * will test the invariant upon entry to the code. If the Asserts fail
57 * then we know from the stack trace that the corruption occurred *before*
58 * entry to this method. We can also be sure it didn't happen in any of
59 * the tested lyxstring methods. It is therefore likely to be due to some
60 * other external force.
61 * Several lyxstring methods have multiple exit points which would otherwise
62 * require us to insert a separate test before each return. But since we
63 * created an object its destructor will be called upon exit (any exit!).
64 * We thus get testing at both start and end of a method with one line of
65 * code at the head of a method. More importantly, we get good testing
66 * everytime we run the code.
67 * NOTE: just because we test the invariant doesn't mean we can forget
68 * about testing pre and post conditions specific to any given method.
69 * This test simply proves that the lyxstring/Srep is in a valid state it
70 * does *not* prove that the method did what it was supposed to.
72 class lyxstringInvariant
75 lyxstringInvariant(lyxstring const *);
76 ~lyxstringInvariant();
79 lyxstring const * object;
82 // To test if this scheme works "as advertised" uncomment the printf's in
83 // the constructor and destructor below and then uncomment the printf and the
84 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
85 // The correct output when LyX has been recompiled and run is:
86 // lyxstringInvariant constructor
87 // lyxstring::operator=(char const *)
88 // lyxstringInvariant constructor
89 // lyxstringInvariant destructor completed
90 // lyxstringInvariant destructor completed
91 // NOTE: The easiest way to catch this snippet of the output is to wait for
92 // the splash screen to disappear and then open and close Help->Credits
94 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
96 // printf("lyxstringInvariant constructor\n");
100 lyxstringInvariant::~lyxstringInvariant()
103 // printf("lyxstringInvariant destructor completed\n");
106 void lyxstringInvariant::helper() const
108 // Some of these tests might look pointless but they are
109 // all part of the invariant and if we want to make sure
110 // we have a bullet proof implementation then we need to
111 // test every last little thing we *know* should be true.
112 // I may have missed a test or two, so feel free to fill
113 // in the gaps. ARRae.
114 // NOTE: Don't put TestlyxstringInvariant() in any of the
115 // lyxstring methods used below otherwise you'll get an
116 // infinite recursion and a crash.
119 Assert(object->rep->s); // s is never 0
120 Assert(object->rep->res); // always some space allocated
121 Assert(object->size() <= object->rep->res);
122 Assert(object->rep->ref >= 1); // its in use so it must be referenced
123 Assert(object->rep->ref < (1 << 8*sizeof(object->rep->ref)) - 1);
124 // if it does ever == then we should be generating a new copy
125 // and starting again. (Is char always 8-bits?)
127 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
129 #define TestlyxstringInvariant(s)
130 #endif //DEVEL_VERSION
131 //-------------------------------------------------------------------------
133 ///////////////////////////////////////
134 // Constructors and Deconstructors.
135 ///////////////////////////////////////
138 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
140 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
145 s = new value_type[res + 1]; // add space for terminator
147 // if sz = 0 nothing gets copied and we have an error
150 // possibly allows for large but empty string
151 sz = 0; // this line should be redundant
157 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
162 s = new value_type[res + 1]; // add space for terminator
165 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
166 // allows for large but empty string
172 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
174 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
180 s = new value_type[res + 1]; // add space for terminator
185 // if sz = 0 nothing gets copied and we have an error
189 sz = 0; // this line should be redundant
195 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
201 s = new value_type[res + 1]; // add space for terminator
205 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
206 // allows for a large empty string
212 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
214 register unsigned int const len = sz + asz;
217 value_type * tmp = new value_type[res + 1];
219 memcpy(tmp + sz, p, asz);
224 memcpy(s + sz, p, asz);
230 void lyxstring::Srep::push_back(value_type c)
232 s[sz] = c; // it is always room to put a value_type at the end
236 value_type * tmp = new value_type[res + 1];
244 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
245 lyxstring::size_type n)
250 value_type * tmp = new value_type[res + 1];
252 memcpy(tmp + pos, p, n);
253 memcpy(tmp + pos + n, & s[pos], sz - pos);
258 memmove(s + pos + n, & s[pos], sz - pos);
259 memcpy(s + pos, p, n);
265 void lyxstring::Srep::resize(size_type n, value_type c)
268 // This resets sz to res_arg
269 res = min(n, npos - 2); // We keep no xtra when we resize
270 value_type * tmp = new value_type[res + 1];
271 memcpy(tmp, s, min(sz, res));
273 memset(tmp + sz, c, res - sz);
280 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
282 // This keeps the old sz, but
283 // increases res with res_arg
285 value_type * tmp = new value_type[res + 1];
292 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
293 value_type const * p, size_type n2)
295 // can be called with p=0 and n2=0
296 Assert(i < sz && ((!p && !n2) || p));
299 if (res >= n2 + sz) {
300 memmove(s + i + n2, &s[i + n], sz - i);
301 memcpy(s + i, p, n2);
304 res = sz + n2 + xtra;
305 value_type * tmp = new value_type[res + 1];
307 memcpy(tmp + i, p, n2);
308 memcpy(tmp + i + n2, &s[i + n], sz - i);
316 lyxstring::size_type const lyxstring::npos = static_cast<lyxstring::size_type>(-1);
318 lyxstring::lyxstring()
320 static Srep empty_rep(0, "");
326 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
328 Assert(pos < x.rep->sz || pos == 0);
329 if (pos == 0 && n >= x.length()) { // this is the default
333 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
338 lyxstring::lyxstring(value_type const * s, size_type n)
340 Assert(s); // we don't allow null pointers
341 static Srep empty_rep(0, "");
342 if (*s && n) { // s is not empty string and n > 0
343 rep = new Srep(min(strlen(s), n), s);
351 lyxstring::lyxstring(value_type const * s)
353 Assert(s); // we don't allow null pointers
354 static Srep empty_rep(0, "");
355 if (*s) { // s is not empty string
356 rep = new Srep(strlen(s), s);
364 lyxstring::lyxstring(size_type n, value_type c)
366 rep = new Srep(n, c);
370 lyxstring::lyxstring(iterator first, iterator last)
372 rep = new Srep(last - first, first);
376 ///////////////////////
378 ///////////////////////
380 lyxstring::iterator lyxstring::begin()
386 lyxstring::const_iterator lyxstring::begin() const
392 lyxstring::iterator lyxstring::end()
394 return rep->s + rep->sz;
398 lyxstring::const_iterator lyxstring::end() const
400 return rep->s + rep->sz;
404 reverse_iterator lyxstring::rbegin()
406 return reverse_iterator( end() );
410 const_reverse_iterator lyxstring::rbegin() const
412 return const_reverse_iterator( end() );
416 reverse_iterator lyxstring::rend()
418 return reverse_iterator( begin() );
422 const_reverse_iterator lyxstring::rend() const
424 return const_reverse_iterator( begin() );
428 ///////////////////////
430 ///////////////////////
432 void lyxstring::resize(size_type n, value_type c)
434 TestlyxstringInvariant(this);
436 // This resets sz to res_arg
437 rep = rep->get_own_copy();
442 lyxstring::size_type lyxstring::capacity() const
448 void lyxstring::reserve(size_type res_arg)
450 TestlyxstringInvariant(this);
452 rep = rep->get_own_copy();
453 rep->reserve(res_arg);
461 lyxstring & lyxstring::operator=(lyxstring const & x)
463 TestlyxstringInvariant(this);
469 lyxstring & lyxstring::operator=(value_type const * s)
472 TestlyxstringInvariant(this);
473 // printf("lyxstring::operator=(value_type const *)\n");
479 lyxstring & lyxstring::operator=(value_type c)
481 TestlyxstringInvariant(this);
485 if (rep->ref == 1) // recycle rep
489 rep = new Srep(1, s);
495 lyxstring & lyxstring::assign(lyxstring const & x)
497 TestlyxstringInvariant(this);
499 x.rep->ref++; // protect against ``st = st''
500 if (--rep->ref == 0) delete rep;
501 rep = x.rep; // share representation
506 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
508 TestlyxstringInvariant(this);
510 return assign(x.substr(pos, n));
514 lyxstring & lyxstring::assign(value_type const * s, size_type n)
517 TestlyxstringInvariant(this);
519 n = min(strlen(s), n);
520 if (rep->ref == 1) // recycle rep
524 rep = new Srep(n, s);
530 lyxstring & lyxstring::assign(value_type const * s)
533 TestlyxstringInvariant(this);
535 return assign(s, strlen(s));
539 lyxstring & lyxstring::assign(size_type n, value_type ch)
541 TestlyxstringInvariant(this);
543 rep = rep->get_own_copy();
549 lyxstring & lyxstring::assign(iterator first, iterator last)
551 TestlyxstringInvariant(this);
553 rep = rep->get_own_copy();
554 rep->assign(last - first, first);
563 lyxstring::const_reference lyxstring::operator[](size_type pos) const
565 Assert(pos < rep->sz);
571 lyxstring::reference lyxstring::operator[](size_type pos)
573 Assert(pos < rep->sz);
574 TestlyxstringInvariant(this);
576 rep = rep->get_own_copy();
581 lyxstring::const_reference lyxstring::at(size_type n) const
589 lyxstring::reference lyxstring::at(size_type n)
592 TestlyxstringInvariant(this);
594 rep = rep->get_own_copy();
603 lyxstring & lyxstring::operator+=(lyxstring const & x)
605 TestlyxstringInvariant(this);
611 lyxstring & lyxstring::operator+=(value_type const * x)
614 TestlyxstringInvariant(this);
620 lyxstring & lyxstring::operator+=(value_type c)
622 TestlyxstringInvariant(this);
629 void lyxstring::push_back(value_type c)
631 TestlyxstringInvariant(this);
633 rep = rep->get_own_copy();
638 lyxstring & lyxstring::append(lyxstring const & x)
640 TestlyxstringInvariant(this);
642 if (x.empty()) return *this;
643 rep = rep->get_own_copy();
644 rep->append(x.length(), x.rep->s);
649 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
651 TestlyxstringInvariant(this);
653 return append(x.substr(pos, n));
657 lyxstring & lyxstring::append(value_type const * p, size_type n)
660 TestlyxstringInvariant(this);
662 if (!*p || !n) return *this;
663 rep = rep->get_own_copy();
664 rep->append(min(n, strlen(p)), p);
669 lyxstring & lyxstring::append(value_type const * p)
672 TestlyxstringInvariant(this);
674 if (!*p) return *this;
675 rep = rep->get_own_copy();
676 rep->append(strlen(p), p);
681 lyxstring & lyxstring::append(size_type n, value_type c)
683 TestlyxstringInvariant(this);
685 value_type * tmp = new value_type[n];
687 rep = rep->get_own_copy();
694 lyxstring & lyxstring::append(iterator first, iterator last)
696 TestlyxstringInvariant(this);
698 rep = rep->get_own_copy();
699 rep->append(last - first, first);
703 // insert value_typeacters before (*this)[pos]
705 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
707 TestlyxstringInvariant(this);
709 return insert(pos, x, 0, x.rep->sz);
713 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
714 size_type pos2, size_type n)
716 TestlyxstringInvariant(this);
718 rep = rep->get_own_copy();
719 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
724 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
727 TestlyxstringInvariant(this);
730 // insert nothing and you change nothing
731 rep = rep->get_own_copy();
732 rep->insert(pos, p, min(n, strlen(p)));
738 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
741 TestlyxstringInvariant(this);
744 // insert nothing and you change nothing
745 rep = rep->get_own_copy();
746 rep->insert(pos, p, strlen(p));
752 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
754 TestlyxstringInvariant(this);
756 rep = rep->get_own_copy();
757 value_type * tmp = new value_type[n];
759 rep->insert(pos, tmp, n);
765 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
767 TestlyxstringInvariant(this);
769 // what iterator is this supposed to return??
770 size_type tmp = p - begin();
771 insert(p - begin(), 1, c);
772 return begin() + tmp + 1; // ??
776 void lyxstring::insert(iterator p, size_type n , value_type c)
778 TestlyxstringInvariant(this);
780 insert(p - begin(), n , c);
784 void lyxstring::insert(iterator p, iterator first, iterator last)
786 TestlyxstringInvariant(this);
788 insert(p - begin(), first, last - first);
796 // All the below find functions should be verified,
797 // it is very likely that I have mixed up or interpreted
798 // some of the parameters wrong, also some of the funcs can surely
799 // be written more effectively.
801 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
803 if (!rep->sz) return npos;
806 TestlyxstringInvariant(this);
808 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
809 // search until (*this)[i] == a[0]
810 if (rep->s[t] == a[0]) {
811 // check if the rest of the value_types match
813 for (size_type j = 0; j < a.length(); ++j) {
814 if (rep->s[t + j] != a[j]) {
826 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
830 if (!rep->sz || !*ptr) return npos;
833 TestlyxstringInvariant(this);
835 // What is "n" here? is it the number of value_types to use in ptr
836 // or does "i" and "n" togeter form a substring to search
837 // for ptr in? For now I will assume that "n" tells the length
839 n = min(n, strlen(ptr));
840 for (size_type t = i; length() - t >= n; ++t) {
841 // search until (*this)[i] == a[0]
842 if (rep->s[t] == ptr[0]) {
843 // check if the rest of the value_types match
845 for (size_type j = 0; j < n; ++j) {
846 if (rep->s[t + j] != ptr[j]) {
858 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
861 if (!rep->sz) return npos;
864 TestlyxstringInvariant(this);
866 if (!s || !*s) return npos;
867 return find(s, i, strlen(s));
871 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
873 if (!rep->sz) return npos;
876 TestlyxstringInvariant(this);
878 for (size_type t = 0; t + i < length(); ++t) {
879 if (rep->s[t + i] == c) return t + i;
885 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
887 TestlyxstringInvariant(this);
889 size_type ii = min(length() - 1, i);
891 if (a[a.length() - 1] == rep->s[ii]) {
892 int t = length() - 2;
893 size_type l = ii - 1;
894 for (; t >= 0; --t, --l) {
895 if (a[t] != rep->s[l]) break;
897 if (a[t] == rep->s[l]) return l;
904 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
908 TestlyxstringInvariant(this);
909 if (!*ptr) return npos;
911 size_type ii = min(length() - 1, i);
913 if (ptr[n - 1] == rep->s[ii]) {
915 size_type l = ii - 1;
916 for (; t >= 0; --t, --l) {
917 if (ptr[t] != rep->s[l]) break;
919 if (ptr[t] == rep->s[l]) return l;
926 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
929 TestlyxstringInvariant(this);
930 if (!*ptr) return npos;
932 size_type ii = min(length() - 1, i);
934 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
935 int t = strlen(ptr) - 2;
936 size_type l = ii - 1;
937 for (; t >= 0; --t, --l) {
938 if (ptr[t] != rep->s[l]) break;
940 if (ptr[t] == rep->s[l]) return l;
947 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
949 TestlyxstringInvariant(this);
951 size_type ii = min(length() - 1, i);
952 for (size_type t = ii; t != 0; --t) {
953 if (rep->s[t] == c) return t;
959 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
963 TestlyxstringInvariant(this);
965 for (size_type t = i; t < length(); ++t) {
966 if (a.find(rep->s[t]) != npos) return t;
972 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
975 Assert(ptr && i < rep->sz);
976 TestlyxstringInvariant(this);
979 for (size_type t = i; t < length(); ++t) {
980 if(memchr(ptr, rep->s[t], n) != 0) return t;
986 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
989 Assert(ptr && i < rep->sz);
990 TestlyxstringInvariant(this);
992 for (size_type t = i; t < length(); ++t) {
993 if (strchr(ptr, rep->s[t]) != 0) return t;
999 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1001 Assert(i < rep->sz);
1002 TestlyxstringInvariant(this);
1004 for (size_type t = i; t < length(); ++t) {
1005 if (rep->s[t] == c) return t;
1011 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1014 TestlyxstringInvariant(this);
1016 size_type ii = min(length() - 1, i);
1017 for (int t = ii; t >= 0; --t) {
1018 if (a.find(rep->s[t]) != npos) return t;
1023 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1027 TestlyxstringInvariant(this);
1028 if (!n) return npos;
1030 size_type ii = min(length() - 1, i);
1031 for (int t = ii; t >= 0; --t) {
1032 if(memchr(ptr, rep->s[t], n) != 0) return t;
1038 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1042 TestlyxstringInvariant(this);
1044 size_type ii = min(length() - 1, i);
1045 for (int t = ii; t >= 0; --t) {
1046 if (strchr(ptr, rep->s[t]) != 0) return t;
1052 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1054 TestlyxstringInvariant(this);
1056 if (!rep->sz) return npos;
1057 size_type ii = min(length() - 1, i);
1058 for (int t = ii; t >= 0; --t) {
1059 if (rep->s[t] == c) return t;
1065 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1068 TestlyxstringInvariant(this);
1070 if (!rep->sz) return npos;
1071 Assert(i < rep->sz);
1072 for (size_type t = i; t < length(); ++t) {
1073 if (a.find(rep->s[t]) == npos) return t;
1079 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1082 Assert(ptr && i < rep->sz);
1083 TestlyxstringInvariant(this);
1085 if (!n) return (i < length()) ? i : npos;
1086 for (size_type t = i; t < length(); ++t) {
1087 if(memchr(ptr, rep->s[t], n) == 0) return t;
1093 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1096 Assert(ptr && i < rep->sz);
1097 TestlyxstringInvariant(this);
1099 for (size_type t = i; t < length(); ++t) {
1100 if (strchr(ptr, rep->s[t]) == 0) return t;
1106 lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const
1108 if (!rep->sz) return npos;
1109 Assert(i < rep->sz);
1110 TestlyxstringInvariant(this);
1112 for (size_type t = i; t < length(); ++t) {
1113 if (rep->s[t] != c) return t;
1119 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1122 TestlyxstringInvariant(this);
1124 size_type ii = min(length() - 1, i);
1125 for (int t = ii; t >= 0; --t) {
1126 if (a.find(rep->s[t]) == npos) return t;
1132 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1137 TestlyxstringInvariant(this);
1139 if (!n) return npos;
1140 size_type ii = min(length() - 1, i);
1141 //if (!n) return (ii >= 0) ? ii : npos;
1142 for (int t = ii; t >= 0; --t) {
1143 if(memchr(ptr, rep->s[t], n) == 0) return t;
1149 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1153 TestlyxstringInvariant(this);
1155 size_type ii = min(length() - 1, i);
1156 for (int t = ii; t >= 0; --t) {
1157 if (strchr(ptr, rep->s[t]) == 0) return t;
1163 lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) const
1165 TestlyxstringInvariant(this);
1167 size_type ii = min(length() - 1, i);
1168 for (int t = ii; t >= 0; --t) {
1169 if (rep->s[t] != c) return t;
1179 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1181 Assert(i < rep->sz || i == 0);
1182 TestlyxstringInvariant(this);
1184 return replace(i, n, x, 0, x.length());
1188 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1189 size_type i2, size_type n2)
1191 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1192 TestlyxstringInvariant(this);
1194 rep = rep->get_own_copy();
1195 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1200 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
1203 Assert(p && i < rep->sz);
1204 TestlyxstringInvariant(this);
1206 rep = rep->get_own_copy();
1207 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1212 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1214 Assert(p && i < rep->sz);
1215 TestlyxstringInvariant(this);
1217 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1221 lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c)
1223 Assert(i < rep->sz);
1224 TestlyxstringInvariant(this);
1226 rep = rep->get_own_copy();
1227 value_type * tmp = new value_type[n2];
1229 rep->replace(i, min(n, rep->sz), tmp, n2);
1235 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1237 TestlyxstringInvariant(this);
1239 return replace(i - begin(), i2 - i, str);
1243 lyxstring & lyxstring::replace(iterator i, iterator i2,
1244 value_type const * p, size_type n)
1247 TestlyxstringInvariant(this);
1249 return replace(i - begin(), i2 - i, p, n);
1253 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1256 TestlyxstringInvariant(this);
1258 return replace(i - begin(), i2 - i, p);
1262 lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type c)
1264 TestlyxstringInvariant(this);
1266 return replace(i - begin(), i2 - i, n, c);
1270 lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
1272 TestlyxstringInvariant(this);
1274 return replace(i - begin(), i2 - i, j, j2 - j);
1278 lyxstring & lyxstring::erase(size_type i, size_type n)
1280 Assert(i < rep->sz || i == 0);
1281 TestlyxstringInvariant(this);
1283 rep = rep->get_own_copy();
1284 if (i == 0 && n >= rep->sz) {
1287 n = min(n, rep->sz - i);
1288 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1295 lyxstring::iterator lyxstring::erase(iterator i)
1297 TestlyxstringInvariant(this);
1299 // what iterator is this supposed to return?
1300 // the iterator after the one erased
1301 erase(i - begin(), 1);
1302 return begin(); // BUG
1306 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1308 TestlyxstringInvariant(this);
1310 erase(first - begin(), last - first);
1311 return begin(); // BUG
1315 /////////////////////////////////////
1316 // Conversion to C-style Strings
1317 /////////////////////////////////////
1319 lyxstring::value_type const * lyxstring::c_str() const
1321 rep->s[length()] = '\0';
1326 lyxstring::value_type const * lyxstring::data() const
1332 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const
1335 TestlyxstringInvariant(this);
1337 register int nn = min(len, length() - pos);
1338 memcpy(buf, &(rep->s[pos]), nn);
1343 ////////////////////
1345 ////////////////////
1347 // Compare funcs should be verified.
1348 // Should we try to make them work with '\0' value_types?
1349 // An STL string can usually contain '\0' value_types.
1351 int lyxstring::compare(lyxstring const & str) const
1353 TestlyxstringInvariant(this);
1355 return compare(0, rep->sz, str.rep->s, str.rep->sz);
1359 int lyxstring::compare(value_type const * s) const
1362 TestlyxstringInvariant(this);
1364 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1368 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1370 TestlyxstringInvariant(this);
1372 return compare(pos, n, str.rep->s, str.rep->sz);
1376 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1377 size_type pos2, size_type n2) const
1379 TestlyxstringInvariant(this);
1381 return compare(pos, n, str.rep->s + pos2, n2);
1385 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1388 Assert(s && (pos < rep->sz || pos == 0));
1389 TestlyxstringInvariant(this);
1391 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1393 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1395 // remember that n can very well be a lot larger than rep->sz
1396 // so we have to ensure that n is no larger than rep->sz
1397 n = min(n, rep->sz);
1398 n2 = min(n2, strlen(s));
1400 return memcmp(&(rep->s[pos]), s, n);
1401 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1414 // i = index, n = length
1415 lyxstring lyxstring::substr(size_type i, size_type n) const
1417 Assert(i < rep->sz || i == 0);
1418 TestlyxstringInvariant(this);
1420 return lyxstring(*this, i, n);
1425 /////////////////////////////////////////////
1426 // String operators, non member functions
1427 /////////////////////////////////////////////
1429 bool operator==(lyxstring const & a, lyxstring const & b)
1431 return a.compare(b) == 0;
1435 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1438 return b.compare(a) == 0;
1442 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1445 return a.compare(b) == 0;
1449 bool operator!=(lyxstring const & a, lyxstring const & b)
1451 return a.compare(b) != 0;
1455 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1458 return b.compare(a) != 0;
1462 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1465 return a.compare(b) != 0;
1469 bool operator>(lyxstring const & a, lyxstring const & b)
1471 return a.compare(b) > 0;
1475 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1478 return b.compare(a) < 0; // since we reverse the parameters
1482 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1485 return a.compare(b) > 0;
1489 bool operator<(lyxstring const & a, lyxstring const & b)
1491 return a.compare(b) < 0;
1495 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1498 return b.compare(a) > 0; // since we reverse the parameters
1502 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1505 return a.compare(b) < 0;
1509 bool operator>=(lyxstring const & a, lyxstring const & b)
1511 return a.compare(b) >= 0;
1515 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1518 return b.compare(a) <= 0; // since we reverse the parameters
1522 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1525 return a.compare(b) >= 0;
1529 bool operator<=(lyxstring const & a, lyxstring const & b)
1531 return a.compare(b) <= 0;
1535 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1538 return b.compare(a) >= 0; // since we reverse the parameters
1542 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1545 return a.compare(b) <= 0;
1549 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1557 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1566 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1575 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1584 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1593 istream & operator>>(istream & is, lyxstring & s)
1595 // very bad solution
1596 char * nome = new char[1024];
1598 lyxstring tmp(nome);
1600 if (!tmp.empty()) s = tmp;
1604 ostream & operator<<(ostream & o, lyxstring const & s)
1606 return o.write(s.data(), s.length());
1609 istream & getline(istream & is, lyxstring & s,
1610 lyxstring::value_type delim)
1612 // very bad solution