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 ///////////////////////////////////////
67 struct lyxstring::Srep {
70 static size_t const xtra = static_cast<size_t>(8);
76 /// The total amount of data reserved for this representaion
78 /// Data. At least 1 char for trailing null.
79 lyxstring::value_type * s;
82 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
84 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
86 ~Srep() { delete[] s; }
88 Srep * get_own_copy() {
89 if (ref == 1) return this;
91 return new Srep(sz, s);
95 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
97 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
99 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
101 void push_back(lyxstring::value_type c);
103 void insert(lyxstring::size_type pos,
104 const lyxstring::value_type * p,
105 lyxstring::size_type n);
107 void resize(lyxstring::size_type n, lyxstring::value_type c);
109 void reserve(lyxstring::size_type res_arg);
111 void replace(lyxstring::size_type i, lyxstring::size_type n,
112 lyxstring::value_type const * p, lyxstring::size_type n2);
115 Srep & operator=(const Srep &);
119 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
121 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
130 s = new value_type[res + 1]; // add space for terminator
132 // if sz = 0 nothing gets copied and we have an error
135 // possibly allows for large but empty string
136 sz = 0; // this line should be redundant
142 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
151 s = new value_type[res + 1]; // add space for terminator
154 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
155 // allows for large but empty string
161 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
163 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
173 s = new value_type[res + 1]; // add space for terminator
178 // if sz = 0 nothing gets copied and we have an error
182 sz = 0; // this line should be redundant
188 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
198 s = new value_type[res + 1]; // add space for terminator
202 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
203 // allows for a large empty string
209 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
211 register unsigned int const len = sz + asz;
220 value_type * tmp = new value_type[res + 1];
222 memcpy(tmp + sz, p, asz);
227 memcpy(s + sz, p, asz);
233 void lyxstring::Srep::push_back(value_type c)
235 s[sz] = c; // it is always room to put a value_type at the end
245 value_type * tmp = new value_type[res + 1];
253 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
254 lyxstring::size_type n)
260 } while (res < n + sz);
264 value_type * tmp = new value_type[res + 1];
266 memcpy(tmp + pos, p, n);
267 memcpy(tmp + pos + n, & s[pos], sz - pos);
272 memmove(s + pos + n, & s[pos], sz - pos);
273 memcpy(s + pos, p, n);
279 void lyxstring::Srep::resize(size_type n, value_type c)
281 // This resets sz to res_arg
282 res = min(n, npos - 2); // We keep no xtra when we resize
283 value_type * tmp = new value_type[res + 1];
284 memcpy(tmp, s, min(sz, res));
286 memset(tmp + sz, c, res - sz);
293 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
295 // This keeps the old sz, but
296 // increases res with res_arg
298 value_type * tmp = new value_type[res + 1];
305 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
306 value_type const * p, size_type n2)
308 // can be called with p= 0 and n2= 0
311 if (res >= n2 + sz) {
312 memmove(s + i + n2, &s[i + n], sz - i);
313 memcpy(s + i, p, n2);
319 } while (res < n2 + sz);
321 res = sz + n2 + xtra;
323 value_type * tmp = new value_type[res + 1];
325 memcpy(tmp + i, p, n2);
326 memcpy(tmp + i + n2, &s[i + n], sz - i);
334 ///////////////////////////////////////
335 // The lyxstring Invariant tester
336 ///////////////////////////////////////
339 /** Testing of the lyxstring invariant
340 * By creating an object that tests the lyxstring invariant during its
341 * construction *and* its deconstruction we greatly simplify our code.
342 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
343 * will test the invariant upon entry to the code. If the Asserts fail
344 * then we know from the stack trace that the corruption occurred *before*
345 * entry to this method. We can also be sure it didn't happen in any of
346 * the tested lyxstring methods. It is therefore likely to be due to some
347 * other external force.
348 * Several lyxstring methods have multiple exit points which would otherwise
349 * require us to insert a separate test before each return. But since we
350 * created an object its destructor will be called upon exit (any exit!).
351 * We thus get testing at both start and end of a method with one line of
352 * code at the head of a method. More importantly, we get good testing
353 * everytime we run the code.
354 * NOTE: just because we test the invariant doesn't mean we can forget
355 * about testing pre and post conditions specific to any given method.
356 * This test simply proves that the lyxstring/Srep is in a valid state it
357 * does *not* prove that the method did what it was supposed to.
359 class lyxstringInvariant {
361 lyxstringInvariant(lyxstring const *);
362 ~lyxstringInvariant();
365 lyxstring const * object;
369 // To test if this scheme works "as advertised" uncomment the printf's in
370 // the constructor and destructor below and then uncomment the printf and the
371 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
372 // The correct output when LyX has been recompiled and run is:
373 // lyxstringInvariant constructor
374 // lyxstring::operator=(char const *)
375 // lyxstringInvariant constructor
376 // lyxstringInvariant destructor completed
377 // lyxstringInvariant destructor completed
378 // NOTE: The easiest way to catch this snippet of the output is to wait for
379 // the splash screen to disappear and then open and close Help->Credits
381 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
383 // printf("lyxstringInvariant constructor\n");
387 lyxstringInvariant::~lyxstringInvariant()
390 // printf("lyxstringInvariant destructor completed\n");
393 void lyxstringInvariant::helper() const
395 // Some of these tests might look pointless but they are
396 // all part of the invariant and if we want to make sure
397 // we have a bullet proof implementation then we need to
398 // test every last little thing we *know* should be true.
399 // I may have missed a test or two, so feel free to fill
400 // in the gaps. ARRae.
403 Assert(object->rep->s); // s is never 0
404 Assert(object->rep->res); // res cannot be 0
405 Assert(object->rep->sz <= object->rep->res);
406 Assert(object->rep->ref >= 1); // its in use so it must be referenced
407 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
408 // if it does ever == then we should be generating a new copy
409 // and starting again. (Is char always 8-bits?)
411 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
413 #define TestlyxstringInvariant(s)
414 #endif //DEVEL_VERSION
417 ///////////////////////////////////////
418 // Constructors and Deconstructors.
419 ///////////////////////////////////////
421 lyxstring::size_type const lyxstring::npos =
422 static_cast<lyxstring::size_type>(-1);
424 lyxstring::lyxstring()
426 static Srep empty_rep(0, "");
432 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
434 Assert(pos <= x.rep->sz); // STD!
435 if (pos == 0 && n >= x.length()) { // this is the default
439 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
444 lyxstring::lyxstring(value_type const * s, size_type n)
446 Assert(s && n < npos); // STD!
447 static Srep empty_rep(0, "");
448 if (*s && n) { // s is not empty string and n > 0
449 rep = new Srep(min(strlen(s), n), s);
457 lyxstring::lyxstring(value_type const * s)
460 static Srep empty_rep(0, "");
461 if (*s) { // s is not empty string
462 rep = new Srep(strlen(s), s);
470 lyxstring::lyxstring(size_type n, value_type c)
472 Assert(n < npos); // STD!
473 rep = new Srep(n, c);
477 lyxstring::lyxstring(const_iterator first, const_iterator last)
479 rep = new Srep(last - first, first);
483 lyxstring::~lyxstring()
485 if (--rep->ref == 0) delete rep;
488 ///////////////////////
490 ///////////////////////
492 lyxstring::iterator lyxstring::begin()
498 lyxstring::const_iterator lyxstring::begin() const
504 lyxstring::iterator lyxstring::end()
506 return rep->s + rep->sz;
510 lyxstring::const_iterator lyxstring::end() const
512 return rep->s + rep->sz;
516 reverse_iterator lyxstring::rbegin()
518 return reverse_iterator( end() );
522 const_reverse_iterator lyxstring::rbegin() const
524 return const_reverse_iterator( end() );
528 reverse_iterator lyxstring::rend()
530 return reverse_iterator( begin() );
534 const_reverse_iterator lyxstring::rend() const
536 return const_reverse_iterator( begin() );
541 ///////////////////////
543 ///////////////////////
545 lyxstring::size_type lyxstring::size() const
551 void lyxstring::resize(size_type n, value_type c)
553 Assert(n <= npos); // STD!
554 TestlyxstringInvariant(this);
556 // This resets sz to res_arg
557 rep = rep->get_own_copy();
562 lyxstring::size_type lyxstring::capacity() const
568 void lyxstring::reserve(size_type res_arg)
570 TestlyxstringInvariant(this);
572 rep = rep->get_own_copy();
573 rep->reserve(res_arg);
581 lyxstring & lyxstring::operator= (lyxstring const & x)
583 TestlyxstringInvariant(this);
589 lyxstring & lyxstring::operator= (value_type const * s)
592 TestlyxstringInvariant(this);
593 // printf("lyxstring::operator= (value_type const *)\n");
599 lyxstring & lyxstring::operator=(value_type c)
601 TestlyxstringInvariant(this);
605 if (rep->ref == 1) // recycle rep
609 rep = new Srep(1, s);
615 lyxstring & lyxstring::assign(lyxstring const & x)
617 TestlyxstringInvariant(this);
619 x.rep->ref++; // protect against ``st = st''
620 if (--rep->ref == 0) delete rep;
621 rep = x.rep; // share representation
626 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
628 Assert(pos <= x.rep->sz); // STD!
629 TestlyxstringInvariant(this);
631 return assign(x.substr(pos, n));
635 lyxstring & lyxstring::assign(value_type const * s, size_type n)
638 TestlyxstringInvariant(this);
640 n = min(strlen(s), n);
641 if (rep->ref == 1) // recycle rep
645 rep = new Srep(n, s);
651 lyxstring & lyxstring::assign(value_type const * s)
654 TestlyxstringInvariant(this);
656 return assign(s, strlen(s));
660 lyxstring & lyxstring::assign(size_type n, value_type ch)
662 TestlyxstringInvariant(this);
664 rep = rep->get_own_copy();
670 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
672 TestlyxstringInvariant(this);
674 rep = rep->get_own_copy();
675 rep->assign(last - first, first);
684 lyxstring::const_reference lyxstring::operator[](size_type pos) const
686 Assert(pos <= rep->sz); // OURS!
687 static char helper = '\0';
688 return pos == rep->sz ? helper : rep->s[pos];
692 lyxstring::reference lyxstring::operator[](size_type pos)
694 Assert(pos < rep->sz); // OURS!
695 TestlyxstringInvariant(this);
697 rep = rep->get_own_copy();
702 lyxstring::const_reference lyxstring::at(size_type n) const
704 Assert(n < rep->sz); // STD!
709 lyxstring::reference lyxstring::at(size_type n)
711 Assert(n < rep->sz); // STD!
712 TestlyxstringInvariant(this);
714 rep = rep->get_own_copy();
723 lyxstring & lyxstring::operator+=(lyxstring const & x)
725 TestlyxstringInvariant(this);
731 lyxstring & lyxstring::operator+=(value_type const * x)
734 TestlyxstringInvariant(this);
740 lyxstring & lyxstring::operator+=(value_type c)
742 TestlyxstringInvariant(this);
749 void lyxstring::push_back(value_type c)
751 TestlyxstringInvariant(this);
753 rep = rep->get_own_copy();
758 lyxstring & lyxstring::append(lyxstring const & x)
760 TestlyxstringInvariant(this);
762 if (x.empty()) return *this;
763 rep = rep->get_own_copy();
764 rep->append(x.length(), x.rep->s);
769 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
771 Assert(pos <= x.rep->sz); // STD!
772 TestlyxstringInvariant(this);
774 return append(x.substr(pos, n));
778 lyxstring & lyxstring::append(value_type const * p, size_type n)
781 TestlyxstringInvariant(this);
783 if (!*p || !n) return *this;
784 rep = rep->get_own_copy();
785 rep->append(min(n, strlen(p)), p);
790 lyxstring & lyxstring::append(value_type const * p)
793 TestlyxstringInvariant(this);
795 if (!*p) return *this;
796 rep = rep->get_own_copy();
797 rep->append(strlen(p), p);
802 lyxstring & lyxstring::append(size_type n, value_type c)
804 TestlyxstringInvariant(this);
806 value_type * tmp = new value_type[n];
808 rep = rep->get_own_copy();
815 lyxstring & lyxstring::append(iterator first, iterator last)
817 TestlyxstringInvariant(this);
819 rep = rep->get_own_copy();
820 rep->append(last - first, first);
824 // insert value_typeacters before (*this)[pos]
826 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
828 TestlyxstringInvariant(this);
830 return insert(pos, x, 0, x.rep->sz);
834 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
835 size_type pos2, size_type n)
837 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
838 TestlyxstringInvariant(this);
840 rep = rep->get_own_copy();
841 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
846 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
849 TestlyxstringInvariant(this);
852 // insert nothing and you change nothing
853 rep = rep->get_own_copy();
854 rep->insert(pos, p, min(n, strlen(p)));
860 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
863 TestlyxstringInvariant(this);
866 // insert nothing and you change nothing
867 rep = rep->get_own_copy();
868 rep->insert(pos, p, strlen(p));
874 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
876 TestlyxstringInvariant(this);
878 rep = rep->get_own_copy();
879 value_type * tmp = new value_type[n];
881 rep->insert(pos, tmp, n);
887 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
889 TestlyxstringInvariant(this);
891 // what iterator is this supposed to return??
892 size_type tmp = p - begin();
893 insert(p - begin(), 1, c);
894 return begin() + tmp + 1; // ??
898 void lyxstring::insert(iterator p, size_type n , value_type c)
900 TestlyxstringInvariant(this);
902 insert(p - begin(), n , c);
906 void lyxstring::insert(iterator p, iterator first, iterator last)
908 TestlyxstringInvariant(this);
910 insert(p - begin(), first, last - first);
918 // All the below find functions should be verified,
919 // it is very likely that I have mixed up or interpreted
920 // some of the parameters wrong, also some of the funcs can surely
921 // be written more effectively.
923 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
925 if (!rep->sz || i >= rep->sz) return npos;
927 TestlyxstringInvariant(this);
929 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
930 // search until (*this)[i] == a[0]
931 if (rep->s[t] == a[0]) {
932 // check if the rest of the value_types match
934 for (size_type j = 0; j < a.length(); ++j) {
935 if (rep->s[t + j] != a[j]) {
947 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
950 Assert(ptr); // OURS!
951 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
953 TestlyxstringInvariant(this);
955 // What is "n" here? is it the number of value_types to use in ptr
956 // or does "i" and "n" togeter form a substring to search
957 // for ptr in? For now I will assume that "n" tells the length
959 n = min(n, strlen(ptr));
960 for (size_type t = i; rep->sz - t >= n; ++t) {
961 // search until (*this)[i] == a[0]
962 if (rep->s[t] == ptr[0]) {
963 // check if the rest of the value_types match
965 for (size_type j = 0; j < n; ++j) {
966 if (rep->s[t + j] != ptr[j]) {
978 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
981 if (!rep->sz || i >= rep->sz) return npos;
983 TestlyxstringInvariant(this);
985 if (!s || !*s) return npos;
986 return find(s, i, strlen(s));
990 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
992 if (!rep->sz || i >= rep->sz) return npos;
994 TestlyxstringInvariant(this);
996 for (size_type t = 0; t + i < rep->sz; ++t) {
997 if (rep->s[t + i] == c) return t + i;
1003 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
1005 TestlyxstringInvariant(this);
1007 size_type ii = min(rep->sz - 1, i);
1009 if (a[a.length() - 1] == rep->s[ii]) {
1010 int t = rep->sz - 2;
1011 size_type l = ii - 1;
1012 for (; t >= 0; --t, --l) {
1013 if (a[t] != rep->s[l]) break;
1015 if (a[t] == rep->s[l]) return l;
1022 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1025 Assert(ptr); // OURS!
1026 TestlyxstringInvariant(this);
1027 if (!*ptr) return npos;
1029 size_type ii = min(rep->sz - 1, i);
1031 if (ptr[n - 1] == rep->s[ii]) {
1033 size_type l = ii - 1;
1034 for (; t >= 0; --t, --l) {
1035 if (ptr[t] != rep->s[l]) break;
1037 if (ptr[t] == rep->s[l]) return l;
1044 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1047 Assert(ptr); // OURS!
1048 TestlyxstringInvariant(this);
1049 if (!*ptr) return npos;
1051 size_type ii = min(rep->sz - 1, i);
1053 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1054 int t = strlen(ptr) - 2;
1055 size_type l = ii - 1;
1056 for (; t >= 0; --t, --l) {
1057 if (ptr[t] != rep->s[l]) break;
1059 if (ptr[t] == rep->s[l]) return l;
1066 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1068 TestlyxstringInvariant(this);
1070 size_type ii = min(rep->sz - 1, i);
1071 for (size_type t = ii; t != 0; --t) {
1072 if (rep->s[t] == c) return t;
1078 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1081 Assert(i < rep->sz); // OURS!
1082 TestlyxstringInvariant(this);
1084 for (size_type t = i; t < rep->sz; ++t) {
1085 if (a.find(rep->s[t]) != npos) return t;
1091 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1094 Assert(ptr && i < rep->sz); // OURS!
1095 TestlyxstringInvariant(this);
1096 if (!n) return npos;
1098 for (size_type t = i; t < rep->sz; ++t) {
1099 if(memchr(ptr, rep->s[t], n) != 0) return t;
1105 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1108 Assert(ptr && i < rep->sz); // OURS!
1109 TestlyxstringInvariant(this);
1111 for (size_type t = i; t < rep->sz; ++t) {
1112 if (strchr(ptr, rep->s[t]) != 0) return t;
1118 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1120 Assert(i < rep->sz); // OURS!
1121 TestlyxstringInvariant(this);
1123 for (size_type t = i; t < rep->sz; ++t) {
1124 if (rep->s[t] == c) return t;
1130 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1133 TestlyxstringInvariant(this);
1135 size_type ii = min(rep->sz - 1, i);
1136 for (int t = ii; t >= 0; --t) {
1137 if (a.find(rep->s[t]) != npos) return t;
1142 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1145 Assert(ptr); // OURS!
1146 TestlyxstringInvariant(this);
1147 if (!n) return npos;
1149 size_type ii = min(rep->sz - 1, i);
1150 for (int t = ii; t >= 0; --t) {
1151 if(memchr(ptr, rep->s[t], n) != 0) return t;
1157 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1160 Assert(ptr); // OURS!
1161 TestlyxstringInvariant(this);
1163 size_type ii = min(rep->sz - 1, i);
1164 for (int t = ii; t >= 0; --t) {
1165 if (strchr(ptr, rep->s[t]) != 0) return t;
1171 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1173 TestlyxstringInvariant(this);
1175 if (!rep->sz) return npos;
1176 size_type ii = min(rep->sz - 1, i);
1177 for (int t = ii; t >= 0; --t) {
1178 if (rep->s[t] == c) return t;
1184 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1187 TestlyxstringInvariant(this);
1189 if (!rep->sz) return npos;
1190 Assert(i < rep->sz);
1191 for (size_type t = i; t < rep->sz; ++t) {
1192 if (a.find(rep->s[t]) == npos) return t;
1198 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1202 Assert(ptr && i < rep->sz); // OURS!
1203 TestlyxstringInvariant(this);
1205 if (!n) return (i < rep->sz) ? i : npos;
1206 for (size_type t = i; t < rep->sz; ++t) {
1207 if(memchr(ptr, rep->s[t], n) == 0) return t;
1213 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1216 Assert(ptr && i < rep->sz); // OURS!
1217 TestlyxstringInvariant(this);
1219 for (size_type t = i; t < rep->sz; ++t) {
1220 if (strchr(ptr, rep->s[t]) == 0) return t;
1226 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1229 if (!rep->sz) return npos;
1230 Assert(i < rep->sz); // OURS!
1231 TestlyxstringInvariant(this);
1233 for (size_type t = i; t < rep->sz; ++t) {
1234 if (rep->s[t] != c) return t;
1240 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1243 TestlyxstringInvariant(this);
1245 size_type ii = min(rep->sz - 1, i);
1246 for (int t = ii; t >= 0; --t) {
1247 if (a.find(rep->s[t]) == npos) return t;
1253 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1257 Assert(ptr); // OURS!
1258 TestlyxstringInvariant(this);
1260 if (!n) return npos;
1261 size_type ii = min(rep->sz - 1, i);
1263 for (int t = ii; t >= 0; --t) {
1264 if(memchr(ptr, rep->s[t], n) == 0) return t;
1270 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1273 Assert(ptr); // OURS!
1274 TestlyxstringInvariant(this);
1276 size_type ii = min(rep->sz - 1, i);
1277 for (int t = ii; t >= 0; --t) {
1278 if (strchr(ptr, rep->s[t]) == 0) return t;
1284 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1287 TestlyxstringInvariant(this);
1289 size_type ii = min(rep->sz - 1, i);
1290 for (int t = ii; t >= 0; --t) {
1291 if (rep->s[t] != c) return t;
1301 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1303 Assert(i <= rep->sz); // OURS!
1304 TestlyxstringInvariant(this);
1306 return replace(i, n, x, 0, x.rep->sz);
1310 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1311 size_type i2, size_type n2)
1313 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1314 TestlyxstringInvariant(this);
1316 rep = rep->get_own_copy();
1317 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1322 lyxstring & lyxstring::replace(size_type i, size_type n,
1323 value_type const * p, size_type n2)
1325 Assert(p && i < rep->sz); // OURS!
1326 TestlyxstringInvariant(this);
1328 rep = rep->get_own_copy();
1329 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1334 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1336 Assert(p && i < rep->sz); // OURS!
1337 TestlyxstringInvariant(this);
1339 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1343 lyxstring & lyxstring::replace(size_type i, size_type n,
1344 size_type n2, value_type c)
1346 Assert(i < rep->sz); // OURS!
1347 TestlyxstringInvariant(this);
1349 rep = rep->get_own_copy();
1350 value_type * tmp = new value_type[n2];
1352 rep->replace(i, min(n, rep->sz), tmp, n2);
1358 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1360 TestlyxstringInvariant(this);
1362 return replace(i - begin(), i2 - i, str);
1366 lyxstring & lyxstring::replace(iterator i, iterator i2,
1367 value_type const * p, size_type n)
1370 TestlyxstringInvariant(this);
1372 return replace(i - begin(), i2 - i, p, n);
1376 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1379 TestlyxstringInvariant(this);
1381 return replace(i - begin(), i2 - i, p);
1385 lyxstring & lyxstring::replace(iterator i, iterator i2,
1386 size_type n , value_type c)
1388 TestlyxstringInvariant(this);
1390 return replace(i - begin(), i2 - i, n, c);
1394 lyxstring & lyxstring::replace(iterator i, iterator i2,
1395 iterator j, iterator j2)
1397 TestlyxstringInvariant(this);
1399 return replace(i - begin(), i2 - i, j, j2 - j);
1403 void lyxstring::swap(lyxstring & str)
1405 if (rep == str.rep) return;
1406 Srep * tmp = str.rep;
1412 lyxstring & lyxstring::erase(size_type i, size_type n)
1414 Assert(i <= rep->sz); // STD!
1415 TestlyxstringInvariant(this);
1417 rep = rep->get_own_copy();
1418 if (i == 0 && n >= rep->sz) {
1421 n = min(n, rep->sz - i);
1422 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1429 lyxstring::iterator lyxstring::erase(iterator i)
1431 TestlyxstringInvariant(this);
1433 // what iterator is this supposed to return?
1434 // the iterator after the one erased
1435 erase(i - begin(), 1);
1436 return begin(); // BUG
1440 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1442 TestlyxstringInvariant(this);
1444 erase(first - begin(), last - first);
1445 return begin(); // BUG
1449 /////////////////////////////////////
1450 // Conversion to C-style Strings
1451 /////////////////////////////////////
1453 lyxstring::value_type const * lyxstring::c_str() const
1455 rep->s[length()] = '\0';
1460 lyxstring::value_type const * lyxstring::data() const
1466 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1467 size_type pos) const
1469 Assert(buf); // OURS!
1470 Assert(pos <= rep->sz); // STD!
1471 TestlyxstringInvariant(this);
1473 register int nn = min(len, length() - pos);
1474 memcpy(buf, &(rep->s[pos]), nn);
1479 ////////////////////
1481 ////////////////////
1483 // Compare funcs should be verified.
1485 int lyxstring::internal_compare(size_type pos, size_type n,
1486 value_type const * s,
1487 size_type slen, size_type n2) const
1489 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1491 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1493 // remember that n can very well be a lot larger than rep->sz
1494 // so we have to ensure that n is no larger than rep->sz
1495 n = min(n, rep->sz);
1498 return memcmp(&(rep->s[pos]), s, n);
1499 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1508 int lyxstring::compare(lyxstring const & str) const
1510 TestlyxstringInvariant(this);
1511 return internal_compare(0, rep->sz, str.rep->s,
1512 str.rep->sz, str.rep->sz);
1516 int lyxstring::compare(value_type const * s) const
1519 TestlyxstringInvariant(this);
1520 int n = (!s) ? 0 : strlen(s);
1521 return internal_compare(0, rep->sz, s, n, n);
1525 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1527 Assert(pos <= rep->sz); // OURS!
1528 TestlyxstringInvariant(this);
1529 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1533 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1534 size_type pos2, size_type n2) const
1536 Assert(pos <= rep->sz); // OURS!
1537 Assert(pos2 <= str.rep->sz); // OURS!
1538 TestlyxstringInvariant(this);
1539 return internal_compare(pos, n,
1541 str.rep->sz - pos2, n2);
1545 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1548 Assert(s && pos <= rep->sz); // OURS!
1549 TestlyxstringInvariant(this);
1550 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1558 // i = index, n = length
1559 lyxstring lyxstring::substr(size_type i, size_type n) const
1561 Assert(i <= rep->sz); // STD!
1562 TestlyxstringInvariant(this);
1564 return lyxstring(*this, i, n);
1568 /////////////////////////////////////////////
1569 // String operators, non member functions
1570 /////////////////////////////////////////////
1572 bool operator==(lyxstring const & a, lyxstring const & b)
1574 return a.compare(b) == 0;
1578 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1581 return b.compare(a) == 0;
1585 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1588 return a.compare(b) == 0;
1592 bool operator!=(lyxstring const & a, lyxstring const & b)
1594 return a.compare(b) != 0;
1598 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1601 return b.compare(a) != 0;
1605 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1608 return a.compare(b) != 0;
1612 bool operator>(lyxstring const & a, lyxstring const & b)
1614 return a.compare(b) > 0;
1618 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1621 return b.compare(a) < 0; // since we reverse the parameters
1625 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1628 return a.compare(b) > 0;
1632 bool operator<(lyxstring const & a, lyxstring const & b)
1634 return a.compare(b) < 0;
1638 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1641 return b.compare(a) > 0; // since we reverse the parameters
1645 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1648 return a.compare(b) < 0;
1652 bool operator>=(lyxstring const & a, lyxstring const & b)
1654 return a.compare(b) >= 0;
1658 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1661 return b.compare(a) <= 0; // since we reverse the parameters
1665 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1668 return a.compare(b) >= 0;
1672 bool operator<=(lyxstring const & a, lyxstring const & b)
1674 return a.compare(b) <= 0;
1678 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1681 return b.compare(a) >= 0; // since we reverse the parameters
1685 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1688 return a.compare(b) <= 0;
1692 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1700 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1709 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1718 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1727 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1735 void swap(lyxstring & str1, lyxstring & str2)
1743 istream & operator>>(istream & is, lyxstring & s)
1746 // very bad solution
1747 char * nome = new char[1024];
1749 lyxstring tmp(nome);
1751 if (!tmp.empty()) s = tmp;
1754 int w = is.widdth(0);
1758 if (isspace(c)) { is.putback(c); break; }
1760 if (--w == 1) break;
1762 if (s.empty()) is.setstate(ios::failbit);
1767 ostream & operator<<(ostream & o, lyxstring const & s)
1769 return o.write(s.data(), s.length());
1772 istream & getline(istream & is, lyxstring & s,
1773 lyxstring::value_type delim)
1775 // very bad solution