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"
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 ///////////////////////////////////////
303 #ifdef ENABLE_ASSERTIONS
305 /** Testing of the lyxstring invariant
306 * By creating an object that tests the lyxstring invariant during its
307 * construction *and* its deconstruction we greatly simplify our code.
308 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
309 * will test the invariant upon entry to the code. If the Asserts fail
310 * then we know from the stack trace that the corruption occurred *before*
311 * entry to this method. We can also be sure it didn't happen in any of
312 * the tested lyxstring methods. It is therefore likely to be due to some
313 * other external force.
314 * Several lyxstring methods have multiple exit points which would otherwise
315 * require us to insert a separate test before each return. But since we
316 * created an object its destructor will be called upon exit (any exit!).
317 * We thus get testing at both start and end of a method with one line of
318 * code at the head of a method. More importantly, we get good testing
319 * everytime we run the code.
320 * NOTE: just because we test the invariant doesn't mean we can forget
321 * about testing pre and post conditions specific to any given method.
322 * This test simply proves that the lyxstring/Srep is in a valid state it
323 * does *not* prove that the method did what it was supposed to.
325 class lyxstringInvariant {
327 lyxstringInvariant(lyxstring const *);
328 ~lyxstringInvariant();
331 lyxstring const * object;
335 // To test if this scheme works "as advertised" uncomment the printf's in
336 // the constructor and destructor below and then uncomment the printf and the
337 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
338 // The correct output when LyX has been recompiled and run is:
339 // lyxstringInvariant constructor
340 // lyxstring::operator=(char const *)
341 // lyxstringInvariant constructor
342 // lyxstringInvariant destructor completed
343 // lyxstringInvariant destructor completed
344 // NOTE: The easiest way to catch this snippet of the output is to wait for
345 // the splash screen to disappear and then open and close Help->Credits
347 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
349 // printf("lyxstringInvariant constructor\n");
354 lyxstringInvariant::~lyxstringInvariant()
357 // printf("lyxstringInvariant destructor completed\n");
361 void lyxstringInvariant::helper() const
363 // Some of these tests might look pointless but they are
364 // all part of the invariant and if we want to make sure
365 // we have a bullet proof implementation then we need to
366 // test every last little thing we *know* should be true.
367 // I may have missed a test or two, so feel free to fill
368 // in the gaps. ARRae.
371 Assert(object->rep->s); // s is never 0
372 Assert(object->rep->res); // res cannot be 0
373 Assert(object->rep->sz <= object->rep->res);
374 Assert(object->rep->ref >= 1); // its in use so it must be referenced
375 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
376 // if it does ever == then we should be generating a new copy
377 // and starting again. (Is char always 8-bits?)
379 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
381 #define TestlyxstringInvariant(s)
382 #endif /* ENABLE_ASSERTIONS */
385 ///////////////////////////////////////
386 // Constructors and Deconstructors.
387 ///////////////////////////////////////
389 lyxstring::size_type const lyxstring::npos =
390 static_cast<lyxstring::size_type>(-1);
393 lyxstring::lyxstring()
395 static Srep empty_rep(0, "");
401 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
403 Assert(pos <= x.rep->sz); // STD!
404 if (pos == 0 && n >= x.length()) { // this is the default
408 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
413 lyxstring::lyxstring(value_type const * s, size_type n)
415 Assert(s && n < npos); // STD!
416 static Srep empty_rep(0, "");
417 if (*s && n) { // s is not empty string and n > 0
419 while (l < n && s[l])
421 rep = new Srep(l, s);
422 // rep = new Srep(min(strlen(s),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)
613 TestlyxstringInvariant(this);
615 n = min(strlen(s), n);
616 if (rep->ref == 1) // recycle rep
620 rep = new Srep(n, s);
626 lyxstring & lyxstring::assign(value_type const * s)
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 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 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 Assert(n < rep->sz); // STD!
684 lyxstring::reference lyxstring::at(size_type n)
686 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)
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 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)
756 TestlyxstringInvariant(this);
758 if (!*p || !n) return *this;
759 rep = rep->get_own_copy();
760 rep->append(min(n, strlen(p)), p);
765 lyxstring & lyxstring::append(value_type const * p)
768 TestlyxstringInvariant(this);
770 if (!*p) return *this;
771 rep = rep->get_own_copy();
772 rep->append(strlen(p), p);
777 lyxstring & lyxstring::append(size_type n, value_type c)
779 TestlyxstringInvariant(this);
781 value_type * tmp = new value_type[n];
783 rep = rep->get_own_copy();
790 lyxstring & lyxstring::append(iterator first, iterator last)
792 TestlyxstringInvariant(this);
794 rep = rep->get_own_copy();
795 rep->append(last - first, first);
799 // insert value_typeacters before (*this)[pos]
801 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
803 TestlyxstringInvariant(this);
805 return insert(pos, x, 0, x.rep->sz);
809 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
810 size_type pos2, size_type n)
812 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
813 TestlyxstringInvariant(this);
815 rep = rep->get_own_copy();
816 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
821 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
824 TestlyxstringInvariant(this);
827 // insert nothing and you change nothing
828 rep = rep->get_own_copy();
829 rep->insert(pos, p, min(n, strlen(p)));
835 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
838 TestlyxstringInvariant(this);
841 // insert nothing and you change nothing
842 rep = rep->get_own_copy();
843 rep->insert(pos, p, strlen(p));
849 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
851 TestlyxstringInvariant(this);
853 rep = rep->get_own_copy();
854 value_type * tmp = new value_type[n];
856 rep->insert(pos, tmp, n);
862 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
864 TestlyxstringInvariant(this);
866 // what iterator is this supposed to return??
867 size_type tmp = p - begin();
868 insert(p - begin(), 1, c);
869 return begin() + tmp + 1; // ??
873 void lyxstring::insert(iterator p, size_type n , value_type c)
875 TestlyxstringInvariant(this);
877 insert(p - begin(), n , c);
881 void lyxstring::insert(iterator p, iterator first, iterator last)
883 TestlyxstringInvariant(this);
885 insert(p - begin(), first, last - first);
893 // All the below find functions should be verified,
894 // it is very likely that I have mixed up or interpreted
895 // some of the parameters wrong, also some of the funcs can surely
896 // be written more effectively.
898 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
900 if (!rep->sz || i >= rep->sz) return npos;
902 TestlyxstringInvariant(this);
904 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
905 // search until (*this)[i] == a[0]
906 if (rep->s[t] == a[0]) {
907 // check if the rest of the value_types match
909 for (size_type j = 0; j < a.length(); ++j) {
910 if (rep->s[t + j] != a[j]) {
922 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
925 Assert(ptr); // OURS!
926 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
928 TestlyxstringInvariant(this);
930 // What is "n" here? is it the number of value_types to use in ptr
931 // or does "i" and "n" togeter form a substring to search
932 // for ptr in? For now I will assume that "n" tells the length
934 n = min(n, strlen(ptr));
935 for (size_type t = i; rep->sz - t >= n; ++t) {
936 // search until (*this)[i] == a[0]
937 if (rep->s[t] == ptr[0]) {
938 // check if the rest of the value_types match
940 for (size_type j = 0; j < n; ++j) {
941 if (rep->s[t + j] != ptr[j]) {
953 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
956 if (!rep->sz || i >= rep->sz) return npos;
958 TestlyxstringInvariant(this);
960 if (!s || !*s) return npos;
961 return find(s, i, strlen(s));
965 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
967 if (!rep->sz || i >= rep->sz) return npos;
969 TestlyxstringInvariant(this);
971 for (size_type t = 0; t + i < rep->sz; ++t) {
972 if (rep->s[t + i] == c) return t + i;
978 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
980 TestlyxstringInvariant(this);
982 size_type ii = min(rep->sz - 1, i);
984 if (a[a.length() - 1] == rep->s[ii]) {
986 size_type l = ii - 1;
987 for (; t >= 0; --t, --l) {
988 if (a[t] != rep->s[l]) break;
990 if (a[t] == rep->s[l]) return l;
997 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1000 Assert(ptr); // OURS!
1001 TestlyxstringInvariant(this);
1002 if (!*ptr) return npos;
1004 size_type ii = min(rep->sz - 1, i);
1006 if (ptr[n - 1] == rep->s[ii]) {
1008 size_type l = ii - 1;
1009 for (; t >= 0; --t, --l) {
1010 if (ptr[t] != rep->s[l]) break;
1012 if (ptr[t] == rep->s[l]) return l;
1019 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1022 Assert(ptr); // OURS!
1023 TestlyxstringInvariant(this);
1024 if (!*ptr) return npos;
1026 size_type ii = min(rep->sz - 1, i);
1028 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1029 int t = strlen(ptr) - 2;
1030 size_type l = ii - 1;
1031 for (; t >= 0; --t, --l) {
1032 if (ptr[t] != rep->s[l]) break;
1034 if (ptr[t] == rep->s[l]) return l;
1041 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1043 TestlyxstringInvariant(this);
1045 size_type ii = min(rep->sz - 1, i);
1046 for (size_type t = ii; t != 0; --t) {
1047 if (rep->s[t] == c) return t;
1053 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1056 Assert(i < rep->sz); // OURS!
1057 TestlyxstringInvariant(this);
1059 for (size_type t = i; t < rep->sz; ++t) {
1060 if (a.find(rep->s[t]) != npos) return t;
1066 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1070 Assert(ptr && i < rep->sz); // OURS!
1071 TestlyxstringInvariant(this);
1072 if (!n) return npos;
1074 for (size_type t = i; t < rep->sz; ++t) {
1075 if(memchr(ptr, rep->s[t], n) != 0) return t;
1081 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1084 Assert(ptr && i < rep->sz); // OURS!
1085 TestlyxstringInvariant(this);
1087 for (size_type t = i; t < rep->sz; ++t) {
1088 if (strchr(ptr, rep->s[t]) != 0) return t;
1094 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1096 Assert(i < rep->sz); // OURS!
1097 TestlyxstringInvariant(this);
1099 for (size_type t = i; t < rep->sz; ++t) {
1100 if (rep->s[t] == c) return t;
1106 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1109 TestlyxstringInvariant(this);
1111 size_type ii = min(rep->sz - 1, i);
1112 for (int t = ii; t >= 0; --t) {
1113 if (a.find(rep->s[t]) != npos) return t;
1119 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1123 Assert(ptr); // OURS!
1124 TestlyxstringInvariant(this);
1125 if (!n) return npos;
1127 size_type ii = min(rep->sz - 1, i);
1128 for (int t = ii; t >= 0; --t) {
1129 if(memchr(ptr, rep->s[t], n) != 0) return t;
1135 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1138 Assert(ptr); // OURS!
1139 TestlyxstringInvariant(this);
1141 size_type ii = min(rep->sz - 1, i);
1142 for (int t = ii; t >= 0; --t) {
1143 if (strchr(ptr, rep->s[t]) != 0) return t;
1149 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1151 TestlyxstringInvariant(this);
1153 if (!rep->sz) return npos;
1154 size_type ii = min(rep->sz - 1, i);
1155 for (int t = ii; t >= 0; --t) {
1156 if (rep->s[t] == c) return t;
1162 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1165 TestlyxstringInvariant(this);
1167 if (!rep->sz) return npos;
1168 Assert(i < rep->sz);
1169 for (size_type t = i; t < rep->sz; ++t) {
1170 if (a.find(rep->s[t]) == npos) return t;
1176 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1180 Assert(ptr && i < rep->sz); // OURS!
1181 TestlyxstringInvariant(this);
1183 if (!n) return (i < rep->sz) ? i : npos;
1184 for (size_type t = i; t < rep->sz; ++t) {
1185 if(memchr(ptr, rep->s[t], n) == 0) return t;
1191 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1194 Assert(ptr && i < rep->sz); // OURS!
1195 TestlyxstringInvariant(this);
1197 for (size_type t = i; t < rep->sz; ++t) {
1198 if (strchr(ptr, rep->s[t]) == 0) return t;
1204 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1207 if (!rep->sz) return npos;
1208 Assert(i < rep->sz); // OURS!
1209 TestlyxstringInvariant(this);
1211 for (size_type t = i; t < rep->sz; ++t) {
1212 if (rep->s[t] != c) return t;
1218 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1221 TestlyxstringInvariant(this);
1223 size_type ii = min(rep->sz - 1, i);
1224 for (int t = ii; t >= 0; --t) {
1225 if (a.find(rep->s[t]) == npos) return t;
1231 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1235 Assert(ptr); // OURS!
1236 TestlyxstringInvariant(this);
1238 if (!n) return npos;
1239 size_type ii = min(rep->sz - 1, i);
1241 for (int t = ii; t >= 0; --t) {
1242 if(memchr(ptr, rep->s[t], n) == 0) return t;
1248 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1251 Assert(ptr); // OURS!
1252 TestlyxstringInvariant(this);
1254 size_type ii = min(rep->sz - 1, i);
1255 for (int t = ii; t >= 0; --t) {
1256 if (strchr(ptr, rep->s[t]) == 0) return t;
1262 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1265 TestlyxstringInvariant(this);
1267 size_type ii = min(rep->sz - 1, i);
1268 for (int t = ii; t >= 0; --t) {
1269 if (rep->s[t] != c) return t;
1279 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1281 Assert(i <= rep->sz); // OURS!
1282 TestlyxstringInvariant(this);
1284 return replace(i, n, x, 0, x.rep->sz);
1288 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1289 size_type i2, size_type n2)
1291 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1292 TestlyxstringInvariant(this);
1294 rep = rep->get_own_copy();
1295 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1300 lyxstring & lyxstring::replace(size_type i, size_type n,
1301 value_type const * p, size_type n2)
1303 Assert(p && i < rep->sz); // OURS!
1304 TestlyxstringInvariant(this);
1306 rep = rep->get_own_copy();
1307 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1312 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1314 Assert(p && i < rep->sz); // OURS!
1315 TestlyxstringInvariant(this);
1317 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1321 lyxstring & lyxstring::replace(size_type i, size_type n,
1322 size_type n2, value_type c)
1324 Assert(i <= rep->sz); // OURS!
1325 TestlyxstringInvariant(this);
1327 rep = rep->get_own_copy();
1328 value_type * tmp = new value_type[n2];
1330 rep->replace(i, min(n, rep->sz), tmp, n2);
1336 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1338 TestlyxstringInvariant(this);
1340 return replace(i - begin(), i2 - i, str);
1344 lyxstring & lyxstring::replace(iterator i, iterator i2,
1345 value_type const * p, size_type n)
1348 TestlyxstringInvariant(this);
1350 return replace(i - begin(), i2 - i, p, n);
1354 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1357 TestlyxstringInvariant(this);
1359 return replace(i - begin(), i2 - i, p);
1363 lyxstring & lyxstring::replace(iterator i, iterator i2,
1364 size_type n , value_type c)
1366 TestlyxstringInvariant(this);
1368 return replace(i - begin(), i2 - i, n, c);
1372 lyxstring & lyxstring::replace(iterator i, iterator i2,
1373 iterator j, iterator j2)
1375 TestlyxstringInvariant(this);
1377 return replace(i - begin(), i2 - i, j, j2 - j);
1381 void lyxstring::swap(lyxstring & str)
1383 if (rep == str.rep) return;
1384 Srep * tmp = str.rep;
1390 lyxstring & lyxstring::erase(size_type i, size_type n)
1392 Assert(i <= rep->sz); // STD!
1393 TestlyxstringInvariant(this);
1395 rep = rep->get_own_copy();
1396 if (i == 0 && n >= rep->sz) {
1399 n = min(n, rep->sz - i);
1400 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1407 lyxstring::iterator lyxstring::erase(iterator i)
1409 TestlyxstringInvariant(this);
1411 // what iterator is this supposed to return?
1412 // the iterator after the one erased
1413 erase(i - begin(), 1);
1414 return begin(); // BUG
1418 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1420 TestlyxstringInvariant(this);
1422 erase(first - begin(), last - first);
1423 return begin(); // BUG
1427 /////////////////////////////////////
1428 // Conversion to C-style Strings
1429 /////////////////////////////////////
1431 lyxstring::value_type const * lyxstring::c_str() const
1433 rep->s[length()] = '\0';
1438 lyxstring::value_type const * lyxstring::data() const
1444 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1445 size_type pos) const
1447 Assert(buf); // OURS!
1448 Assert(pos <= rep->sz); // STD!
1449 TestlyxstringInvariant(this);
1451 register int nn = min(len, length() - pos);
1452 memcpy(buf, &(rep->s[pos]), nn);
1457 ////////////////////
1459 ////////////////////
1461 // Compare funcs should be verified.
1463 int lyxstring::internal_compare(size_type pos, size_type n,
1464 value_type const * s,
1465 size_type slen, size_type n2) const
1467 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1469 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1471 // remember that n can very well be a lot larger than rep->sz
1472 // so we have to ensure that n is no larger than rep->sz
1473 n = min(n, rep->sz);
1476 return memcmp(&(rep->s[pos]), s, n);
1477 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1486 int lyxstring::compare(lyxstring const & str) const
1488 TestlyxstringInvariant(this);
1489 return internal_compare(0, rep->sz, str.rep->s,
1490 str.rep->sz, str.rep->sz);
1494 int lyxstring::compare(value_type const * s) const
1497 TestlyxstringInvariant(this);
1498 int n = (!s) ? 0 : strlen(s);
1499 return internal_compare(0, rep->sz, s, n, n);
1503 int lyxstring::compare(size_type pos, size_type n,
1504 lyxstring const & str) const
1506 Assert(pos <= rep->sz); // OURS!
1507 TestlyxstringInvariant(this);
1508 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1512 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1513 size_type pos2, size_type n2) const
1515 Assert(pos <= rep->sz); // OURS!
1516 Assert(pos2 <= str.rep->sz); // OURS!
1517 TestlyxstringInvariant(this);
1518 return internal_compare(pos, n,
1520 str.rep->sz - pos2, n2);
1524 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1527 Assert(s && pos <= rep->sz); // OURS!
1528 TestlyxstringInvariant(this);
1529 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1537 // i = index, n = length
1538 lyxstring lyxstring::substr(size_type i, size_type n) const
1540 Assert(i <= rep->sz); // STD!
1541 TestlyxstringInvariant(this);
1543 return lyxstring(*this, i, n);
1547 /////////////////////////////////////////////
1548 // String operators, non member functions
1549 /////////////////////////////////////////////
1551 bool operator==(lyxstring const & a, lyxstring const & b)
1553 return a.compare(b) == 0;
1557 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1560 return b.compare(a) == 0;
1564 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1567 return a.compare(b) == 0;
1571 bool operator!=(lyxstring const & a, lyxstring const & b)
1573 return a.compare(b) != 0;
1577 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1580 return b.compare(a) != 0;
1584 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1587 return a.compare(b) != 0;
1591 bool operator>(lyxstring const & a, lyxstring const & b)
1593 return a.compare(b) > 0;
1597 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1600 return b.compare(a) < 0; // since we reverse the parameters
1604 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1607 return a.compare(b) > 0;
1611 bool operator<(lyxstring const & a, lyxstring const & b)
1613 return a.compare(b) < 0;
1617 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1620 return b.compare(a) > 0; // since we reverse the parameters
1624 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1627 return a.compare(b) < 0;
1631 bool operator>=(lyxstring const & a, lyxstring const & b)
1633 return a.compare(b) >= 0;
1637 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1640 return b.compare(a) <= 0; // since we reverse the parameters
1644 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1647 return a.compare(b) >= 0;
1651 bool operator<=(lyxstring const & a, lyxstring const & b)
1653 return a.compare(b) <= 0;
1657 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1660 return b.compare(a) >= 0; // since we reverse the parameters
1664 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1667 return a.compare(b) <= 0;
1671 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1679 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1688 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1697 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1706 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1714 void swap(lyxstring & str1, lyxstring & str2)
1722 istream & operator>>(istream & is, lyxstring & s)
1725 // very bad solution
1726 char * nome = new char[1024];
1728 lyxstring tmp(nome);
1730 if (!tmp.empty()) s = tmp;
1733 int w = is.widdth(0);
1737 if (isspace(c)) { is.putback(c); break; }
1739 if (--w == 1) break;
1741 if (s.empty()) is.setstate(ios::failbit);
1747 ostream & operator<<(ostream & o, lyxstring const & s)
1749 return o.write(s.data(), s.length());
1753 istream & getline(istream & is, lyxstring & s,
1754 lyxstring::value_type delim)
1756 // very bad solution