3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
15 #include "lyxstring.h"
28 // This class is supposed to be functionaly equivalent to a
29 // standard conformant string. This mean among others that we
30 // are useing the same requirements. Before you change anything
31 // in this file consult me and/or the standard to discover the
34 // Asserts with a STD! are required by the standard.
35 // Asserts with a OURS! are added by me.
36 // Some asserts could still be missing and some of the existing
37 // ones might be wrong or not needed.
39 // Reference count has been checked, empty_rep removed and
40 // introduced again in a similar guise. Where is empty_rep _really_
43 // We are missing a couple of imporant things from the standard:
44 // reverse iterators and methods taking InputIterators as paramters.
45 // Also the methods returning iterators is returning the wrong value.
47 // All the different find functions need a good look over.
48 // I have so far not tested them extensively and would be
49 // happy if others took the time to have a peek.
51 // Space allocation of string.
52 // I have tried to do this very simple without using any special tricks.
53 // Earlier we used a fixed value to enlarge the string with this would
54 // cause a lot of reallocations with large strings (especially if
55 // push_back was used) and wasting space for very small strings.
56 // I have now changed the allocation to use a doubling of reserved
57 // space until it is large enough. So far tests show a small speed
58 // increase and a noticable memory saving.
63 ///////////////////////////////////////
64 // The internal string representation
65 ///////////////////////////////////////
67 struct lyxstring::Srep {
72 /// The total amount of data reserved for this representaion
74 /// Data. At least 1 char for trailing null.
75 lyxstring::value_type * s;
78 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
80 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
82 ~Srep() { delete[] s; }
84 Srep * get_own_copy() {
85 if (ref == 1) return this;
87 return new Srep(sz, s);
91 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
93 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
95 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
97 void push_back(lyxstring::value_type c);
99 void insert(lyxstring::size_type pos,
100 const lyxstring::value_type * p,
101 lyxstring::size_type n);
103 void resize(lyxstring::size_type n, lyxstring::value_type c);
105 void reserve(lyxstring::size_type res_arg);
107 void replace(lyxstring::size_type i, lyxstring::size_type n,
108 lyxstring::value_type const * p, lyxstring::size_type n2);
111 Srep & operator=(const Srep &);
115 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
117 // can be called with p == 0 by
118 // lyxstring::assign(const value_type *, size_type)
123 s = new value_type[res + 1]; // add space for terminator
125 // if sz = 0 nothing gets copied and we have an error
128 // possibly allows for large but empty string
129 sz = 0; // this line should be redundant
135 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
140 s = new value_type[res + 1]; // add space for terminator
143 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
144 // allows for large but empty string
150 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
152 // can be called with p == 0
153 // by lyxstring::assign(const value_type *, size_type)
159 s = new value_type[res + 1]; // add space for terminator
164 // if sz = 0 nothing gets copied and we have an error
168 sz = 0; // this line should be redundant
174 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
180 s = new value_type[res + 1]; // add space for terminator
184 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
185 // allows for a large empty string
191 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
193 register unsigned int const len = sz + asz;
198 value_type * tmp = new value_type[res + 1];
200 memcpy(tmp + sz, p, asz);
205 memcpy(s + sz, p, asz);
211 void lyxstring::Srep::push_back(value_type c)
213 s[sz] = c; // it is always room to put a value_type at the end
219 value_type * tmp = new value_type[res + 1];
227 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
228 lyxstring::size_type n)
233 } while (res < n + sz);
234 value_type * tmp = new value_type[res + 1];
236 memcpy(tmp + pos, p, n);
237 memcpy(tmp + pos + n, &s[pos], sz - pos);
242 memmove(s + pos + n, &s[pos], sz - pos);
243 memcpy(s + pos, p, n);
249 void lyxstring::Srep::resize(size_type n, value_type c)
251 // This resets sz to res_arg
252 res = min(n, npos - 2); // We keep no xtra when we resize
253 value_type * tmp = new value_type[res + 1];
254 memcpy(tmp, s, min(sz, res));
256 memset(tmp + sz, c, res - sz);
263 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
265 // This keeps the old sz, but
266 // increases res with res_arg
268 value_type * tmp = new value_type[res + 1];
275 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
276 value_type const * p, size_type n2)
278 // can be called with p= 0 and n2= 0
281 if (res >= n2 + sz) {
282 memmove(s + i + n2, &s[i + n], sz - i);
283 memcpy(s + i, p, n2);
288 } while (res < n2 + sz);
289 value_type * tmp = new value_type[res + 1];
291 memcpy(tmp + i, p, n2);
292 memcpy(tmp + i + n2, &s[i + n], sz - i);
300 ///////////////////////////////////////
301 // The lyxstring Invariant tester
302 ///////////////////////////////////////
304 // There are no know bugs in lyxstring now, and it have been
305 // tested for a long time. so we disable the invariant checker. (Lgb)
306 #undef ENABLE_ASSERTIONS
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.
376 Assert(object->rep->s); // s is never 0
377 Assert(object->rep->res); // res cannot be 0
378 Assert(object->rep->sz <= object->rep->res);
379 Assert(object->rep->ref >= 1); // its in use so it must be referenced
380 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
381 // if it does ever == then we should be generating a new copy
382 // and starting again. (Is char always 8-bits?)
384 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
386 #define TestlyxstringInvariant(s)
387 #endif /* ENABLE_ASSERTIONS */
390 ///////////////////////////////////////
391 // Constructors and Deconstructors.
392 ///////////////////////////////////////
394 lyxstring::size_type const lyxstring::npos =
395 static_cast<lyxstring::size_type>(-1);
398 lyxstring::lyxstring()
400 static Srep empty_rep(0, "");
406 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
408 lyx::Assert(pos <= x.rep->sz); // STD!
409 if (pos == 0 && n >= x.length()) { // this is the default
413 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
418 lyxstring::lyxstring(value_type const * s, size_type n)
420 lyx::Assert(s && n < npos); // STD!
421 static Srep empty_rep(0, "");
423 rep = new Srep(n, s);
431 lyxstring::lyxstring(value_type const * s)
433 lyx::Assert(s); // STD!
434 static Srep empty_rep(0, "");
435 if (*s) { // s is not empty string
436 rep = new Srep(strlen(s), s);
444 lyxstring::lyxstring(size_type n, value_type c)
446 lyx::Assert(n < npos); // STD!
447 rep = new Srep(n, c);
451 lyxstring::lyxstring(const_iterator first, const_iterator last)
453 rep = new Srep(last - first, first);
457 lyxstring::~lyxstring()
459 if (--rep->ref == 0) delete rep;
462 ///////////////////////
464 ///////////////////////
466 lyxstring::iterator lyxstring::begin()
468 rep = rep->get_own_copy();
473 lyxstring::const_iterator lyxstring::begin() const
479 lyxstring::iterator lyxstring::end()
481 rep = rep->get_own_copy();
482 return rep->s + rep->sz;
486 lyxstring::const_iterator lyxstring::end() const
488 return rep->s + rep->sz;
492 reverse_iterator lyxstring::rbegin()
494 return reverse_iterator( end() );
498 const_reverse_iterator lyxstring::rbegin() const
500 return const_reverse_iterator( end() );
504 reverse_iterator lyxstring::rend()
506 return reverse_iterator( begin() );
510 const_reverse_iterator lyxstring::rend() const
512 return const_reverse_iterator( begin() );
517 ///////////////////////
519 ///////////////////////
521 lyxstring::size_type lyxstring::size() const
527 void lyxstring::resize(size_type n, value_type c)
529 lyx::Assert(n <= npos); // STD!
530 TestlyxstringInvariant(this);
532 // This resets sz to res_arg
533 rep = rep->get_own_copy();
538 lyxstring::size_type lyxstring::capacity() const
544 void lyxstring::reserve(size_type res_arg)
546 TestlyxstringInvariant(this);
548 rep = rep->get_own_copy();
549 rep->reserve(res_arg);
557 lyxstring & lyxstring::operator=(lyxstring const & x)
559 TestlyxstringInvariant(this);
565 lyxstring & lyxstring::operator=(value_type const * s)
567 lyx::Assert(s); // OURS!
568 TestlyxstringInvariant(this);
569 // printf("lyxstring::operator= (value_type const *)\n");
575 lyxstring & lyxstring::operator=(value_type c)
577 TestlyxstringInvariant(this);
581 if (rep->ref == 1) // recycle rep
585 rep = new Srep(1, s);
591 lyxstring & lyxstring::assign(lyxstring const & x)
593 TestlyxstringInvariant(this);
595 x.rep->ref++; // protect against ``st = st''
596 if (--rep->ref == 0) delete rep;
597 rep = x.rep; // share representation
602 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
604 lyx::Assert(pos <= x.rep->sz); // STD!
605 TestlyxstringInvariant(this);
607 return assign(x.substr(pos, n));
611 lyxstring & lyxstring::assign(value_type const * s, size_type n)
613 lyx::Assert(s && n < npos); // STD!
614 TestlyxstringInvariant(this);
616 if (rep->ref == 1) // recycle rep
620 rep = new Srep(n, s);
626 lyxstring & lyxstring::assign(value_type const * s)
628 lyx::Assert(s); // OURS!
629 TestlyxstringInvariant(this);
631 return assign(s, strlen(s));
635 lyxstring & lyxstring::assign(size_type n, value_type ch)
637 TestlyxstringInvariant(this);
639 rep = rep->get_own_copy();
645 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
647 TestlyxstringInvariant(this);
649 rep = rep->get_own_copy();
650 rep->assign(last - first, first);
659 lyxstring::const_reference lyxstring::operator[](size_type pos) const
662 // This is actually what the standard requires,
663 lyx::Assert(pos <= rep->sz); // OURS!
664 static char const helper = '\0';
665 return pos == rep->sz ? helper : rep->s[pos];
667 // but we use this one since it is stricter
668 // and more according to the real intent of std::string.
669 lyx::Assert(pos < rep->sz); // OURS!
675 lyxstring::reference lyxstring::operator[](size_type pos)
677 lyx::Assert(pos < rep->sz); // OURS!
678 TestlyxstringInvariant(this);
680 rep = rep->get_own_copy();
685 lyxstring::const_reference lyxstring::at(size_type n) const
687 lyx::Assert(n < rep->sz); // STD!
692 lyxstring::reference lyxstring::at(size_type n)
694 lyx::Assert(n < rep->sz); // STD!
695 TestlyxstringInvariant(this);
697 rep = rep->get_own_copy();
706 lyxstring & lyxstring::operator+=(lyxstring const & x)
708 TestlyxstringInvariant(this);
714 lyxstring & lyxstring::operator+=(value_type const * x)
716 lyx::Assert(x); // OURS!
717 TestlyxstringInvariant(this);
723 lyxstring & lyxstring::operator+=(value_type c)
725 TestlyxstringInvariant(this);
732 void lyxstring::push_back(value_type c)
734 TestlyxstringInvariant(this);
736 rep = rep->get_own_copy();
741 lyxstring & lyxstring::append(lyxstring const & x)
743 TestlyxstringInvariant(this);
745 if (x.empty()) return *this;
746 rep = rep->get_own_copy();
747 rep->append(x.length(), x.rep->s);
752 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
754 lyx::Assert(pos <= x.rep->sz); // STD!
755 TestlyxstringInvariant(this);
757 return append(x.substr(pos, n));
761 lyxstring & lyxstring::append(value_type const * p, size_type n)
763 lyx::Assert(p); // OURS!
764 TestlyxstringInvariant(this);
766 if (!*p || !n) return *this;
767 rep = rep->get_own_copy();
773 lyxstring & lyxstring::append(value_type const * p)
775 lyx::Assert(p); // OURS!
776 return append(p, strlen(p));
780 lyxstring & lyxstring::append(size_type n, value_type c)
782 TestlyxstringInvariant(this);
784 value_type * tmp = new value_type[n];
786 rep = rep->get_own_copy();
793 lyxstring & lyxstring::append(iterator first, iterator last)
795 TestlyxstringInvariant(this);
797 rep = rep->get_own_copy();
798 rep->append(last - first, first);
802 // insert characters before (*this)[pos]
804 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
806 TestlyxstringInvariant(this);
808 return insert(pos, x, 0, x.rep->sz);
812 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
813 size_type pos2, size_type n)
815 lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
816 TestlyxstringInvariant(this);
818 rep = rep->get_own_copy();
819 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
824 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
826 lyx::Assert(p); // OURS!
827 TestlyxstringInvariant(this);
830 // insert nothing and you change nothing
831 rep = rep->get_own_copy();
832 rep->insert(pos, p, n);
838 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
840 lyx::Assert(p); // OURS!
841 return insert(pos, p, strlen(p));
845 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
847 TestlyxstringInvariant(this);
849 rep = rep->get_own_copy();
850 value_type * tmp = new value_type[n];
852 rep->insert(pos, tmp, n);
858 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
860 TestlyxstringInvariant(this);
862 // what iterator is this supposed to return??
863 size_type tmp = p - begin();
864 insert(p - begin(), 1, c);
865 return begin() + tmp + 1; // ??
869 void lyxstring::insert(iterator p, size_type n , value_type c)
871 TestlyxstringInvariant(this);
873 insert(p - begin(), n , c);
877 void lyxstring::insert(iterator p, iterator first, iterator last)
879 TestlyxstringInvariant(this);
881 insert(p - begin(), first, last - first);
889 // All the below find functions should be verified,
890 // it is very likely that I have mixed up or interpreted
891 // some of the parameters wrong, also some of the funcs can surely
892 // be written more effectively.
894 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
896 if (!rep->sz || i >= rep->sz) return npos;
898 TestlyxstringInvariant(this);
900 size_type n = a.length();
902 for (size_type t = i; rep->sz - t >= n; ++t) {
903 // search until (*this)[i] == a[0]
904 if (rep->s[t] == a[0]) {
905 // check if the rest of the value_types match
907 for (size_type j = 1; j < n; ++j) {
908 if (rep->s[t + j] != a[j]) {
920 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
923 lyx::Assert(ptr); // OURS!
924 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
926 TestlyxstringInvariant(this);
928 // What is "n" here? is it the number of value_types to use in ptr
929 // or does "i" and "n" togeter form a substring to search
930 // for ptr in? For now I will assume that "n" tells the length
932 n = min(n, strlen(ptr));
934 for (size_type t = i; rep->sz - t >= n; ++t) {
935 // search until (*this)[i] == a[0]
936 if (rep->s[t] == ptr[0]) {
937 // check if the rest of the value_types match
939 for (size_type j = 1; j < n; ++j) {
940 if (rep->s[t + j] != ptr[j]) {
952 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
954 lyx::Assert(s); // OURS!
955 if (!rep->sz || i >= rep->sz) return npos;
957 TestlyxstringInvariant(this);
959 if (!s || !*s) return npos;
960 return find(s, i, strlen(s));
964 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
966 if (!rep->sz || i >= rep->sz) return npos;
968 TestlyxstringInvariant(this);
970 for (size_type t = 0; t + i < rep->sz; ++t) {
971 if (rep->s[t + i] == c) return t + i;
977 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
979 TestlyxstringInvariant(this);
981 size_type n = a.length();
982 if (!n || rep->sz < n)
985 size_type t = min(rep->sz - n, i);
987 if (rep->s[t] == a[0]) {
988 // check if the rest of the value_types match
990 for (size_type j = 1; j < n; ++j) {
991 if (rep->s[t + j] != a[j]) {
1003 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1006 lyx::Assert(ptr); // OURS!
1007 TestlyxstringInvariant(this);
1009 n = min(n, strlen(ptr));
1010 if (!n || rep->sz < n)
1013 size_type t = min(rep->sz - n, i);
1015 if (rep->s[t] == ptr[0]) {
1016 // check if the rest of the value_types match
1018 for (size_type j = 1; j < n; ++j) {
1019 if (rep->s[t + j] != ptr[j]) {
1024 if (equal) return t;
1031 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1034 lyx::Assert(ptr); // OURS!
1036 if (!ptr || !*ptr) return npos;
1037 return rfind(ptr, i, strlen(ptr));
1041 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1043 TestlyxstringInvariant(this);
1045 size_type const sz = rep->sz;
1046 if (sz < 1) return npos;
1047 size_type ii = min(sz - 1, i);
1049 if (rep->s[ii] == c) return ii;
1055 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1058 lyx::Assert(i <= rep->sz); // OURS!
1059 TestlyxstringInvariant(this);
1061 for (size_type t = i; t < rep->sz; ++t) {
1062 if (a.find(rep->s[t]) != npos) return t;
1068 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1072 lyx::Assert(ptr && i <= rep->sz); // OURS!
1073 TestlyxstringInvariant(this);
1074 if (!n) return npos;
1076 for (size_type t = i; t < rep->sz; ++t) {
1077 if (memchr(ptr, rep->s[t], n) != 0) return t;
1083 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1086 lyx::Assert(ptr && i <= rep->sz); // OURS!
1087 TestlyxstringInvariant(this);
1089 for (size_type t = i; t < rep->sz; ++t) {
1090 if (strchr(ptr, rep->s[t]) != 0) return t;
1096 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1098 lyx::Assert(i <= rep->sz); // OURS!
1099 TestlyxstringInvariant(this);
1101 for (size_type t = i; t < rep->sz; ++t) {
1102 if (rep->s[t] == c) return t;
1108 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1111 TestlyxstringInvariant(this);
1113 size_type ii = min(rep->sz - 1, i);
1114 for (int t = ii; t >= 0; --t) {
1115 if (a.find(rep->s[t]) != npos) return t;
1121 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1125 lyx::Assert(ptr); // OURS!
1126 TestlyxstringInvariant(this);
1127 if (!n) return npos;
1129 size_type ii = min(rep->sz - 1, i);
1130 for (int t = ii; t >= 0; --t) {
1131 if (memchr(ptr, rep->s[t], n) != 0) return t;
1137 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1140 lyx::Assert(ptr); // OURS!
1141 TestlyxstringInvariant(this);
1143 size_type ii = min(rep->sz - 1, i);
1144 for (int t = ii; t >= 0; --t) {
1145 if (strchr(ptr, rep->s[t]) != 0) return t;
1151 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1153 TestlyxstringInvariant(this);
1155 if (!rep->sz) return npos;
1156 size_type ii = min(rep->sz - 1, i);
1157 for (int t = ii; t >= 0; --t) {
1158 if (rep->s[t] == c) return t;
1164 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1167 TestlyxstringInvariant(this);
1169 if (!rep->sz) return npos;
1170 lyx::Assert(i <= rep->sz);
1171 for (size_type t = i; t < rep->sz; ++t) {
1172 if (a.find(rep->s[t]) == npos) return t;
1178 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1182 lyx::Assert(ptr && i <= rep->sz); // OURS!
1183 TestlyxstringInvariant(this);
1185 if (!n) return (i < rep->sz) ? i : npos;
1186 for (size_type t = i; t < rep->sz; ++t) {
1187 if (memchr(ptr, rep->s[t], n) == 0) return t;
1193 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1196 lyx::Assert(ptr && i <= rep->sz); // OURS!
1197 TestlyxstringInvariant(this);
1199 for (size_type t = i; t < rep->sz; ++t) {
1200 if (strchr(ptr, rep->s[t]) == 0) return t;
1206 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1209 if (!rep->sz) return npos;
1210 lyx::Assert(i <= rep->sz); // OURS!
1211 TestlyxstringInvariant(this);
1213 for (size_type t = i; t < rep->sz; ++t) {
1214 if (rep->s[t] != c) return t;
1220 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1223 TestlyxstringInvariant(this);
1225 size_type ii = min(rep->sz - 1, i);
1226 for (int t = ii; t >= 0; --t) {
1227 if (a.find(rep->s[t]) == npos) return t;
1233 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1237 lyx::Assert(ptr); // OURS!
1238 TestlyxstringInvariant(this);
1240 if (!n) return npos;
1241 size_type ii = min(rep->sz - 1, i);
1243 for (int t = ii; t >= 0; --t) {
1244 if (memchr(ptr, rep->s[t], n) == 0) return t;
1250 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1253 lyx::Assert(ptr); // OURS!
1254 TestlyxstringInvariant(this);
1256 size_type ii = min(rep->sz - 1, i);
1257 for (int t = ii; t >= 0; --t) {
1258 if (strchr(ptr, rep->s[t]) == 0) return t;
1264 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1267 TestlyxstringInvariant(this);
1269 size_type ii = min(rep->sz - 1, i);
1270 for (int t = ii; t >= 0; --t) {
1271 if (rep->s[t] != c) return t;
1281 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1283 lyx::Assert(i <= rep->sz); // OURS!
1284 TestlyxstringInvariant(this);
1286 return replace(i, n, x, 0, x.rep->sz);
1290 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1291 size_type i2, size_type n2)
1293 lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1294 TestlyxstringInvariant(this);
1296 rep = rep->get_own_copy();
1297 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1302 lyxstring & lyxstring::replace(size_type i, size_type n,
1303 value_type const * p, size_type n2)
1305 lyx::Assert(p && i <= rep->sz); // OURS!
1306 TestlyxstringInvariant(this);
1308 rep = rep->get_own_copy();
1309 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1314 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1316 lyx::Assert(p && i <= rep->sz); // OURS!
1317 TestlyxstringInvariant(this);
1319 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1323 lyxstring & lyxstring::replace(size_type i, size_type n,
1324 size_type n2, value_type c)
1326 lyx::Assert(i <= rep->sz); // OURS!
1327 TestlyxstringInvariant(this);
1329 rep = rep->get_own_copy();
1330 value_type * tmp = new value_type[n2];
1332 rep->replace(i, min(n, rep->sz), tmp, n2);
1338 /// FY! FY! FY! go away !
1339 lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
1341 return replace(i, n, 1, c);
1345 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1347 TestlyxstringInvariant(this);
1349 return replace(i - begin(), i2 - i, str);
1353 lyxstring & lyxstring::replace(iterator i, iterator i2,
1354 value_type const * p, size_type n)
1356 lyx::Assert(p); // OURS!
1357 TestlyxstringInvariant(this);
1359 return replace(i - begin(), i2 - i, p, n);
1363 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1365 lyx::Assert(p); // OURS!
1366 TestlyxstringInvariant(this);
1368 return replace(i - begin(), i2 - i, p);
1372 lyxstring & lyxstring::replace(iterator i, iterator i2,
1373 size_type n , value_type c)
1375 TestlyxstringInvariant(this);
1377 return replace(i - begin(), i2 - i, n, c);
1381 lyxstring & lyxstring::replace(iterator i, iterator i2,
1382 iterator j, iterator j2)
1384 TestlyxstringInvariant(this);
1386 return replace(i - begin(), i2 - i, j, j2 - j);
1390 void lyxstring::swap(lyxstring & str)
1392 if (rep == str.rep) return;
1393 Srep * tmp = str.rep;
1399 lyxstring & lyxstring::erase(size_type i, size_type n)
1401 lyx::Assert(i <= rep->sz); // STD!
1402 TestlyxstringInvariant(this);
1404 rep = rep->get_own_copy();
1405 if (i == 0 && n >= rep->sz) {
1408 n = min(n, rep->sz - i);
1409 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1416 lyxstring::iterator lyxstring::erase(iterator i)
1418 TestlyxstringInvariant(this);
1420 // what iterator is this supposed to return?
1421 // the iterator after the one erased
1422 erase(i - begin(), 1);
1423 return begin(); // BUG
1427 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1429 TestlyxstringInvariant(this);
1431 erase(first - begin(), last - first);
1432 return begin(); // BUG
1436 /////////////////////////////////////
1437 // Conversion to C-style Strings
1438 /////////////////////////////////////
1440 lyxstring::value_type const * lyxstring::c_str() const
1442 rep->s[length()] = '\0';
1447 lyxstring::value_type const * lyxstring::data() const
1453 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1454 size_type pos) const
1456 lyx::Assert(buf); // OURS!
1457 lyx::Assert(pos <= rep->sz); // STD!
1458 TestlyxstringInvariant(this);
1460 register int nn = min(len, length() - pos);
1461 memcpy(buf, &(rep->s[pos]), nn);
1466 ////////////////////
1468 ////////////////////
1470 // Compare funcs should be verified.
1472 int lyxstring::internal_compare(size_type pos, size_type n,
1473 value_type const * s,
1474 size_type slen, size_type n2) const
1476 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1478 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1480 // remember that n can very well be a lot larger than rep->sz
1481 // so we have to ensure that n is no larger than rep->sz
1482 n = min(n, rep->sz);
1485 return memcmp(&(rep->s[pos]), s, n);
1486 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1495 int lyxstring::compare(lyxstring const & str) const
1497 TestlyxstringInvariant(this);
1498 return internal_compare(0, rep->sz, str.rep->s,
1499 str.rep->sz, str.rep->sz);
1503 int lyxstring::compare(value_type const * s) const
1505 lyx::Assert(s); //OURS!
1506 TestlyxstringInvariant(this);
1507 int n = (!s) ? 0 : strlen(s);
1508 return internal_compare(0, rep->sz, s, n, n);
1512 int lyxstring::compare(size_type pos, size_type n,
1513 lyxstring const & str) const
1515 lyx::Assert(pos <= rep->sz); // OURS!
1516 TestlyxstringInvariant(this);
1517 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1521 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1522 size_type pos2, size_type n2) const
1524 lyx::Assert(pos <= rep->sz); // OURS!
1525 lyx::Assert(pos2 <= str.rep->sz); // OURS!
1526 TestlyxstringInvariant(this);
1527 return internal_compare(pos, n,
1529 str.rep->sz - pos2, n2);
1533 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1536 lyx::Assert(s && pos <= rep->sz); // OURS!
1537 TestlyxstringInvariant(this);
1538 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1546 // i = index, n = length
1547 lyxstring lyxstring::substr(size_type i, size_type n) const
1549 lyx::Assert(i <= rep->sz); // STD!
1550 TestlyxstringInvariant(this);
1552 return lyxstring(*this, i, n);
1556 /////////////////////////////////////////////
1557 // String operators, non member functions
1558 /////////////////////////////////////////////
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)
1568 lyx::Assert(a); // OURS!
1569 return b.compare(a) == 0;
1573 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1575 lyx::Assert(b); // OURS!
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)
1588 lyx::Assert(a); // OURS!
1589 return b.compare(a) != 0;
1593 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1595 lyx::Assert(b); // OURS!
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)
1608 lyx::Assert(a); // OURS!
1609 return b.compare(a) < 0; // since we reverse the parameters
1613 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1615 lyx::Assert(b); // OURS!
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)
1628 lyx::Assert(a); // OURS!
1629 return b.compare(a) > 0; // since we reverse the parameters
1633 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1635 lyx::Assert(b); // OURS!
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)
1648 lyx::Assert(a); // OURS!
1649 return b.compare(a) <= 0; // since we reverse the parameters
1653 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1655 lyx::Assert(b); // OURS!
1656 return a.compare(b) >= 0;
1660 bool operator<=(lyxstring const & a, lyxstring const & b)
1662 return a.compare(b) <= 0;
1666 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1668 lyx::Assert(a); // OURS!
1669 return b.compare(a) >= 0; // since we reverse the parameters
1673 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1675 lyx::Assert(b); // OURS!
1676 return a.compare(b) <= 0;
1680 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1688 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1690 lyx::Assert(a); // OURS!
1697 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1706 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1708 lyx::Assert(b); // OURS!
1715 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1723 void swap(lyxstring & str1, lyxstring & str2)
1731 istream & operator>>(istream & is, lyxstring & s)
1734 // very bad solution
1735 char * nome = new char[1024];
1737 lyxstring tmp(nome);
1739 if (!tmp.empty()) s = tmp;
1742 int w = is.width(0);
1745 bool skipspace = true;
1756 if (--w == 1) break;
1758 if (s.empty()) is.setstate(std::ios::failbit);
1764 ostream & operator<<(ostream & o, lyxstring const & s)
1766 return o.write(s.data(), s.length());
1770 istream & getline(istream & is, lyxstring & s,
1771 lyxstring::value_type delim)
1773 // very bad solution
1789 lyxstring a = "abcac";
1790 cout << a.rfind("ab") << endl;
1791 cout << a.rfind("c") << endl;
1792 cout << a.rfind("d") << endl;