1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
16 #pragma implementation "lyxstring.h"
19 #include "lyxstring.h"
30 // This class is supposed to be functionaly equivalent to a
31 // standard conformant string. This mean among others that we
32 // are useing the same requirements. Before you change anything
33 // in this file consult me and/or the standard to discover the
36 // Asserts with a STD! are required by the standard.
37 // Asserts with a OURS! are added by me.
38 // Some asserts could still be missing and some of the existing
39 // ones might be wrong or not needed.
41 // Reference count has been checked, empty_rep removed and
42 // introduced again in a similar guise. Where is empty_rep _really_
45 // We are missing a couple of imporant things from the standard:
46 // reverse iterators and methods taking InputIterators as paramters.
47 // Also the methods returning iterators is returning the wrong value.
49 // All the different find functions need a good look over.
50 // I have so far not tested them extensively and would be
51 // happy if others took the time to have a peek.
53 // Space allocation of string.
54 // I have tried to do this very simple without using any special tricks.
55 // Earlier we used a fixed value to enlarge the string with this would
56 // cause a lot of reallocations with large strings (especially if
57 // push_back was used) and wasting space for very small strings.
58 // I have now changed the allocation to use a doubling of reserved
59 // space until it is large enough. So far tests show a small speed
60 // increase and a noticable memory saving.
65 ///////////////////////////////////////
66 // The internal string representation
67 ///////////////////////////////////////
69 struct lyxstring::Srep {
74 /// The total amount of data reserved for this representaion
76 /// Data. At least 1 char for trailing null.
77 lyxstring::value_type * s;
80 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
82 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
84 ~Srep() { delete[] s; }
86 Srep * get_own_copy() {
87 if (ref == 1) return this;
89 return new Srep(sz, s);
93 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
95 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
97 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
99 void push_back(lyxstring::value_type c);
101 void insert(lyxstring::size_type pos,
102 const lyxstring::value_type * p,
103 lyxstring::size_type n);
105 void resize(lyxstring::size_type n, lyxstring::value_type c);
107 void reserve(lyxstring::size_type res_arg);
109 void replace(lyxstring::size_type i, lyxstring::size_type n,
110 lyxstring::value_type const * p, lyxstring::size_type n2);
113 Srep & operator=(const Srep &);
117 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
119 // can be called with p == 0 by
120 // lyxstring::assign(const value_type *, size_type)
125 s = new value_type[res + 1]; // add space for terminator
127 // if sz = 0 nothing gets copied and we have an error
130 // possibly allows for large but empty string
131 sz = 0; // this line should be redundant
137 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
142 s = new value_type[res + 1]; // add space for terminator
145 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
146 // allows for large but empty string
152 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
154 // can be called with p == 0
155 // by lyxstring::assign(const value_type *, size_type)
161 s = new value_type[res + 1]; // add space for terminator
166 // if sz = 0 nothing gets copied and we have an error
170 sz = 0; // this line should be redundant
176 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
182 s = new value_type[res + 1]; // add space for terminator
186 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
187 // allows for a large empty string
193 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
195 register unsigned int const len = sz + asz;
200 value_type * tmp = new value_type[res + 1];
202 memcpy(tmp + sz, p, asz);
207 memcpy(s + sz, p, asz);
213 void lyxstring::Srep::push_back(value_type c)
215 s[sz] = c; // it is always room to put a value_type at the end
221 value_type * tmp = new value_type[res + 1];
229 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
230 lyxstring::size_type n)
235 } while (res < n + sz);
236 value_type * tmp = new value_type[res + 1];
238 memcpy(tmp + pos, p, n);
239 memcpy(tmp + pos + n, &s[pos], sz - pos);
244 memmove(s + pos + n, &s[pos], sz - pos);
245 memcpy(s + pos, p, n);
251 void lyxstring::Srep::resize(size_type n, value_type c)
253 // This resets sz to res_arg
254 res = min(n, npos - 2); // We keep no xtra when we resize
255 value_type * tmp = new value_type[res + 1];
256 memcpy(tmp, s, min(sz, res));
258 memset(tmp + sz, c, res - sz);
265 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
267 // This keeps the old sz, but
268 // increases res with res_arg
270 value_type * tmp = new value_type[res + 1];
277 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
278 value_type const * p, size_type n2)
280 // can be called with p= 0 and n2= 0
283 if (res >= n2 + sz) {
284 memmove(s + i + n2, &s[i + n], sz - i);
285 memcpy(s + i, p, n2);
290 } while (res < n2 + sz);
291 value_type * tmp = new value_type[res + 1];
293 memcpy(tmp + i, p, n2);
294 memcpy(tmp + i + n2, &s[i + n], sz - i);
302 ///////////////////////////////////////
303 // The lyxstring Invariant tester
304 ///////////////////////////////////////
305 #ifdef ENABLE_ASSERTIONS
307 /** Testing of the lyxstring invariant
308 * By creating an object that tests the lyxstring invariant during its
309 * construction *and* its deconstruction we greatly simplify our code.
310 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
311 * will test the invariant upon entry to the code. If the Asserts fail
312 * then we know from the stack trace that the corruption occurred *before*
313 * entry to this method. We can also be sure it didn't happen in any of
314 * the tested lyxstring methods. It is therefore likely to be due to some
315 * other external force.
316 * Several lyxstring methods have multiple exit points which would otherwise
317 * require us to insert a separate test before each return. But since we
318 * created an object its destructor will be called upon exit (any exit!).
319 * We thus get testing at both start and end of a method with one line of
320 * code at the head of a method. More importantly, we get good testing
321 * everytime we run the code.
322 * NOTE: just because we test the invariant doesn't mean we can forget
323 * about testing pre and post conditions specific to any given method.
324 * This test simply proves that the lyxstring/Srep is in a valid state it
325 * does *not* prove that the method did what it was supposed to.
327 class lyxstringInvariant {
329 lyxstringInvariant(lyxstring const *);
330 ~lyxstringInvariant();
333 lyxstring const * object;
337 // To test if this scheme works "as advertised" uncomment the printf's in
338 // the constructor and destructor below and then uncomment the printf and the
339 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
340 // The correct output when LyX has been recompiled and run is:
341 // lyxstringInvariant constructor
342 // lyxstring::operator=(char const *)
343 // lyxstringInvariant constructor
344 // lyxstringInvariant destructor completed
345 // lyxstringInvariant destructor completed
346 // NOTE: The easiest way to catch this snippet of the output is to wait for
347 // the splash screen to disappear and then open and close Help->Credits
349 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
351 // printf("lyxstringInvariant constructor\n");
356 lyxstringInvariant::~lyxstringInvariant()
359 // printf("lyxstringInvariant destructor completed\n");
363 void lyxstringInvariant::helper() const
365 // Some of these tests might look pointless but they are
366 // all part of the invariant and if we want to make sure
367 // we have a bullet proof implementation then we need to
368 // test every last little thing we *know* should be true.
369 // I may have missed a test or two, so feel free to fill
370 // in the gaps. ARRae.
373 Assert(object->rep->s); // s is never 0
374 Assert(object->rep->res); // res cannot be 0
375 Assert(object->rep->sz <= object->rep->res);
376 Assert(object->rep->ref >= 1); // its in use so it must be referenced
377 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
378 // if it does ever == then we should be generating a new copy
379 // and starting again. (Is char always 8-bits?)
381 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
383 #define TestlyxstringInvariant(s)
384 #endif /* ENABLE_ASSERTIONS */
387 ///////////////////////////////////////
388 // Constructors and Deconstructors.
389 ///////////////////////////////////////
391 lyxstring::size_type const lyxstring::npos =
392 static_cast<lyxstring::size_type>(-1);
395 lyxstring::lyxstring()
397 static Srep empty_rep(0, "");
403 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
405 Assert(pos <= x.rep->sz); // STD!
406 if (pos == 0 && n >= x.length()) { // this is the default
410 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
415 lyxstring::lyxstring(value_type const * s, size_type n)
417 Assert(s && n < npos); // STD!
418 static Srep empty_rep(0, "");
420 rep = new Srep(n, s);
428 lyxstring::lyxstring(value_type const * s)
431 static Srep empty_rep(0, "");
432 if (*s) { // s is not empty string
433 rep = new Srep(strlen(s), s);
441 lyxstring::lyxstring(size_type n, value_type c)
443 Assert(n < npos); // STD!
444 rep = new Srep(n, c);
448 lyxstring::lyxstring(const_iterator first, const_iterator last)
450 rep = new Srep(last - first, first);
454 lyxstring::~lyxstring()
456 if (--rep->ref == 0) delete rep;
459 ///////////////////////
461 ///////////////////////
463 lyxstring::iterator lyxstring::begin()
465 rep = rep->get_own_copy();
470 lyxstring::const_iterator lyxstring::begin() const
476 lyxstring::iterator lyxstring::end()
478 rep = rep->get_own_copy();
479 return rep->s + rep->sz;
483 lyxstring::const_iterator lyxstring::end() const
485 return rep->s + rep->sz;
489 reverse_iterator lyxstring::rbegin()
491 return reverse_iterator( end() );
495 const_reverse_iterator lyxstring::rbegin() const
497 return const_reverse_iterator( end() );
501 reverse_iterator lyxstring::rend()
503 return reverse_iterator( begin() );
507 const_reverse_iterator lyxstring::rend() const
509 return const_reverse_iterator( begin() );
514 ///////////////////////
516 ///////////////////////
518 lyxstring::size_type lyxstring::size() const
524 void lyxstring::resize(size_type n, value_type c)
526 Assert(n <= npos); // STD!
527 TestlyxstringInvariant(this);
529 // This resets sz to res_arg
530 rep = rep->get_own_copy();
535 lyxstring::size_type lyxstring::capacity() const
541 void lyxstring::reserve(size_type res_arg)
543 TestlyxstringInvariant(this);
545 rep = rep->get_own_copy();
546 rep->reserve(res_arg);
554 lyxstring & lyxstring::operator=(lyxstring const & x)
556 TestlyxstringInvariant(this);
562 lyxstring & lyxstring::operator=(value_type const * s)
565 TestlyxstringInvariant(this);
566 // printf("lyxstring::operator= (value_type const *)\n");
572 lyxstring & lyxstring::operator=(value_type c)
574 TestlyxstringInvariant(this);
578 if (rep->ref == 1) // recycle rep
582 rep = new Srep(1, s);
588 lyxstring & lyxstring::assign(lyxstring const & x)
590 TestlyxstringInvariant(this);
592 x.rep->ref++; // protect against ``st = st''
593 if (--rep->ref == 0) delete rep;
594 rep = x.rep; // share representation
599 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
601 Assert(pos <= x.rep->sz); // STD!
602 TestlyxstringInvariant(this);
604 return assign(x.substr(pos, n));
608 lyxstring & lyxstring::assign(value_type const * s, size_type n)
610 Assert(s && n < npos); // STD!
611 TestlyxstringInvariant(this);
613 if (rep->ref == 1) // recycle rep
617 rep = new Srep(n, s);
623 lyxstring & lyxstring::assign(value_type const * s)
626 TestlyxstringInvariant(this);
628 return assign(s, strlen(s));
632 lyxstring & lyxstring::assign(size_type n, value_type ch)
634 TestlyxstringInvariant(this);
636 rep = rep->get_own_copy();
642 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
644 TestlyxstringInvariant(this);
646 rep = rep->get_own_copy();
647 rep->assign(last - first, first);
656 lyxstring::const_reference lyxstring::operator[](size_type pos) const
658 Assert(pos <= rep->sz); // OURS!
659 static char helper = '\0';
660 return pos == rep->sz ? helper : rep->s[pos];
664 lyxstring::reference lyxstring::operator[](size_type pos)
666 Assert(pos < rep->sz); // OURS!
667 TestlyxstringInvariant(this);
669 rep = rep->get_own_copy();
674 lyxstring::const_reference lyxstring::at(size_type n) const
676 Assert(n < rep->sz); // STD!
681 lyxstring::reference lyxstring::at(size_type n)
683 Assert(n < rep->sz); // STD!
684 TestlyxstringInvariant(this);
686 rep = rep->get_own_copy();
695 lyxstring & lyxstring::operator+=(lyxstring const & x)
697 TestlyxstringInvariant(this);
703 lyxstring & lyxstring::operator+=(value_type const * x)
706 TestlyxstringInvariant(this);
712 lyxstring & lyxstring::operator+=(value_type c)
714 TestlyxstringInvariant(this);
721 void lyxstring::push_back(value_type c)
723 TestlyxstringInvariant(this);
725 rep = rep->get_own_copy();
730 lyxstring & lyxstring::append(lyxstring const & x)
732 TestlyxstringInvariant(this);
734 if (x.empty()) return *this;
735 rep = rep->get_own_copy();
736 rep->append(x.length(), x.rep->s);
741 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
743 Assert(pos <= x.rep->sz); // STD!
744 TestlyxstringInvariant(this);
746 return append(x.substr(pos, n));
750 lyxstring & lyxstring::append(value_type const * p, size_type n)
753 TestlyxstringInvariant(this);
755 if (!*p || !n) return *this;
756 rep = rep->get_own_copy();
762 lyxstring & lyxstring::append(value_type const * p)
765 return append(p, strlen(p));
769 lyxstring & lyxstring::append(size_type n, value_type c)
771 TestlyxstringInvariant(this);
773 value_type * tmp = new value_type[n];
775 rep = rep->get_own_copy();
782 lyxstring & lyxstring::append(iterator first, iterator last)
784 TestlyxstringInvariant(this);
786 rep = rep->get_own_copy();
787 rep->append(last - first, first);
791 // insert characters before (*this)[pos]
793 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
795 TestlyxstringInvariant(this);
797 return insert(pos, x, 0, x.rep->sz);
801 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
802 size_type pos2, size_type n)
804 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
805 TestlyxstringInvariant(this);
807 rep = rep->get_own_copy();
808 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
813 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
816 TestlyxstringInvariant(this);
819 // insert nothing and you change nothing
820 rep = rep->get_own_copy();
821 rep->insert(pos, p, n);
827 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
830 return insert(pos, p, strlen(p));
834 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
836 TestlyxstringInvariant(this);
838 rep = rep->get_own_copy();
839 value_type * tmp = new value_type[n];
841 rep->insert(pos, tmp, n);
847 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
849 TestlyxstringInvariant(this);
851 // what iterator is this supposed to return??
852 size_type tmp = p - begin();
853 insert(p - begin(), 1, c);
854 return begin() + tmp + 1; // ??
858 void lyxstring::insert(iterator p, size_type n , value_type c)
860 TestlyxstringInvariant(this);
862 insert(p - begin(), n , c);
866 void lyxstring::insert(iterator p, iterator first, iterator last)
868 TestlyxstringInvariant(this);
870 insert(p - begin(), first, last - first);
878 // All the below find functions should be verified,
879 // it is very likely that I have mixed up or interpreted
880 // some of the parameters wrong, also some of the funcs can surely
881 // be written more effectively.
883 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
885 if (!rep->sz || i >= rep->sz) return npos;
887 TestlyxstringInvariant(this);
889 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
890 // search until (*this)[i] == a[0]
891 if (rep->s[t] == a[0]) {
892 // check if the rest of the value_types match
894 for (size_type j = 0; j < a.length(); ++j) {
895 if (rep->s[t + j] != a[j]) {
907 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
910 Assert(ptr); // OURS!
911 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
913 TestlyxstringInvariant(this);
915 // What is "n" here? is it the number of value_types to use in ptr
916 // or does "i" and "n" togeter form a substring to search
917 // for ptr in? For now I will assume that "n" tells the length
919 n = min(n, strlen(ptr));
920 for (size_type t = i; rep->sz - t >= n; ++t) {
921 // search until (*this)[i] == a[0]
922 if (rep->s[t] == ptr[0]) {
923 // check if the rest of the value_types match
925 for (size_type j = 0; j < n; ++j) {
926 if (rep->s[t + j] != ptr[j]) {
938 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
941 if (!rep->sz || i >= rep->sz) return npos;
943 TestlyxstringInvariant(this);
945 if (!s || !*s) return npos;
946 return find(s, i, strlen(s));
950 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
952 if (!rep->sz || i >= rep->sz) return npos;
954 TestlyxstringInvariant(this);
956 for (size_type t = 0; t + i < rep->sz; ++t) {
957 if (rep->s[t + i] == c) return t + i;
963 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
965 TestlyxstringInvariant(this);
967 size_type ii = min(rep->sz - 1, i);
969 if (a[a.length() - 1] == rep->s[ii]) {
971 size_type l = ii - 1;
972 for (; t >= 0; --t, --l) {
973 if (a[t] != rep->s[l]) break;
975 if (a[t] == rep->s[l]) return l;
982 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
985 Assert(ptr); // OURS!
986 TestlyxstringInvariant(this);
987 if (!*ptr) return npos;
989 size_type ii = min(rep->sz - 1, i);
991 if (ptr[n - 1] == rep->s[ii]) {
993 size_type l = ii - 1;
994 for (; t >= 0; --t, --l) {
995 if (ptr[t] != rep->s[l]) break;
997 if (ptr[t] == rep->s[l]) return l;
1004 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1007 Assert(ptr); // OURS!
1008 TestlyxstringInvariant(this);
1009 if (!*ptr) return npos;
1011 size_type ii = min(rep->sz - 1, i);
1013 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1014 int t = strlen(ptr) - 2;
1015 size_type l = ii - 1;
1016 for (; t >= 0; --t, --l) {
1017 if (ptr[t] != rep->s[l]) break;
1019 if (ptr[t] == rep->s[l]) return l;
1026 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1028 TestlyxstringInvariant(this);
1030 size_type ii = min(rep->sz - 1, i);
1031 for (size_type t = ii; t >= 0; --t) {
1032 if (rep->s[t] == c) return t;
1038 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1041 Assert(i < rep->sz); // OURS!
1042 TestlyxstringInvariant(this);
1044 for (size_type t = i; t < rep->sz; ++t) {
1045 if (a.find(rep->s[t]) != npos) return t;
1051 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1055 Assert(ptr && i < rep->sz); // OURS!
1056 TestlyxstringInvariant(this);
1057 if (!n) return npos;
1059 for (size_type t = i; t < rep->sz; ++t) {
1060 if (memchr(ptr, rep->s[t], n) != 0) return t;
1066 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1069 Assert(ptr && i < rep->sz); // OURS!
1070 TestlyxstringInvariant(this);
1072 for (size_type t = i; t < rep->sz; ++t) {
1073 if (strchr(ptr, rep->s[t]) != 0) return t;
1079 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1081 Assert(i < rep->sz); // OURS!
1082 TestlyxstringInvariant(this);
1084 for (size_type t = i; t < rep->sz; ++t) {
1085 if (rep->s[t] == c) return t;
1091 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1094 TestlyxstringInvariant(this);
1096 size_type ii = min(rep->sz - 1, i);
1097 for (int t = ii; t >= 0; --t) {
1098 if (a.find(rep->s[t]) != npos) return t;
1104 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1108 Assert(ptr); // OURS!
1109 TestlyxstringInvariant(this);
1110 if (!n) return npos;
1112 size_type ii = min(rep->sz - 1, i);
1113 for (int t = ii; t >= 0; --t) {
1114 if (memchr(ptr, rep->s[t], n) != 0) return t;
1120 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1123 Assert(ptr); // OURS!
1124 TestlyxstringInvariant(this);
1126 size_type ii = min(rep->sz - 1, i);
1127 for (int t = ii; t >= 0; --t) {
1128 if (strchr(ptr, rep->s[t]) != 0) return t;
1134 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1136 TestlyxstringInvariant(this);
1138 if (!rep->sz) return npos;
1139 size_type ii = min(rep->sz - 1, i);
1140 for (int t = ii; t >= 0; --t) {
1141 if (rep->s[t] == c) return t;
1147 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1150 TestlyxstringInvariant(this);
1152 if (!rep->sz) return npos;
1153 Assert(i < rep->sz);
1154 for (size_type t = i; t < rep->sz; ++t) {
1155 if (a.find(rep->s[t]) == npos) return t;
1161 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1165 Assert(ptr && i < rep->sz); // OURS!
1166 TestlyxstringInvariant(this);
1168 if (!n) return (i < rep->sz) ? i : npos;
1169 for (size_type t = i; t < rep->sz; ++t) {
1170 if (memchr(ptr, rep->s[t], n) == 0) return t;
1176 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1179 Assert(ptr && i < rep->sz); // OURS!
1180 TestlyxstringInvariant(this);
1182 for (size_type t = i; t < rep->sz; ++t) {
1183 if (strchr(ptr, rep->s[t]) == 0) return t;
1189 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1192 if (!rep->sz) return npos;
1193 Assert(i < rep->sz); // OURS!
1194 TestlyxstringInvariant(this);
1196 for (size_type t = i; t < rep->sz; ++t) {
1197 if (rep->s[t] != c) return t;
1203 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1206 TestlyxstringInvariant(this);
1208 size_type ii = min(rep->sz - 1, i);
1209 for (int t = ii; t >= 0; --t) {
1210 if (a.find(rep->s[t]) == npos) return t;
1216 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1220 Assert(ptr); // OURS!
1221 TestlyxstringInvariant(this);
1223 if (!n) return npos;
1224 size_type ii = min(rep->sz - 1, i);
1226 for (int t = ii; t >= 0; --t) {
1227 if (memchr(ptr, rep->s[t], n) == 0) return t;
1233 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1236 Assert(ptr); // OURS!
1237 TestlyxstringInvariant(this);
1239 size_type ii = min(rep->sz - 1, i);
1240 for (int t = ii; t >= 0; --t) {
1241 if (strchr(ptr, rep->s[t]) == 0) return t;
1247 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1250 TestlyxstringInvariant(this);
1252 size_type ii = min(rep->sz - 1, i);
1253 for (int t = ii; t >= 0; --t) {
1254 if (rep->s[t] != c) return t;
1264 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1266 Assert(i <= rep->sz); // OURS!
1267 TestlyxstringInvariant(this);
1269 return replace(i, n, x, 0, x.rep->sz);
1273 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1274 size_type i2, size_type n2)
1276 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1277 TestlyxstringInvariant(this);
1279 rep = rep->get_own_copy();
1280 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1285 lyxstring & lyxstring::replace(size_type i, size_type n,
1286 value_type const * p, size_type n2)
1288 Assert(p && i < rep->sz); // OURS!
1289 TestlyxstringInvariant(this);
1291 rep = rep->get_own_copy();
1292 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1297 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1299 Assert(p && i < rep->sz); // OURS!
1300 TestlyxstringInvariant(this);
1302 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1306 lyxstring & lyxstring::replace(size_type i, size_type n,
1307 size_type n2, value_type c)
1309 Assert(i <= rep->sz); // OURS!
1310 TestlyxstringInvariant(this);
1312 rep = rep->get_own_copy();
1313 value_type * tmp = new value_type[n2];
1315 rep->replace(i, min(n, rep->sz), tmp, n2);
1321 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1323 TestlyxstringInvariant(this);
1325 return replace(i - begin(), i2 - i, str);
1329 lyxstring & lyxstring::replace(iterator i, iterator i2,
1330 value_type const * p, size_type n)
1333 TestlyxstringInvariant(this);
1335 return replace(i - begin(), i2 - i, p, n);
1339 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1342 TestlyxstringInvariant(this);
1344 return replace(i - begin(), i2 - i, p);
1348 lyxstring & lyxstring::replace(iterator i, iterator i2,
1349 size_type n , value_type c)
1351 TestlyxstringInvariant(this);
1353 return replace(i - begin(), i2 - i, n, c);
1357 lyxstring & lyxstring::replace(iterator i, iterator i2,
1358 iterator j, iterator j2)
1360 TestlyxstringInvariant(this);
1362 return replace(i - begin(), i2 - i, j, j2 - j);
1366 void lyxstring::swap(lyxstring & str)
1368 if (rep == str.rep) return;
1369 Srep * tmp = str.rep;
1375 lyxstring & lyxstring::erase(size_type i, size_type n)
1377 Assert(i <= rep->sz); // STD!
1378 TestlyxstringInvariant(this);
1380 rep = rep->get_own_copy();
1381 if (i == 0 && n >= rep->sz) {
1384 n = min(n, rep->sz - i);
1385 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1392 lyxstring::iterator lyxstring::erase(iterator i)
1394 TestlyxstringInvariant(this);
1396 // what iterator is this supposed to return?
1397 // the iterator after the one erased
1398 erase(i - begin(), 1);
1399 return begin(); // BUG
1403 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1405 TestlyxstringInvariant(this);
1407 erase(first - begin(), last - first);
1408 return begin(); // BUG
1412 /////////////////////////////////////
1413 // Conversion to C-style Strings
1414 /////////////////////////////////////
1416 lyxstring::value_type const * lyxstring::c_str() const
1418 rep->s[length()] = '\0';
1423 lyxstring::value_type const * lyxstring::data() const
1429 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1430 size_type pos) const
1432 Assert(buf); // OURS!
1433 Assert(pos <= rep->sz); // STD!
1434 TestlyxstringInvariant(this);
1436 register int nn = min(len, length() - pos);
1437 memcpy(buf, &(rep->s[pos]), nn);
1442 ////////////////////
1444 ////////////////////
1446 // Compare funcs should be verified.
1448 int lyxstring::internal_compare(size_type pos, size_type n,
1449 value_type const * s,
1450 size_type slen, size_type n2) const
1452 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1454 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1456 // remember that n can very well be a lot larger than rep->sz
1457 // so we have to ensure that n is no larger than rep->sz
1458 n = min(n, rep->sz);
1461 return memcmp(&(rep->s[pos]), s, n);
1462 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1471 int lyxstring::compare(lyxstring const & str) const
1473 TestlyxstringInvariant(this);
1474 return internal_compare(0, rep->sz, str.rep->s,
1475 str.rep->sz, str.rep->sz);
1479 int lyxstring::compare(value_type const * s) const
1482 TestlyxstringInvariant(this);
1483 int n = (!s) ? 0 : strlen(s);
1484 return internal_compare(0, rep->sz, s, n, n);
1488 int lyxstring::compare(size_type pos, size_type n,
1489 lyxstring const & str) const
1491 Assert(pos <= rep->sz); // OURS!
1492 TestlyxstringInvariant(this);
1493 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1497 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1498 size_type pos2, size_type n2) const
1500 Assert(pos <= rep->sz); // OURS!
1501 Assert(pos2 <= str.rep->sz); // OURS!
1502 TestlyxstringInvariant(this);
1503 return internal_compare(pos, n,
1505 str.rep->sz - pos2, n2);
1509 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1512 Assert(s && pos <= rep->sz); // OURS!
1513 TestlyxstringInvariant(this);
1514 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1522 // i = index, n = length
1523 lyxstring lyxstring::substr(size_type i, size_type n) const
1525 Assert(i <= rep->sz); // STD!
1526 TestlyxstringInvariant(this);
1528 return lyxstring(*this, i, n);
1532 /////////////////////////////////////////////
1533 // String operators, non member functions
1534 /////////////////////////////////////////////
1536 bool operator==(lyxstring const & a, lyxstring const & b)
1538 return a.compare(b) == 0;
1542 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1545 return b.compare(a) == 0;
1549 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1552 return a.compare(b) == 0;
1556 bool operator!=(lyxstring const & a, lyxstring const & b)
1558 return a.compare(b) != 0;
1562 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1565 return b.compare(a) != 0;
1569 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1572 return a.compare(b) != 0;
1576 bool operator>(lyxstring const & a, lyxstring const & b)
1578 return a.compare(b) > 0;
1582 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1585 return b.compare(a) < 0; // since we reverse the parameters
1589 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1592 return a.compare(b) > 0;
1596 bool operator<(lyxstring const & a, lyxstring const & b)
1598 return a.compare(b) < 0;
1602 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1605 return b.compare(a) > 0; // since we reverse the parameters
1609 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1612 return a.compare(b) < 0;
1616 bool operator>=(lyxstring const & a, lyxstring const & b)
1618 return a.compare(b) >= 0;
1622 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1625 return b.compare(a) <= 0; // since we reverse the parameters
1629 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1632 return a.compare(b) >= 0;
1636 bool operator<=(lyxstring const & a, lyxstring const & b)
1638 return a.compare(b) <= 0;
1642 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1645 return b.compare(a) >= 0; // since we reverse the parameters
1649 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1652 return a.compare(b) <= 0;
1656 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1664 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1673 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1682 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1691 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1699 void swap(lyxstring & str1, lyxstring & str2)
1707 istream & operator>>(istream & is, lyxstring & s)
1710 // very bad solution
1711 char * nome = new char[1024];
1713 lyxstring tmp(nome);
1715 if (!tmp.empty()) s = tmp;
1718 int w = is.widdth(0);
1722 if (isspace(c)) { is.putback(c); break; }
1724 if (--w == 1) break;
1726 if (s.empty()) is.setstate(ios::failbit);
1732 ostream & operator<<(ostream & o, lyxstring const & s)
1734 return o.write(s.data(), s.length());
1738 istream & getline(istream & is, lyxstring & s,
1739 lyxstring::value_type delim)
1741 // very bad solution