1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1999 The LyX Team.
9 * ======================================================*/
16 #pragma implementation "lyxstring.h"
19 #include "lyxstring.h"
28 // This class is supposed to be functionaly equivalent to a
29 // standard conformant string. This mean among others that we
30 // are useing the same requirements. Before you change anything
31 // in this file consult me and/or the standard to discover the
34 // Asserts with a STD! are required by the standard.
35 // Asserts with a OURS! are added by me.
36 // Reference count has been checked, empty_rep removed and
37 // introduced again in a similar guise. Where is empty_rep _really_
40 // Insertion and replace is implemented, as far as I can see everything
41 // works, but could perhaps be done smarter.
43 // All the different find functions need a good look over.
44 // I have so far not tested them extensively and would be
45 // happy if others took the time to have a peek.
49 ///////////////////////////////////////
50 // The internal string representation
51 ///////////////////////////////////////
53 struct lyxstring::Srep {
55 static size_t const xtra = static_cast<size_t>(8);
60 /// The total amount of data reserved for this representaion
62 /// Data. At least 1 char for trailing null.
63 lyxstring::value_type * s;
66 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
68 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
70 ~Srep() { delete[] s; }
74 if (ref == 1) return this;
76 return new Srep(sz, s);
80 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
82 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
84 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
86 void push_back(lyxstring::value_type c);
88 void insert(lyxstring::size_type pos,
89 const lyxstring::value_type * p,
90 lyxstring::size_type n);
92 void resize(lyxstring::size_type n, lyxstring::value_type c);
94 void reserve(lyxstring::size_type res_arg);
96 void replace(lyxstring::size_type i, lyxstring::size_type n,
97 lyxstring::value_type const * p, lyxstring::size_type n2);
100 Srep & operator=(const Srep &);
104 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
106 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
111 s = new value_type[res + 1]; // add space for terminator
113 // if sz = 0 nothing gets copied and we have an error
116 // possibly allows for large but empty string
117 sz = 0; // this line should be redundant
123 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
128 s = new value_type[res + 1]; // add space for terminator
131 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
132 // allows for large but empty string
138 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
140 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
146 s = new value_type[res + 1]; // add space for terminator
151 // if sz = 0 nothing gets copied and we have an error
155 sz = 0; // this line should be redundant
161 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
167 s = new value_type[res + 1]; // add space for terminator
171 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
172 // allows for a large empty string
178 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
180 register unsigned int const len = sz + asz;
183 value_type * tmp = new value_type[res + 1];
185 memcpy(tmp + sz, p, asz);
190 memcpy(s + sz, p, asz);
196 void lyxstring::Srep::push_back(value_type c)
198 s[sz] = c; // it is always room to put a value_type at the end
202 value_type * tmp = new value_type[res + 1];
210 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
211 lyxstring::size_type n)
215 value_type * tmp = new value_type[res + 1];
217 memcpy(tmp + pos, p, n);
218 memcpy(tmp + pos + n, & s[pos], sz - pos);
223 memmove(s + pos + n, & s[pos], sz - pos);
224 memcpy(s + pos, p, n);
230 void lyxstring::Srep::resize(size_type n, value_type c)
232 // This resets sz to res_arg
233 res = min(n, npos - 2); // We keep no xtra when we resize
234 value_type * tmp = new value_type[res + 1];
235 memcpy(tmp, s, min(sz, res));
237 memset(tmp + sz, c, res - sz);
244 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
246 // This keeps the old sz, but
247 // increases res with res_arg
249 value_type * tmp = new value_type[res + 1];
256 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
257 value_type const * p, size_type n2)
259 // can be called with p=0 and n2=0
262 if (res >= n2 + sz) {
263 memmove(s + i + n2, &s[i + n], sz - i);
264 memcpy(s + i, p, n2);
267 res = sz + n2 + xtra;
268 value_type * tmp = new value_type[res + 1];
270 memcpy(tmp + i, p, n2);
271 memcpy(tmp + i + n2, &s[i + n], sz - i);
279 ///////////////////////////////////////
280 // The lyxstring Invariant tester
281 ///////////////////////////////////////
284 /** Testing of the lyxstring invariant
285 * By creating an object that tests the lyxstring invariant during its
286 * construction *and* its deconstruction we greatly simplify our code.
287 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
288 * will test the invariant upon entry to the code. If the Asserts fail
289 * then we know from the stack trace that the corruption occurred *before*
290 * entry to this method. We can also be sure it didn't happen in any of
291 * the tested lyxstring methods. It is therefore likely to be due to some
292 * other external force.
293 * Several lyxstring methods have multiple exit points which would otherwise
294 * require us to insert a separate test before each return. But since we
295 * created an object its destructor will be called upon exit (any exit!).
296 * We thus get testing at both start and end of a method with one line of
297 * code at the head of a method. More importantly, we get good testing
298 * everytime we run the code.
299 * NOTE: just because we test the invariant doesn't mean we can forget
300 * about testing pre and post conditions specific to any given method.
301 * This test simply proves that the lyxstring/Srep is in a valid state it
302 * does *not* prove that the method did what it was supposed to.
304 class lyxstringInvariant {
306 lyxstringInvariant(lyxstring const *);
307 ~lyxstringInvariant();
310 lyxstring const * object;
314 // To test if this scheme works "as advertised" uncomment the printf's in
315 // the constructor and destructor below and then uncomment the printf and the
316 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
317 // The correct output when LyX has been recompiled and run is:
318 // lyxstringInvariant constructor
319 // lyxstring::operator=(char const *)
320 // lyxstringInvariant constructor
321 // lyxstringInvariant destructor completed
322 // lyxstringInvariant destructor completed
323 // NOTE: The easiest way to catch this snippet of the output is to wait for
324 // the splash screen to disappear and then open and close Help->Credits
326 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
328 // printf("lyxstringInvariant constructor\n");
332 lyxstringInvariant::~lyxstringInvariant()
335 // printf("lyxstringInvariant destructor completed\n");
338 void lyxstringInvariant::helper() const
340 // Some of these tests might look pointless but they are
341 // all part of the invariant and if we want to make sure
342 // we have a bullet proof implementation then we need to
343 // test every last little thing we *know* should be true.
344 // I may have missed a test or two, so feel free to fill
345 // in the gaps. ARRae.
348 Assert(object->rep->s); // s is never 0
349 Assert(object->rep->res); // always some space allocated
350 Assert(object->rep->sz <= object->rep->res);
351 Assert(object->rep->ref >= 1); // its in use so it must be referenced
352 Assert(object->rep->ref < static_cast<size_t>(1) << (8 * sizeof(object->rep->ref) - 1));
353 // if it does ever == then we should be generating a new copy
354 // and starting again. (Is char always 8-bits?)
356 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
358 #define TestlyxstringInvariant(s)
359 #endif //DEVEL_VERSION
362 ///////////////////////////////////////
363 // Constructors and Deconstructors.
364 ///////////////////////////////////////
366 lyxstring::size_type const lyxstring::npos =
367 static_cast<lyxstring::size_type>(-1);
369 lyxstring::lyxstring()
371 static Srep empty_rep(0, "");
377 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
379 Assert(pos <= x.rep->sz); // STD!
380 if (pos == 0 && n >= x.length()) { // this is the default
384 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
389 lyxstring::lyxstring(value_type const * s, size_type n)
391 Assert(s && n < npos); // STD!
392 static Srep empty_rep(0, "");
393 if (*s && n) { // s is not empty string and n > 0
394 rep = new Srep(min(strlen(s), n), s);
402 lyxstring::lyxstring(value_type const * s)
405 static Srep empty_rep(0, "");
406 if (*s) { // s is not empty string
407 rep = new Srep(strlen(s), s);
415 lyxstring::lyxstring(size_type n, value_type c)
417 Assert(n < npos); // STD!
418 rep = new Srep(n, c);
422 lyxstring::lyxstring(iterator first, iterator last)
424 rep = new Srep(last - first, first);
428 lyxstring::~lyxstring()
430 if (--rep->ref == 0) delete rep;
433 ///////////////////////
435 ///////////////////////
437 lyxstring::iterator lyxstring::begin()
443 lyxstring::const_iterator lyxstring::begin() const
449 lyxstring::iterator lyxstring::end()
451 return rep->s + rep->sz;
455 lyxstring::const_iterator lyxstring::end() const
457 return rep->s + rep->sz;
461 reverse_iterator lyxstring::rbegin()
463 return reverse_iterator( end() );
467 const_reverse_iterator lyxstring::rbegin() const
469 return const_reverse_iterator( end() );
473 reverse_iterator lyxstring::rend()
475 return reverse_iterator( begin() );
479 const_reverse_iterator lyxstring::rend() const
481 return const_reverse_iterator( begin() );
486 ///////////////////////
488 ///////////////////////
490 lyxstring::size_type lyxstring::size() const
496 void lyxstring::resize(size_type n, value_type c)
498 Assert(n <= npos); // STD!
499 TestlyxstringInvariant(this);
501 // This resets sz to res_arg
502 rep = rep->get_own_copy();
507 lyxstring::size_type lyxstring::capacity() const
513 void lyxstring::reserve(size_type res_arg)
515 TestlyxstringInvariant(this);
517 rep = rep->get_own_copy();
518 rep->reserve(res_arg);
526 lyxstring & lyxstring::operator=(lyxstring const & x)
528 TestlyxstringInvariant(this);
534 lyxstring & lyxstring::operator=(value_type const * s)
537 TestlyxstringInvariant(this);
538 // printf("lyxstring::operator=(value_type const *)\n");
544 lyxstring & lyxstring::operator=(value_type c)
546 TestlyxstringInvariant(this);
550 if (rep->ref == 1) // recycle rep
554 rep = new Srep(1, s);
560 lyxstring & lyxstring::assign(lyxstring const & x)
562 TestlyxstringInvariant(this);
564 x.rep->ref++; // protect against ``st = st''
565 if (--rep->ref == 0) delete rep;
566 rep = x.rep; // share representation
571 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
573 Assert(pos <= x.rep->sz); // STD!
574 TestlyxstringInvariant(this);
576 return assign(x.substr(pos, n));
580 lyxstring & lyxstring::assign(value_type const * s, size_type n)
583 TestlyxstringInvariant(this);
585 n = min(strlen(s), n);
586 if (rep->ref == 1) // recycle rep
590 rep = new Srep(n, s);
596 lyxstring & lyxstring::assign(value_type const * s)
599 TestlyxstringInvariant(this);
601 return assign(s, strlen(s));
605 lyxstring & lyxstring::assign(size_type n, value_type ch)
607 TestlyxstringInvariant(this);
609 rep = rep->get_own_copy();
615 lyxstring & lyxstring::assign(iterator first, iterator last)
617 TestlyxstringInvariant(this);
619 rep = rep->get_own_copy();
620 rep->assign(last - first, first);
629 lyxstring::const_reference lyxstring::operator[](size_type pos) const
631 Assert(pos <= rep->sz); // OURS!
632 static char helper = '\0';
633 return pos == rep->sz ? helper : rep->s[pos];
637 lyxstring::reference lyxstring::operator[](size_type pos)
639 Assert(pos < rep->sz); // OURS!
640 TestlyxstringInvariant(this);
642 rep = rep->get_own_copy();
647 lyxstring::const_reference lyxstring::at(size_type n) const
649 Assert(n < rep->sz); // STD!
654 lyxstring::reference lyxstring::at(size_type n)
656 Assert(n < rep->sz); // STD!
657 TestlyxstringInvariant(this);
659 rep = rep->get_own_copy();
668 lyxstring & lyxstring::operator+=(lyxstring const & x)
670 TestlyxstringInvariant(this);
676 lyxstring & lyxstring::operator+=(value_type const * x)
679 TestlyxstringInvariant(this);
685 lyxstring & lyxstring::operator+=(value_type c)
687 TestlyxstringInvariant(this);
694 void lyxstring::push_back(value_type c)
696 TestlyxstringInvariant(this);
698 rep = rep->get_own_copy();
703 lyxstring & lyxstring::append(lyxstring const & x)
705 TestlyxstringInvariant(this);
707 if (x.empty()) return *this;
708 rep = rep->get_own_copy();
709 rep->append(x.length(), x.rep->s);
714 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
716 Assert(pos <= x.rep->sz); // STD!
717 TestlyxstringInvariant(this);
719 return append(x.substr(pos, n));
723 lyxstring & lyxstring::append(value_type const * p, size_type n)
726 TestlyxstringInvariant(this);
728 if (!*p || !n) return *this;
729 rep = rep->get_own_copy();
730 rep->append(min(n, strlen(p)), p);
735 lyxstring & lyxstring::append(value_type const * p)
738 TestlyxstringInvariant(this);
740 if (!*p) return *this;
741 rep = rep->get_own_copy();
742 rep->append(strlen(p), p);
747 lyxstring & lyxstring::append(size_type n, value_type c)
749 TestlyxstringInvariant(this);
751 value_type * tmp = new value_type[n];
753 rep = rep->get_own_copy();
760 lyxstring & lyxstring::append(iterator first, iterator last)
762 TestlyxstringInvariant(this);
764 rep = rep->get_own_copy();
765 rep->append(last - first, first);
769 // insert value_typeacters before (*this)[pos]
771 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
773 TestlyxstringInvariant(this);
775 return insert(pos, x, 0, x.rep->sz);
779 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
780 size_type pos2, size_type n)
782 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
783 TestlyxstringInvariant(this);
785 rep = rep->get_own_copy();
786 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
791 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
794 TestlyxstringInvariant(this);
797 // insert nothing and you change nothing
798 rep = rep->get_own_copy();
799 rep->insert(pos, p, min(n, strlen(p)));
805 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
808 TestlyxstringInvariant(this);
811 // insert nothing and you change nothing
812 rep = rep->get_own_copy();
813 rep->insert(pos, p, strlen(p));
819 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
821 TestlyxstringInvariant(this);
823 rep = rep->get_own_copy();
824 value_type * tmp = new value_type[n];
826 rep->insert(pos, tmp, n);
832 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
834 TestlyxstringInvariant(this);
836 // what iterator is this supposed to return??
837 size_type tmp = p - begin();
838 insert(p - begin(), 1, c);
839 return begin() + tmp + 1; // ??
843 void lyxstring::insert(iterator p, size_type n , value_type c)
845 TestlyxstringInvariant(this);
847 insert(p - begin(), n , c);
851 void lyxstring::insert(iterator p, iterator first, iterator last)
853 TestlyxstringInvariant(this);
855 insert(p - begin(), first, last - first);
863 // All the below find functions should be verified,
864 // it is very likely that I have mixed up or interpreted
865 // some of the parameters wrong, also some of the funcs can surely
866 // be written more effectively.
868 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
870 if (!rep->sz || i >= rep->sz) return npos;
872 TestlyxstringInvariant(this);
874 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
875 // search until (*this)[i] == a[0]
876 if (rep->s[t] == a[0]) {
877 // check if the rest of the value_types match
879 for (size_type j = 0; j < a.length(); ++j) {
880 if (rep->s[t + j] != a[j]) {
892 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
895 Assert(ptr); // OURS!
896 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
898 TestlyxstringInvariant(this);
900 // What is "n" here? is it the number of value_types to use in ptr
901 // or does "i" and "n" togeter form a substring to search
902 // for ptr in? For now I will assume that "n" tells the length
904 n = min(n, strlen(ptr));
905 for (size_type t = i; rep->sz - t >= n; ++t) {
906 // search until (*this)[i] == a[0]
907 if (rep->s[t] == ptr[0]) {
908 // check if the rest of the value_types match
910 for (size_type j = 0; j < n; ++j) {
911 if (rep->s[t + j] != ptr[j]) {
923 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
926 if (!rep->sz || i >= rep->sz) return npos;
928 TestlyxstringInvariant(this);
930 if (!s || !*s) return npos;
931 return find(s, i, strlen(s));
935 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
937 if (!rep->sz || i >= rep->sz) return npos;
939 TestlyxstringInvariant(this);
941 for (size_type t = 0; t + i < rep->sz; ++t) {
942 if (rep->s[t + i] == c) return t + i;
948 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
950 TestlyxstringInvariant(this);
952 size_type ii = min(rep->sz - 1, i);
954 if (a[a.length() - 1] == rep->s[ii]) {
956 size_type l = ii - 1;
957 for (; t >= 0; --t, --l) {
958 if (a[t] != rep->s[l]) break;
960 if (a[t] == rep->s[l]) return l;
967 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
970 Assert(ptr); // OURS!
971 TestlyxstringInvariant(this);
972 if (!*ptr) return npos;
974 size_type ii = min(rep->sz - 1, i);
976 if (ptr[n - 1] == rep->s[ii]) {
978 size_type l = ii - 1;
979 for (; t >= 0; --t, --l) {
980 if (ptr[t] != rep->s[l]) break;
982 if (ptr[t] == rep->s[l]) return l;
989 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
992 Assert(ptr); // OURS!
993 TestlyxstringInvariant(this);
994 if (!*ptr) return npos;
996 size_type ii = min(rep->sz - 1, i);
998 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
999 int t = strlen(ptr) - 2;
1000 size_type l = ii - 1;
1001 for (; t >= 0; --t, --l) {
1002 if (ptr[t] != rep->s[l]) break;
1004 if (ptr[t] == rep->s[l]) return l;
1011 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1013 TestlyxstringInvariant(this);
1015 size_type ii = min(rep->sz - 1, i);
1016 for (size_type t = ii; t != 0; --t) {
1017 if (rep->s[t] == c) return t;
1023 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1026 Assert(i < rep->sz); // OURS!
1027 TestlyxstringInvariant(this);
1029 for (size_type t = i; t < rep->sz; ++t) {
1030 if (a.find(rep->s[t]) != npos) return t;
1036 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1039 Assert(ptr && i < rep->sz); // OURS!
1040 TestlyxstringInvariant(this);
1041 if (!n) return npos;
1043 for (size_type t = i; t < rep->sz; ++t) {
1044 if(memchr(ptr, rep->s[t], n) != 0) return t;
1050 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1053 Assert(ptr && i < rep->sz); // OURS!
1054 TestlyxstringInvariant(this);
1056 for (size_type t = i; t < rep->sz; ++t) {
1057 if (strchr(ptr, rep->s[t]) != 0) return t;
1063 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1065 Assert(i < rep->sz); // OURS!
1066 TestlyxstringInvariant(this);
1068 for (size_type t = i; t < rep->sz; ++t) {
1069 if (rep->s[t] == c) return t;
1075 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1078 TestlyxstringInvariant(this);
1080 size_type ii = min(rep->sz - 1, i);
1081 for (int t = ii; t >= 0; --t) {
1082 if (a.find(rep->s[t]) != npos) return t;
1087 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1090 Assert(ptr); // OURS!
1091 TestlyxstringInvariant(this);
1092 if (!n) return npos;
1094 size_type ii = min(rep->sz - 1, i);
1095 for (int t = ii; t >= 0; --t) {
1096 if(memchr(ptr, rep->s[t], n) != 0) return t;
1102 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1105 Assert(ptr); // OURS!
1106 TestlyxstringInvariant(this);
1108 size_type ii = min(rep->sz - 1, i);
1109 for (int t = ii; t >= 0; --t) {
1110 if (strchr(ptr, rep->s[t]) != 0) return t;
1116 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1118 TestlyxstringInvariant(this);
1120 if (!rep->sz) return npos;
1121 size_type ii = min(rep->sz - 1, i);
1122 for (int t = ii; t >= 0; --t) {
1123 if (rep->s[t] == c) return t;
1129 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1132 TestlyxstringInvariant(this);
1134 if (!rep->sz) return npos;
1135 Assert(i < rep->sz);
1136 for (size_type t = i; t < rep->sz; ++t) {
1137 if (a.find(rep->s[t]) == npos) return t;
1143 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1147 Assert(ptr && i < rep->sz); // OURS!
1148 TestlyxstringInvariant(this);
1150 if (!n) return (i < rep->sz) ? i : npos;
1151 for (size_type t = i; t < rep->sz; ++t) {
1152 if(memchr(ptr, rep->s[t], n) == 0) return t;
1158 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1161 Assert(ptr && i < rep->sz); // OURS!
1162 TestlyxstringInvariant(this);
1164 for (size_type t = i; t < rep->sz; ++t) {
1165 if (strchr(ptr, rep->s[t]) == 0) return t;
1171 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1174 if (!rep->sz) return npos;
1175 Assert(i < rep->sz); // OURS!
1176 TestlyxstringInvariant(this);
1178 for (size_type t = i; t < rep->sz; ++t) {
1179 if (rep->s[t] != c) return t;
1185 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1188 TestlyxstringInvariant(this);
1190 size_type ii = min(rep->sz - 1, i);
1191 for (int t = ii; t >= 0; --t) {
1192 if (a.find(rep->s[t]) == npos) return t;
1198 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1202 Assert(ptr); // OURS!
1203 TestlyxstringInvariant(this);
1205 if (!n) return npos;
1206 size_type ii = min(rep->sz - 1, i);
1208 for (int t = ii; t >= 0; --t) {
1209 if(memchr(ptr, rep->s[t], n) == 0) return t;
1215 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1218 Assert(ptr); // OURS!
1219 TestlyxstringInvariant(this);
1221 size_type ii = min(rep->sz - 1, i);
1222 for (int t = ii; t >= 0; --t) {
1223 if (strchr(ptr, rep->s[t]) == 0) return t;
1229 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1232 TestlyxstringInvariant(this);
1234 size_type ii = min(rep->sz - 1, i);
1235 for (int t = ii; t >= 0; --t) {
1236 if (rep->s[t] != c) return t;
1246 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1248 Assert(i <= rep->sz); // OURS!
1249 TestlyxstringInvariant(this);
1251 return replace(i, n, x, 0, x.rep->sz);
1255 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1256 size_type i2, size_type n2)
1258 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1259 TestlyxstringInvariant(this);
1261 rep = rep->get_own_copy();
1262 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1267 lyxstring & lyxstring::replace(size_type i, size_type n,
1268 value_type const * p, size_type n2)
1270 Assert(p && i < rep->sz); // OURS!
1271 TestlyxstringInvariant(this);
1273 rep = rep->get_own_copy();
1274 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1279 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1281 Assert(p && i < rep->sz); // OURS!
1282 TestlyxstringInvariant(this);
1284 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1288 lyxstring & lyxstring::replace(size_type i, size_type n,
1289 size_type n2, value_type c)
1291 Assert(i < rep->sz); // OURS!
1292 TestlyxstringInvariant(this);
1294 rep = rep->get_own_copy();
1295 value_type * tmp = new value_type[n2];
1297 rep->replace(i, min(n, rep->sz), tmp, n2);
1303 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1305 TestlyxstringInvariant(this);
1307 return replace(i - begin(), i2 - i, str);
1311 lyxstring & lyxstring::replace(iterator i, iterator i2,
1312 value_type const * p, size_type n)
1315 TestlyxstringInvariant(this);
1317 return replace(i - begin(), i2 - i, p, n);
1321 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1324 TestlyxstringInvariant(this);
1326 return replace(i - begin(), i2 - i, p);
1330 lyxstring & lyxstring::replace(iterator i, iterator i2,
1331 size_type n , value_type c)
1333 TestlyxstringInvariant(this);
1335 return replace(i - begin(), i2 - i, n, c);
1339 lyxstring & lyxstring::replace(iterator i, iterator i2,
1340 iterator j, iterator j2)
1342 TestlyxstringInvariant(this);
1344 return replace(i - begin(), i2 - i, j, j2 - j);
1348 lyxstring & lyxstring::erase(size_type i, size_type n)
1350 Assert(i <= rep->sz); // STD!
1351 TestlyxstringInvariant(this);
1353 rep = rep->get_own_copy();
1354 if (i == 0 && n >= rep->sz) {
1357 n = min(n, rep->sz - i);
1358 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1365 lyxstring::iterator lyxstring::erase(iterator i)
1367 TestlyxstringInvariant(this);
1369 // what iterator is this supposed to return?
1370 // the iterator after the one erased
1371 erase(i - begin(), 1);
1372 return begin(); // BUG
1376 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1378 TestlyxstringInvariant(this);
1380 erase(first - begin(), last - first);
1381 return begin(); // BUG
1385 /////////////////////////////////////
1386 // Conversion to C-style Strings
1387 /////////////////////////////////////
1389 lyxstring::value_type const * lyxstring::c_str() const
1391 rep->s[length()] = '\0';
1396 lyxstring::value_type const * lyxstring::data() const
1402 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1403 size_type pos) const
1405 Assert(buf); // OURS!
1406 Assert(pos <= rep->sz); // STD!
1407 TestlyxstringInvariant(this);
1409 register int nn = min(len, length() - pos);
1410 memcpy(buf, &(rep->s[pos]), nn);
1415 ////////////////////
1417 ////////////////////
1419 // Compare funcs should be verified.
1421 int lyxstring::internal_compare(size_type pos, size_type n,
1422 value_type const * s,
1423 size_type slen, size_type n2) const
1425 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1427 // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1429 // remember that n can very well be a lot larger than rep->sz
1430 // so we have to ensure that n is no larger than rep->sz
1431 n = min(n, rep->sz);
1434 return memcmp(&(rep->s[pos]), s, n);
1435 int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1444 int lyxstring::compare(lyxstring const & str) const
1446 TestlyxstringInvariant(this);
1447 return internal_compare(0, rep->sz, str.rep->s,
1448 str.rep->sz, str.rep->sz);
1452 int lyxstring::compare(value_type const * s) const
1455 TestlyxstringInvariant(this);
1456 int n = (!s) ? 0 : strlen(s);
1457 return internal_compare(0, rep->sz, s, n, n);
1461 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1463 Assert(pos <= rep->sz); // OURS!
1464 TestlyxstringInvariant(this);
1465 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1469 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1470 size_type pos2, size_type n2) const
1472 Assert(pos <= rep->sz); // OURS!
1473 Assert(pos2 <= str.rep->sz); // OURS!
1474 TestlyxstringInvariant(this);
1475 return internal_compare(pos, n,
1477 str.rep->sz - pos2, n2);
1481 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1484 Assert(s && pos <= rep->sz); // OURS!
1485 TestlyxstringInvariant(this);
1486 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1494 // i = index, n = length
1495 lyxstring lyxstring::substr(size_type i, size_type n) const
1497 Assert(i <= rep->sz); // STD!
1498 TestlyxstringInvariant(this);
1500 return lyxstring(*this, i, n);
1504 /////////////////////////////////////////////
1505 // String operators, non member functions
1506 /////////////////////////////////////////////
1508 bool operator==(lyxstring const & a, lyxstring const & b)
1510 return a.compare(b) == 0;
1514 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1517 return b.compare(a) == 0;
1521 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1524 return a.compare(b) == 0;
1528 bool operator!=(lyxstring const & a, lyxstring const & b)
1530 return a.compare(b) != 0;
1534 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1537 return b.compare(a) != 0;
1541 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1544 return a.compare(b) != 0;
1548 bool operator>(lyxstring const & a, lyxstring const & b)
1550 return a.compare(b) > 0;
1554 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1557 return b.compare(a) < 0; // since we reverse the parameters
1561 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1564 return a.compare(b) > 0;
1568 bool operator<(lyxstring const & a, lyxstring const & b)
1570 return a.compare(b) < 0;
1574 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1577 return b.compare(a) > 0; // since we reverse the parameters
1581 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1584 return a.compare(b) < 0;
1588 bool operator>=(lyxstring const & a, lyxstring const & b)
1590 return a.compare(b) >= 0;
1594 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1597 return b.compare(a) <= 0; // since we reverse the parameters
1601 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1604 return a.compare(b) >= 0;
1608 bool operator<=(lyxstring const & a, lyxstring const & b)
1610 return a.compare(b) <= 0;
1614 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1617 return b.compare(a) >= 0; // since we reverse the parameters
1621 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1624 return a.compare(b) <= 0;
1628 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1636 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1645 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1654 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1663 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1672 istream & operator>>(istream & is, lyxstring & s)
1674 // very bad solution
1675 char * nome = new char[1024];
1677 lyxstring tmp(nome);
1679 if (!tmp.empty()) s = tmp;
1683 ostream & operator<<(ostream & o, lyxstring const & s)
1685 return o.write(s.data(), s.length());
1688 istream & getline(istream & is, lyxstring & s,
1689 lyxstring::value_type delim)
1691 // very bad solution