1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1999 The LyX Team.
9 * ======================================================*/
16 #pragma implementation "lyxstring.h"
19 #include "lyxstring.h"
28 // This class is supposed to be functionaly equivalent to a
29 // standard conformant string. This mean among others that we
30 // are useing the same requirements. Before you change anything
31 // in this file consult me and/or the standard to discover the
34 // Asserts with a STD! are required by the standard.
35 // Asserts with a OURS! are added by me.
36 // Reference count has been checked, empty_rep removed and
37 // introduced again in a similar guise. Where is empty_rep _really_
40 // Insertion and replace is implemented, as far as I can see everything
41 // works, but could perhaps be done smarter.
43 // All the different find functions need a good look over.
44 // I have so far not tested them extensively and would be
45 // happy if others took the time to have a peek.
49 ///////////////////////////////////////
50 // The internal string representation
51 ///////////////////////////////////////
53 struct lyxstring::Srep {
55 static size_t const xtra = static_cast<size_t>(8);
60 /// The total amount of data reserved for this representaion
62 /// Data. At least 1 char for trailing null.
63 lyxstring::value_type * s;
66 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
68 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
70 ~Srep() { delete[] s; }
74 if (ref == 1) return this;
76 return new Srep(sz, s);
80 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
82 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
84 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
86 void push_back(lyxstring::value_type c);
88 void insert(lyxstring::size_type pos,
89 const lyxstring::value_type * p,
90 lyxstring::size_type n);
92 void resize(lyxstring::size_type n, lyxstring::value_type c);
94 void reserve(lyxstring::size_type res_arg);
96 void replace(lyxstring::size_type i, lyxstring::size_type n,
97 lyxstring::value_type const * p, lyxstring::size_type n2);
100 Srep & operator=(const Srep &);
104 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
106 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
111 s = new value_type[res + 1]; // add space for terminator
113 // if sz = 0 nothing gets copied and we have an error
116 // possibly allows for large but empty string
117 sz = 0; // this line should be redundant
123 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
128 s = new value_type[res + 1]; // add space for terminator
131 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
132 // allows for large but empty string
138 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
140 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
146 s = new value_type[res + 1]; // add space for terminator
151 // if sz = 0 nothing gets copied and we have an error
155 sz = 0; // this line should be redundant
161 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
167 s = new value_type[res + 1]; // add space for terminator
171 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
172 // allows for a large empty string
178 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
180 register unsigned int const len = sz + asz;
183 value_type * tmp = new value_type[res + 1];
185 memcpy(tmp + sz, p, asz);
190 memcpy(s + sz, p, asz);
196 void lyxstring::Srep::push_back(value_type c)
198 s[sz] = c; // it is always room to put a value_type at the end
202 value_type * tmp = new value_type[res + 1];
210 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
211 lyxstring::size_type n)
215 value_type * tmp = new value_type[res + 1];
217 memcpy(tmp + pos, p, n);
218 memcpy(tmp + pos + n, & s[pos], sz - pos);
223 memmove(s + pos + n, & s[pos], sz - pos);
224 memcpy(s + pos, p, n);
230 void lyxstring::Srep::resize(size_type n, value_type c)
232 // This resets sz to res_arg
233 res = min(n, npos - 2); // We keep no xtra when we resize
234 value_type * tmp = new value_type[res + 1];
235 memcpy(tmp, s, min(sz, res));
237 memset(tmp + sz, c, res - sz);
244 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
246 // This keeps the old sz, but
247 // increases res with res_arg
249 value_type * tmp = new value_type[res + 1];
256 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
257 value_type const * p, size_type n2)
259 // can be called with p=0 and n2=0
262 if (res >= n2 + sz) {
263 memmove(s + i + n2, &s[i + n], sz - i);
264 memcpy(s + i, p, n2);
267 res = sz + n2 + xtra;
268 value_type * tmp = new value_type[res + 1];
270 memcpy(tmp + i, p, n2);
271 memcpy(tmp + i + n2, &s[i + n], sz - i);
279 ///////////////////////////////////////
280 // The lyxstring Invariant tester
281 ///////////////////////////////////////
284 /** Testing of the lyxstring invariant
285 * By creating an object that tests the lyxstring invariant during its
286 * construction *and* its deconstruction we greatly simplify our code.
287 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
288 * will test the invariant upon entry to the code. If the Asserts fail
289 * then we know from the stack trace that the corruption occurred *before*
290 * entry to this method. We can also be sure it didn't happen in any of
291 * the tested lyxstring methods. It is therefore likely to be due to some
292 * other external force.
293 * Several lyxstring methods have multiple exit points which would otherwise
294 * require us to insert a separate test before each return. But since we
295 * created an object its destructor will be called upon exit (any exit!).
296 * We thus get testing at both start and end of a method with one line of
297 * code at the head of a method. More importantly, we get good testing
298 * everytime we run the code.
299 * NOTE: just because we test the invariant doesn't mean we can forget
300 * about testing pre and post conditions specific to any given method.
301 * This test simply proves that the lyxstring/Srep is in a valid state it
302 * does *not* prove that the method did what it was supposed to.
304 class lyxstringInvariant {
306 lyxstringInvariant(lyxstring const *);
307 ~lyxstringInvariant();
310 lyxstring const * object;
314 // To test if this scheme works "as advertised" uncomment the printf's in
315 // the constructor and destructor below and then uncomment the printf and the
316 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
317 // The correct output when LyX has been recompiled and run is:
318 // lyxstringInvariant constructor
319 // lyxstring::operator=(char const *)
320 // lyxstringInvariant constructor
321 // lyxstringInvariant destructor completed
322 // lyxstringInvariant destructor completed
323 // NOTE: The easiest way to catch this snippet of the output is to wait for
324 // the splash screen to disappear and then open and close Help->Credits
326 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
328 // printf("lyxstringInvariant constructor\n");
332 lyxstringInvariant::~lyxstringInvariant()
335 // printf("lyxstringInvariant destructor completed\n");
338 void lyxstringInvariant::helper() const
340 // Some of these tests might look pointless but they are
341 // all part of the invariant and if we want to make sure
342 // we have a bullet proof implementation then we need to
343 // test every last little thing we *know* should be true.
344 // I may have missed a test or two, so feel free to fill
345 // in the gaps. ARRae.
348 Assert(object->rep->s); // s is never 0
349 Assert(object->rep->res); // always some space allocated
350 Assert(object->rep->sz <= object->rep->res);
351 Assert(object->rep->ref >= 1); // its in use so it must be referenced
352 Assert(object->rep->ref < static_cast<size_t>(1 << (8 * sizeof(object->rep->ref) - 1)));
353 // if it does ever == then we should be generating a new copy
354 // and starting again. (Is char always 8-bits?)
356 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
358 #define TestlyxstringInvariant(s)
359 #endif //DEVEL_VERSION
362 ///////////////////////////////////////
363 // Constructors and Deconstructors.
364 ///////////////////////////////////////
366 lyxstring::size_type const lyxstring::npos =
367 static_cast<lyxstring::size_type>(-1);
369 lyxstring::lyxstring()
371 static Srep empty_rep(0, "");
377 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
379 Assert(pos <= x.rep->sz); // STD!
380 if (pos == 0 && n >= x.length()) { // this is the default
384 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
389 lyxstring::lyxstring(value_type const * s, size_type n)
391 Assert(s && n < npos); // STD!
392 static Srep empty_rep(0, "");
393 if (*s && n) { // s is not empty string and n > 0
394 rep = new Srep(min(strlen(s), n), s);
402 lyxstring::lyxstring(value_type const * s)
405 static Srep empty_rep(0, "");
406 if (*s) { // s is not empty string
407 rep = new Srep(strlen(s), s);
415 lyxstring::lyxstring(size_type n, value_type c)
417 Assert(n < npos); // STD!
418 rep = new Srep(n, c);
422 lyxstring::lyxstring(iterator first, iterator last)
424 rep = new Srep(last - first, first);
428 lyxstring::~lyxstring()
430 if (--rep->ref == 0) delete rep;
433 ///////////////////////
435 ///////////////////////
437 lyxstring::iterator lyxstring::begin()
443 lyxstring::const_iterator lyxstring::begin() const
449 lyxstring::iterator lyxstring::end()
451 return rep->s + rep->sz;
455 lyxstring::const_iterator lyxstring::end() const
457 return rep->s + rep->sz;
461 reverse_iterator lyxstring::rbegin()
463 return reverse_iterator( end() );
467 const_reverse_iterator lyxstring::rbegin() const
469 return const_reverse_iterator( end() );
473 reverse_iterator lyxstring::rend()
475 return reverse_iterator( begin() );
479 const_reverse_iterator lyxstring::rend() const
481 return const_reverse_iterator( begin() );
486 ///////////////////////
488 ///////////////////////
490 lyxstring::size_type lyxstring::size() const
496 void lyxstring::resize(size_type n, value_type c)
498 Assert(n <= npos); // STD!
499 TestlyxstringInvariant(this);
501 // This resets sz to res_arg
502 rep = rep->get_own_copy();
507 lyxstring::size_type lyxstring::capacity() const
513 void lyxstring::reserve(size_type res_arg)
515 TestlyxstringInvariant(this);
517 rep = rep->get_own_copy();
518 rep->reserve(res_arg);
526 lyxstring & lyxstring::operator=(lyxstring const & x)
528 TestlyxstringInvariant(this);
534 lyxstring & lyxstring::operator=(value_type const * s)
537 TestlyxstringInvariant(this);
538 // printf("lyxstring::operator=(value_type const *)\n");
544 lyxstring & lyxstring::operator=(value_type c)
546 TestlyxstringInvariant(this);
550 if (rep->ref == 1) // recycle rep
554 rep = new Srep(1, s);
560 lyxstring & lyxstring::assign(lyxstring const & x)
562 TestlyxstringInvariant(this);
564 x.rep->ref++; // protect against ``st = st''
565 if (--rep->ref == 0) delete rep;
566 rep = x.rep; // share representation
571 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
573 Assert(pos <= x.rep->sz); // STD!
574 TestlyxstringInvariant(this);
576 return assign(x.substr(pos, n));
580 lyxstring & lyxstring::assign(value_type const * s, size_type n)
583 TestlyxstringInvariant(this);
585 n = min(strlen(s), n);
586 if (rep->ref == 1) // recycle rep
590 rep = new Srep(n, s);
596 lyxstring & lyxstring::assign(value_type const * s)
599 TestlyxstringInvariant(this);
601 return assign(s, strlen(s));
605 lyxstring & lyxstring::assign(size_type n, value_type ch)
607 TestlyxstringInvariant(this);
609 rep = rep->get_own_copy();
615 lyxstring & lyxstring::assign(iterator first, iterator last)
617 TestlyxstringInvariant(this);
619 rep = rep->get_own_copy();
620 rep->assign(last - first, first);
629 lyxstring::const_reference lyxstring::operator[](size_type pos) const
631 Assert(pos <= rep->sz); // OURS!
632 return pos == rep->sz ? '\0' : rep->s[pos];
636 lyxstring::reference lyxstring::operator[](size_type pos)
638 Assert(pos < rep->sz); // OURS!
639 TestlyxstringInvariant(this);
641 rep = rep->get_own_copy();
646 lyxstring::const_reference lyxstring::at(size_type n) const
648 Assert(n < rep->sz); // STD!
653 lyxstring::reference lyxstring::at(size_type n)
655 Assert(n < rep->sz); // STD!
656 TestlyxstringInvariant(this);
658 rep = rep->get_own_copy();
667 lyxstring & lyxstring::operator+=(lyxstring const & x)
669 TestlyxstringInvariant(this);
675 lyxstring & lyxstring::operator+=(value_type const * x)
678 TestlyxstringInvariant(this);
684 lyxstring & lyxstring::operator+=(value_type c)
686 TestlyxstringInvariant(this);
693 void lyxstring::push_back(value_type c)
695 TestlyxstringInvariant(this);
697 rep = rep->get_own_copy();
702 lyxstring & lyxstring::append(lyxstring const & x)
704 TestlyxstringInvariant(this);
706 if (x.empty()) return *this;
707 rep = rep->get_own_copy();
708 rep->append(x.length(), x.rep->s);
713 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
715 Assert(pos <= x.rep->sz); // STD!
716 TestlyxstringInvariant(this);
718 return append(x.substr(pos, n));
722 lyxstring & lyxstring::append(value_type const * p, size_type n)
725 TestlyxstringInvariant(this);
727 if (!*p || !n) return *this;
728 rep = rep->get_own_copy();
729 rep->append(min(n, strlen(p)), p);
734 lyxstring & lyxstring::append(value_type const * p)
737 TestlyxstringInvariant(this);
739 if (!*p) return *this;
740 rep = rep->get_own_copy();
741 rep->append(strlen(p), p);
746 lyxstring & lyxstring::append(size_type n, value_type c)
748 TestlyxstringInvariant(this);
750 value_type * tmp = new value_type[n];
752 rep = rep->get_own_copy();
759 lyxstring & lyxstring::append(iterator first, iterator last)
761 TestlyxstringInvariant(this);
763 rep = rep->get_own_copy();
764 rep->append(last - first, first);
768 // insert value_typeacters before (*this)[pos]
770 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
772 TestlyxstringInvariant(this);
774 return insert(pos, x, 0, x.rep->sz);
778 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
779 size_type pos2, size_type n)
781 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
782 TestlyxstringInvariant(this);
784 rep = rep->get_own_copy();
785 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
790 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
793 TestlyxstringInvariant(this);
796 // insert nothing and you change nothing
797 rep = rep->get_own_copy();
798 rep->insert(pos, p, min(n, strlen(p)));
804 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
807 TestlyxstringInvariant(this);
810 // insert nothing and you change nothing
811 rep = rep->get_own_copy();
812 rep->insert(pos, p, strlen(p));
818 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
820 TestlyxstringInvariant(this);
822 rep = rep->get_own_copy();
823 value_type * tmp = new value_type[n];
825 rep->insert(pos, tmp, n);
831 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
833 TestlyxstringInvariant(this);
835 // what iterator is this supposed to return??
836 size_type tmp = p - begin();
837 insert(p - begin(), 1, c);
838 return begin() + tmp + 1; // ??
842 void lyxstring::insert(iterator p, size_type n , value_type c)
844 TestlyxstringInvariant(this);
846 insert(p - begin(), n , c);
850 void lyxstring::insert(iterator p, iterator first, iterator last)
852 TestlyxstringInvariant(this);
854 insert(p - begin(), first, last - first);
862 // All the below find functions should be verified,
863 // it is very likely that I have mixed up or interpreted
864 // some of the parameters wrong, also some of the funcs can surely
865 // be written more effectively.
867 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
869 if (!rep->sz || i >= rep->sz) return npos;
871 TestlyxstringInvariant(this);
873 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
874 // search until (*this)[i] == a[0]
875 if (rep->s[t] == a[0]) {
876 // check if the rest of the value_types match
878 for (size_type j = 0; j < a.length(); ++j) {
879 if (rep->s[t + j] != a[j]) {
891 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
894 Assert(ptr); // OURS!
895 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
897 TestlyxstringInvariant(this);
899 // What is "n" here? is it the number of value_types to use in ptr
900 // or does "i" and "n" togeter form a substring to search
901 // for ptr in? For now I will assume that "n" tells the length
903 n = min(n, strlen(ptr));
904 for (size_type t = i; rep->sz - t >= n; ++t) {
905 // search until (*this)[i] == a[0]
906 if (rep->s[t] == ptr[0]) {
907 // check if the rest of the value_types match
909 for (size_type j = 0; j < n; ++j) {
910 if (rep->s[t + j] != ptr[j]) {
922 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
925 if (!rep->sz || i >= rep->sz) return npos;
927 TestlyxstringInvariant(this);
929 if (!s || !*s) return npos;
930 return find(s, i, strlen(s));
934 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
936 if (!rep->sz || i >= rep->sz) return npos;
938 TestlyxstringInvariant(this);
940 for (size_type t = 0; t + i < rep->sz; ++t) {
941 if (rep->s[t + i] == c) return t + i;
947 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
949 TestlyxstringInvariant(this);
951 size_type ii = min(rep->sz - 1, i);
953 if (a[a.length() - 1] == rep->s[ii]) {
955 size_type l = ii - 1;
956 for (; t >= 0; --t, --l) {
957 if (a[t] != rep->s[l]) break;
959 if (a[t] == rep->s[l]) return l;
966 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
969 Assert(ptr); // OURS!
970 TestlyxstringInvariant(this);
971 if (!*ptr) return npos;
973 size_type ii = min(rep->sz - 1, i);
975 if (ptr[n - 1] == rep->s[ii]) {
977 size_type l = ii - 1;
978 for (; t >= 0; --t, --l) {
979 if (ptr[t] != rep->s[l]) break;
981 if (ptr[t] == rep->s[l]) return l;
988 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
991 Assert(ptr); // OURS!
992 TestlyxstringInvariant(this);
993 if (!*ptr) return npos;
995 size_type ii = min(rep->sz - 1, i);
997 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
998 int t = strlen(ptr) - 2;
999 size_type l = ii - 1;
1000 for (; t >= 0; --t, --l) {
1001 if (ptr[t] != rep->s[l]) break;
1003 if (ptr[t] == rep->s[l]) return l;
1010 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1012 TestlyxstringInvariant(this);
1014 size_type ii = min(rep->sz - 1, i);
1015 for (size_type t = ii; t != 0; --t) {
1016 if (rep->s[t] == c) return t;
1022 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1025 Assert(i < rep->sz); // OURS!
1026 TestlyxstringInvariant(this);
1028 for (size_type t = i; t < rep->sz; ++t) {
1029 if (a.find(rep->s[t]) != npos) return t;
1035 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1038 Assert(ptr && i < rep->sz); // OURS!
1039 TestlyxstringInvariant(this);
1040 if (!n) return npos;
1042 for (size_type t = i; t < rep->sz; ++t) {
1043 if(memchr(ptr, rep->s[t], n) != 0) return t;
1049 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1052 Assert(ptr && i < rep->sz); // OURS!
1053 TestlyxstringInvariant(this);
1055 for (size_type t = i; t < rep->sz; ++t) {
1056 if (strchr(ptr, rep->s[t]) != 0) return t;
1062 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1064 Assert(i < rep->sz); // OURS!
1065 TestlyxstringInvariant(this);
1067 for (size_type t = i; t < rep->sz; ++t) {
1068 if (rep->s[t] == c) return t;
1074 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1077 TestlyxstringInvariant(this);
1079 size_type ii = min(rep->sz - 1, i);
1080 for (int t = ii; t >= 0; --t) {
1081 if (a.find(rep->s[t]) != npos) return t;
1086 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1089 Assert(ptr); // OURS!
1090 TestlyxstringInvariant(this);
1091 if (!n) return npos;
1093 size_type ii = min(rep->sz - 1, i);
1094 for (int t = ii; t >= 0; --t) {
1095 if(memchr(ptr, rep->s[t], n) != 0) return t;
1101 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1104 Assert(ptr); // OURS!
1105 TestlyxstringInvariant(this);
1107 size_type ii = min(rep->sz - 1, i);
1108 for (int t = ii; t >= 0; --t) {
1109 if (strchr(ptr, rep->s[t]) != 0) return t;
1115 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1117 TestlyxstringInvariant(this);
1119 if (!rep->sz) return npos;
1120 size_type ii = min(rep->sz - 1, i);
1121 for (int t = ii; t >= 0; --t) {
1122 if (rep->s[t] == c) return t;
1128 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1131 TestlyxstringInvariant(this);
1133 if (!rep->sz) return npos;
1134 Assert(i < rep->sz);
1135 for (size_type t = i; t < rep->sz; ++t) {
1136 if (a.find(rep->s[t]) == npos) return t;
1142 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1146 Assert(ptr && i < rep->sz); // OURS!
1147 TestlyxstringInvariant(this);
1149 if (!n) return (i < rep->sz) ? i : npos;
1150 for (size_type t = i; t < rep->sz; ++t) {
1151 if(memchr(ptr, rep->s[t], n) == 0) return t;
1157 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1160 Assert(ptr && i < rep->sz); // OURS!
1161 TestlyxstringInvariant(this);
1163 for (size_type t = i; t < rep->sz; ++t) {
1164 if (strchr(ptr, rep->s[t]) == 0) return t;
1170 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1173 if (!rep->sz) return npos;
1174 Assert(i < rep->sz); // OURS!
1175 TestlyxstringInvariant(this);
1177 for (size_type t = i; t < rep->sz; ++t) {
1178 if (rep->s[t] != c) return t;
1184 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1187 TestlyxstringInvariant(this);
1189 size_type ii = min(rep->sz - 1, i);
1190 for (int t = ii; t >= 0; --t) {
1191 if (a.find(rep->s[t]) == npos) return t;
1197 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1201 Assert(ptr); // OURS!
1202 TestlyxstringInvariant(this);
1204 if (!n) return npos;
1205 size_type ii = min(rep->sz - 1, i);
1207 for (int t = ii; t >= 0; --t) {
1208 if(memchr(ptr, rep->s[t], n) == 0) return t;
1214 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1217 Assert(ptr); // OURS!
1218 TestlyxstringInvariant(this);
1220 size_type ii = min(rep->sz - 1, i);
1221 for (int t = ii; t >= 0; --t) {
1222 if (strchr(ptr, rep->s[t]) == 0) return t;
1228 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1231 TestlyxstringInvariant(this);
1233 size_type ii = min(rep->sz - 1, i);
1234 for (int t = ii; t >= 0; --t) {
1235 if (rep->s[t] != c) return t;
1245 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1247 Assert(i <= rep->sz); // OURS!
1248 TestlyxstringInvariant(this);
1250 return replace(i, n, x, 0, x.rep->sz);
1254 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1255 size_type i2, size_type n2)
1257 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1258 TestlyxstringInvariant(this);
1260 rep = rep->get_own_copy();
1261 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1266 lyxstring & lyxstring::replace(size_type i, size_type n,
1267 value_type const * p, size_type n2)
1269 Assert(p && i < rep->sz); // OURS!
1270 TestlyxstringInvariant(this);
1272 rep = rep->get_own_copy();
1273 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1278 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1280 Assert(p && i < rep->sz); // OURS!
1281 TestlyxstringInvariant(this);
1283 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1287 lyxstring & lyxstring::replace(size_type i, size_type n,
1288 size_type n2, value_type c)
1290 Assert(i < rep->sz); // OURS!
1291 TestlyxstringInvariant(this);
1293 rep = rep->get_own_copy();
1294 value_type * tmp = new value_type[n2];
1296 rep->replace(i, min(n, rep->sz), tmp, n2);
1302 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1304 TestlyxstringInvariant(this);
1306 return replace(i - begin(), i2 - i, str);
1310 lyxstring & lyxstring::replace(iterator i, iterator i2,
1311 value_type const * p, size_type n)
1314 TestlyxstringInvariant(this);
1316 return replace(i - begin(), i2 - i, p, n);
1320 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1323 TestlyxstringInvariant(this);
1325 return replace(i - begin(), i2 - i, p);
1329 lyxstring & lyxstring::replace(iterator i, iterator i2,
1330 size_type n , value_type c)
1332 TestlyxstringInvariant(this);
1334 return replace(i - begin(), i2 - i, n, c);
1338 lyxstring & lyxstring::replace(iterator i, iterator i2,
1339 iterator j, iterator j2)
1341 TestlyxstringInvariant(this);
1343 return replace(i - begin(), i2 - i, j, j2 - j);
1347 lyxstring & lyxstring::erase(size_type i, size_type n)
1349 Assert(i <= rep->sz); // STD!
1350 TestlyxstringInvariant(this);
1352 rep = rep->get_own_copy();
1353 if (i == 0 && n >= rep->sz) {
1356 n = min(n, rep->sz - i);
1357 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1364 lyxstring::iterator lyxstring::erase(iterator i)
1366 TestlyxstringInvariant(this);
1368 // what iterator is this supposed to return?
1369 // the iterator after the one erased
1370 erase(i - begin(), 1);
1371 return begin(); // BUG
1375 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1377 TestlyxstringInvariant(this);
1379 erase(first - begin(), last - first);
1380 return begin(); // BUG
1384 /////////////////////////////////////
1385 // Conversion to C-style Strings
1386 /////////////////////////////////////
1388 lyxstring::value_type const * lyxstring::c_str() const
1390 rep->s[length()] = '\0';
1395 lyxstring::value_type const * lyxstring::data() const
1401 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1402 size_type pos) const
1404 Assert(buf); // OURS!
1405 Assert(pos <= rep->sz); // STD!
1406 TestlyxstringInvariant(this);
1408 register int nn = min(len, length() - pos);
1409 memcpy(buf, &(rep->s[pos]), nn);
1414 ////////////////////
1416 ////////////////////
1418 // Compare funcs should be verified.
1420 int lyxstring::internal_compare(size_type pos, size_type n,
1421 value_type const * s,
1422 size_type slen, size_type n2) const
1424 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1426 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1428 // remember that n can very well be a lot larger than rep->sz
1429 // so we have to ensure that n is no larger than rep->sz
1430 n = min(n, rep->sz);
1433 return memcmp(&(rep->s[pos]), s, n);
1434 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1443 int lyxstring::compare(lyxstring const & str) const
1445 TestlyxstringInvariant(this);
1446 return internal_compare(0, rep->sz, str.rep->s,
1447 str.rep->sz, str.rep->sz);
1451 int lyxstring::compare(value_type const * s) const
1454 TestlyxstringInvariant(this);
1455 int n = (!s) ? 0 : strlen(s);
1456 return internal_compare(0, rep->sz, s, n, n);
1460 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1462 Assert(pos <= rep->sz); // OURS!
1463 TestlyxstringInvariant(this);
1464 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1468 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1469 size_type pos2, size_type n2) const
1471 Assert(pos <= rep->sz); // OURS!
1472 Assert(pos2 <= str.rep->sz); // OURS!
1473 TestlyxstringInvariant(this);
1474 return internal_compare(pos, n,
1476 str.rep->sz - pos2, n2);
1480 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1483 Assert(s && pos <= rep->sz); // OURS!
1484 TestlyxstringInvariant(this);
1485 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1493 // i = index, n = length
1494 lyxstring lyxstring::substr(size_type i, size_type n) const
1496 Assert(i <= rep->sz); // STD!
1497 TestlyxstringInvariant(this);
1499 return lyxstring(*this, i, n);
1503 /////////////////////////////////////////////
1504 // String operators, non member functions
1505 /////////////////////////////////////////////
1507 bool operator==(lyxstring const & a, lyxstring const & b)
1509 return a.compare(b) == 0;
1513 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1516 return b.compare(a) == 0;
1520 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1523 return a.compare(b) == 0;
1527 bool operator!=(lyxstring const & a, lyxstring const & b)
1529 return a.compare(b) != 0;
1533 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1536 return b.compare(a) != 0;
1540 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1543 return a.compare(b) != 0;
1547 bool operator>(lyxstring const & a, lyxstring const & b)
1549 return a.compare(b) > 0;
1553 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1556 return b.compare(a) < 0; // since we reverse the parameters
1560 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1563 return a.compare(b) > 0;
1567 bool operator<(lyxstring const & a, lyxstring const & b)
1569 return a.compare(b) < 0;
1573 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1576 return b.compare(a) > 0; // since we reverse the parameters
1580 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1583 return a.compare(b) < 0;
1587 bool operator>=(lyxstring const & a, lyxstring const & b)
1589 return a.compare(b) >= 0;
1593 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1596 return b.compare(a) <= 0; // since we reverse the parameters
1600 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1603 return a.compare(b) >= 0;
1607 bool operator<=(lyxstring const & a, lyxstring const & b)
1609 return a.compare(b) <= 0;
1613 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1616 return b.compare(a) >= 0; // since we reverse the parameters
1620 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1623 return a.compare(b) <= 0;
1627 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1635 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1644 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1653 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1662 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1671 istream & operator>>(istream & is, lyxstring & s)
1673 // very bad solution
1674 char * nome = new char[1024];
1676 lyxstring tmp(nome);
1678 if (!tmp.empty()) s = tmp;
1682 ostream & operator<<(ostream & o, lyxstring const & s)
1684 return o.write(s.data(), s.length());
1687 istream & getline(istream & is, lyxstring & s,
1688 lyxstring::value_type delim)
1690 // very bad solution