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.
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
661 lyx::Assert(pos <= rep->sz); // OURS!
662 static char helper = '\0';
663 return pos == rep->sz ? helper : rep->s[pos];
667 lyxstring::reference lyxstring::operator[](size_type pos)
669 lyx::Assert(pos < rep->sz); // OURS!
670 TestlyxstringInvariant(this);
672 rep = rep->get_own_copy();
677 lyxstring::const_reference lyxstring::at(size_type n) const
679 lyx::Assert(n < rep->sz); // STD!
684 lyxstring::reference lyxstring::at(size_type n)
686 lyx::Assert(n < rep->sz); // STD!
687 TestlyxstringInvariant(this);
689 rep = rep->get_own_copy();
698 lyxstring & lyxstring::operator+=(lyxstring const & x)
700 TestlyxstringInvariant(this);
706 lyxstring & lyxstring::operator+=(value_type const * x)
708 lyx::Assert(x); // OURS!
709 TestlyxstringInvariant(this);
715 lyxstring & lyxstring::operator+=(value_type c)
717 TestlyxstringInvariant(this);
724 void lyxstring::push_back(value_type c)
726 TestlyxstringInvariant(this);
728 rep = rep->get_own_copy();
733 lyxstring & lyxstring::append(lyxstring const & x)
735 TestlyxstringInvariant(this);
737 if (x.empty()) return *this;
738 rep = rep->get_own_copy();
739 rep->append(x.length(), x.rep->s);
744 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
746 lyx::Assert(pos <= x.rep->sz); // STD!
747 TestlyxstringInvariant(this);
749 return append(x.substr(pos, n));
753 lyxstring & lyxstring::append(value_type const * p, size_type n)
755 lyx::Assert(p); // OURS!
756 TestlyxstringInvariant(this);
758 if (!*p || !n) return *this;
759 rep = rep->get_own_copy();
765 lyxstring & lyxstring::append(value_type const * p)
767 lyx::Assert(p); // OURS!
768 return append(p, strlen(p));
772 lyxstring & lyxstring::append(size_type n, value_type c)
774 TestlyxstringInvariant(this);
776 value_type * tmp = new value_type[n];
778 rep = rep->get_own_copy();
785 lyxstring & lyxstring::append(iterator first, iterator last)
787 TestlyxstringInvariant(this);
789 rep = rep->get_own_copy();
790 rep->append(last - first, first);
794 // insert characters before (*this)[pos]
796 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
798 TestlyxstringInvariant(this);
800 return insert(pos, x, 0, x.rep->sz);
804 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
805 size_type pos2, size_type n)
807 lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
808 TestlyxstringInvariant(this);
810 rep = rep->get_own_copy();
811 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
816 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
818 lyx::Assert(p); // OURS!
819 TestlyxstringInvariant(this);
822 // insert nothing and you change nothing
823 rep = rep->get_own_copy();
824 rep->insert(pos, p, n);
830 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
832 lyx::Assert(p); // OURS!
833 return insert(pos, p, strlen(p));
837 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
839 TestlyxstringInvariant(this);
841 rep = rep->get_own_copy();
842 value_type * tmp = new value_type[n];
844 rep->insert(pos, tmp, n);
850 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
852 TestlyxstringInvariant(this);
854 // what iterator is this supposed to return??
855 size_type tmp = p - begin();
856 insert(p - begin(), 1, c);
857 return begin() + tmp + 1; // ??
861 void lyxstring::insert(iterator p, size_type n , value_type c)
863 TestlyxstringInvariant(this);
865 insert(p - begin(), n , c);
869 void lyxstring::insert(iterator p, iterator first, iterator last)
871 TestlyxstringInvariant(this);
873 insert(p - begin(), first, last - first);
881 // All the below find functions should be verified,
882 // it is very likely that I have mixed up or interpreted
883 // some of the parameters wrong, also some of the funcs can surely
884 // be written more effectively.
886 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
888 if (!rep->sz || i >= rep->sz) return npos;
890 TestlyxstringInvariant(this);
892 size_type n = a.length();
894 for (size_type t = i; rep->sz - t >= n; ++t) {
895 // search until (*this)[i] == a[0]
896 if (rep->s[t] == a[0]) {
897 // check if the rest of the value_types match
899 for (size_type j = 1; j < n; ++j) {
900 if (rep->s[t + j] != a[j]) {
912 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
915 lyx::Assert(ptr); // OURS!
916 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
918 TestlyxstringInvariant(this);
920 // What is "n" here? is it the number of value_types to use in ptr
921 // or does "i" and "n" togeter form a substring to search
922 // for ptr in? For now I will assume that "n" tells the length
924 n = min(n, strlen(ptr));
926 for (size_type t = i; rep->sz - t >= n; ++t) {
927 // search until (*this)[i] == a[0]
928 if (rep->s[t] == ptr[0]) {
929 // check if the rest of the value_types match
931 for (size_type j = 1; j < n; ++j) {
932 if (rep->s[t + j] != ptr[j]) {
944 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
946 lyx::Assert(s); // OURS!
947 if (!rep->sz || i >= rep->sz) return npos;
949 TestlyxstringInvariant(this);
951 if (!s || !*s) return npos;
952 return find(s, i, strlen(s));
956 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
958 if (!rep->sz || i >= rep->sz) return npos;
960 TestlyxstringInvariant(this);
962 for (size_type t = 0; t + i < rep->sz; ++t) {
963 if (rep->s[t + i] == c) return t + i;
969 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
971 TestlyxstringInvariant(this);
973 size_type n = a.length();
974 if (!n || rep->sz < n)
977 size_type t = min(rep->sz - n, i);
979 if (rep->s[t] == a[0]) {
980 // check if the rest of the value_types match
982 for (size_type j = 1; j < n; ++j) {
983 if (rep->s[t + j] != a[j]) {
995 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
998 lyx::Assert(ptr); // OURS!
999 TestlyxstringInvariant(this);
1001 n = min(n, strlen(ptr));
1002 if (!n || rep->sz < n)
1005 size_type t = min(rep->sz - n, i);
1007 if (rep->s[t] == ptr[0]) {
1008 // check if the rest of the value_types match
1010 for (size_type j = 1; j < n; ++j) {
1011 if (rep->s[t + j] != ptr[j]) {
1016 if (equal) return t;
1023 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1026 lyx::Assert(ptr); // OURS!
1028 if (!ptr || !*ptr) return npos;
1029 return rfind(ptr, i, strlen(ptr));
1033 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1035 TestlyxstringInvariant(this);
1037 size_type const sz = rep->sz;
1038 if (sz < 1) return npos;
1039 size_type ii = min(sz - 1, i);
1041 if (rep->s[ii] == c) return ii;
1047 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1050 lyx::Assert(i <= rep->sz); // OURS!
1051 TestlyxstringInvariant(this);
1053 for (size_type t = i; t < rep->sz; ++t) {
1054 if (a.find(rep->s[t]) != npos) return t;
1060 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1064 lyx::Assert(ptr && i <= rep->sz); // OURS!
1065 TestlyxstringInvariant(this);
1066 if (!n) return npos;
1068 for (size_type t = i; t < rep->sz; ++t) {
1069 if (memchr(ptr, rep->s[t], n) != 0) return t;
1075 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1078 lyx::Assert(ptr && i <= rep->sz); // OURS!
1079 TestlyxstringInvariant(this);
1081 for (size_type t = i; t < rep->sz; ++t) {
1082 if (strchr(ptr, rep->s[t]) != 0) return t;
1088 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1090 lyx::Assert(i <= rep->sz); // OURS!
1091 TestlyxstringInvariant(this);
1093 for (size_type t = i; t < rep->sz; ++t) {
1094 if (rep->s[t] == c) return t;
1100 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1103 TestlyxstringInvariant(this);
1105 size_type ii = min(rep->sz - 1, i);
1106 for (int t = ii; t >= 0; --t) {
1107 if (a.find(rep->s[t]) != npos) return t;
1113 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1117 lyx::Assert(ptr); // OURS!
1118 TestlyxstringInvariant(this);
1119 if (!n) return npos;
1121 size_type ii = min(rep->sz - 1, i);
1122 for (int t = ii; t >= 0; --t) {
1123 if (memchr(ptr, rep->s[t], n) != 0) return t;
1129 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1132 lyx::Assert(ptr); // OURS!
1133 TestlyxstringInvariant(this);
1135 size_type ii = min(rep->sz - 1, i);
1136 for (int t = ii; t >= 0; --t) {
1137 if (strchr(ptr, rep->s[t]) != 0) return t;
1143 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1145 TestlyxstringInvariant(this);
1147 if (!rep->sz) return npos;
1148 size_type ii = min(rep->sz - 1, i);
1149 for (int t = ii; t >= 0; --t) {
1150 if (rep->s[t] == c) return t;
1156 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1159 TestlyxstringInvariant(this);
1161 if (!rep->sz) return npos;
1162 lyx::Assert(i <= rep->sz);
1163 for (size_type t = i; t < rep->sz; ++t) {
1164 if (a.find(rep->s[t]) == npos) return t;
1170 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1174 lyx::Assert(ptr && i <= rep->sz); // OURS!
1175 TestlyxstringInvariant(this);
1177 if (!n) return (i < rep->sz) ? i : npos;
1178 for (size_type t = i; t < rep->sz; ++t) {
1179 if (memchr(ptr, rep->s[t], n) == 0) return t;
1185 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1188 lyx::Assert(ptr && i <= rep->sz); // OURS!
1189 TestlyxstringInvariant(this);
1191 for (size_type t = i; t < rep->sz; ++t) {
1192 if (strchr(ptr, rep->s[t]) == 0) return t;
1198 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1201 if (!rep->sz) return npos;
1202 lyx::Assert(i <= rep->sz); // OURS!
1203 TestlyxstringInvariant(this);
1205 for (size_type t = i; t < rep->sz; ++t) {
1206 if (rep->s[t] != c) return t;
1212 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1215 TestlyxstringInvariant(this);
1217 size_type ii = min(rep->sz - 1, i);
1218 for (int t = ii; t >= 0; --t) {
1219 if (a.find(rep->s[t]) == npos) return t;
1225 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1229 lyx::Assert(ptr); // OURS!
1230 TestlyxstringInvariant(this);
1232 if (!n) return npos;
1233 size_type ii = min(rep->sz - 1, i);
1235 for (int t = ii; t >= 0; --t) {
1236 if (memchr(ptr, rep->s[t], n) == 0) return t;
1242 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1245 lyx::Assert(ptr); // OURS!
1246 TestlyxstringInvariant(this);
1248 size_type ii = min(rep->sz - 1, i);
1249 for (int t = ii; t >= 0; --t) {
1250 if (strchr(ptr, rep->s[t]) == 0) return t;
1256 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1259 TestlyxstringInvariant(this);
1261 size_type ii = min(rep->sz - 1, i);
1262 for (int t = ii; t >= 0; --t) {
1263 if (rep->s[t] != c) return t;
1273 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1275 lyx::Assert(i <= rep->sz); // OURS!
1276 TestlyxstringInvariant(this);
1278 return replace(i, n, x, 0, x.rep->sz);
1282 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1283 size_type i2, size_type n2)
1285 lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1286 TestlyxstringInvariant(this);
1288 rep = rep->get_own_copy();
1289 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1294 lyxstring & lyxstring::replace(size_type i, size_type n,
1295 value_type const * p, size_type n2)
1297 lyx::Assert(p && i <= rep->sz); // OURS!
1298 TestlyxstringInvariant(this);
1300 rep = rep->get_own_copy();
1301 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1306 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1308 lyx::Assert(p && i <= rep->sz); // OURS!
1309 TestlyxstringInvariant(this);
1311 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1315 lyxstring & lyxstring::replace(size_type i, size_type n,
1316 size_type n2, value_type c)
1318 lyx::Assert(i <= rep->sz); // OURS!
1319 TestlyxstringInvariant(this);
1321 rep = rep->get_own_copy();
1322 value_type * tmp = new value_type[n2];
1324 rep->replace(i, min(n, rep->sz), tmp, n2);
1330 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1332 TestlyxstringInvariant(this);
1334 return replace(i - begin(), i2 - i, str);
1338 lyxstring & lyxstring::replace(iterator i, iterator i2,
1339 value_type const * p, size_type n)
1341 lyx::Assert(p); // OURS!
1342 TestlyxstringInvariant(this);
1344 return replace(i - begin(), i2 - i, p, n);
1348 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1350 lyx::Assert(p); // OURS!
1351 TestlyxstringInvariant(this);
1353 return replace(i - begin(), i2 - i, p);
1357 lyxstring & lyxstring::replace(iterator i, iterator i2,
1358 size_type n , value_type c)
1360 TestlyxstringInvariant(this);
1362 return replace(i - begin(), i2 - i, n, c);
1366 lyxstring & lyxstring::replace(iterator i, iterator i2,
1367 iterator j, iterator j2)
1369 TestlyxstringInvariant(this);
1371 return replace(i - begin(), i2 - i, j, j2 - j);
1375 void lyxstring::swap(lyxstring & str)
1377 if (rep == str.rep) return;
1378 Srep * tmp = str.rep;
1384 lyxstring & lyxstring::erase(size_type i, size_type n)
1386 lyx::Assert(i <= rep->sz); // STD!
1387 TestlyxstringInvariant(this);
1389 rep = rep->get_own_copy();
1390 if (i == 0 && n >= rep->sz) {
1393 n = min(n, rep->sz - i);
1394 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1401 lyxstring::iterator lyxstring::erase(iterator i)
1403 TestlyxstringInvariant(this);
1405 // what iterator is this supposed to return?
1406 // the iterator after the one erased
1407 erase(i - begin(), 1);
1408 return begin(); // BUG
1412 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1414 TestlyxstringInvariant(this);
1416 erase(first - begin(), last - first);
1417 return begin(); // BUG
1421 /////////////////////////////////////
1422 // Conversion to C-style Strings
1423 /////////////////////////////////////
1425 lyxstring::value_type const * lyxstring::c_str() const
1427 rep->s[length()] = '\0';
1432 lyxstring::value_type const * lyxstring::data() const
1438 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1439 size_type pos) const
1441 lyx::Assert(buf); // OURS!
1442 lyx::Assert(pos <= rep->sz); // STD!
1443 TestlyxstringInvariant(this);
1445 register int nn = min(len, length() - pos);
1446 memcpy(buf, &(rep->s[pos]), nn);
1451 ////////////////////
1453 ////////////////////
1455 // Compare funcs should be verified.
1457 int lyxstring::internal_compare(size_type pos, size_type n,
1458 value_type const * s,
1459 size_type slen, size_type n2) const
1461 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1463 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1465 // remember that n can very well be a lot larger than rep->sz
1466 // so we have to ensure that n is no larger than rep->sz
1467 n = min(n, rep->sz);
1470 return memcmp(&(rep->s[pos]), s, n);
1471 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1480 int lyxstring::compare(lyxstring const & str) const
1482 TestlyxstringInvariant(this);
1483 return internal_compare(0, rep->sz, str.rep->s,
1484 str.rep->sz, str.rep->sz);
1488 int lyxstring::compare(value_type const * s) const
1490 lyx::Assert(s); //OURS!
1491 TestlyxstringInvariant(this);
1492 int n = (!s) ? 0 : strlen(s);
1493 return internal_compare(0, rep->sz, s, n, n);
1497 int lyxstring::compare(size_type pos, size_type n,
1498 lyxstring const & str) const
1500 lyx::Assert(pos <= rep->sz); // OURS!
1501 TestlyxstringInvariant(this);
1502 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1506 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1507 size_type pos2, size_type n2) const
1509 lyx::Assert(pos <= rep->sz); // OURS!
1510 lyx::Assert(pos2 <= str.rep->sz); // OURS!
1511 TestlyxstringInvariant(this);
1512 return internal_compare(pos, n,
1514 str.rep->sz - pos2, n2);
1518 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1521 lyx::Assert(s && pos <= rep->sz); // OURS!
1522 TestlyxstringInvariant(this);
1523 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1531 // i = index, n = length
1532 lyxstring lyxstring::substr(size_type i, size_type n) const
1534 lyx::Assert(i <= rep->sz); // STD!
1535 TestlyxstringInvariant(this);
1537 return lyxstring(*this, i, n);
1541 /////////////////////////////////////////////
1542 // String operators, non member functions
1543 /////////////////////////////////////////////
1545 bool operator==(lyxstring const & a, lyxstring const & b)
1547 return a.compare(b) == 0;
1551 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1553 lyx::Assert(a); // OURS!
1554 return b.compare(a) == 0;
1558 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1560 lyx::Assert(b); // OURS!
1561 return a.compare(b) == 0;
1565 bool operator!=(lyxstring const & a, lyxstring const & b)
1567 return a.compare(b) != 0;
1571 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1573 lyx::Assert(a); // OURS!
1574 return b.compare(a) != 0;
1578 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1580 lyx::Assert(b); // OURS!
1581 return a.compare(b) != 0;
1585 bool operator>(lyxstring const & a, lyxstring const & b)
1587 return a.compare(b) > 0;
1591 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1593 lyx::Assert(a); // OURS!
1594 return b.compare(a) < 0; // since we reverse the parameters
1598 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1600 lyx::Assert(b); // OURS!
1601 return a.compare(b) > 0;
1605 bool operator<(lyxstring const & a, lyxstring const & b)
1607 return a.compare(b) < 0;
1611 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1613 lyx::Assert(a); // OURS!
1614 return b.compare(a) > 0; // since we reverse the parameters
1618 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1620 lyx::Assert(b); // OURS!
1621 return a.compare(b) < 0;
1625 bool operator>=(lyxstring const & a, lyxstring const & b)
1627 return a.compare(b) >= 0;
1631 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1633 lyx::Assert(a); // OURS!
1634 return b.compare(a) <= 0; // since we reverse the parameters
1638 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1640 lyx::Assert(b); // OURS!
1641 return a.compare(b) >= 0;
1645 bool operator<=(lyxstring const & a, lyxstring const & b)
1647 return a.compare(b) <= 0;
1651 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1653 lyx::Assert(a); // OURS!
1654 return b.compare(a) >= 0; // since we reverse the parameters
1658 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1660 lyx::Assert(b); // OURS!
1661 return a.compare(b) <= 0;
1665 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1673 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1675 lyx::Assert(a); // OURS!
1682 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1691 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1693 lyx::Assert(b); // OURS!
1700 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1708 void swap(lyxstring & str1, lyxstring & str2)
1716 istream & operator>>(istream & is, lyxstring & s)
1719 // very bad solution
1720 char * nome = new char[1024];
1722 lyxstring tmp(nome);
1724 if (!tmp.empty()) s = tmp;
1727 int w = is.widdth(0);
1731 if (isspace(c)) { is.putback(c); break; }
1733 if (--w == 1) break;
1735 if (s.empty()) is.setstate(ios::failbit);
1741 ostream & operator<<(ostream & o, lyxstring const & s)
1743 return o.write(s.data(), s.length());
1747 istream & getline(istream & is, lyxstring & s,
1748 lyxstring::value_type delim)
1750 // very bad solution
1766 lyxstring a = "abcac";
1767 cout << a.rfind("ab") << endl;
1768 cout << a.rfind("c") << endl;
1769 cout << a.rfind("d") << endl;