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(lyxstring::Srep::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 && *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 // yes we allow them just don't initalize them
354 // Assert(s); // we don't allow null pointers
355 static Srep empty_rep(0, "");
356 if (s && *s) { // s is not empty string
357 rep = new Srep(strlen(s), s);
365 lyxstring::lyxstring(size_type n, value_type c)
367 rep = new Srep(n, c);
371 lyxstring::lyxstring(iterator first, iterator last)
373 rep = new Srep(last - first, first);
377 ///////////////////////
379 ///////////////////////
381 lyxstring::iterator lyxstring::begin()
387 lyxstring::const_iterator lyxstring::begin() const
393 lyxstring::iterator lyxstring::end()
395 return rep->s + rep->sz;
399 lyxstring::const_iterator lyxstring::end() const
401 return rep->s + rep->sz;
405 reverse_iterator lyxstring::rbegin()
407 return reverse_iterator( end() );
411 const_reverse_iterator lyxstring::rbegin() const
413 return const_reverse_iterator( end() );
417 reverse_iterator lyxstring::rend()
419 return reverse_iterator( begin() );
423 const_reverse_iterator lyxstring::rend() const
425 return const_reverse_iterator( begin() );
429 ///////////////////////
431 ///////////////////////
433 void lyxstring::resize(size_type n, value_type c)
435 TestlyxstringInvariant(this);
437 // This resets sz to res_arg
438 rep = rep->get_own_copy();
443 lyxstring::size_type lyxstring::capacity() const
449 void lyxstring::reserve(size_type res_arg)
451 TestlyxstringInvariant(this);
453 rep = rep->get_own_copy();
454 rep->reserve(res_arg);
462 lyxstring & lyxstring::operator=(lyxstring const & x)
464 TestlyxstringInvariant(this);
470 lyxstring & lyxstring::operator=(value_type const * s)
473 TestlyxstringInvariant(this);
474 // printf("lyxstring::operator=(value_type const *)\n");
480 lyxstring & lyxstring::operator=(value_type c)
482 TestlyxstringInvariant(this);
486 if (rep->ref == 1) // recycle rep
490 rep = new Srep(1, s);
496 lyxstring & lyxstring::assign(lyxstring const & x)
498 TestlyxstringInvariant(this);
500 x.rep->ref++; // protect against ``st = st''
501 if (--rep->ref == 0) delete rep;
502 rep = x.rep; // share representation
507 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
509 TestlyxstringInvariant(this);
511 return assign(x.substr(pos, n));
515 lyxstring & lyxstring::assign(value_type const * s, size_type n)
518 TestlyxstringInvariant(this);
520 n = min(strlen(s), n);
521 if (rep->ref == 1) // recycle rep
525 rep = new Srep(n, s);
531 lyxstring & lyxstring::assign(value_type const * s)
534 TestlyxstringInvariant(this);
536 return assign(s, strlen(s));
540 lyxstring & lyxstring::assign(size_type n, value_type ch)
542 TestlyxstringInvariant(this);
544 rep = rep->get_own_copy();
550 lyxstring & lyxstring::assign(iterator first, iterator last)
552 TestlyxstringInvariant(this);
554 rep = rep->get_own_copy();
555 rep->assign(last - first, first);
564 lyxstring::const_reference lyxstring::operator[](size_type pos) const
566 Assert(pos < rep->sz);
572 lyxstring::reference lyxstring::operator[](size_type pos)
574 Assert(pos < rep->sz);
575 TestlyxstringInvariant(this);
577 rep = rep->get_own_copy();
582 lyxstring::const_reference lyxstring::at(size_type n) const
590 lyxstring::reference lyxstring::at(size_type n)
593 TestlyxstringInvariant(this);
595 rep = rep->get_own_copy();
604 lyxstring & lyxstring::operator+=(lyxstring const & x)
606 TestlyxstringInvariant(this);
612 lyxstring & lyxstring::operator+=(value_type const * x)
615 TestlyxstringInvariant(this);
621 lyxstring & lyxstring::operator+=(value_type c)
623 TestlyxstringInvariant(this);
630 void lyxstring::push_back(value_type c)
632 TestlyxstringInvariant(this);
634 rep = rep->get_own_copy();
639 lyxstring & lyxstring::append(lyxstring const & x)
641 TestlyxstringInvariant(this);
643 if (x.empty()) return *this;
644 rep = rep->get_own_copy();
645 rep->append(x.length(), x.rep->s);
650 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
652 TestlyxstringInvariant(this);
654 return append(x.substr(pos, n));
658 lyxstring & lyxstring::append(value_type const * p, size_type n)
661 TestlyxstringInvariant(this);
663 if (!*p || !n) return *this;
664 rep = rep->get_own_copy();
665 rep->append(min(n, strlen(p)), p);
670 lyxstring & lyxstring::append(value_type const * p)
673 TestlyxstringInvariant(this);
675 if (!*p) return *this;
676 rep = rep->get_own_copy();
677 rep->append(strlen(p), p);
682 lyxstring & lyxstring::append(size_type n, value_type c)
684 TestlyxstringInvariant(this);
686 value_type * tmp = new value_type[n];
688 rep = rep->get_own_copy();
695 lyxstring & lyxstring::append(iterator first, iterator last)
697 TestlyxstringInvariant(this);
699 rep = rep->get_own_copy();
700 rep->append(last - first, first);
704 // insert value_typeacters before (*this)[pos]
706 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
708 TestlyxstringInvariant(this);
710 return insert(pos, x, 0, x.rep->sz);
714 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
715 size_type pos2, size_type n)
717 TestlyxstringInvariant(this);
719 rep = rep->get_own_copy();
720 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
725 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
728 TestlyxstringInvariant(this);
731 // insert nothing and you change nothing
732 rep = rep->get_own_copy();
733 rep->insert(pos, p, min(n, strlen(p)));
739 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
742 TestlyxstringInvariant(this);
745 // insert nothing and you change nothing
746 rep = rep->get_own_copy();
747 rep->insert(pos, p, strlen(p));
753 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
755 TestlyxstringInvariant(this);
757 rep = rep->get_own_copy();
758 value_type * tmp = new value_type[n];
760 rep->insert(pos, tmp, n);
766 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
768 TestlyxstringInvariant(this);
770 // what iterator is this supposed to return??
771 size_type tmp = p - begin();
772 insert(p - begin(), 1, c);
773 return begin() + tmp + 1; // ??
777 void lyxstring::insert(iterator p, size_type n , value_type c)
779 TestlyxstringInvariant(this);
781 insert(p - begin(), n , c);
785 void lyxstring::insert(iterator p, iterator first, iterator last)
787 TestlyxstringInvariant(this);
789 insert(p - begin(), first, last - first);
797 // All the below find functions should be verified,
798 // it is very likely that I have mixed up or interpreted
799 // some of the parameters wrong, also some of the funcs can surely
800 // be written more effectively.
802 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
804 if (!rep->sz) return npos;
807 TestlyxstringInvariant(this);
809 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
810 // search until (*this)[i] == a[0]
811 if (rep->s[t] == a[0]) {
812 // check if the rest of the value_types match
814 for (size_type j = 0; j < a.length(); ++j) {
815 if (rep->s[t + j] != a[j]) {
827 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
831 if (!rep->sz || !*ptr) return npos;
834 TestlyxstringInvariant(this);
836 // What is "n" here? is it the number of value_types to use in ptr
837 // or does "i" and "n" togeter form a substring to search
838 // for ptr in? For now I will assume that "n" tells the length
840 n = min(n, strlen(ptr));
841 for (size_type t = i; length() - t >= n; ++t) {
842 // search until (*this)[i] == a[0]
843 if (rep->s[t] == ptr[0]) {
844 // check if the rest of the value_types match
846 for (size_type j = 0; j < n; ++j) {
847 if (rep->s[t + j] != ptr[j]) {
859 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
862 if (!rep->sz) return npos;
865 TestlyxstringInvariant(this);
867 if (!s || !*s) return npos;
868 return find(s, i, strlen(s));
872 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
874 if (!rep->sz) return npos;
877 TestlyxstringInvariant(this);
879 for (size_type t = 0; t + i < length(); ++t) {
880 if (rep->s[t + i] == c) return t + i;
886 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
888 TestlyxstringInvariant(this);
890 size_type ii = min(length() - 1, i);
892 if (a[a.length() - 1] == rep->s[ii]) {
893 int t = length() - 2;
894 size_type l = ii - 1;
895 for (; t >= 0; --t, --l) {
896 if (a[t] != rep->s[l]) break;
898 if (a[t] == rep->s[l]) return l;
905 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
909 TestlyxstringInvariant(this);
910 if (!*ptr) return npos;
912 size_type ii = min(length() - 1, i);
914 if (ptr[n - 1] == rep->s[ii]) {
916 size_type l = ii - 1;
917 for (; t >= 0; --t, --l) {
918 if (ptr[t] != rep->s[l]) break;
920 if (ptr[t] == rep->s[l]) return l;
927 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
930 TestlyxstringInvariant(this);
931 if (!*ptr) return npos;
933 size_type ii = min(length() - 1, i);
935 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
936 int t = strlen(ptr) - 2;
937 size_type l = ii - 1;
938 for (; t >= 0; --t, --l) {
939 if (ptr[t] != rep->s[l]) break;
941 if (ptr[t] == rep->s[l]) return l;
948 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
950 TestlyxstringInvariant(this);
952 size_type ii = min(length() - 1, i);
953 for (size_type t = ii; t != 0; --t) {
954 if (rep->s[t] == c) return t;
960 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
964 TestlyxstringInvariant(this);
966 for (size_type t = i; t < length(); ++t) {
967 if (a.find(rep->s[t]) != npos) return t;
973 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
976 Assert(ptr && i < rep->sz);
977 TestlyxstringInvariant(this);
980 for (size_type t = i; t < length(); ++t) {
981 if(memchr(ptr, rep->s[t], n) != 0) return t;
987 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
990 Assert(ptr && i < rep->sz);
991 TestlyxstringInvariant(this);
993 for (size_type t = i; t < length(); ++t) {
994 if (strchr(ptr, rep->s[t]) != 0) return t;
1000 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1002 Assert(i < rep->sz);
1003 TestlyxstringInvariant(this);
1005 for (size_type t = i; t < length(); ++t) {
1006 if (rep->s[t] == c) return t;
1012 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1015 TestlyxstringInvariant(this);
1017 size_type ii = min(length() - 1, i);
1018 for (int t = ii; t >= 0; --t) {
1019 if (a.find(rep->s[t]) != npos) return t;
1024 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1028 TestlyxstringInvariant(this);
1029 if (!n) return npos;
1031 size_type ii = min(length() - 1, i);
1032 for (int t = ii; t >= 0; --t) {
1033 if(memchr(ptr, rep->s[t], n) != 0) return t;
1039 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1043 TestlyxstringInvariant(this);
1045 size_type ii = min(length() - 1, i);
1046 for (int t = ii; t >= 0; --t) {
1047 if (strchr(ptr, rep->s[t]) != 0) return t;
1053 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1055 TestlyxstringInvariant(this);
1057 if (!rep->sz) return npos;
1058 size_type ii = min(length() - 1, i);
1059 for (int t = ii; t >= 0; --t) {
1060 if (rep->s[t] == c) return t;
1066 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1069 TestlyxstringInvariant(this);
1071 if (!rep->sz) return npos;
1072 Assert(i < rep->sz);
1073 for (size_type t = i; t < length(); ++t) {
1074 if (a.find(rep->s[t]) == npos) return t;
1080 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1083 Assert(ptr && i < rep->sz);
1084 TestlyxstringInvariant(this);
1086 if (!n) return (i < length()) ? i : npos;
1087 for (size_type t = i; t < length(); ++t) {
1088 if(memchr(ptr, rep->s[t], n) == 0) return t;
1094 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1097 Assert(ptr && i < rep->sz);
1098 TestlyxstringInvariant(this);
1100 for (size_type t = i; t < length(); ++t) {
1101 if (strchr(ptr, rep->s[t]) == 0) return t;
1107 lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const
1109 if (!rep->sz) return npos;
1110 Assert(i < rep->sz);
1111 TestlyxstringInvariant(this);
1113 for (size_type t = i; t < length(); ++t) {
1114 if (rep->s[t] != c) return t;
1120 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1123 TestlyxstringInvariant(this);
1125 size_type ii = min(length() - 1, i);
1126 for (int t = ii; t >= 0; --t) {
1127 if (a.find(rep->s[t]) == npos) return t;
1133 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, size_type i,
1137 TestlyxstringInvariant(this);
1139 size_type ii = min(length() - 1, i);
1140 if (!n) return (ii >= 0) ? ii : npos;
1141 for (int t = ii; t >= 0; --t) {
1142 if(memchr(ptr, rep->s[t], n) == 0) return t;
1148 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1152 TestlyxstringInvariant(this);
1154 size_type ii = min(length() - 1, i);
1155 for (int t = ii; t >= 0; --t) {
1156 if (strchr(ptr, rep->s[t]) == 0) return t;
1162 lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) const
1164 TestlyxstringInvariant(this);
1166 size_type ii = min(length() - 1, i);
1167 for (int t = ii; t >= 0; --t) {
1168 if (rep->s[t] != c) return t;
1178 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1180 Assert(i < rep->sz || i == 0);
1181 TestlyxstringInvariant(this);
1183 return replace(i, n, x, 0, x.length());
1187 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1188 size_type i2, size_type n2)
1190 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1191 TestlyxstringInvariant(this);
1193 rep = rep->get_own_copy();
1194 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1199 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
1202 Assert(p && i < rep->sz);
1203 TestlyxstringInvariant(this);
1205 rep = rep->get_own_copy();
1206 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1211 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1213 Assert(p && i < rep->sz);
1214 TestlyxstringInvariant(this);
1216 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1220 lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c)
1222 Assert(i < rep->sz);
1223 TestlyxstringInvariant(this);
1225 rep = rep->get_own_copy();
1226 value_type * tmp = new value_type[n2];
1228 rep->replace(i, min(n, rep->sz), tmp, n2);
1234 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1236 TestlyxstringInvariant(this);
1238 return replace(i - begin(), i2 - i, str);
1242 lyxstring & lyxstring::replace(iterator i, iterator i2,
1243 value_type const * p, size_type n)
1246 TestlyxstringInvariant(this);
1248 return replace(i - begin(), i2 - i, p, n);
1252 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1255 TestlyxstringInvariant(this);
1257 return replace(i - begin(), i2 - i, p);
1261 lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type c)
1263 TestlyxstringInvariant(this);
1265 return replace(i - begin(), i2 - i, n, c);
1269 lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
1271 TestlyxstringInvariant(this);
1273 return replace(i - begin(), i2 - i, j, j2 - j);
1277 lyxstring & lyxstring::erase(size_type i, size_type n)
1279 Assert(i < rep->sz || i == 0);
1280 TestlyxstringInvariant(this);
1282 rep = rep->get_own_copy();
1283 if (i == 0 && n >= rep->sz) {
1286 n = min(n, rep->sz - i);
1287 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1294 lyxstring::iterator lyxstring::erase(iterator i)
1296 TestlyxstringInvariant(this);
1298 // what iterator is this supposed to return?
1299 // the iterator after the one erased
1300 erase(i - begin(), 1);
1301 return begin(); // BUG
1305 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1307 TestlyxstringInvariant(this);
1309 erase(first - begin(), last - first);
1310 return begin(); // BUG
1314 /////////////////////////////////////
1315 // Conversion to C-style Strings
1316 /////////////////////////////////////
1318 lyxstring::value_type const * lyxstring::c_str() const
1320 rep->s[length()] = '\0';
1325 lyxstring::value_type const * lyxstring::data() const
1331 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const
1334 TestlyxstringInvariant(this);
1336 register int nn = min(len, length() - pos);
1337 memcpy(buf, &(rep->s[pos]), nn);
1342 ////////////////////
1344 ////////////////////
1346 // Compare funcs should be verified.
1347 // Should we try to make them work with '\0' value_types?
1348 // An STL string can usually contain '\0' value_types.
1350 int lyxstring::compare(lyxstring const & str) const
1352 TestlyxstringInvariant(this);
1354 return compare(0, rep->sz, str.rep->s, str.rep->sz);
1358 int lyxstring::compare(value_type const * s) const
1361 TestlyxstringInvariant(this);
1363 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1367 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1369 TestlyxstringInvariant(this);
1371 return compare(pos, n, str.rep->s, str.rep->sz);
1375 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1376 size_type pos2, size_type n2) const
1378 TestlyxstringInvariant(this);
1380 return compare(pos, n, str.rep->s + pos2, n2);
1384 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1387 Assert(s && (pos < rep->sz || pos == 0));
1388 TestlyxstringInvariant(this);
1390 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1392 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1394 // remember that n can very well be a lot larger than rep->sz
1395 // so we have to ensure that n is no larger than rep->sz
1396 n = min(n, rep->sz);
1397 n2 = min(n2, strlen(s));
1399 return memcmp(&(rep->s[pos]), s, n);
1400 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1413 // i = index, n = length
1414 lyxstring lyxstring::substr(size_type i, size_type n) const
1416 Assert(i < rep->sz || i == 0);
1417 TestlyxstringInvariant(this);
1419 return lyxstring(*this, i, n);
1424 /////////////////////////////////////////////
1425 // String operators, non member functions
1426 /////////////////////////////////////////////
1428 bool operator==(lyxstring const & a, lyxstring const & b)
1430 return a.compare(b) == 0;
1434 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1437 return b.compare(a) == 0;
1441 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1444 return a.compare(b) == 0;
1448 bool operator!=(lyxstring const & a, lyxstring const & b)
1450 return a.compare(b) != 0;
1454 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1457 return b.compare(a) != 0;
1461 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1464 return a.compare(b) != 0;
1468 bool operator>(lyxstring const & a, lyxstring const & b)
1470 return a.compare(b) > 0;
1474 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1477 return b.compare(a) < 0; // since we reverse the parameters
1481 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1484 return a.compare(b) > 0;
1488 bool operator<(lyxstring const & a, lyxstring const & b)
1490 return a.compare(b) < 0;
1494 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1497 return b.compare(a) > 0; // since we reverse the parameters
1501 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1504 return a.compare(b) < 0;
1508 bool operator>=(lyxstring const & a, lyxstring const & b)
1510 return a.compare(b) >= 0;
1514 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1517 return b.compare(a) <= 0; // since we reverse the parameters
1521 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1524 return a.compare(b) >= 0;
1528 bool operator<=(lyxstring const & a, lyxstring const & b)
1530 return a.compare(b) <= 0;
1534 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1537 return b.compare(a) >= 0; // since we reverse the parameters
1541 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1544 return a.compare(b) <= 0;
1548 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1556 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1565 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1574 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1583 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1592 istream & operator>>(istream & is, lyxstring & s)
1594 // very bad solution
1597 lyxstring tmp(nome);
1598 if (!tmp.empty()) s = tmp;
1602 ostream & operator<<(ostream & o, lyxstring const & s)
1604 return o.write(s.data(), s.length());
1607 istream & getline(istream & is, lyxstring & s,
1608 lyxstring::value_type delim)
1610 // very bad solution