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 * ======================================================*/
15 // If we use STL string, there is no need to compile this class.
16 #ifdef USE_INCLUDED_STRING
19 #pragma implementation "lyxstring.h"
22 #include "lyxstring.h"
31 // Reference count has been checked, empty_rep removed and
32 // introduced again in a similar guise. Where is empty_rep _really_
35 // Insertion and replace is implemented, as far as I can see everything
36 // works, but could perhaps be done smarter.
38 // All the different find functions need a good look over.
39 // I have so far not tested them extensively and would be
40 // happy if others took the time to have a peek.
43 #warning temporarily here for debugging purposes only
45 lyxstring::size_type lyxstring::size() const
51 //--------------------------------------------------------------------------
55 /** Testing of the lyxstring invariant
56 * By creating an object that tests the lyxstring invariant during its
57 * construction *and* its deconstruction we greatly simplify our code.
58 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
59 * will test the invariant upon entry to the code. If the Asserts fail
60 * then we know from the stack trace that the corruption occurred *before*
61 * entry to this method. We can also be sure it didn't happen in any of
62 * the tested lyxstring methods. It is therefore likely to be due to some
63 * other external force.
64 * Several lyxstring methods have multiple exit points which would otherwise
65 * require us to insert a separate test before each return. But since we
66 * created an object its destructor will be called upon exit (any exit!).
67 * We thus get testing at both start and end of a method with one line of
68 * code at the head of a method. More importantly, we get good testing
69 * everytime we run the code.
70 * NOTE: just because we test the invariant doesn't mean we can forget
71 * about testing pre and post conditions specific to any given method.
72 * This test simply proves that the lyxstring/Srep is in a valid state it
73 * does *not* prove that the method did what it was supposed to.
75 class lyxstringInvariant
78 lyxstringInvariant(lyxstring const *);
79 ~lyxstringInvariant();
82 lyxstring const * object;
85 // To test if this scheme works "as advertised" uncomment the printf's in
86 // the constructor and destructor below and then uncomment the printf and the
87 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
88 // The correct output when LyX has been recompiled and run is:
89 // lyxstringInvariant constructor
90 // lyxstring::operator=(char const *)
91 // lyxstringInvariant constructor
92 // lyxstringInvariant destructor completed
93 // lyxstringInvariant destructor completed
94 // NOTE: The easiest way to catch this snippet of the output is to wait for
95 // the splash screen to disappear and then open and close Help->Credits
97 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
99 // printf("lyxstringInvariant constructor\n");
103 lyxstringInvariant::~lyxstringInvariant()
106 // printf("lyxstringInvariant destructor completed\n");
109 void lyxstringInvariant::helper() const
111 // Some of these tests might look pointless but they are
112 // all part of the invariant and if we want to make sure
113 // we have a bullet proof implementation then we need to
114 // test every last little thing we *know* should be true.
115 // I may have missed a test or two, so feel free to fill
116 // in the gaps. ARRae.
117 // NOTE: Don't put TestlyxstringInvariant() in any of the
118 // lyxstring methods used below otherwise you'll get an
119 // infinite recursion and a crash.
122 Assert(object->rep->s); // s is never 0
123 Assert(object->rep->res); // always some space allocated
124 Assert(object->size() <= object->rep->res);
125 Assert(object->rep->ref >= 1); // its in use so it must be referenced
126 Assert(object->rep->ref < (1 << 8*sizeof(lyxstring::Srep::ref)) - 1);
127 // if it does ever == then we should be generating a new copy
128 // and starting again. (Is char always 8-bits?)
130 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
132 #define TestlyxstringInvariant(s)
133 #endif //DEVEL_VERSION
134 //-------------------------------------------------------------------------
136 ///////////////////////////////////////
137 // Constructors and Deconstructors.
138 ///////////////////////////////////////
141 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
143 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
148 s = new value_type[res + 1]; // add space for terminator
150 // if sz = 0 nothing gets copied and we have an error
153 // possibly allows for large but empty string
154 sz = 0; // this line should be redundant
160 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
165 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 large but empty string
175 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
177 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
183 s = new value_type[res + 1]; // add space for terminator
188 // if sz = 0 nothing gets copied and we have an error
192 sz = 0; // this line should be redundant
198 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
204 s = new value_type[res + 1]; // add space for terminator
208 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
209 // allows for a large empty string
215 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
217 register unsigned int const len = sz + asz;
220 value_type * tmp = new value_type[res + 1];
222 memcpy(tmp + sz, p, asz);
227 memcpy(s + sz, p, asz);
233 void lyxstring::Srep::push_back(value_type c)
235 s[sz] = c; // it is always room to put a value_type at the end
239 value_type * tmp = new value_type[res + 1];
247 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
248 lyxstring::size_type n)
253 value_type * tmp = new value_type[res + 1];
255 memcpy(tmp + pos, p, n);
256 memcpy(tmp + pos + n, & s[pos], sz - pos);
261 memmove(s + pos + n, & s[pos], sz - pos);
262 memcpy(s + pos, p, n);
268 void lyxstring::Srep::resize(size_type n, value_type c)
271 // This resets sz to res_arg
272 res = min(n, npos - 2); // We keep no xtra when we resize
273 value_type * tmp = new value_type[res + 1];
274 memcpy(tmp, s, min(sz, res));
276 memset(tmp + sz, c, res - sz);
283 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
285 // This keeps the old sz, but
286 // increases res with res_arg
288 value_type * tmp = new value_type[res + 1];
295 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
296 value_type const * p, size_type n2)
298 // can be called with p=0 and n2=0
299 Assert(i < sz && ((!p && !n2) || p));
302 if (res >= n2 + sz) {
303 memmove(s + i + n2, &s[i + n], sz - i);
304 memcpy(s + i, p, n2);
307 res = sz + n2 + xtra;
308 value_type * tmp = new value_type[res + 1];
310 memcpy(tmp + i, p, n2);
311 memcpy(tmp + i + n2, &s[i + n], sz - i);
319 lyxstring::size_type const lyxstring::npos = static_cast<lyxstring::size_type>(-1);
321 lyxstring::lyxstring()
323 static Srep empty_rep(0, "");
329 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
331 Assert(pos < x.rep->sz || pos == 0);
332 if (pos == 0 && n >= x.length()) { // this is the default
336 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
341 lyxstring::lyxstring(value_type const * s, size_type n)
343 Assert(s); // we don't allow null pointers
344 static Srep empty_rep(0, "");
345 if (*s && n) { // s is not empty string and n > 0
346 rep = new Srep(min(strlen(s), n), s);
354 lyxstring::lyxstring(value_type const * s)
356 Assert(s); // we don't allow null pointers
357 static Srep empty_rep(0, "");
358 if (*s) { // s is not empty string
359 rep = new Srep(strlen(s), s);
367 lyxstring::lyxstring(size_type n, value_type c)
369 rep = new Srep(n, c);
373 lyxstring::lyxstring(iterator first, iterator last)
375 rep = new Srep(last - first, first);
379 ///////////////////////
381 ///////////////////////
383 lyxstring::iterator lyxstring::begin()
389 lyxstring::const_iterator lyxstring::begin() const
395 lyxstring::iterator lyxstring::end()
397 return rep->s + rep->sz;
401 lyxstring::const_iterator lyxstring::end() const
403 return rep->s + rep->sz;
407 reverse_iterator lyxstring::rbegin()
409 return reverse_iterator( end() );
413 const_reverse_iterator lyxstring::rbegin() const
415 return const_reverse_iterator( end() );
419 reverse_iterator lyxstring::rend()
421 return reverse_iterator( begin() );
425 const_reverse_iterator lyxstring::rend() const
427 return const_reverse_iterator( begin() );
431 ///////////////////////
433 ///////////////////////
435 void lyxstring::resize(size_type n, value_type c)
437 TestlyxstringInvariant(this);
439 // This resets sz to res_arg
440 rep = rep->get_own_copy();
445 lyxstring::size_type lyxstring::capacity() const
451 void lyxstring::reserve(size_type res_arg)
453 TestlyxstringInvariant(this);
455 rep = rep->get_own_copy();
456 rep->reserve(res_arg);
464 lyxstring & lyxstring::operator=(lyxstring const & x)
466 TestlyxstringInvariant(this);
472 lyxstring & lyxstring::operator=(value_type const * s)
475 TestlyxstringInvariant(this);
476 // printf("lyxstring::operator=(value_type const *)\n");
482 lyxstring & lyxstring::operator=(value_type c)
484 TestlyxstringInvariant(this);
488 if (rep->ref == 1) // recycle rep
492 rep = new Srep(1, s);
498 lyxstring & lyxstring::assign(lyxstring const & x)
500 TestlyxstringInvariant(this);
502 x.rep->ref++; // protect against ``st = st''
503 if (--rep->ref == 0) delete rep;
504 rep = x.rep; // share representation
509 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
511 TestlyxstringInvariant(this);
513 return assign(x.substr(pos, n));
517 lyxstring & lyxstring::assign(value_type const * s, size_type n)
520 TestlyxstringInvariant(this);
522 n = min(strlen(s), n);
523 if (rep->ref == 1) // recycle rep
527 rep = new Srep(n, s);
533 lyxstring & lyxstring::assign(value_type const * s)
536 TestlyxstringInvariant(this);
538 return assign(s, strlen(s));
542 lyxstring & lyxstring::assign(size_type n, value_type ch)
544 TestlyxstringInvariant(this);
546 rep = rep->get_own_copy();
552 lyxstring & lyxstring::assign(iterator first, iterator last)
554 TestlyxstringInvariant(this);
556 rep = rep->get_own_copy();
557 rep->assign(last - first, first);
566 lyxstring::const_reference lyxstring::operator[](size_type pos) const
568 Assert(pos < rep->sz);
574 lyxstring::reference lyxstring::operator[](size_type pos)
576 Assert(pos < rep->sz);
577 TestlyxstringInvariant(this);
579 rep = rep->get_own_copy();
584 lyxstring::const_reference lyxstring::at(size_type n) const
592 lyxstring::reference lyxstring::at(size_type n)
595 TestlyxstringInvariant(this);
597 rep = rep->get_own_copy();
606 lyxstring & lyxstring::operator+=(lyxstring const & x)
608 TestlyxstringInvariant(this);
614 lyxstring & lyxstring::operator+=(value_type const * x)
617 TestlyxstringInvariant(this);
623 lyxstring & lyxstring::operator+=(value_type c)
625 TestlyxstringInvariant(this);
632 void lyxstring::push_back(value_type c)
634 TestlyxstringInvariant(this);
636 rep = rep->get_own_copy();
641 lyxstring & lyxstring::append(lyxstring const & x)
643 TestlyxstringInvariant(this);
645 if (x.empty()) return *this;
646 rep = rep->get_own_copy();
647 rep->append(x.length(), x.rep->s);
652 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
654 TestlyxstringInvariant(this);
656 return append(x.substr(pos, n));
660 lyxstring & lyxstring::append(value_type const * p, size_type n)
663 TestlyxstringInvariant(this);
665 if (!*p || !n) return *this;
666 rep = rep->get_own_copy();
667 rep->append(min(n, strlen(p)), p);
672 lyxstring & lyxstring::append(value_type const * p)
675 TestlyxstringInvariant(this);
677 if (!*p) return *this;
678 rep = rep->get_own_copy();
679 rep->append(strlen(p), p);
684 lyxstring & lyxstring::append(size_type n, value_type c)
686 TestlyxstringInvariant(this);
688 value_type * tmp = new value_type[n];
690 rep = rep->get_own_copy();
697 lyxstring & lyxstring::append(iterator first, iterator last)
699 TestlyxstringInvariant(this);
701 rep = rep->get_own_copy();
702 rep->append(last - first, first);
706 // insert value_typeacters before (*this)[pos]
708 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
710 TestlyxstringInvariant(this);
712 return insert(pos, x, 0, x.rep->sz);
716 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
717 size_type pos2, size_type n)
719 TestlyxstringInvariant(this);
721 rep = rep->get_own_copy();
722 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
727 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
730 TestlyxstringInvariant(this);
733 // insert nothing and you change nothing
734 rep = rep->get_own_copy();
735 rep->insert(pos, p, min(n, strlen(p)));
741 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
744 TestlyxstringInvariant(this);
747 // insert nothing and you change nothing
748 rep = rep->get_own_copy();
749 rep->insert(pos, p, strlen(p));
755 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
757 TestlyxstringInvariant(this);
759 rep = rep->get_own_copy();
760 value_type * tmp = new value_type[n];
762 rep->insert(pos, tmp, n);
768 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
770 TestlyxstringInvariant(this);
772 // what iterator is this supposed to return??
773 size_type tmp = p - begin();
774 insert(p - begin(), 1, c);
775 return begin() + tmp + 1; // ??
779 void lyxstring::insert(iterator p, size_type n , value_type c)
781 TestlyxstringInvariant(this);
783 insert(p - begin(), n , c);
787 void lyxstring::insert(iterator p, iterator first, iterator last)
789 TestlyxstringInvariant(this);
791 insert(p - begin(), first, last - first);
799 // All the below find functions should be verified,
800 // it is very likely that I have mixed up or interpreted
801 // some of the parameters wrong, also some of the funcs can surely
802 // be written more effectively.
804 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
806 if (!rep->sz) return npos;
809 TestlyxstringInvariant(this);
811 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
812 // search until (*this)[i] == a[0]
813 if (rep->s[t] == a[0]) {
814 // check if the rest of the value_types match
816 for (size_type j = 0; j < a.length(); ++j) {
817 if (rep->s[t + j] != a[j]) {
829 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
833 if (!rep->sz || !*ptr) return npos;
836 TestlyxstringInvariant(this);
838 // What is "n" here? is it the number of value_types to use in ptr
839 // or does "i" and "n" togeter form a substring to search
840 // for ptr in? For now I will assume that "n" tells the length
842 n = min(n, strlen(ptr));
843 for (size_type t = i; length() - t >= n; ++t) {
844 // search until (*this)[i] == a[0]
845 if (rep->s[t] == ptr[0]) {
846 // check if the rest of the value_types match
848 for (size_type j = 0; j < n; ++j) {
849 if (rep->s[t + j] != ptr[j]) {
861 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
864 if (!rep->sz) return npos;
867 TestlyxstringInvariant(this);
869 if (!s || !*s) return npos;
870 return find(s, i, strlen(s));
874 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
876 if (!rep->sz) return npos;
879 TestlyxstringInvariant(this);
881 for (size_type t = 0; t + i < length(); ++t) {
882 if (rep->s[t + i] == c) return t + i;
888 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
890 TestlyxstringInvariant(this);
892 size_type ii = min(length() - 1, i);
894 if (a[a.length() - 1] == rep->s[ii]) {
895 int t = length() - 2;
896 size_type l = ii - 1;
897 for (; t >= 0; --t, --l) {
898 if (a[t] != rep->s[l]) break;
900 if (a[t] == rep->s[l]) return l;
907 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
911 TestlyxstringInvariant(this);
912 if (!*ptr) return npos;
914 size_type ii = min(length() - 1, i);
916 if (ptr[n - 1] == rep->s[ii]) {
918 size_type l = ii - 1;
919 for (; t >= 0; --t, --l) {
920 if (ptr[t] != rep->s[l]) break;
922 if (ptr[t] == rep->s[l]) return l;
929 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
932 TestlyxstringInvariant(this);
933 if (!*ptr) return npos;
935 size_type ii = min(length() - 1, i);
937 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
938 int t = strlen(ptr) - 2;
939 size_type l = ii - 1;
940 for (; t >= 0; --t, --l) {
941 if (ptr[t] != rep->s[l]) break;
943 if (ptr[t] == rep->s[l]) return l;
950 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
952 TestlyxstringInvariant(this);
954 size_type ii = min(length() - 1, i);
955 for (size_type t = ii; t != 0; --t) {
956 if (rep->s[t] == c) return t;
962 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
966 TestlyxstringInvariant(this);
968 for (size_type t = i; t < length(); ++t) {
969 if (a.find(rep->s[t]) != npos) return t;
975 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
978 Assert(ptr && i < rep->sz);
979 TestlyxstringInvariant(this);
982 for (size_type t = i; t < length(); ++t) {
983 if(memchr(ptr, rep->s[t], n) != 0) return t;
989 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
992 Assert(ptr && i < rep->sz);
993 TestlyxstringInvariant(this);
995 for (size_type t = i; t < length(); ++t) {
996 if (strchr(ptr, rep->s[t]) != 0) return t;
1002 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1004 Assert(i < rep->sz);
1005 TestlyxstringInvariant(this);
1007 for (size_type t = i; t < length(); ++t) {
1008 if (rep->s[t] == c) return t;
1014 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1017 TestlyxstringInvariant(this);
1019 size_type ii = min(length() - 1, i);
1020 for (int t = ii; t >= 0; --t) {
1021 if (a.find(rep->s[t]) != npos) return t;
1026 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1030 TestlyxstringInvariant(this);
1031 if (!n) return npos;
1033 size_type ii = min(length() - 1, i);
1034 for (int t = ii; t >= 0; --t) {
1035 if(memchr(ptr, rep->s[t], n) != 0) return t;
1041 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1045 TestlyxstringInvariant(this);
1047 size_type ii = min(length() - 1, i);
1048 for (int t = ii; t >= 0; --t) {
1049 if (strchr(ptr, rep->s[t]) != 0) return t;
1055 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1057 TestlyxstringInvariant(this);
1059 if (!rep->sz) return npos;
1060 size_type ii = min(length() - 1, i);
1061 for (int t = ii; t >= 0; --t) {
1062 if (rep->s[t] == c) return t;
1068 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1071 TestlyxstringInvariant(this);
1073 if (!rep->sz) return npos;
1074 Assert(i < rep->sz);
1075 for (size_type t = i; t < length(); ++t) {
1076 if (a.find(rep->s[t]) == npos) return t;
1082 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1085 Assert(ptr && i < rep->sz);
1086 TestlyxstringInvariant(this);
1088 if (!n) return (i < length()) ? i : npos;
1089 for (size_type t = i; t < length(); ++t) {
1090 if(memchr(ptr, rep->s[t], n) == 0) return t;
1096 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1099 Assert(ptr && i < rep->sz);
1100 TestlyxstringInvariant(this);
1102 for (size_type t = i; t < length(); ++t) {
1103 if (strchr(ptr, rep->s[t]) == 0) return t;
1109 lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const
1111 if (!rep->sz) return npos;
1112 Assert(i < rep->sz);
1113 TestlyxstringInvariant(this);
1115 for (size_type t = i; t < length(); ++t) {
1116 if (rep->s[t] != c) return t;
1122 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1125 TestlyxstringInvariant(this);
1127 size_type ii = min(length() - 1, i);
1128 for (int t = ii; t >= 0; --t) {
1129 if (a.find(rep->s[t]) == npos) return t;
1135 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, size_type i,
1139 TestlyxstringInvariant(this);
1141 size_type ii = min(length() - 1, i);
1142 if (!n) return (ii >= 0) ? ii : npos;
1143 for (int t = ii; t >= 0; --t) {
1144 if(memchr(ptr, rep->s[t], n) == 0) return t;
1150 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1154 TestlyxstringInvariant(this);
1156 size_type ii = min(length() - 1, i);
1157 for (int t = ii; t >= 0; --t) {
1158 if (strchr(ptr, rep->s[t]) == 0) return t;
1164 lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) const
1166 TestlyxstringInvariant(this);
1168 size_type ii = min(length() - 1, i);
1169 for (int t = ii; t >= 0; --t) {
1170 if (rep->s[t] != c) return t;
1180 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1182 Assert(i < rep->sz || i == 0);
1183 TestlyxstringInvariant(this);
1185 return replace(i, n, x, 0, x.length());
1189 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1190 size_type i2, size_type n2)
1192 Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1193 TestlyxstringInvariant(this);
1195 rep = rep->get_own_copy();
1196 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1201 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
1204 Assert(p && i < rep->sz);
1205 TestlyxstringInvariant(this);
1207 rep = rep->get_own_copy();
1208 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1213 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1215 Assert(p && i < rep->sz);
1216 TestlyxstringInvariant(this);
1218 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1222 lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c)
1224 Assert(i < rep->sz);
1225 TestlyxstringInvariant(this);
1227 rep = rep->get_own_copy();
1228 value_type * tmp = new value_type[n2];
1230 rep->replace(i, min(n, rep->sz), tmp, n2);
1236 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1238 TestlyxstringInvariant(this);
1240 return replace(i - begin(), i2 - i, str);
1244 lyxstring & lyxstring::replace(iterator i, iterator i2,
1245 value_type const * p, size_type n)
1248 TestlyxstringInvariant(this);
1250 return replace(i - begin(), i2 - i, p, n);
1254 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1257 TestlyxstringInvariant(this);
1259 return replace(i - begin(), i2 - i, p);
1263 lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type c)
1265 TestlyxstringInvariant(this);
1267 return replace(i - begin(), i2 - i, n, c);
1271 lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
1273 TestlyxstringInvariant(this);
1275 return replace(i - begin(), i2 - i, j, j2 - j);
1279 lyxstring & lyxstring::erase(size_type i, size_type n)
1281 Assert(i < rep->sz || i == 0);
1282 TestlyxstringInvariant(this);
1284 rep = rep->get_own_copy();
1285 if (i == 0 && n >= rep->sz) {
1288 n = min(n, rep->sz - i);
1289 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1296 lyxstring::iterator lyxstring::erase(iterator i)
1298 TestlyxstringInvariant(this);
1300 // what iterator is this supposed to return?
1301 // the iterator after the one erased
1302 erase(i - begin(), 1);
1303 return begin(); // BUG
1307 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1309 TestlyxstringInvariant(this);
1311 erase(first - begin(), last - first);
1312 return begin(); // BUG
1316 /////////////////////////////////////
1317 // Conversion to C-style Strings
1318 /////////////////////////////////////
1320 lyxstring::value_type const * lyxstring::c_str() const
1322 rep->s[length()] = '\0';
1327 lyxstring::value_type const * lyxstring::data() const
1333 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const
1336 TestlyxstringInvariant(this);
1338 register int nn = min(len, length() - pos);
1339 memcpy(buf, &(rep->s[pos]), nn);
1344 ////////////////////
1346 ////////////////////
1348 // Compare funcs should be verified.
1349 // Should we try to make them work with '\0' value_types?
1350 // An STL string can usually contain '\0' value_types.
1352 int lyxstring::compare(lyxstring const & str) const
1354 TestlyxstringInvariant(this);
1356 return compare(0, rep->sz, str.rep->s, str.rep->sz);
1360 int lyxstring::compare(value_type const * s) const
1363 TestlyxstringInvariant(this);
1365 return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1369 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1371 TestlyxstringInvariant(this);
1373 return compare(pos, n, str.rep->s, str.rep->sz);
1377 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1378 size_type pos2, size_type n2) const
1380 TestlyxstringInvariant(this);
1382 return compare(pos, n, str.rep->s + pos2, n2);
1386 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1389 Assert(s && (pos < rep->sz || pos == 0));
1390 TestlyxstringInvariant(this);
1392 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1394 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1396 // remember that n can very well be a lot larger than rep->sz
1397 // so we have to ensure that n is no larger than rep->sz
1398 n = min(n, rep->sz);
1399 n2 = min(n2, strlen(s));
1401 return memcmp(&(rep->s[pos]), s, n);
1402 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1415 // i = index, n = length
1416 lyxstring lyxstring::substr(size_type i, size_type n) const
1418 Assert(i < rep->sz || i == 0);
1419 TestlyxstringInvariant(this);
1421 return lyxstring(*this, i, n);
1426 /////////////////////////////////////////////
1427 // String operators, non member functions
1428 /////////////////////////////////////////////
1430 bool operator==(lyxstring const & a, lyxstring const & b)
1432 return a.compare(b) == 0;
1436 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1439 return b.compare(a) == 0;
1443 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1446 return a.compare(b) == 0;
1450 bool operator!=(lyxstring const & a, lyxstring const & b)
1452 return a.compare(b) != 0;
1456 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1459 return b.compare(a) != 0;
1463 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1466 return a.compare(b) != 0;
1470 bool operator>(lyxstring const & a, lyxstring const & b)
1472 return a.compare(b) > 0;
1476 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1479 return b.compare(a) < 0; // since we reverse the parameters
1483 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1486 return a.compare(b) > 0;
1490 bool operator<(lyxstring const & a, lyxstring const & b)
1492 return a.compare(b) < 0;
1496 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1499 return b.compare(a) > 0; // since we reverse the parameters
1503 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1506 return a.compare(b) < 0;
1510 bool operator>=(lyxstring const & a, lyxstring const & b)
1512 return a.compare(b) >= 0;
1516 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1519 return b.compare(a) <= 0; // since we reverse the parameters
1523 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1526 return a.compare(b) >= 0;
1530 bool operator<=(lyxstring const & a, lyxstring const & b)
1532 return a.compare(b) <= 0;
1536 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1539 return b.compare(a) >= 0; // since we reverse the parameters
1543 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1546 return a.compare(b) <= 0;
1550 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1558 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1567 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1576 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1585 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1594 istream & operator>>(istream & is, lyxstring & s)
1596 // very bad solution
1599 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
1626 #endif /* USE_INCLUDED_STRING */