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 // Some asserts could still be missing and some of the existing
37 // ones might be wrong or not needed.
39 // Reference count has been checked, empty_rep removed and
40 // introduced again in a similar guise. Where is empty_rep _really_
43 // We are missing a couple of imporant things from the standard:
44 // reverse iterators and methods taking InputIterators as paramters.
45 // Also the methods returning iterators is returning the wrong value.
47 // All the different find functions need a good look over.
48 // I have so far not tested them extensively and would be
49 // happy if others took the time to have a peek.
51 // Space allocation of string.
52 // I have tried to do this very simple without using any special tricks.
53 // Earlier we used a fixed value to enlarge the string with this would
54 // cause a lot of reallocations with large strings (especially if
55 // push_back was used) and wasting space for very small strings.
56 // I have now changed the allocation to use a doubling of reserved
57 // space until it is large enough. So far tests show a small speed
58 // increase and a noticable memory saving.
62 ///////////////////////////////////////
63 // The internal string representation
64 ///////////////////////////////////////
66 struct lyxstring::Srep {
71 /// The total amount of data reserved for this representaion
73 /// Data. At least 1 char for trailing null.
74 lyxstring::value_type * s;
77 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
79 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
81 ~Srep() { delete[] s; }
83 Srep * get_own_copy() {
84 if (ref == 1) return this;
86 return new Srep(sz, s);
90 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
92 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
94 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
96 void push_back(lyxstring::value_type c);
98 void insert(lyxstring::size_type pos,
99 const lyxstring::value_type * p,
100 lyxstring::size_type n);
102 void resize(lyxstring::size_type n, lyxstring::value_type c);
104 void reserve(lyxstring::size_type res_arg);
106 void replace(lyxstring::size_type i, lyxstring::size_type n,
107 lyxstring::value_type const * p, lyxstring::size_type n2);
110 Srep & operator=(const Srep &);
114 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
116 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
121 s = new value_type[res + 1]; // add space for terminator
123 // if sz = 0 nothing gets copied and we have an error
126 // possibly allows for large but empty string
127 sz = 0; // this line should be redundant
133 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
138 s = new value_type[res + 1]; // add space for terminator
141 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
142 // allows for large but empty string
148 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
150 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
156 s = new value_type[res + 1]; // add space for terminator
161 // if sz = 0 nothing gets copied and we have an error
165 sz = 0; // this line should be redundant
171 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
177 s = new value_type[res + 1]; // add space for terminator
181 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
182 // allows for a large empty string
188 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
190 register unsigned int const len = sz + asz;
195 value_type * tmp = new value_type[res + 1];
197 memcpy(tmp + sz, p, asz);
202 memcpy(s + sz, p, asz);
208 void lyxstring::Srep::push_back(value_type c)
210 s[sz] = c; // it is always room to put a value_type at the end
216 value_type * tmp = new value_type[res + 1];
224 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
225 lyxstring::size_type n)
230 } while (res < n + sz);
231 value_type * tmp = new value_type[res + 1];
233 memcpy(tmp + pos, p, n);
234 memcpy(tmp + pos + n, &s[pos], sz - pos);
239 memmove(s + pos + n, &s[pos], sz - pos);
240 memcpy(s + pos, p, n);
246 void lyxstring::Srep::resize(size_type n, value_type c)
248 // This resets sz to res_arg
249 res = min(n, npos - 2); // We keep no xtra when we resize
250 value_type * tmp = new value_type[res + 1];
251 memcpy(tmp, s, min(sz, res));
253 memset(tmp + sz, c, res - sz);
260 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
262 // This keeps the old sz, but
263 // increases res with res_arg
265 value_type * tmp = new value_type[res + 1];
272 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
273 value_type const * p, size_type n2)
275 // can be called with p= 0 and n2= 0
278 if (res >= n2 + sz) {
279 memmove(s + i + n2, &s[i + n], sz - i);
280 memcpy(s + i, p, n2);
285 } while (res < n2 + sz);
286 value_type * tmp = new value_type[res + 1];
288 memcpy(tmp + i, p, n2);
289 memcpy(tmp + i + n2, &s[i + n], sz - i);
297 ///////////////////////////////////////
298 // The lyxstring Invariant tester
299 ///////////////////////////////////////
302 /** Testing of the lyxstring invariant
303 * By creating an object that tests the lyxstring invariant during its
304 * construction *and* its deconstruction we greatly simplify our code.
305 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
306 * will test the invariant upon entry to the code. If the Asserts fail
307 * then we know from the stack trace that the corruption occurred *before*
308 * entry to this method. We can also be sure it didn't happen in any of
309 * the tested lyxstring methods. It is therefore likely to be due to some
310 * other external force.
311 * Several lyxstring methods have multiple exit points which would otherwise
312 * require us to insert a separate test before each return. But since we
313 * created an object its destructor will be called upon exit (any exit!).
314 * We thus get testing at both start and end of a method with one line of
315 * code at the head of a method. More importantly, we get good testing
316 * everytime we run the code.
317 * NOTE: just because we test the invariant doesn't mean we can forget
318 * about testing pre and post conditions specific to any given method.
319 * This test simply proves that the lyxstring/Srep is in a valid state it
320 * does *not* prove that the method did what it was supposed to.
322 class lyxstringInvariant {
324 lyxstringInvariant(lyxstring const *);
325 ~lyxstringInvariant();
328 lyxstring const * object;
332 // To test if this scheme works "as advertised" uncomment the printf's in
333 // the constructor and destructor below and then uncomment the printf and the
334 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
335 // The correct output when LyX has been recompiled and run is:
336 // lyxstringInvariant constructor
337 // lyxstring::operator=(char const *)
338 // lyxstringInvariant constructor
339 // lyxstringInvariant destructor completed
340 // lyxstringInvariant destructor completed
341 // NOTE: The easiest way to catch this snippet of the output is to wait for
342 // the splash screen to disappear and then open and close Help->Credits
344 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
346 // printf("lyxstringInvariant constructor\n");
351 lyxstringInvariant::~lyxstringInvariant()
354 // printf("lyxstringInvariant destructor completed\n");
358 void lyxstringInvariant::helper() const
360 // Some of these tests might look pointless but they are
361 // all part of the invariant and if we want to make sure
362 // we have a bullet proof implementation then we need to
363 // test every last little thing we *know* should be true.
364 // I may have missed a test or two, so feel free to fill
365 // in the gaps. ARRae.
368 Assert(object->rep->s); // s is never 0
369 Assert(object->rep->res); // res cannot be 0
370 Assert(object->rep->sz <= object->rep->res);
371 Assert(object->rep->ref >= 1); // its in use so it must be referenced
372 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
373 // if it does ever == then we should be generating a new copy
374 // and starting again. (Is char always 8-bits?)
376 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
378 #define TestlyxstringInvariant(s)
379 #endif //DEVEL_VERSION
382 ///////////////////////////////////////
383 // Constructors and Deconstructors.
384 ///////////////////////////////////////
386 lyxstring::size_type const lyxstring::npos =
387 static_cast<lyxstring::size_type>(-1);
390 lyxstring::lyxstring()
392 static Srep empty_rep(0, "");
398 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
400 Assert(pos <= x.rep->sz); // STD!
401 if (pos == 0 && n >= x.length()) { // this is the default
405 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
410 lyxstring::lyxstring(value_type const * s, size_type n)
412 Assert(s && n < npos); // STD!
413 static Srep empty_rep(0, "");
414 if (*s && n) { // s is not empty string and n > 0
415 rep = new Srep(min(strlen(s), n), s);
423 lyxstring::lyxstring(value_type const * s)
426 static Srep empty_rep(0, "");
427 if (*s) { // s is not empty string
428 rep = new Srep(strlen(s), s);
436 lyxstring::lyxstring(size_type n, value_type c)
438 Assert(n < npos); // STD!
439 rep = new Srep(n, c);
443 lyxstring::lyxstring(const_iterator first, const_iterator last)
445 rep = new Srep(last - first, first);
449 lyxstring::~lyxstring()
451 if (--rep->ref == 0) delete rep;
454 ///////////////////////
456 ///////////////////////
458 lyxstring::iterator lyxstring::begin()
464 lyxstring::const_iterator lyxstring::begin() const
470 lyxstring::iterator lyxstring::end()
472 return rep->s + rep->sz;
476 lyxstring::const_iterator lyxstring::end() const
478 return rep->s + rep->sz;
482 reverse_iterator lyxstring::rbegin()
484 return reverse_iterator( end() );
488 const_reverse_iterator lyxstring::rbegin() const
490 return const_reverse_iterator( end() );
494 reverse_iterator lyxstring::rend()
496 return reverse_iterator( begin() );
500 const_reverse_iterator lyxstring::rend() const
502 return const_reverse_iterator( begin() );
507 ///////////////////////
509 ///////////////////////
511 lyxstring::size_type lyxstring::size() const
517 void lyxstring::resize(size_type n, value_type c)
519 Assert(n <= npos); // STD!
520 TestlyxstringInvariant(this);
522 // This resets sz to res_arg
523 rep = rep->get_own_copy();
528 lyxstring::size_type lyxstring::capacity() const
534 void lyxstring::reserve(size_type res_arg)
536 TestlyxstringInvariant(this);
538 rep = rep->get_own_copy();
539 rep->reserve(res_arg);
547 lyxstring & lyxstring::operator= (lyxstring const & x)
549 TestlyxstringInvariant(this);
555 lyxstring & lyxstring::operator= (value_type const * s)
558 TestlyxstringInvariant(this);
559 // printf("lyxstring::operator= (value_type const *)\n");
565 lyxstring & lyxstring::operator=(value_type c)
567 TestlyxstringInvariant(this);
571 if (rep->ref == 1) // recycle rep
575 rep = new Srep(1, s);
581 lyxstring & lyxstring::assign(lyxstring const & x)
583 TestlyxstringInvariant(this);
585 x.rep->ref++; // protect against ``st = st''
586 if (--rep->ref == 0) delete rep;
587 rep = x.rep; // share representation
592 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
594 Assert(pos <= x.rep->sz); // STD!
595 TestlyxstringInvariant(this);
597 return assign(x.substr(pos, n));
601 lyxstring & lyxstring::assign(value_type const * s, size_type n)
604 TestlyxstringInvariant(this);
606 n = min(strlen(s), n);
607 if (rep->ref == 1) // recycle rep
611 rep = new Srep(n, s);
617 lyxstring & lyxstring::assign(value_type const * s)
620 TestlyxstringInvariant(this);
622 return assign(s, strlen(s));
626 lyxstring & lyxstring::assign(size_type n, value_type ch)
628 TestlyxstringInvariant(this);
630 rep = rep->get_own_copy();
636 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
638 TestlyxstringInvariant(this);
640 rep = rep->get_own_copy();
641 rep->assign(last - first, first);
650 lyxstring::const_reference lyxstring::operator[](size_type pos) const
652 Assert(pos <= rep->sz); // OURS!
653 static char helper = '\0';
654 return pos == rep->sz ? helper : rep->s[pos];
658 lyxstring::reference lyxstring::operator[](size_type pos)
660 Assert(pos < rep->sz); // OURS!
661 TestlyxstringInvariant(this);
663 rep = rep->get_own_copy();
668 lyxstring::const_reference lyxstring::at(size_type n) const
670 Assert(n < rep->sz); // STD!
675 lyxstring::reference lyxstring::at(size_type n)
677 Assert(n < rep->sz); // STD!
678 TestlyxstringInvariant(this);
680 rep = rep->get_own_copy();
689 lyxstring & lyxstring::operator+=(lyxstring const & x)
691 TestlyxstringInvariant(this);
697 lyxstring & lyxstring::operator+=(value_type const * x)
700 TestlyxstringInvariant(this);
706 lyxstring & lyxstring::operator+=(value_type c)
708 TestlyxstringInvariant(this);
715 void lyxstring::push_back(value_type c)
717 TestlyxstringInvariant(this);
719 rep = rep->get_own_copy();
724 lyxstring & lyxstring::append(lyxstring const & x)
726 TestlyxstringInvariant(this);
728 if (x.empty()) return *this;
729 rep = rep->get_own_copy();
730 rep->append(x.length(), x.rep->s);
735 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
737 Assert(pos <= x.rep->sz); // STD!
738 TestlyxstringInvariant(this);
740 return append(x.substr(pos, n));
744 lyxstring & lyxstring::append(value_type const * p, size_type n)
747 TestlyxstringInvariant(this);
749 if (!*p || !n) return *this;
750 rep = rep->get_own_copy();
751 rep->append(min(n, strlen(p)), p);
756 lyxstring & lyxstring::append(value_type const * p)
759 TestlyxstringInvariant(this);
761 if (!*p) return *this;
762 rep = rep->get_own_copy();
763 rep->append(strlen(p), p);
768 lyxstring & lyxstring::append(size_type n, value_type c)
770 TestlyxstringInvariant(this);
772 value_type * tmp = new value_type[n];
774 rep = rep->get_own_copy();
781 lyxstring & lyxstring::append(iterator first, iterator last)
783 TestlyxstringInvariant(this);
785 rep = rep->get_own_copy();
786 rep->append(last - first, first);
790 // insert value_typeacters before (*this)[pos]
792 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
794 TestlyxstringInvariant(this);
796 return insert(pos, x, 0, x.rep->sz);
800 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
801 size_type pos2, size_type n)
803 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
804 TestlyxstringInvariant(this);
806 rep = rep->get_own_copy();
807 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
812 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
815 TestlyxstringInvariant(this);
818 // insert nothing and you change nothing
819 rep = rep->get_own_copy();
820 rep->insert(pos, p, min(n, strlen(p)));
826 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
829 TestlyxstringInvariant(this);
832 // insert nothing and you change nothing
833 rep = rep->get_own_copy();
834 rep->insert(pos, p, strlen(p));
840 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
842 TestlyxstringInvariant(this);
844 rep = rep->get_own_copy();
845 value_type * tmp = new value_type[n];
847 rep->insert(pos, tmp, n);
853 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
855 TestlyxstringInvariant(this);
857 // what iterator is this supposed to return??
858 size_type tmp = p - begin();
859 insert(p - begin(), 1, c);
860 return begin() + tmp + 1; // ??
864 void lyxstring::insert(iterator p, size_type n , value_type c)
866 TestlyxstringInvariant(this);
868 insert(p - begin(), n , c);
872 void lyxstring::insert(iterator p, iterator first, iterator last)
874 TestlyxstringInvariant(this);
876 insert(p - begin(), first, last - first);
884 // All the below find functions should be verified,
885 // it is very likely that I have mixed up or interpreted
886 // some of the parameters wrong, also some of the funcs can surely
887 // be written more effectively.
889 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
891 if (!rep->sz || i >= rep->sz) return npos;
893 TestlyxstringInvariant(this);
895 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
896 // search until (*this)[i] == a[0]
897 if (rep->s[t] == a[0]) {
898 // check if the rest of the value_types match
900 for (size_type j = 0; j < a.length(); ++j) {
901 if (rep->s[t + j] != a[j]) {
913 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
916 Assert(ptr); // OURS!
917 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
919 TestlyxstringInvariant(this);
921 // What is "n" here? is it the number of value_types to use in ptr
922 // or does "i" and "n" togeter form a substring to search
923 // for ptr in? For now I will assume that "n" tells the length
925 n = min(n, strlen(ptr));
926 for (size_type t = i; rep->sz - t >= n; ++t) {
927 // search until (*this)[i] == a[0]
928 if (rep->s[t] == ptr[0]) {
929 // check if the rest of the value_types match
931 for (size_type j = 0; j < n; ++j) {
932 if (rep->s[t + j] != ptr[j]) {
944 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
947 if (!rep->sz || i >= rep->sz) return npos;
949 TestlyxstringInvariant(this);
951 if (!s || !*s) return npos;
952 return find(s, i, strlen(s));
956 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
958 if (!rep->sz || i >= rep->sz) return npos;
960 TestlyxstringInvariant(this);
962 for (size_type t = 0; t + i < rep->sz; ++t) {
963 if (rep->s[t + i] == c) return t + i;
969 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
971 TestlyxstringInvariant(this);
973 size_type ii = min(rep->sz - 1, i);
975 if (a[a.length() - 1] == rep->s[ii]) {
977 size_type l = ii - 1;
978 for (; t >= 0; --t, --l) {
979 if (a[t] != rep->s[l]) break;
981 if (a[t] == rep->s[l]) return l;
988 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
991 Assert(ptr); // OURS!
992 TestlyxstringInvariant(this);
993 if (!*ptr) return npos;
995 size_type ii = min(rep->sz - 1, i);
997 if (ptr[n - 1] == rep->s[ii]) {
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 const * ptr,
1013 Assert(ptr); // OURS!
1014 TestlyxstringInvariant(this);
1015 if (!*ptr) return npos;
1017 size_type ii = min(rep->sz - 1, i);
1019 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1020 int t = strlen(ptr) - 2;
1021 size_type l = ii - 1;
1022 for (; t >= 0; --t, --l) {
1023 if (ptr[t] != rep->s[l]) break;
1025 if (ptr[t] == rep->s[l]) return l;
1032 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1034 TestlyxstringInvariant(this);
1036 size_type ii = min(rep->sz - 1, i);
1037 for (size_type t = ii; t != 0; --t) {
1038 if (rep->s[t] == c) return t;
1044 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1047 Assert(i < rep->sz); // OURS!
1048 TestlyxstringInvariant(this);
1050 for (size_type t = i; t < rep->sz; ++t) {
1051 if (a.find(rep->s[t]) != npos) return t;
1057 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1061 Assert(ptr && i < rep->sz); // OURS!
1062 TestlyxstringInvariant(this);
1063 if (!n) return npos;
1065 for (size_type t = i; t < rep->sz; ++t) {
1066 if(memchr(ptr, rep->s[t], n) != 0) return t;
1072 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1075 Assert(ptr && i < rep->sz); // OURS!
1076 TestlyxstringInvariant(this);
1078 for (size_type t = i; t < rep->sz; ++t) {
1079 if (strchr(ptr, rep->s[t]) != 0) return t;
1085 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1087 Assert(i < rep->sz); // OURS!
1088 TestlyxstringInvariant(this);
1090 for (size_type t = i; t < rep->sz; ++t) {
1091 if (rep->s[t] == c) return t;
1097 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1100 TestlyxstringInvariant(this);
1102 size_type ii = min(rep->sz - 1, i);
1103 for (int t = ii; t >= 0; --t) {
1104 if (a.find(rep->s[t]) != npos) return t;
1110 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1114 Assert(ptr); // OURS!
1115 TestlyxstringInvariant(this);
1116 if (!n) return npos;
1118 size_type ii = min(rep->sz - 1, i);
1119 for (int t = ii; t >= 0; --t) {
1120 if(memchr(ptr, rep->s[t], n) != 0) return t;
1126 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1129 Assert(ptr); // OURS!
1130 TestlyxstringInvariant(this);
1132 size_type ii = min(rep->sz - 1, i);
1133 for (int t = ii; t >= 0; --t) {
1134 if (strchr(ptr, rep->s[t]) != 0) return t;
1140 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1142 TestlyxstringInvariant(this);
1144 if (!rep->sz) return npos;
1145 size_type ii = min(rep->sz - 1, i);
1146 for (int t = ii; t >= 0; --t) {
1147 if (rep->s[t] == c) return t;
1153 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1156 TestlyxstringInvariant(this);
1158 if (!rep->sz) return npos;
1159 Assert(i < rep->sz);
1160 for (size_type t = i; t < rep->sz; ++t) {
1161 if (a.find(rep->s[t]) == npos) return t;
1167 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1171 Assert(ptr && i < rep->sz); // OURS!
1172 TestlyxstringInvariant(this);
1174 if (!n) return (i < rep->sz) ? i : npos;
1175 for (size_type t = i; t < rep->sz; ++t) {
1176 if(memchr(ptr, rep->s[t], n) == 0) return t;
1182 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1185 Assert(ptr && i < rep->sz); // OURS!
1186 TestlyxstringInvariant(this);
1188 for (size_type t = i; t < rep->sz; ++t) {
1189 if (strchr(ptr, rep->s[t]) == 0) return t;
1195 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1198 if (!rep->sz) return npos;
1199 Assert(i < rep->sz); // OURS!
1200 TestlyxstringInvariant(this);
1202 for (size_type t = i; t < rep->sz; ++t) {
1203 if (rep->s[t] != c) return t;
1209 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1212 TestlyxstringInvariant(this);
1214 size_type ii = min(rep->sz - 1, i);
1215 for (int t = ii; t >= 0; --t) {
1216 if (a.find(rep->s[t]) == npos) return t;
1222 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1226 Assert(ptr); // OURS!
1227 TestlyxstringInvariant(this);
1229 if (!n) return npos;
1230 size_type ii = min(rep->sz - 1, i);
1232 for (int t = ii; t >= 0; --t) {
1233 if(memchr(ptr, rep->s[t], n) == 0) return t;
1239 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1242 Assert(ptr); // OURS!
1243 TestlyxstringInvariant(this);
1245 size_type ii = min(rep->sz - 1, i);
1246 for (int t = ii; t >= 0; --t) {
1247 if (strchr(ptr, rep->s[t]) == 0) return t;
1253 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1256 TestlyxstringInvariant(this);
1258 size_type ii = min(rep->sz - 1, i);
1259 for (int t = ii; t >= 0; --t) {
1260 if (rep->s[t] != c) return t;
1270 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1272 Assert(i <= rep->sz); // OURS!
1273 TestlyxstringInvariant(this);
1275 return replace(i, n, x, 0, x.rep->sz);
1279 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1280 size_type i2, size_type n2)
1282 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1283 TestlyxstringInvariant(this);
1285 rep = rep->get_own_copy();
1286 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1291 lyxstring & lyxstring::replace(size_type i, size_type n,
1292 value_type const * p, size_type n2)
1294 Assert(p && i < rep->sz); // OURS!
1295 TestlyxstringInvariant(this);
1297 rep = rep->get_own_copy();
1298 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1303 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1305 Assert(p && i < rep->sz); // OURS!
1306 TestlyxstringInvariant(this);
1308 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1312 lyxstring & lyxstring::replace(size_type i, size_type n,
1313 size_type n2, value_type c)
1315 Assert(i < rep->sz); // OURS!
1316 TestlyxstringInvariant(this);
1318 rep = rep->get_own_copy();
1319 value_type * tmp = new value_type[n2];
1321 rep->replace(i, min(n, rep->sz), tmp, n2);
1327 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1329 TestlyxstringInvariant(this);
1331 return replace(i - begin(), i2 - i, str);
1335 lyxstring & lyxstring::replace(iterator i, iterator i2,
1336 value_type const * p, size_type n)
1339 TestlyxstringInvariant(this);
1341 return replace(i - begin(), i2 - i, p, n);
1345 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1348 TestlyxstringInvariant(this);
1350 return replace(i - begin(), i2 - i, p);
1354 lyxstring & lyxstring::replace(iterator i, iterator i2,
1355 size_type n , value_type c)
1357 TestlyxstringInvariant(this);
1359 return replace(i - begin(), i2 - i, n, c);
1363 lyxstring & lyxstring::replace(iterator i, iterator i2,
1364 iterator j, iterator j2)
1366 TestlyxstringInvariant(this);
1368 return replace(i - begin(), i2 - i, j, j2 - j);
1372 void lyxstring::swap(lyxstring & str)
1374 if (rep == str.rep) return;
1375 Srep * tmp = str.rep;
1381 lyxstring & lyxstring::erase(size_type i, size_type n)
1383 Assert(i <= rep->sz); // STD!
1384 TestlyxstringInvariant(this);
1386 rep = rep->get_own_copy();
1387 if (i == 0 && n >= rep->sz) {
1390 n = min(n, rep->sz - i);
1391 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1398 lyxstring::iterator lyxstring::erase(iterator i)
1400 TestlyxstringInvariant(this);
1402 // what iterator is this supposed to return?
1403 // the iterator after the one erased
1404 erase(i - begin(), 1);
1405 return begin(); // BUG
1409 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1411 TestlyxstringInvariant(this);
1413 erase(first - begin(), last - first);
1414 return begin(); // BUG
1418 /////////////////////////////////////
1419 // Conversion to C-style Strings
1420 /////////////////////////////////////
1422 lyxstring::value_type const * lyxstring::c_str() const
1424 rep->s[length()] = '\0';
1429 lyxstring::value_type const * lyxstring::data() const
1435 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1436 size_type pos) const
1438 Assert(buf); // OURS!
1439 Assert(pos <= rep->sz); // STD!
1440 TestlyxstringInvariant(this);
1442 register int nn = min(len, length() - pos);
1443 memcpy(buf, &(rep->s[pos]), nn);
1448 ////////////////////
1450 ////////////////////
1452 // Compare funcs should be verified.
1454 int lyxstring::internal_compare(size_type pos, size_type n,
1455 value_type const * s,
1456 size_type slen, size_type n2) const
1458 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1460 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1462 // remember that n can very well be a lot larger than rep->sz
1463 // so we have to ensure that n is no larger than rep->sz
1464 n = min(n, rep->sz);
1467 return memcmp(&(rep->s[pos]), s, n);
1468 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1477 int lyxstring::compare(lyxstring const & str) const
1479 TestlyxstringInvariant(this);
1480 return internal_compare(0, rep->sz, str.rep->s,
1481 str.rep->sz, str.rep->sz);
1485 int lyxstring::compare(value_type const * s) const
1488 TestlyxstringInvariant(this);
1489 int n = (!s) ? 0 : strlen(s);
1490 return internal_compare(0, rep->sz, s, n, n);
1494 int lyxstring::compare(size_type pos, size_type n,
1495 lyxstring const & str) const
1497 Assert(pos <= rep->sz); // OURS!
1498 TestlyxstringInvariant(this);
1499 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1503 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1504 size_type pos2, size_type n2) const
1506 Assert(pos <= rep->sz); // OURS!
1507 Assert(pos2 <= str.rep->sz); // OURS!
1508 TestlyxstringInvariant(this);
1509 return internal_compare(pos, n,
1511 str.rep->sz - pos2, n2);
1515 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1518 Assert(s && pos <= rep->sz); // OURS!
1519 TestlyxstringInvariant(this);
1520 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1528 // i = index, n = length
1529 lyxstring lyxstring::substr(size_type i, size_type n) const
1531 Assert(i <= rep->sz); // STD!
1532 TestlyxstringInvariant(this);
1534 return lyxstring(*this, i, n);
1538 /////////////////////////////////////////////
1539 // String operators, non member functions
1540 /////////////////////////////////////////////
1542 bool operator==(lyxstring const & a, lyxstring const & b)
1544 return a.compare(b) == 0;
1548 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1551 return b.compare(a) == 0;
1555 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1558 return a.compare(b) == 0;
1562 bool operator!=(lyxstring const & a, lyxstring const & b)
1564 return a.compare(b) != 0;
1568 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1571 return b.compare(a) != 0;
1575 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1578 return a.compare(b) != 0;
1582 bool operator>(lyxstring const & a, lyxstring const & b)
1584 return a.compare(b) > 0;
1588 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1591 return b.compare(a) < 0; // since we reverse the parameters
1595 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1598 return a.compare(b) > 0;
1602 bool operator<(lyxstring const & a, lyxstring const & b)
1604 return a.compare(b) < 0;
1608 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1611 return b.compare(a) > 0; // since we reverse the parameters
1615 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1618 return a.compare(b) < 0;
1622 bool operator>=(lyxstring const & a, lyxstring const & b)
1624 return a.compare(b) >= 0;
1628 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1631 return b.compare(a) <= 0; // since we reverse the parameters
1635 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1638 return a.compare(b) >= 0;
1642 bool operator<=(lyxstring const & a, lyxstring const & b)
1644 return a.compare(b) <= 0;
1648 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1651 return b.compare(a) >= 0; // since we reverse the parameters
1655 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1658 return a.compare(b) <= 0;
1662 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1670 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1679 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1688 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1697 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1705 void swap(lyxstring & str1, lyxstring & str2)
1713 istream & operator>>(istream & is, lyxstring & s)
1716 // very bad solution
1717 char * nome = new char[1024];
1719 lyxstring tmp(nome);
1721 if (!tmp.empty()) s = tmp;
1724 int w = is.widdth(0);
1728 if (isspace(c)) { is.putback(c); break; }
1730 if (--w == 1) break;
1732 if (s.empty()) is.setstate(ios::failbit);
1738 ostream & operator<<(ostream & o, lyxstring const & s)
1740 return o.write(s.data(), s.length());
1744 istream & getline(istream & is, lyxstring & s,
1745 lyxstring::value_type delim)
1747 // very bad solution