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 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
892 // search until (*this)[i] == a[0]
893 if (rep->s[t] == a[0]) {
894 // check if the rest of the value_types match
896 for (size_type j = 0; j < a.length(); ++j) {
897 if (rep->s[t + j] != a[j]) {
909 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
912 Assert(ptr); // OURS!
913 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
915 TestlyxstringInvariant(this);
917 // What is "n" here? is it the number of value_types to use in ptr
918 // or does "i" and "n" togeter form a substring to search
919 // for ptr in? For now I will assume that "n" tells the length
921 n = min(n, strlen(ptr));
922 for (size_type t = i; rep->sz - t >= n; ++t) {
923 // search until (*this)[i] == a[0]
924 if (rep->s[t] == ptr[0]) {
925 // check if the rest of the value_types match
927 for (size_type j = 0; j < n; ++j) {
928 if (rep->s[t + j] != ptr[j]) {
940 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
943 if (!rep->sz || i >= rep->sz) return npos;
945 TestlyxstringInvariant(this);
947 if (!s || !*s) return npos;
948 return find(s, i, strlen(s));
952 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
954 if (!rep->sz || i >= rep->sz) return npos;
956 TestlyxstringInvariant(this);
958 for (size_type t = 0; t + i < rep->sz; ++t) {
959 if (rep->s[t + i] == c) return t + i;
965 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
967 TestlyxstringInvariant(this);
969 size_type ii = min(rep->sz - 1, i);
971 if (a[a.length() - 1] == rep->s[ii]) {
973 size_type l = ii - 1;
974 for (; t >= 0; --t, --l) {
975 if (a[t] != rep->s[l]) break;
977 if (a[t] == rep->s[l]) return l;
984 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
987 Assert(ptr); // OURS!
988 TestlyxstringInvariant(this);
989 if (!*ptr) return npos;
991 size_type ii = min(rep->sz - 1, i);
993 if (ptr[n - 1] == rep->s[ii]) {
995 size_type l = ii - 1;
996 for (; t >= 0; --t, --l) {
997 if (ptr[t] != rep->s[l]) break;
999 if (ptr[t] == rep->s[l]) return l;
1006 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1009 Assert(ptr); // OURS!
1010 TestlyxstringInvariant(this);
1011 if (!*ptr) return npos;
1013 size_type ii = min(rep->sz - 1, i);
1015 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1016 int t = strlen(ptr) - 2;
1017 size_type l = ii - 1;
1018 for (; t >= 0; --t, --l) {
1019 if (ptr[t] != rep->s[l]) break;
1021 if (ptr[t] == rep->s[l]) return l;
1028 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1030 TestlyxstringInvariant(this);
1032 size_type const sz = rep->sz;
1033 if (sz < 1) return npos;
1034 size_type ii = min(sz - 1, i);
1036 if (rep->s[ii] == c) return ii;
1042 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1045 Assert(i < rep->sz); // OURS!
1046 TestlyxstringInvariant(this);
1048 for (size_type t = i; t < rep->sz; ++t) {
1049 if (a.find(rep->s[t]) != npos) return t;
1055 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1059 Assert(ptr && i < rep->sz); // OURS!
1060 TestlyxstringInvariant(this);
1061 if (!n) return npos;
1063 for (size_type t = i; t < rep->sz; ++t) {
1064 if (memchr(ptr, rep->s[t], n) != 0) return t;
1070 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1073 Assert(ptr && i < rep->sz); // OURS!
1074 TestlyxstringInvariant(this);
1076 for (size_type t = i; t < rep->sz; ++t) {
1077 if (strchr(ptr, rep->s[t]) != 0) return t;
1083 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1085 Assert(i < rep->sz); // OURS!
1086 TestlyxstringInvariant(this);
1088 for (size_type t = i; t < rep->sz; ++t) {
1089 if (rep->s[t] == c) return t;
1095 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1098 TestlyxstringInvariant(this);
1100 size_type ii = min(rep->sz - 1, i);
1101 for (int t = ii; t >= 0; --t) {
1102 if (a.find(rep->s[t]) != npos) return t;
1108 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1112 Assert(ptr); // OURS!
1113 TestlyxstringInvariant(this);
1114 if (!n) return npos;
1116 size_type ii = min(rep->sz - 1, i);
1117 for (int t = ii; t >= 0; --t) {
1118 if (memchr(ptr, rep->s[t], n) != 0) return t;
1124 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1127 Assert(ptr); // OURS!
1128 TestlyxstringInvariant(this);
1130 size_type ii = min(rep->sz - 1, i);
1131 for (int t = ii; t >= 0; --t) {
1132 if (strchr(ptr, rep->s[t]) != 0) return t;
1138 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1140 TestlyxstringInvariant(this);
1142 if (!rep->sz) return npos;
1143 size_type ii = min(rep->sz - 1, i);
1144 for (int t = ii; t >= 0; --t) {
1145 if (rep->s[t] == c) return t;
1151 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1154 TestlyxstringInvariant(this);
1156 if (!rep->sz) return npos;
1157 Assert(i < rep->sz);
1158 for (size_type t = i; t < rep->sz; ++t) {
1159 if (a.find(rep->s[t]) == npos) return t;
1165 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1169 Assert(ptr && i < rep->sz); // OURS!
1170 TestlyxstringInvariant(this);
1172 if (!n) return (i < rep->sz) ? i : npos;
1173 for (size_type t = i; t < rep->sz; ++t) {
1174 if (memchr(ptr, rep->s[t], n) == 0) return t;
1180 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1183 Assert(ptr && i < rep->sz); // OURS!
1184 TestlyxstringInvariant(this);
1186 for (size_type t = i; t < rep->sz; ++t) {
1187 if (strchr(ptr, rep->s[t]) == 0) return t;
1193 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1196 if (!rep->sz) return npos;
1197 Assert(i < rep->sz); // OURS!
1198 TestlyxstringInvariant(this);
1200 for (size_type t = i; t < rep->sz; ++t) {
1201 if (rep->s[t] != c) return t;
1207 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1210 TestlyxstringInvariant(this);
1212 size_type ii = min(rep->sz - 1, i);
1213 for (int t = ii; t >= 0; --t) {
1214 if (a.find(rep->s[t]) == npos) return t;
1220 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1224 Assert(ptr); // OURS!
1225 TestlyxstringInvariant(this);
1227 if (!n) return npos;
1228 size_type ii = min(rep->sz - 1, i);
1230 for (int t = ii; t >= 0; --t) {
1231 if (memchr(ptr, rep->s[t], n) == 0) return t;
1237 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1240 Assert(ptr); // OURS!
1241 TestlyxstringInvariant(this);
1243 size_type ii = min(rep->sz - 1, i);
1244 for (int t = ii; t >= 0; --t) {
1245 if (strchr(ptr, rep->s[t]) == 0) return t;
1251 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1254 TestlyxstringInvariant(this);
1256 size_type ii = min(rep->sz - 1, i);
1257 for (int t = ii; t >= 0; --t) {
1258 if (rep->s[t] != c) return t;
1268 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1270 Assert(i <= rep->sz); // OURS!
1271 TestlyxstringInvariant(this);
1273 return replace(i, n, x, 0, x.rep->sz);
1277 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1278 size_type i2, size_type n2)
1280 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1281 TestlyxstringInvariant(this);
1283 rep = rep->get_own_copy();
1284 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1289 lyxstring & lyxstring::replace(size_type i, size_type n,
1290 value_type const * p, size_type n2)
1292 Assert(p && i < rep->sz); // OURS!
1293 TestlyxstringInvariant(this);
1295 rep = rep->get_own_copy();
1296 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1301 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1303 Assert(p && i < rep->sz); // OURS!
1304 TestlyxstringInvariant(this);
1306 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1310 lyxstring & lyxstring::replace(size_type i, size_type n,
1311 size_type n2, value_type c)
1313 Assert(i <= rep->sz); // OURS!
1314 TestlyxstringInvariant(this);
1316 rep = rep->get_own_copy();
1317 value_type * tmp = new value_type[n2];
1319 rep->replace(i, min(n, rep->sz), tmp, n2);
1325 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1327 TestlyxstringInvariant(this);
1329 return replace(i - begin(), i2 - i, str);
1333 lyxstring & lyxstring::replace(iterator i, iterator i2,
1334 value_type const * p, size_type n)
1337 TestlyxstringInvariant(this);
1339 return replace(i - begin(), i2 - i, p, n);
1343 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1346 TestlyxstringInvariant(this);
1348 return replace(i - begin(), i2 - i, p);
1352 lyxstring & lyxstring::replace(iterator i, iterator i2,
1353 size_type n , value_type c)
1355 TestlyxstringInvariant(this);
1357 return replace(i - begin(), i2 - i, n, c);
1361 lyxstring & lyxstring::replace(iterator i, iterator i2,
1362 iterator j, iterator j2)
1364 TestlyxstringInvariant(this);
1366 return replace(i - begin(), i2 - i, j, j2 - j);
1370 void lyxstring::swap(lyxstring & str)
1372 if (rep == str.rep) return;
1373 Srep * tmp = str.rep;
1379 lyxstring & lyxstring::erase(size_type i, size_type n)
1381 Assert(i <= rep->sz); // STD!
1382 TestlyxstringInvariant(this);
1384 rep = rep->get_own_copy();
1385 if (i == 0 && n >= rep->sz) {
1388 n = min(n, rep->sz - i);
1389 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1396 lyxstring::iterator lyxstring::erase(iterator i)
1398 TestlyxstringInvariant(this);
1400 // what iterator is this supposed to return?
1401 // the iterator after the one erased
1402 erase(i - begin(), 1);
1403 return begin(); // BUG
1407 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1409 TestlyxstringInvariant(this);
1411 erase(first - begin(), last - first);
1412 return begin(); // BUG
1416 /////////////////////////////////////
1417 // Conversion to C-style Strings
1418 /////////////////////////////////////
1420 lyxstring::value_type const * lyxstring::c_str() const
1422 rep->s[length()] = '\0';
1427 lyxstring::value_type const * lyxstring::data() const
1433 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1434 size_type pos) const
1436 Assert(buf); // OURS!
1437 Assert(pos <= rep->sz); // STD!
1438 TestlyxstringInvariant(this);
1440 register int nn = min(len, length() - pos);
1441 memcpy(buf, &(rep->s[pos]), nn);
1446 ////////////////////
1448 ////////////////////
1450 // Compare funcs should be verified.
1452 int lyxstring::internal_compare(size_type pos, size_type n,
1453 value_type const * s,
1454 size_type slen, size_type n2) const
1456 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1458 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1460 // remember that n can very well be a lot larger than rep->sz
1461 // so we have to ensure that n is no larger than rep->sz
1462 n = min(n, rep->sz);
1465 return memcmp(&(rep->s[pos]), s, n);
1466 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1475 int lyxstring::compare(lyxstring const & str) const
1477 TestlyxstringInvariant(this);
1478 return internal_compare(0, rep->sz, str.rep->s,
1479 str.rep->sz, str.rep->sz);
1483 int lyxstring::compare(value_type const * s) const
1486 TestlyxstringInvariant(this);
1487 int n = (!s) ? 0 : strlen(s);
1488 return internal_compare(0, rep->sz, s, n, n);
1492 int lyxstring::compare(size_type pos, size_type n,
1493 lyxstring const & str) const
1495 Assert(pos <= rep->sz); // OURS!
1496 TestlyxstringInvariant(this);
1497 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1501 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1502 size_type pos2, size_type n2) const
1504 Assert(pos <= rep->sz); // OURS!
1505 Assert(pos2 <= str.rep->sz); // OURS!
1506 TestlyxstringInvariant(this);
1507 return internal_compare(pos, n,
1509 str.rep->sz - pos2, n2);
1513 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1516 Assert(s && pos <= rep->sz); // OURS!
1517 TestlyxstringInvariant(this);
1518 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1526 // i = index, n = length
1527 lyxstring lyxstring::substr(size_type i, size_type n) const
1529 Assert(i <= rep->sz); // STD!
1530 TestlyxstringInvariant(this);
1532 return lyxstring(*this, i, n);
1536 /////////////////////////////////////////////
1537 // String operators, non member functions
1538 /////////////////////////////////////////////
1540 bool operator==(lyxstring const & a, lyxstring const & b)
1542 return a.compare(b) == 0;
1546 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1549 return b.compare(a) == 0;
1553 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1556 return a.compare(b) == 0;
1560 bool operator!=(lyxstring const & a, lyxstring const & b)
1562 return a.compare(b) != 0;
1566 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1569 return b.compare(a) != 0;
1573 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1576 return a.compare(b) != 0;
1580 bool operator>(lyxstring const & a, lyxstring const & b)
1582 return a.compare(b) > 0;
1586 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1589 return b.compare(a) < 0; // since we reverse the parameters
1593 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1596 return a.compare(b) > 0;
1600 bool operator<(lyxstring const & a, lyxstring const & b)
1602 return a.compare(b) < 0;
1606 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1609 return b.compare(a) > 0; // since we reverse the parameters
1613 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1616 return a.compare(b) < 0;
1620 bool operator>=(lyxstring const & a, lyxstring const & b)
1622 return a.compare(b) >= 0;
1626 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1629 return b.compare(a) <= 0; // since we reverse the parameters
1633 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1636 return a.compare(b) >= 0;
1640 bool operator<=(lyxstring const & a, lyxstring const & b)
1642 return a.compare(b) <= 0;
1646 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1649 return b.compare(a) >= 0; // since we reverse the parameters
1653 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1656 return a.compare(b) <= 0;
1660 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1668 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1677 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1686 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1695 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1703 void swap(lyxstring & str1, lyxstring & str2)
1711 istream & operator>>(istream & is, lyxstring & s)
1714 // very bad solution
1715 char * nome = new char[1024];
1717 lyxstring tmp(nome);
1719 if (!tmp.empty()) s = tmp;
1722 int w = is.widdth(0);
1726 if (isspace(c)) { is.putback(c); break; }
1728 if (--w == 1) break;
1730 if (s.empty()) is.setstate(ios::failbit);
1736 ostream & operator<<(ostream & o, lyxstring const & s)
1738 return o.write(s.data(), s.length());
1742 istream & getline(istream & is, lyxstring & s,
1743 lyxstring::value_type delim)
1745 // very bad solution