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"
26 // Reference count has been checked, empty_rep removed and
27 // introduced again in a similar guise. Where is empty_rep _really_
30 // Insertion and replace is implemented, as far as I can see everything
31 // works, but could perhaps be done smarter.
33 // All the different find functions need a good look over.
34 // I have so far not tested them extensively and would be
35 // happy if others took the time to have a peek.
38 #warning temporarily here for debugging purposes only
40 lyxstring::size_type lyxstring::size() const
46 //--------------------------------------------------------------------------
50 /** Testing of the lyxstring invariant
51 * By creating an object that tests the lyxstring invariant during its
52 * construction *and* its deconstruction we greatly simplify our code.
53 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
54 * will test the invariant upon entry to the code. If the Asserts fail
55 * then we know from the stack trace that the corruption occurred *before*
56 * entry to this method. We can also be sure it didn't happen in any of
57 * the tested lyxstring methods. It is therefore likely to be due to some
58 * other external force.
59 * Several lyxstring methods have multiple exit points which would otherwise
60 * require us to insert a separate test before each return. But since we
61 * created an object its destructor will be called upon exit (any exit!).
62 * We thus get testing at both start and end of a method with one line of
63 * code at the head of a method. More importantly, we get good testing
64 * everytime we run the code.
65 * NOTE: just because we test the invariant doesn't mean we can forget
66 * about testing pre and post conditions specific to any given method.
67 * This test simply proves that the lyxstring/Srep is in a valid state it
68 * does *not* prove that the method did what it was supposed to.
70 class lyxstringInvariant
73 lyxstringInvariant(lyxstring const *);
74 ~lyxstringInvariant();
77 lyxstring const * object;
80 // To test if this scheme works "as advertised" uncomment the printf's in
81 // the constructor and destructor below and then uncomment the printf and the
82 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
83 // The correct output when LyX has been recompiled and run is:
84 // lyxstringInvariant constructor
85 // lyxstring::operator=(char const *)
86 // lyxstringInvariant constructor
87 // lyxstringInvariant destructor completed
88 // lyxstringInvariant destructor completed
89 // NOTE: The easiest way to catch this snippet of the output is to wait for
90 // the splash screen to disappear and then open and close Help->Credits
92 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
94 // printf("lyxstringInvariant constructor\n");
98 lyxstringInvariant::~lyxstringInvariant()
101 // printf("lyxstringInvariant destructor completed\n");
104 void lyxstringInvariant::helper() const
106 // Some of these tests might look pointless but they are
107 // all part of the invariant and if we want to make sure
108 // we have a bullet proof implementation then we need to
109 // test every last little thing we *know* should be true.
110 // I may have missed a test or two, so feel free to fill
111 // in the gaps. ARRae.
112 // NOTE: Don't put TestlyxstringInvariant() in any of the
113 // lyxstring methods used below otherwise you'll get an
114 // infinite recursion and a crash.
117 Assert(object->rep->s); // s is never 0
118 Assert(object->rep->res); // always some space allocated
119 Assert(strlen(object->c_str()) == object->size());
120 Assert(object->size() <= object->rep->res);
121 Assert(object->rep->ref >= 1); // its in use so it must be referenced
122 Assert(object->rep->ref < (1 << 8*sizeof(lyxstring::Srep::ref)) - 1);
123 // if it does ever == then we should be generating a new copy
124 // and starting again. (Is char always 8-bits?)
126 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
128 #define TestlyxstringInvariant(s)
129 #endif //DEVEL_VERSION
130 //-------------------------------------------------------------------------
132 ///////////////////////////////////////
133 // Constructors and Deconstructors.
134 ///////////////////////////////////////
137 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
139 // 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
146 // if sz = 0 nothing gets copied and we have an error
149 // possibly allows for large but empty string
150 sz = 0; // this line should be redundant
156 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
161 s = new value_type[res + 1]; // add space for terminator
164 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
165 // allows for large but empty string
171 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
173 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
179 s = new value_type[res + 1]; // add space for terminator
184 // if sz = 0 nothing gets copied and we have an error
188 sz = 0; // this line should be redundant
194 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
200 s = new value_type[res + 1]; // add space for terminator
204 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
205 // allows for a large empty string
211 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
213 register unsigned int const len = sz + asz;
216 value_type * tmp = new value_type[res + 1];
218 memcpy(tmp + sz, p, asz);
223 memcpy(s + sz, p, asz);
229 void lyxstring::Srep::push_back(value_type c)
231 s[sz] = c; // it is always room to put a value_type at the end
235 value_type * tmp = new value_type[res + 1];
243 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
244 lyxstring::size_type n)
249 value_type * tmp = new value_type[res + 1];
251 memcpy(tmp + pos, p, n);
252 memcpy(tmp + pos + n, & s[pos], sz - pos);
257 memmove(s + pos + n, & s[pos], sz - pos);
258 memcpy(s + pos, p, n);
264 void lyxstring::Srep::resize(size_type n, value_type c)
267 // This resets sz to res_arg
268 res = min(n, npos - 2); // We keep no xtra when we resize
269 value_type * tmp = new value_type[res + 1];
270 memcpy(tmp, s, min(sz, res));
272 memset(tmp + sz, c, res - sz);
279 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
281 // This keeps the old sz, but
282 // increases res with res_arg
284 value_type * tmp = new value_type[res + 1];
291 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
292 value_type const * p, size_type n2)
294 // can be called with p=0 and n2=0
295 Assert(i < sz && ((!p && !n2) || p));
298 if (res >= n2 + sz) {
299 memmove(s + i + n2, &s[i + n], sz - i);
300 memcpy(s + i, p, n2);
303 res = sz + n2 + xtra;
304 value_type * tmp = new value_type[res + 1];
306 memcpy(tmp + i, p, n2);
307 memcpy(tmp + i + n2, &s[i + n], sz - i);
315 lyxstring::size_type const lyxstring::npos = static_cast<lyxstring::size_type>(-1);
317 lyxstring::lyxstring()
319 static Srep empty_rep(0, "");
325 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
327 Assert(pos < x.rep->sz || pos == 0);
328 if (pos == 0 && n >= x.length()) { // this is the default
332 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
337 lyxstring::lyxstring(value_type const * s, size_type n)
339 Assert(s); // we don't allow null pointers
340 static Srep empty_rep(0, "");
341 if (*s && n) { // s is not empty string and n > 0
342 rep = new Srep(min(strlen(s), n), s);
350 lyxstring::lyxstring(value_type const * s)
352 Assert(s); // we don't allow null pointers
353 static Srep empty_rep(0, "");
354 if (*s) { // s is not empty string
355 rep = new Srep(strlen(s), s);
363 lyxstring::lyxstring(size_type n, value_type c)
365 rep = new Srep(n, c);
369 lyxstring::lyxstring(iterator first, iterator last)
371 rep = new Srep(last - first, first);
375 ///////////////////////
377 ///////////////////////
379 lyxstring::iterator lyxstring::begin()
385 lyxstring::const_iterator lyxstring::begin() const
391 lyxstring::iterator lyxstring::end()
393 return rep->s + rep->sz;
397 lyxstring::const_iterator lyxstring::end() const
399 return rep->s + rep->sz;
403 reverse_iterator lyxstring::rbegin()
405 return reverse_iterator( end() );
409 const_reverse_iterator lyxstring::rbegin() const
411 return const_reverse_iterator( end() );
415 reverse_iterator lyxstring::rend()
417 return reverse_iterator( begin() );
421 const_reverse_iterator lyxstring::rend() const
423 return const_reverse_iterator( begin() );
427 ///////////////////////
429 ///////////////////////
431 void lyxstring::resize(size_type n, value_type c)
433 TestlyxstringInvariant(this);
435 // This resets sz to res_arg
436 rep = rep->get_own_copy();
441 lyxstring::size_type lyxstring::capacity() const
447 void lyxstring::reserve(size_type res_arg)
449 TestlyxstringInvariant(this);
451 rep = rep->get_own_copy();
452 rep->reserve(res_arg);
460 lyxstring & lyxstring::operator=(lyxstring const & x)
462 TestlyxstringInvariant(this);
468 lyxstring & lyxstring::operator=(value_type const * s)
471 TestlyxstringInvariant(this);
472 // printf("lyxstring::operator=(value_type const *)\n");
478 lyxstring & lyxstring::operator=(value_type c)
480 TestlyxstringInvariant(this);
484 if (rep->ref == 1) // recycle rep
488 rep = new Srep(1, s);
494 lyxstring & lyxstring::assign(lyxstring const & x)
496 TestlyxstringInvariant(this);
498 x.rep->ref++; // protect against ``st = st''
499 if (--rep->ref == 0) delete rep;
500 rep = x.rep; // share representation
505 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
507 TestlyxstringInvariant(this);
509 return assign(x.substr(pos, n));
513 lyxstring & lyxstring::assign(value_type const * s, size_type n)
516 TestlyxstringInvariant(this);
518 n = min(strlen(s), n);
519 if (rep->ref == 1) // recycle rep
523 rep = new Srep(n, s);
529 lyxstring & lyxstring::assign(value_type const * s)
532 TestlyxstringInvariant(this);
534 return assign(s, strlen(s));
538 lyxstring & lyxstring::assign(size_type n, value_type ch)
540 TestlyxstringInvariant(this);
542 rep = rep->get_own_copy();
548 lyxstring & lyxstring::assign(iterator first, iterator last)
550 TestlyxstringInvariant(this);
552 rep = rep->get_own_copy();
553 rep->assign(last - first, first);
562 lyxstring::const_reference lyxstring::operator[](size_type pos) const
564 Assert(pos < rep->sz);
570 lyxstring::reference lyxstring::operator[](size_type pos)
572 Assert(pos < rep->sz);
573 TestlyxstringInvariant(this);
575 rep = rep->get_own_copy();
580 lyxstring::const_reference lyxstring::at(size_type n) const
588 lyxstring::reference lyxstring::at(size_type n)
591 TestlyxstringInvariant(this);
593 rep = rep->get_own_copy();
602 lyxstring & lyxstring::operator+=(lyxstring const & x)
604 TestlyxstringInvariant(this);
610 lyxstring & lyxstring::operator+=(value_type const * x)
613 TestlyxstringInvariant(this);
619 lyxstring & lyxstring::operator+=(value_type c)
621 TestlyxstringInvariant(this);
628 void lyxstring::push_back(value_type c)
630 TestlyxstringInvariant(this);
632 rep = rep->get_own_copy();
637 lyxstring & lyxstring::append(lyxstring const & x)
639 TestlyxstringInvariant(this);
641 if (x.empty()) return *this;
642 rep = rep->get_own_copy();
643 rep->append(x.length(), x.rep->s);
648 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
650 TestlyxstringInvariant(this);
652 return append(x.substr(pos, n));
656 lyxstring & lyxstring::append(value_type const * p, size_type n)
659 TestlyxstringInvariant(this);
661 if (!*p || !n) return *this;
662 rep = rep->get_own_copy();
663 rep->append(min(n, strlen(p)), p);
668 lyxstring & lyxstring::append(value_type const * p)
671 TestlyxstringInvariant(this);
673 if (!*p) return *this;
674 rep = rep->get_own_copy();
675 rep->append(strlen(p), p);
680 lyxstring & lyxstring::append(size_type n, value_type c)
682 TestlyxstringInvariant(this);
684 value_type * tmp = new value_type[n];
686 rep = rep->get_own_copy();
693 lyxstring & lyxstring::append(iterator first, iterator last)
695 TestlyxstringInvariant(this);
697 rep = rep->get_own_copy();
698 rep->append(last - first, first);
702 // insert value_typeacters before (*this)[pos]
704 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
706 TestlyxstringInvariant(this);
708 return insert(pos, x, 0, x.rep->sz);
712 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
713 size_type pos2, size_type n)
715 TestlyxstringInvariant(this);
717 rep = rep->get_own_copy();
718 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
723 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
726 TestlyxstringInvariant(this);
729 // insert nothing and you change nothing
730 rep = rep->get_own_copy();
731 rep->insert(pos, p, min(n, strlen(p)));
737 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
740 TestlyxstringInvariant(this);
743 // insert nothing and you change nothing
744 rep = rep->get_own_copy();
745 rep->insert(pos, p, strlen(p));
751 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
753 TestlyxstringInvariant(this);
755 rep = rep->get_own_copy();
756 value_type * tmp = new value_type[n];
758 rep->insert(pos, tmp, n);
764 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
766 TestlyxstringInvariant(this);
768 // what iterator is this supposed to return??
769 size_type tmp = p - begin();
770 insert(p - begin(), 1, c);
771 return begin() + tmp + 1; // ??
775 void lyxstring::insert(iterator p, size_type n , value_type c)
777 TestlyxstringInvariant(this);
779 insert(p - begin(), n , c);
783 void lyxstring::insert(iterator p, iterator first, iterator last)
785 TestlyxstringInvariant(this);
787 insert(p - begin(), first, last - first);
795 // All the below find functions should be verified,
796 // it is very likely that I have mixed up or interpreted
797 // some of the parameters wrong, also some of the funcs can surely
798 // be written more effectively.
800 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
802 if (!rep->sz) return npos;
805 TestlyxstringInvariant(this);
807 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
808 // search until (*this)[i] == a[0]
809 if (rep->s[t] == a[0]) {
810 // check if the rest of the value_types match
812 for (size_type j = 0; j < a.length(); ++j) {
813 if (rep->s[t + j] != a[j]) {
825 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
829 if (!rep->sz || !*ptr) return npos;
832 TestlyxstringInvariant(this);
834 // What is "n" here? is it the number of value_types to use in ptr
835 // or does "i" and "n" togeter form a substring to search
836 // for ptr in? For now I will assume that "n" tells the length
838 n = min(n, strlen(ptr));
839 for (size_type t = i; length() - t >= n; ++t) {
840 // search until (*this)[i] == a[0]
841 if (rep->s[t] == ptr[0]) {
842 // check if the rest of the value_types match
844 for (size_type j = 0; j < n; ++j) {
845 if (rep->s[t + j] != ptr[j]) {
857 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
860 if (!rep->sz) return npos;
863 TestlyxstringInvariant(this);
865 if (!s || !*s) return npos;
866 return find(s, i, strlen(s));
870 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
872 if (!rep->sz) return npos;
875 TestlyxstringInvariant(this);
877 for (size_type t = 0; t + i < length(); ++t) {
878 if (rep->s[t + i] == c) return t + i;
884 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
886 TestlyxstringInvariant(this);
888 size_type ii = min(length() - 1, i);
890 if (a[a.length() - 1] == rep->s[ii]) {
891 int t = length() - 2;
892 size_type l = ii - 1;
893 for (; t >= 0; --t, --l) {
894 if (a[t] != rep->s[l]) break;
896 if (a[t] == rep->s[l]) return l;
903 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
907 TestlyxstringInvariant(this);
908 if (!*ptr) return npos;
910 size_type ii = min(length() - 1, i);
912 if (ptr[n - 1] == rep->s[ii]) {
914 size_type l = ii - 1;
915 for (; t >= 0; --t, --l) {
916 if (ptr[t] != rep->s[l]) break;
918 if (ptr[t] == rep->s[l]) return l;
925 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
928 TestlyxstringInvariant(this);
929 if (!*ptr) return npos;
931 size_type ii = min(length() - 1, i);
933 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
934 int t = strlen(ptr) - 2;
935 size_type l = ii - 1;
936 for (; t >= 0; --t, --l) {
937 if (ptr[t] != rep->s[l]) break;
939 if (ptr[t] == rep->s[l]) return l;
946 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
948 TestlyxstringInvariant(this);
950 size_type ii = min(length() - 1, i);
951 for (size_type t = ii; t != 0; --t) {
952 if (rep->s[t] == c) return t;
958 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
962 TestlyxstringInvariant(this);
964 for (size_type t = i; t < length(); ++t) {
965 if (a.find(rep->s[t]) != npos) return t;
971 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
974 Assert(ptr && i < rep->sz);
975 TestlyxstringInvariant(this);
978 for (size_type t = i; t < length(); ++t) {
979 if(memchr(ptr, rep->s[t], n) != 0) return t;
985 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
988 Assert(ptr && i < rep->sz);
989 TestlyxstringInvariant(this);
991 for (size_type t = i; t < length(); ++t) {
992 if (strchr(ptr, rep->s[t]) != 0) return t;
998 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1000 Assert(i < rep->sz);
1001 TestlyxstringInvariant(this);
1003 for (size_type t = i; t < length(); ++t) {
1004 if (rep->s[t] == c) return t;
1010 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1013 TestlyxstringInvariant(this);
1015 size_type ii = min(length() - 1, i);
1016 for (int t = ii; t >= 0; --t) {
1017 if (a.find(rep->s[t]) != npos) return t;
1022 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1026 TestlyxstringInvariant(this);
1027 if (!n) return npos;
1029 size_type ii = min(length() - 1, i);
1030 for (int t = ii; t >= 0; --t) {
1031 if(memchr(ptr, rep->s[t], n) != 0) return t;
1037 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1041 TestlyxstringInvariant(this);
1043 size_type ii = min(length() - 1, i);
1044 for (int t = ii; t >= 0; --t) {
1045 if (strchr(ptr, rep->s[t]) != 0) return t;
1051 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1053 TestlyxstringInvariant(this);
1055 if (!rep->sz) return npos;
1056 size_type ii = min(length() - 1, i);
1057 for (int t = ii; t >= 0; --t) {
1058 if (rep->s[t] == c) return t;
1064 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1067 TestlyxstringInvariant(this);
1069 if (!rep->sz) return npos;
1070 Assert(i < rep->sz);
1071 for (size_type t = i; t < length(); ++t) {
1072 if (a.find(rep->s[t]) == npos) return t;
1078 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1081 Assert(ptr && i < rep->sz);
1082 TestlyxstringInvariant(this);
1084 if (!n) return (i < length()) ? i : npos;
1085 for (size_type t = i; t < length(); ++t) {
1086 if(memchr(ptr, rep->s[t], n) == 0) return t;
1092 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1095 Assert(ptr && i < rep->sz);
1096 TestlyxstringInvariant(this);
1098 for (size_type t = i; t < length(); ++t) {
1099 if (strchr(ptr, rep->s[t]) == 0) return t;
1105 lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const
1107 if (!rep->sz) return npos;
1108 Assert(i < rep->sz);
1109 TestlyxstringInvariant(this);
1111 for (size_type t = i; t < length(); ++t) {
1112 if (rep->s[t] != c) return t;
1118 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1121 TestlyxstringInvariant(this);
1123 size_type ii = min(length() - 1, i);
1124 for (int t = ii; t >= 0; --t) {
1125 if (a.find(rep->s[t]) == npos) return t;
1131 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, size_type i,
1135 TestlyxstringInvariant(this);
1137 size_type ii = min(length() - 1, i);
1138 if (!n) return (ii >= 0) ? ii : npos;
1139 for (int t = ii; t >= 0; --t) {
1140 if(memchr(ptr, rep->s[t], n) == 0) return t;
1146 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1150 TestlyxstringInvariant(this);
1152 size_type ii = min(length() - 1, i);
1153 for (int t = ii; t >= 0; --t) {
1154 if (strchr(ptr, rep->s[t]) == 0) return t;
1160 lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) const
1162 TestlyxstringInvariant(this);
1164 size_type ii = min(length() - 1, i);
1165 for (int t = ii; t >= 0; --t) {
1166 if (rep->s[t] != c) return t;
1176 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1178 Assert(i < rep->sz || i == 0);
1179 TestlyxstringInvariant(this);
1181 return replace(i, n, x, 0, x.length());
1185 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1186 size_type i2, size_type n2)
1188 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1189 TestlyxstringInvariant(this);
1191 rep = rep->get_own_copy();
1192 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1197 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
1200 Assert(p && i < rep->sz);
1201 TestlyxstringInvariant(this);
1203 rep = rep->get_own_copy();
1204 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1209 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1211 Assert(p && i < rep->sz);
1212 TestlyxstringInvariant(this);
1214 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1218 lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c)
1220 Assert(i < rep->sz);
1221 TestlyxstringInvariant(this);
1223 rep = rep->get_own_copy();
1224 value_type * tmp = new value_type[n2];
1226 rep->replace(i, min(n, rep->sz), tmp, n2);
1232 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1234 TestlyxstringInvariant(this);
1236 return replace(i - begin(), i2 - i, str);
1240 lyxstring & lyxstring::replace(iterator i, iterator i2,
1241 value_type const * p, size_type n)
1244 TestlyxstringInvariant(this);
1246 return replace(i - begin(), i2 - i, p, n);
1250 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1253 TestlyxstringInvariant(this);
1255 return replace(i - begin(), i2 - i, p);
1259 lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type c)
1261 TestlyxstringInvariant(this);
1263 return replace(i - begin(), i2 - i, n, c);
1267 lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
1269 TestlyxstringInvariant(this);
1271 return replace(i - begin(), i2 - i, j, j2 - j);
1275 lyxstring & lyxstring::erase(size_type i, size_type n)
1277 Assert(i < rep->sz || i == 0);
1278 TestlyxstringInvariant(this);
1280 rep = rep->get_own_copy();
1281 if (i == 0 && n >= rep->sz) {
1284 n = min(n, rep->sz - i);
1285 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1292 lyxstring::iterator lyxstring::erase(iterator i)
1294 TestlyxstringInvariant(this);
1296 // what iterator is this supposed to return?
1297 // the iterator after the one erased
1298 erase(i - begin(), 1);
1299 return begin(); // BUG
1303 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1305 TestlyxstringInvariant(this);
1307 erase(first - begin(), last - first);
1308 return begin(); // BUG
1312 /////////////////////////////////////
1313 // Conversion to C-style Strings
1314 /////////////////////////////////////
1316 lyxstring::value_type const * lyxstring::c_str() const
1318 rep->s[length()] = '\0';
1323 lyxstring::value_type const * lyxstring::data() const
1329 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const
1332 TestlyxstringInvariant(this);
1334 register int nn = min(len, length() - pos);
1335 memcpy(buf, &(rep->s[pos]), nn);
1340 ////////////////////
1342 ////////////////////
1344 // Compare funcs should be verified.
1345 // Should we try to make them work with '\0' value_types?
1346 // An STL string can usually contain '\0' value_types.
1348 int lyxstring::compare(lyxstring const & str) const
1350 TestlyxstringInvariant(this);
1352 return compare(0, rep->sz, str.rep->s, str.rep->sz);
1356 int lyxstring::compare(value_type const * s) const
1359 TestlyxstringInvariant(this);
1361 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1365 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1367 TestlyxstringInvariant(this);
1369 return compare(pos, n, str.rep->s, str.rep->sz);
1373 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1374 size_type pos2, size_type n2) const
1376 TestlyxstringInvariant(this);
1378 return compare(pos, n, str.rep->s + pos2, n2);
1382 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1385 Assert(s && (pos < rep->sz || pos == 0));
1386 TestlyxstringInvariant(this);
1388 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1390 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1392 // remember that n can very well be a lot larger than rep->sz
1393 // so we have to ensure that n is no larger than rep->sz
1394 n = min(n, rep->sz);
1395 n2 = min(n2, strlen(s));
1397 return memcmp(&(rep->s[pos]), s, n);
1398 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1411 // i = index, n = length
1412 lyxstring lyxstring::substr(size_type i, size_type n) const
1414 Assert(i < rep->sz || i == 0);
1415 TestlyxstringInvariant(this);
1417 return lyxstring(*this, i, n);
1422 /////////////////////////////////////////////
1423 // String operators, non member functions
1424 /////////////////////////////////////////////
1426 bool operator==(lyxstring const & a, lyxstring const & b)
1428 return a.compare(b) == 0;
1432 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1435 return b.compare(a) == 0;
1439 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1442 return a.compare(b) == 0;
1446 bool operator!=(lyxstring const & a, lyxstring const & b)
1448 return a.compare(b) != 0;
1452 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1455 return b.compare(a) != 0;
1459 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1462 return a.compare(b) != 0;
1466 bool operator>(lyxstring const & a, lyxstring const & b)
1468 return a.compare(b) > 0;
1472 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1475 return b.compare(a) < 0; // since we reverse the parameters
1479 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1482 return a.compare(b) > 0;
1486 bool operator<(lyxstring const & a, lyxstring const & b)
1488 return a.compare(b) < 0;
1492 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1495 return b.compare(a) > 0; // since we reverse the parameters
1499 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1502 return a.compare(b) < 0;
1506 bool operator>=(lyxstring const & a, lyxstring const & b)
1508 return a.compare(b) >= 0;
1512 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1515 return b.compare(a) <= 0; // since we reverse the parameters
1519 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1522 return a.compare(b) >= 0;
1526 bool operator<=(lyxstring const & a, lyxstring const & b)
1528 return a.compare(b) <= 0;
1532 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1535 return b.compare(a) >= 0; // since we reverse the parameters
1539 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1542 return a.compare(b) <= 0;
1546 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1554 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1563 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1572 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1581 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1590 istream & operator>>(istream & is, lyxstring & s)
1592 // very bad solution
1595 lyxstring tmp(nome);
1596 if (!tmp.empty()) s = tmp;
1600 ostream & operator<<(ostream & o, lyxstring const & s)
1602 return o.write(s.data(), s.length());
1605 istream & getline(istream & is, lyxstring & s,
1606 lyxstring::value_type delim = '\n')
1608 // very bad solution