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"
32 // This class is supposed to be functionaly equivalent to a
33 // standard conformant string. This mean among others that we
34 // are useing the same requirements. Before you change anything
35 // in this file consult me and/or the standard to discover the
38 // Asserts with a STD! are required by the standard.
39 // Asserts with a OURS! are added by me.
40 // Some asserts could still be missing and some of the existing
41 // ones might be wrong or not needed.
43 // Reference count has been checked, empty_rep removed and
44 // introduced again in a similar guise. Where is empty_rep _really_
47 // We are missing a couple of imporant things from the standard:
48 // reverse iterators and methods taking InputIterators as paramters.
49 // Also the methods returning iterators is returning the wrong value.
51 // All the different find functions need a good look over.
52 // I have so far not tested them extensively and would be
53 // happy if others took the time to have a peek.
55 // Space allocation of string.
56 // I have tried to do this very simple without using any special tricks.
57 // Earlier we used a fixed value to enlarge the string with this would
58 // cause a lot of reallocations with large strings (especially if
59 // push_back was used) and wasting space for very small strings.
60 // I have now changed the allocation to use a doubling of reserved
61 // space until it is large enough. So far tests show a small speed
62 // increase and a noticable memory saving.
67 ///////////////////////////////////////
68 // The internal string representation
69 ///////////////////////////////////////
71 struct lyxstring::Srep {
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
122 // lyxstring::assign(const value_type *, size_type)
127 s = new value_type[res + 1]; // add space for terminator
129 // if sz = 0 nothing gets copied and we have an error
132 // possibly allows for large but empty string
133 sz = 0; // this line should be redundant
139 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
144 s = new value_type[res + 1]; // add space for terminator
147 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
148 // allows for large but empty string
154 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
156 // can be called with p == 0
157 // by lyxstring::assign(const value_type *, size_type)
163 s = new value_type[res + 1]; // add space for terminator
168 // if sz = 0 nothing gets copied and we have an error
172 sz = 0; // this line should be redundant
178 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
184 s = new value_type[res + 1]; // add space for terminator
188 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
189 // allows for a large empty string
195 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
197 register unsigned int const len = sz + asz;
202 value_type * tmp = new value_type[res + 1];
204 memcpy(tmp + sz, p, asz);
209 memcpy(s + sz, p, asz);
215 void lyxstring::Srep::push_back(value_type c)
217 s[sz] = c; // it is always room to put a value_type at the end
223 value_type * tmp = new value_type[res + 1];
231 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
232 lyxstring::size_type n)
237 } while (res < n + sz);
238 value_type * tmp = new value_type[res + 1];
240 memcpy(tmp + pos, p, n);
241 memcpy(tmp + pos + n, &s[pos], sz - pos);
246 memmove(s + pos + n, &s[pos], sz - pos);
247 memcpy(s + pos, p, n);
253 void lyxstring::Srep::resize(size_type n, value_type c)
255 // This resets sz to res_arg
256 res = min(n, npos - 2); // We keep no xtra when we resize
257 value_type * tmp = new value_type[res + 1];
258 memcpy(tmp, s, min(sz, res));
260 memset(tmp + sz, c, res - sz);
267 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
269 // This keeps the old sz, but
270 // increases res with res_arg
272 value_type * tmp = new value_type[res + 1];
279 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
280 value_type const * p, size_type n2)
282 // can be called with p= 0 and n2= 0
285 if (res >= n2 + sz) {
286 memmove(s + i + n2, &s[i + n], sz - i);
287 memcpy(s + i, p, n2);
292 } while (res < n2 + sz);
293 value_type * tmp = new value_type[res + 1];
295 memcpy(tmp + i, p, n2);
296 memcpy(tmp + i + n2, &s[i + n], sz - i);
304 ///////////////////////////////////////
305 // The lyxstring Invariant tester
306 ///////////////////////////////////////
307 #ifdef ENABLE_ASSERTIONS
309 /** Testing of the lyxstring invariant
310 * By creating an object that tests the lyxstring invariant during its
311 * construction *and* its deconstruction we greatly simplify our code.
312 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
313 * will test the invariant upon entry to the code. If the Asserts fail
314 * then we know from the stack trace that the corruption occurred *before*
315 * entry to this method. We can also be sure it didn't happen in any of
316 * the tested lyxstring methods. It is therefore likely to be due to some
317 * other external force.
318 * Several lyxstring methods have multiple exit points which would otherwise
319 * require us to insert a separate test before each return. But since we
320 * created an object its destructor will be called upon exit (any exit!).
321 * We thus get testing at both start and end of a method with one line of
322 * code at the head of a method. More importantly, we get good testing
323 * everytime we run the code.
324 * NOTE: just because we test the invariant doesn't mean we can forget
325 * about testing pre and post conditions specific to any given method.
326 * This test simply proves that the lyxstring/Srep is in a valid state it
327 * does *not* prove that the method did what it was supposed to.
329 class lyxstringInvariant {
331 lyxstringInvariant(lyxstring const *);
332 ~lyxstringInvariant();
335 lyxstring const * object;
339 // To test if this scheme works "as advertised" uncomment the printf's in
340 // the constructor and destructor below and then uncomment the printf and the
341 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
342 // The correct output when LyX has been recompiled and run is:
343 // lyxstringInvariant constructor
344 // lyxstring::operator=(char const *)
345 // lyxstringInvariant constructor
346 // lyxstringInvariant destructor completed
347 // lyxstringInvariant destructor completed
348 // NOTE: The easiest way to catch this snippet of the output is to wait for
349 // the splash screen to disappear and then open and close Help->Credits
351 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
353 // printf("lyxstringInvariant constructor\n");
358 lyxstringInvariant::~lyxstringInvariant()
361 // printf("lyxstringInvariant destructor completed\n");
365 void lyxstringInvariant::helper() const
367 // Some of these tests might look pointless but they are
368 // all part of the invariant and if we want to make sure
369 // we have a bullet proof implementation then we need to
370 // test every last little thing we *know* should be true.
371 // I may have missed a test or two, so feel free to fill
372 // in the gaps. ARRae.
375 Assert(object->rep->s); // s is never 0
376 Assert(object->rep->res); // res cannot be 0
377 Assert(object->rep->sz <= object->rep->res);
378 Assert(object->rep->ref >= 1); // its in use so it must be referenced
379 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
380 // if it does ever == then we should be generating a new copy
381 // and starting again. (Is char always 8-bits?)
383 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
385 #define TestlyxstringInvariant(s)
386 #endif /* ENABLE_ASSERTIONS */
389 ///////////////////////////////////////
390 // Constructors and Deconstructors.
391 ///////////////////////////////////////
393 lyxstring::size_type const lyxstring::npos =
394 static_cast<lyxstring::size_type>(-1);
397 lyxstring::lyxstring()
399 static Srep empty_rep(0, "");
405 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
407 Assert(pos <= x.rep->sz); // STD!
408 if (pos == 0 && n >= x.length()) { // this is the default
412 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
417 lyxstring::lyxstring(value_type const * s, size_type n)
419 Assert(s && n < npos); // STD!
420 static Srep empty_rep(0, "");
422 rep = new Srep(n, s);
430 lyxstring::lyxstring(value_type const * s)
433 static Srep empty_rep(0, "");
434 if (*s) { // s is not empty string
435 rep = new Srep(strlen(s), s);
443 lyxstring::lyxstring(size_type n, value_type c)
445 Assert(n < npos); // STD!
446 rep = new Srep(n, c);
450 lyxstring::lyxstring(const_iterator first, const_iterator last)
452 rep = new Srep(last - first, first);
456 lyxstring::~lyxstring()
458 if (--rep->ref == 0) delete rep;
461 ///////////////////////
463 ///////////////////////
465 lyxstring::iterator lyxstring::begin()
467 rep = rep->get_own_copy();
472 lyxstring::const_iterator lyxstring::begin() const
478 lyxstring::iterator lyxstring::end()
480 rep = rep->get_own_copy();
481 return rep->s + rep->sz;
485 lyxstring::const_iterator lyxstring::end() const
487 return rep->s + rep->sz;
491 reverse_iterator lyxstring::rbegin()
493 return reverse_iterator( end() );
497 const_reverse_iterator lyxstring::rbegin() const
499 return const_reverse_iterator( end() );
503 reverse_iterator lyxstring::rend()
505 return reverse_iterator( begin() );
509 const_reverse_iterator lyxstring::rend() const
511 return const_reverse_iterator( begin() );
516 ///////////////////////
518 ///////////////////////
520 lyxstring::size_type lyxstring::size() const
526 void lyxstring::resize(size_type n, value_type c)
528 Assert(n <= npos); // STD!
529 TestlyxstringInvariant(this);
531 // This resets sz to res_arg
532 rep = rep->get_own_copy();
537 lyxstring::size_type lyxstring::capacity() const
543 void lyxstring::reserve(size_type res_arg)
545 TestlyxstringInvariant(this);
547 rep = rep->get_own_copy();
548 rep->reserve(res_arg);
556 lyxstring & lyxstring::operator=(lyxstring const & x)
558 TestlyxstringInvariant(this);
564 lyxstring & lyxstring::operator=(value_type const * s)
567 TestlyxstringInvariant(this);
568 // printf("lyxstring::operator= (value_type const *)\n");
574 lyxstring & lyxstring::operator=(value_type c)
576 TestlyxstringInvariant(this);
580 if (rep->ref == 1) // recycle rep
584 rep = new Srep(1, s);
590 lyxstring & lyxstring::assign(lyxstring const & x)
592 TestlyxstringInvariant(this);
594 x.rep->ref++; // protect against ``st = st''
595 if (--rep->ref == 0) delete rep;
596 rep = x.rep; // share representation
601 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
603 Assert(pos <= x.rep->sz); // STD!
604 TestlyxstringInvariant(this);
606 return assign(x.substr(pos, n));
610 lyxstring & lyxstring::assign(value_type const * s, size_type n)
612 Assert(s && n < npos); // STD!
613 TestlyxstringInvariant(this);
615 if (rep->ref == 1) // recycle rep
619 rep = new Srep(n, s);
625 lyxstring & lyxstring::assign(value_type const * s)
628 TestlyxstringInvariant(this);
630 return assign(s, strlen(s));
634 lyxstring & lyxstring::assign(size_type n, value_type ch)
636 TestlyxstringInvariant(this);
638 rep = rep->get_own_copy();
644 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
646 TestlyxstringInvariant(this);
648 rep = rep->get_own_copy();
649 rep->assign(last - first, first);
658 lyxstring::const_reference lyxstring::operator[](size_type pos) const
660 Assert(pos <= rep->sz); // OURS!
661 static char helper = '\0';
662 return pos == rep->sz ? helper : rep->s[pos];
666 lyxstring::reference lyxstring::operator[](size_type pos)
668 Assert(pos < rep->sz); // OURS!
669 TestlyxstringInvariant(this);
671 rep = rep->get_own_copy();
676 lyxstring::const_reference lyxstring::at(size_type n) const
678 Assert(n < rep->sz); // STD!
683 lyxstring::reference lyxstring::at(size_type n)
685 Assert(n < rep->sz); // STD!
686 TestlyxstringInvariant(this);
688 rep = rep->get_own_copy();
697 lyxstring & lyxstring::operator+=(lyxstring const & x)
699 TestlyxstringInvariant(this);
705 lyxstring & lyxstring::operator+=(value_type const * x)
708 TestlyxstringInvariant(this);
714 lyxstring & lyxstring::operator+=(value_type c)
716 TestlyxstringInvariant(this);
723 void lyxstring::push_back(value_type c)
725 TestlyxstringInvariant(this);
727 rep = rep->get_own_copy();
732 lyxstring & lyxstring::append(lyxstring const & x)
734 TestlyxstringInvariant(this);
736 if (x.empty()) return *this;
737 rep = rep->get_own_copy();
738 rep->append(x.length(), x.rep->s);
743 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
745 Assert(pos <= x.rep->sz); // STD!
746 TestlyxstringInvariant(this);
748 return append(x.substr(pos, n));
752 lyxstring & lyxstring::append(value_type const * p, size_type n)
755 TestlyxstringInvariant(this);
757 if (!*p || !n) return *this;
758 rep = rep->get_own_copy();
764 lyxstring & lyxstring::append(value_type const * p)
767 return append(p, strlen(p));
771 lyxstring & lyxstring::append(size_type n, value_type c)
773 TestlyxstringInvariant(this);
775 value_type * tmp = new value_type[n];
777 rep = rep->get_own_copy();
784 lyxstring & lyxstring::append(iterator first, iterator last)
786 TestlyxstringInvariant(this);
788 rep = rep->get_own_copy();
789 rep->append(last - first, first);
793 // insert characters before (*this)[pos]
795 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
797 TestlyxstringInvariant(this);
799 return insert(pos, x, 0, x.rep->sz);
803 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
804 size_type pos2, size_type n)
806 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
807 TestlyxstringInvariant(this);
809 rep = rep->get_own_copy();
810 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
815 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
818 TestlyxstringInvariant(this);
821 // insert nothing and you change nothing
822 rep = rep->get_own_copy();
823 rep->insert(pos, p, n);
829 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
832 return insert(pos, p, strlen(p));
836 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
838 TestlyxstringInvariant(this);
840 rep = rep->get_own_copy();
841 value_type * tmp = new value_type[n];
843 rep->insert(pos, tmp, n);
849 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
851 TestlyxstringInvariant(this);
853 // what iterator is this supposed to return??
854 size_type tmp = p - begin();
855 insert(p - begin(), 1, c);
856 return begin() + tmp + 1; // ??
860 void lyxstring::insert(iterator p, size_type n , value_type c)
862 TestlyxstringInvariant(this);
864 insert(p - begin(), n , c);
868 void lyxstring::insert(iterator p, iterator first, iterator last)
870 TestlyxstringInvariant(this);
872 insert(p - begin(), first, last - first);
880 // All the below find functions should be verified,
881 // it is very likely that I have mixed up or interpreted
882 // some of the parameters wrong, also some of the funcs can surely
883 // be written more effectively.
885 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
887 if (!rep->sz || i >= rep->sz) return npos;
889 TestlyxstringInvariant(this);
891 size_type n = a.length();
893 for (size_type t = i; rep->sz - t >= n; ++t) {
894 // search until (*this)[i] == a[0]
895 if (rep->s[t] == a[0]) {
896 // check if the rest of the value_types match
898 for (size_type j = 1; j < n; ++j) {
899 if (rep->s[t + j] != a[j]) {
911 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
914 Assert(ptr); // OURS!
915 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
917 TestlyxstringInvariant(this);
919 // What is "n" here? is it the number of value_types to use in ptr
920 // or does "i" and "n" togeter form a substring to search
921 // for ptr in? For now I will assume that "n" tells the length
923 n = min(n, strlen(ptr));
925 for (size_type t = i; rep->sz - t >= n; ++t) {
926 // search until (*this)[i] == a[0]
927 if (rep->s[t] == ptr[0]) {
928 // check if the rest of the value_types match
930 for (size_type j = 1; j < n; ++j) {
931 if (rep->s[t + j] != ptr[j]) {
943 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
946 if (!rep->sz || i >= rep->sz) return npos;
948 TestlyxstringInvariant(this);
950 if (!s || !*s) return npos;
951 return find(s, i, strlen(s));
955 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
957 if (!rep->sz || i >= rep->sz) return npos;
959 TestlyxstringInvariant(this);
961 for (size_type t = 0; t + i < rep->sz; ++t) {
962 if (rep->s[t + i] == c) return t + i;
968 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
970 TestlyxstringInvariant(this);
972 size_type n = a.length();
973 if (!n || rep->sz < n)
976 size_type t = min(rep->sz - n, i);
978 if (rep->s[t] == a[0]) {
979 // check if the rest of the value_types match
981 for (size_type j = 1; j < n; ++j) {
982 if (rep->s[t + j] != a[j]) {
994 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
997 Assert(ptr); // OURS!
998 TestlyxstringInvariant(this);
1000 n = min(n, strlen(ptr));
1001 if (!n || rep->sz < n)
1004 size_type t = min(rep->sz - n, i);
1006 if (rep->s[t] == ptr[0]) {
1007 // check if the rest of the value_types match
1009 for (size_type j = 1; j < n; ++j) {
1010 if (rep->s[t + j] != ptr[j]) {
1015 if (equal) return t;
1022 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1025 Assert(ptr); // OURS!
1027 if (!ptr || !*ptr) return npos;
1028 return rfind(ptr, i, strlen(ptr));
1032 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1034 TestlyxstringInvariant(this);
1036 size_type const sz = rep->sz;
1037 if (sz < 1) return npos;
1038 size_type ii = min(sz - 1, i);
1040 if (rep->s[ii] == c) return ii;
1046 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1049 Assert(i < rep->sz); // OURS!
1050 TestlyxstringInvariant(this);
1052 for (size_type t = i; t < rep->sz; ++t) {
1053 if (a.find(rep->s[t]) != npos) return t;
1059 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1063 Assert(ptr && i < rep->sz); // OURS!
1064 TestlyxstringInvariant(this);
1065 if (!n) return npos;
1067 for (size_type t = i; t < rep->sz; ++t) {
1068 if (memchr(ptr, rep->s[t], n) != 0) return t;
1074 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1077 Assert(ptr && i < rep->sz); // OURS!
1078 TestlyxstringInvariant(this);
1080 for (size_type t = i; t < rep->sz; ++t) {
1081 if (strchr(ptr, rep->s[t]) != 0) return t;
1087 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1089 Assert(i < rep->sz); // OURS!
1090 TestlyxstringInvariant(this);
1092 for (size_type t = i; t < rep->sz; ++t) {
1093 if (rep->s[t] == c) return t;
1099 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1102 TestlyxstringInvariant(this);
1104 size_type ii = min(rep->sz - 1, i);
1105 for (int t = ii; t >= 0; --t) {
1106 if (a.find(rep->s[t]) != npos) return t;
1112 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1116 Assert(ptr); // OURS!
1117 TestlyxstringInvariant(this);
1118 if (!n) return npos;
1120 size_type ii = min(rep->sz - 1, i);
1121 for (int t = ii; t >= 0; --t) {
1122 if (memchr(ptr, rep->s[t], n) != 0) return t;
1128 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1131 Assert(ptr); // OURS!
1132 TestlyxstringInvariant(this);
1134 size_type ii = min(rep->sz - 1, i);
1135 for (int t = ii; t >= 0; --t) {
1136 if (strchr(ptr, rep->s[t]) != 0) return t;
1142 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1144 TestlyxstringInvariant(this);
1146 if (!rep->sz) return npos;
1147 size_type ii = min(rep->sz - 1, i);
1148 for (int t = ii; t >= 0; --t) {
1149 if (rep->s[t] == c) return t;
1155 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1158 TestlyxstringInvariant(this);
1160 if (!rep->sz) return npos;
1161 Assert(i < rep->sz);
1162 for (size_type t = i; t < rep->sz; ++t) {
1163 if (a.find(rep->s[t]) == npos) return t;
1169 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1173 Assert(ptr && i < rep->sz); // OURS!
1174 TestlyxstringInvariant(this);
1176 if (!n) return (i < rep->sz) ? i : npos;
1177 for (size_type t = i; t < rep->sz; ++t) {
1178 if (memchr(ptr, rep->s[t], n) == 0) return t;
1184 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1187 Assert(ptr && i < rep->sz); // OURS!
1188 TestlyxstringInvariant(this);
1190 for (size_type t = i; t < rep->sz; ++t) {
1191 if (strchr(ptr, rep->s[t]) == 0) return t;
1197 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1200 if (!rep->sz) return npos;
1201 Assert(i < rep->sz); // OURS!
1202 TestlyxstringInvariant(this);
1204 for (size_type t = i; t < rep->sz; ++t) {
1205 if (rep->s[t] != c) return t;
1211 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1214 TestlyxstringInvariant(this);
1216 size_type ii = min(rep->sz - 1, i);
1217 for (int t = ii; t >= 0; --t) {
1218 if (a.find(rep->s[t]) == npos) return t;
1224 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1228 Assert(ptr); // OURS!
1229 TestlyxstringInvariant(this);
1231 if (!n) return npos;
1232 size_type ii = min(rep->sz - 1, i);
1234 for (int t = ii; t >= 0; --t) {
1235 if (memchr(ptr, rep->s[t], n) == 0) return t;
1241 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1244 Assert(ptr); // OURS!
1245 TestlyxstringInvariant(this);
1247 size_type ii = min(rep->sz - 1, i);
1248 for (int t = ii; t >= 0; --t) {
1249 if (strchr(ptr, rep->s[t]) == 0) return t;
1255 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1258 TestlyxstringInvariant(this);
1260 size_type ii = min(rep->sz - 1, i);
1261 for (int t = ii; t >= 0; --t) {
1262 if (rep->s[t] != c) return t;
1272 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1274 Assert(i <= rep->sz); // OURS!
1275 TestlyxstringInvariant(this);
1277 return replace(i, n, x, 0, x.rep->sz);
1281 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1282 size_type i2, size_type n2)
1284 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1285 TestlyxstringInvariant(this);
1287 rep = rep->get_own_copy();
1288 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1293 lyxstring & lyxstring::replace(size_type i, size_type n,
1294 value_type const * p, size_type n2)
1296 Assert(p && i < rep->sz); // OURS!
1297 TestlyxstringInvariant(this);
1299 rep = rep->get_own_copy();
1300 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1305 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1307 Assert(p && i < rep->sz); // OURS!
1308 TestlyxstringInvariant(this);
1310 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1314 lyxstring & lyxstring::replace(size_type i, size_type n,
1315 size_type n2, value_type c)
1317 Assert(i <= rep->sz); // OURS!
1318 TestlyxstringInvariant(this);
1320 rep = rep->get_own_copy();
1321 value_type * tmp = new value_type[n2];
1323 rep->replace(i, min(n, rep->sz), tmp, n2);
1329 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1331 TestlyxstringInvariant(this);
1333 return replace(i - begin(), i2 - i, str);
1337 lyxstring & lyxstring::replace(iterator i, iterator i2,
1338 value_type const * p, size_type n)
1341 TestlyxstringInvariant(this);
1343 return replace(i - begin(), i2 - i, p, n);
1347 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1350 TestlyxstringInvariant(this);
1352 return replace(i - begin(), i2 - i, p);
1356 lyxstring & lyxstring::replace(iterator i, iterator i2,
1357 size_type n , value_type c)
1359 TestlyxstringInvariant(this);
1361 return replace(i - begin(), i2 - i, n, c);
1365 lyxstring & lyxstring::replace(iterator i, iterator i2,
1366 iterator j, iterator j2)
1368 TestlyxstringInvariant(this);
1370 return replace(i - begin(), i2 - i, j, j2 - j);
1374 void lyxstring::swap(lyxstring & str)
1376 if (rep == str.rep) return;
1377 Srep * tmp = str.rep;
1383 lyxstring & lyxstring::erase(size_type i, size_type n)
1385 Assert(i <= rep->sz); // STD!
1386 TestlyxstringInvariant(this);
1388 rep = rep->get_own_copy();
1389 if (i == 0 && n >= rep->sz) {
1392 n = min(n, rep->sz - i);
1393 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1400 lyxstring::iterator lyxstring::erase(iterator i)
1402 TestlyxstringInvariant(this);
1404 // what iterator is this supposed to return?
1405 // the iterator after the one erased
1406 erase(i - begin(), 1);
1407 return begin(); // BUG
1411 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1413 TestlyxstringInvariant(this);
1415 erase(first - begin(), last - first);
1416 return begin(); // BUG
1420 /////////////////////////////////////
1421 // Conversion to C-style Strings
1422 /////////////////////////////////////
1424 lyxstring::value_type const * lyxstring::c_str() const
1426 rep->s[length()] = '\0';
1431 lyxstring::value_type const * lyxstring::data() const
1437 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1438 size_type pos) const
1440 Assert(buf); // OURS!
1441 Assert(pos <= rep->sz); // STD!
1442 TestlyxstringInvariant(this);
1444 register int nn = min(len, length() - pos);
1445 memcpy(buf, &(rep->s[pos]), nn);
1450 ////////////////////
1452 ////////////////////
1454 // Compare funcs should be verified.
1456 int lyxstring::internal_compare(size_type pos, size_type n,
1457 value_type const * s,
1458 size_type slen, size_type n2) const
1460 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1462 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1464 // remember that n can very well be a lot larger than rep->sz
1465 // so we have to ensure that n is no larger than rep->sz
1466 n = min(n, rep->sz);
1469 return memcmp(&(rep->s[pos]), s, n);
1470 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1479 int lyxstring::compare(lyxstring const & str) const
1481 TestlyxstringInvariant(this);
1482 return internal_compare(0, rep->sz, str.rep->s,
1483 str.rep->sz, str.rep->sz);
1487 int lyxstring::compare(value_type const * s) const
1490 TestlyxstringInvariant(this);
1491 int n = (!s) ? 0 : strlen(s);
1492 return internal_compare(0, rep->sz, s, n, n);
1496 int lyxstring::compare(size_type pos, size_type n,
1497 lyxstring const & str) const
1499 Assert(pos <= rep->sz); // OURS!
1500 TestlyxstringInvariant(this);
1501 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1505 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1506 size_type pos2, size_type n2) const
1508 Assert(pos <= rep->sz); // OURS!
1509 Assert(pos2 <= str.rep->sz); // OURS!
1510 TestlyxstringInvariant(this);
1511 return internal_compare(pos, n,
1513 str.rep->sz - pos2, n2);
1517 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1520 Assert(s && pos <= rep->sz); // OURS!
1521 TestlyxstringInvariant(this);
1522 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1530 // i = index, n = length
1531 lyxstring lyxstring::substr(size_type i, size_type n) const
1533 Assert(i <= rep->sz); // STD!
1534 TestlyxstringInvariant(this);
1536 return lyxstring(*this, i, n);
1540 /////////////////////////////////////////////
1541 // String operators, non member functions
1542 /////////////////////////////////////////////
1544 bool operator==(lyxstring const & a, lyxstring const & b)
1546 return a.compare(b) == 0;
1550 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1553 return b.compare(a) == 0;
1557 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1560 return a.compare(b) == 0;
1564 bool operator!=(lyxstring const & a, lyxstring const & b)
1566 return a.compare(b) != 0;
1570 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1573 return b.compare(a) != 0;
1577 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1580 return a.compare(b) != 0;
1584 bool operator>(lyxstring const & a, lyxstring const & b)
1586 return a.compare(b) > 0;
1590 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1593 return b.compare(a) < 0; // since we reverse the parameters
1597 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1600 return a.compare(b) > 0;
1604 bool operator<(lyxstring const & a, lyxstring const & b)
1606 return a.compare(b) < 0;
1610 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1613 return b.compare(a) > 0; // since we reverse the parameters
1617 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1620 return a.compare(b) < 0;
1624 bool operator>=(lyxstring const & a, lyxstring const & b)
1626 return a.compare(b) >= 0;
1630 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1633 return b.compare(a) <= 0; // since we reverse the parameters
1637 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1640 return a.compare(b) >= 0;
1644 bool operator<=(lyxstring const & a, lyxstring const & b)
1646 return a.compare(b) <= 0;
1650 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1653 return b.compare(a) >= 0; // since we reverse the parameters
1657 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1660 return a.compare(b) <= 0;
1664 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1672 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1681 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1690 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1699 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1707 void swap(lyxstring & str1, lyxstring & str2)
1715 istream & operator>>(istream & is, lyxstring & s)
1718 // very bad solution
1719 char * nome = new char[1024];
1721 lyxstring tmp(nome);
1723 if (!tmp.empty()) s = tmp;
1726 int w = is.widdth(0);
1730 if (isspace(c)) { is.putback(c); break; }
1732 if (--w == 1) break;
1734 if (s.empty()) is.setstate(ios::failbit);
1740 ostream & operator<<(ostream & o, lyxstring const & s)
1742 return o.write(s.data(), s.length());
1746 istream & getline(istream & is, lyxstring & s,
1747 lyxstring::value_type delim)
1749 // very bad solution
1765 lyxstring a = "abcac";
1766 cout << a.rfind("ab") << endl;
1767 cout << a.rfind("c") << endl;
1768 cout << a.rfind("d") << endl;