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, "");
418 rep = new Srep(n, s);
426 lyxstring::lyxstring(value_type const * s)
429 static Srep empty_rep(0, "");
430 if (*s) { // s is not empty string
431 rep = new Srep(strlen(s), s);
439 lyxstring::lyxstring(size_type n, value_type c)
441 Assert(n < npos); // STD!
442 rep = new Srep(n, c);
446 lyxstring::lyxstring(const_iterator first, const_iterator last)
448 rep = new Srep(last - first, first);
452 lyxstring::~lyxstring()
454 if (--rep->ref == 0) delete rep;
457 ///////////////////////
459 ///////////////////////
461 lyxstring::iterator lyxstring::begin()
463 rep = rep->get_own_copy();
468 lyxstring::const_iterator lyxstring::begin() const
474 lyxstring::iterator lyxstring::end()
476 rep = rep->get_own_copy();
477 return rep->s + rep->sz;
481 lyxstring::const_iterator lyxstring::end() const
483 return rep->s + rep->sz;
487 reverse_iterator lyxstring::rbegin()
489 return reverse_iterator( end() );
493 const_reverse_iterator lyxstring::rbegin() const
495 return const_reverse_iterator( end() );
499 reverse_iterator lyxstring::rend()
501 return reverse_iterator( begin() );
505 const_reverse_iterator lyxstring::rend() const
507 return const_reverse_iterator( begin() );
512 ///////////////////////
514 ///////////////////////
516 lyxstring::size_type lyxstring::size() const
522 void lyxstring::resize(size_type n, value_type c)
524 Assert(n <= npos); // STD!
525 TestlyxstringInvariant(this);
527 // This resets sz to res_arg
528 rep = rep->get_own_copy();
533 lyxstring::size_type lyxstring::capacity() const
539 void lyxstring::reserve(size_type res_arg)
541 TestlyxstringInvariant(this);
543 rep = rep->get_own_copy();
544 rep->reserve(res_arg);
552 lyxstring & lyxstring::operator=(lyxstring const & x)
554 TestlyxstringInvariant(this);
560 lyxstring & lyxstring::operator=(value_type const * s)
563 TestlyxstringInvariant(this);
564 // printf("lyxstring::operator= (value_type const *)\n");
570 lyxstring & lyxstring::operator=(value_type c)
572 TestlyxstringInvariant(this);
576 if (rep->ref == 1) // recycle rep
580 rep = new Srep(1, s);
586 lyxstring & lyxstring::assign(lyxstring const & x)
588 TestlyxstringInvariant(this);
590 x.rep->ref++; // protect against ``st = st''
591 if (--rep->ref == 0) delete rep;
592 rep = x.rep; // share representation
597 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
599 Assert(pos <= x.rep->sz); // STD!
600 TestlyxstringInvariant(this);
602 return assign(x.substr(pos, n));
606 lyxstring & lyxstring::assign(value_type const * s, size_type n)
608 Assert(s && n < npos); // STD!
609 TestlyxstringInvariant(this);
611 if (rep->ref == 1) // recycle rep
615 rep = new Srep(n, s);
621 lyxstring & lyxstring::assign(value_type const * s)
624 TestlyxstringInvariant(this);
626 return assign(s, strlen(s));
630 lyxstring & lyxstring::assign(size_type n, value_type ch)
632 TestlyxstringInvariant(this);
634 rep = rep->get_own_copy();
640 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
642 TestlyxstringInvariant(this);
644 rep = rep->get_own_copy();
645 rep->assign(last - first, first);
654 lyxstring::const_reference lyxstring::operator[](size_type pos) const
656 Assert(pos <= rep->sz); // OURS!
657 static char helper = '\0';
658 return pos == rep->sz ? helper : rep->s[pos];
662 lyxstring::reference lyxstring::operator[](size_type pos)
664 Assert(pos < rep->sz); // OURS!
665 TestlyxstringInvariant(this);
667 rep = rep->get_own_copy();
672 lyxstring::const_reference lyxstring::at(size_type n) const
674 Assert(n < rep->sz); // STD!
679 lyxstring::reference lyxstring::at(size_type n)
681 Assert(n < rep->sz); // STD!
682 TestlyxstringInvariant(this);
684 rep = rep->get_own_copy();
693 lyxstring & lyxstring::operator+=(lyxstring const & x)
695 TestlyxstringInvariant(this);
701 lyxstring & lyxstring::operator+=(value_type const * x)
704 TestlyxstringInvariant(this);
710 lyxstring & lyxstring::operator+=(value_type c)
712 TestlyxstringInvariant(this);
719 void lyxstring::push_back(value_type c)
721 TestlyxstringInvariant(this);
723 rep = rep->get_own_copy();
728 lyxstring & lyxstring::append(lyxstring const & x)
730 TestlyxstringInvariant(this);
732 if (x.empty()) return *this;
733 rep = rep->get_own_copy();
734 rep->append(x.length(), x.rep->s);
739 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
741 Assert(pos <= x.rep->sz); // STD!
742 TestlyxstringInvariant(this);
744 return append(x.substr(pos, n));
748 lyxstring & lyxstring::append(value_type const * p, size_type n)
751 TestlyxstringInvariant(this);
753 if (!*p || !n) return *this;
754 rep = rep->get_own_copy();
760 lyxstring & lyxstring::append(value_type const * p)
763 return append(p, strlen(p));
767 lyxstring & lyxstring::append(size_type n, value_type c)
769 TestlyxstringInvariant(this);
771 value_type * tmp = new value_type[n];
773 rep = rep->get_own_copy();
780 lyxstring & lyxstring::append(iterator first, iterator last)
782 TestlyxstringInvariant(this);
784 rep = rep->get_own_copy();
785 rep->append(last - first, first);
789 // insert characters before (*this)[pos]
791 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
793 TestlyxstringInvariant(this);
795 return insert(pos, x, 0, x.rep->sz);
799 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
800 size_type pos2, size_type n)
802 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
803 TestlyxstringInvariant(this);
805 rep = rep->get_own_copy();
806 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
811 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
814 TestlyxstringInvariant(this);
817 // insert nothing and you change nothing
818 rep = rep->get_own_copy();
819 rep->insert(pos, p, n);
825 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
828 return insert(pos, p, strlen(p));
832 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
834 TestlyxstringInvariant(this);
836 rep = rep->get_own_copy();
837 value_type * tmp = new value_type[n];
839 rep->insert(pos, tmp, n);
845 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
847 TestlyxstringInvariant(this);
849 // what iterator is this supposed to return??
850 size_type tmp = p - begin();
851 insert(p - begin(), 1, c);
852 return begin() + tmp + 1; // ??
856 void lyxstring::insert(iterator p, size_type n , value_type c)
858 TestlyxstringInvariant(this);
860 insert(p - begin(), n , c);
864 void lyxstring::insert(iterator p, iterator first, iterator last)
866 TestlyxstringInvariant(this);
868 insert(p - begin(), first, last - first);
876 // All the below find functions should be verified,
877 // it is very likely that I have mixed up or interpreted
878 // some of the parameters wrong, also some of the funcs can surely
879 // be written more effectively.
881 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
883 if (!rep->sz || i >= rep->sz) return npos;
885 TestlyxstringInvariant(this);
887 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
888 // search until (*this)[i] == a[0]
889 if (rep->s[t] == a[0]) {
890 // check if the rest of the value_types match
892 for (size_type j = 0; j < a.length(); ++j) {
893 if (rep->s[t + j] != a[j]) {
905 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
908 Assert(ptr); // OURS!
909 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
911 TestlyxstringInvariant(this);
913 // What is "n" here? is it the number of value_types to use in ptr
914 // or does "i" and "n" togeter form a substring to search
915 // for ptr in? For now I will assume that "n" tells the length
917 n = min(n, strlen(ptr));
918 for (size_type t = i; rep->sz - t >= n; ++t) {
919 // search until (*this)[i] == a[0]
920 if (rep->s[t] == ptr[0]) {
921 // check if the rest of the value_types match
923 for (size_type j = 0; j < n; ++j) {
924 if (rep->s[t + j] != ptr[j]) {
936 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
939 if (!rep->sz || i >= rep->sz) return npos;
941 TestlyxstringInvariant(this);
943 if (!s || !*s) return npos;
944 return find(s, i, strlen(s));
948 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
950 if (!rep->sz || i >= rep->sz) return npos;
952 TestlyxstringInvariant(this);
954 for (size_type t = 0; t + i < rep->sz; ++t) {
955 if (rep->s[t + i] == c) return t + i;
961 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
963 TestlyxstringInvariant(this);
965 size_type ii = min(rep->sz - 1, i);
967 if (a[a.length() - 1] == rep->s[ii]) {
969 size_type l = ii - 1;
970 for (; t >= 0; --t, --l) {
971 if (a[t] != rep->s[l]) break;
973 if (a[t] == rep->s[l]) return l;
980 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
983 Assert(ptr); // OURS!
984 TestlyxstringInvariant(this);
985 if (!*ptr) return npos;
987 size_type ii = min(rep->sz - 1, i);
989 if (ptr[n - 1] == rep->s[ii]) {
991 size_type l = ii - 1;
992 for (; t >= 0; --t, --l) {
993 if (ptr[t] != rep->s[l]) break;
995 if (ptr[t] == rep->s[l]) return l;
1002 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1005 Assert(ptr); // OURS!
1006 TestlyxstringInvariant(this);
1007 if (!*ptr) return npos;
1009 size_type ii = min(rep->sz - 1, i);
1011 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1012 int t = strlen(ptr) - 2;
1013 size_type l = ii - 1;
1014 for (; t >= 0; --t, --l) {
1015 if (ptr[t] != rep->s[l]) break;
1017 if (ptr[t] == rep->s[l]) return l;
1024 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1026 TestlyxstringInvariant(this);
1028 size_type ii = min(rep->sz - 1, i);
1029 for (size_type t = ii; t != 0; --t) {
1030 if (rep->s[t] == c) return t;
1036 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1039 Assert(i < rep->sz); // OURS!
1040 TestlyxstringInvariant(this);
1042 for (size_type t = i; t < rep->sz; ++t) {
1043 if (a.find(rep->s[t]) != npos) return t;
1049 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1053 Assert(ptr && i < rep->sz); // OURS!
1054 TestlyxstringInvariant(this);
1055 if (!n) return npos;
1057 for (size_type t = i; t < rep->sz; ++t) {
1058 if(memchr(ptr, rep->s[t], n) != 0) return t;
1064 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1067 Assert(ptr && i < rep->sz); // OURS!
1068 TestlyxstringInvariant(this);
1070 for (size_type t = i; t < rep->sz; ++t) {
1071 if (strchr(ptr, rep->s[t]) != 0) return t;
1077 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1079 Assert(i < rep->sz); // OURS!
1080 TestlyxstringInvariant(this);
1082 for (size_type t = i; t < rep->sz; ++t) {
1083 if (rep->s[t] == c) return t;
1089 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1092 TestlyxstringInvariant(this);
1094 size_type ii = min(rep->sz - 1, i);
1095 for (int t = ii; t >= 0; --t) {
1096 if (a.find(rep->s[t]) != npos) return t;
1102 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1106 Assert(ptr); // OURS!
1107 TestlyxstringInvariant(this);
1108 if (!n) return npos;
1110 size_type ii = min(rep->sz - 1, i);
1111 for (int t = ii; t >= 0; --t) {
1112 if(memchr(ptr, rep->s[t], n) != 0) return t;
1118 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1121 Assert(ptr); // OURS!
1122 TestlyxstringInvariant(this);
1124 size_type ii = min(rep->sz - 1, i);
1125 for (int t = ii; t >= 0; --t) {
1126 if (strchr(ptr, rep->s[t]) != 0) return t;
1132 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1134 TestlyxstringInvariant(this);
1136 if (!rep->sz) return npos;
1137 size_type ii = min(rep->sz - 1, i);
1138 for (int t = ii; t >= 0; --t) {
1139 if (rep->s[t] == c) return t;
1145 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1148 TestlyxstringInvariant(this);
1150 if (!rep->sz) return npos;
1151 Assert(i < rep->sz);
1152 for (size_type t = i; t < rep->sz; ++t) {
1153 if (a.find(rep->s[t]) == npos) return t;
1159 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1163 Assert(ptr && i < rep->sz); // OURS!
1164 TestlyxstringInvariant(this);
1166 if (!n) return (i < rep->sz) ? i : npos;
1167 for (size_type t = i; t < rep->sz; ++t) {
1168 if(memchr(ptr, rep->s[t], n) == 0) return t;
1174 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1177 Assert(ptr && i < rep->sz); // OURS!
1178 TestlyxstringInvariant(this);
1180 for (size_type t = i; t < rep->sz; ++t) {
1181 if (strchr(ptr, rep->s[t]) == 0) return t;
1187 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1190 if (!rep->sz) return npos;
1191 Assert(i < rep->sz); // OURS!
1192 TestlyxstringInvariant(this);
1194 for (size_type t = i; t < rep->sz; ++t) {
1195 if (rep->s[t] != c) return t;
1201 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1204 TestlyxstringInvariant(this);
1206 size_type ii = min(rep->sz - 1, i);
1207 for (int t = ii; t >= 0; --t) {
1208 if (a.find(rep->s[t]) == npos) return t;
1214 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1218 Assert(ptr); // OURS!
1219 TestlyxstringInvariant(this);
1221 if (!n) return npos;
1222 size_type ii = min(rep->sz - 1, i);
1224 for (int t = ii; t >= 0; --t) {
1225 if(memchr(ptr, rep->s[t], n) == 0) return t;
1231 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1234 Assert(ptr); // OURS!
1235 TestlyxstringInvariant(this);
1237 size_type ii = min(rep->sz - 1, i);
1238 for (int t = ii; t >= 0; --t) {
1239 if (strchr(ptr, rep->s[t]) == 0) return t;
1245 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1248 TestlyxstringInvariant(this);
1250 size_type ii = min(rep->sz - 1, i);
1251 for (int t = ii; t >= 0; --t) {
1252 if (rep->s[t] != c) return t;
1262 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1264 Assert(i <= rep->sz); // OURS!
1265 TestlyxstringInvariant(this);
1267 return replace(i, n, x, 0, x.rep->sz);
1271 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1272 size_type i2, size_type n2)
1274 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1275 TestlyxstringInvariant(this);
1277 rep = rep->get_own_copy();
1278 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1283 lyxstring & lyxstring::replace(size_type i, size_type n,
1284 value_type const * p, size_type n2)
1286 Assert(p && i < rep->sz); // OURS!
1287 TestlyxstringInvariant(this);
1289 rep = rep->get_own_copy();
1290 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1295 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1297 Assert(p && i < rep->sz); // OURS!
1298 TestlyxstringInvariant(this);
1300 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1304 lyxstring & lyxstring::replace(size_type i, size_type n,
1305 size_type n2, value_type c)
1307 Assert(i <= rep->sz); // OURS!
1308 TestlyxstringInvariant(this);
1310 rep = rep->get_own_copy();
1311 value_type * tmp = new value_type[n2];
1313 rep->replace(i, min(n, rep->sz), tmp, n2);
1319 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1321 TestlyxstringInvariant(this);
1323 return replace(i - begin(), i2 - i, str);
1327 lyxstring & lyxstring::replace(iterator i, iterator i2,
1328 value_type const * p, size_type n)
1331 TestlyxstringInvariant(this);
1333 return replace(i - begin(), i2 - i, p, n);
1337 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1340 TestlyxstringInvariant(this);
1342 return replace(i - begin(), i2 - i, p);
1346 lyxstring & lyxstring::replace(iterator i, iterator i2,
1347 size_type n , value_type c)
1349 TestlyxstringInvariant(this);
1351 return replace(i - begin(), i2 - i, n, c);
1355 lyxstring & lyxstring::replace(iterator i, iterator i2,
1356 iterator j, iterator j2)
1358 TestlyxstringInvariant(this);
1360 return replace(i - begin(), i2 - i, j, j2 - j);
1364 void lyxstring::swap(lyxstring & str)
1366 if (rep == str.rep) return;
1367 Srep * tmp = str.rep;
1373 lyxstring & lyxstring::erase(size_type i, size_type n)
1375 Assert(i <= rep->sz); // STD!
1376 TestlyxstringInvariant(this);
1378 rep = rep->get_own_copy();
1379 if (i == 0 && n >= rep->sz) {
1382 n = min(n, rep->sz - i);
1383 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1390 lyxstring::iterator lyxstring::erase(iterator i)
1392 TestlyxstringInvariant(this);
1394 // what iterator is this supposed to return?
1395 // the iterator after the one erased
1396 erase(i - begin(), 1);
1397 return begin(); // BUG
1401 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1403 TestlyxstringInvariant(this);
1405 erase(first - begin(), last - first);
1406 return begin(); // BUG
1410 /////////////////////////////////////
1411 // Conversion to C-style Strings
1412 /////////////////////////////////////
1414 lyxstring::value_type const * lyxstring::c_str() const
1416 rep->s[length()] = '\0';
1421 lyxstring::value_type const * lyxstring::data() const
1427 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1428 size_type pos) const
1430 Assert(buf); // OURS!
1431 Assert(pos <= rep->sz); // STD!
1432 TestlyxstringInvariant(this);
1434 register int nn = min(len, length() - pos);
1435 memcpy(buf, &(rep->s[pos]), nn);
1440 ////////////////////
1442 ////////////////////
1444 // Compare funcs should be verified.
1446 int lyxstring::internal_compare(size_type pos, size_type n,
1447 value_type const * s,
1448 size_type slen, size_type n2) const
1450 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1452 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1454 // remember that n can very well be a lot larger than rep->sz
1455 // so we have to ensure that n is no larger than rep->sz
1456 n = min(n, rep->sz);
1459 return memcmp(&(rep->s[pos]), s, n);
1460 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1469 int lyxstring::compare(lyxstring const & str) const
1471 TestlyxstringInvariant(this);
1472 return internal_compare(0, rep->sz, str.rep->s,
1473 str.rep->sz, str.rep->sz);
1477 int lyxstring::compare(value_type const * s) const
1480 TestlyxstringInvariant(this);
1481 int n = (!s) ? 0 : strlen(s);
1482 return internal_compare(0, rep->sz, s, n, n);
1486 int lyxstring::compare(size_type pos, size_type n,
1487 lyxstring const & str) const
1489 Assert(pos <= rep->sz); // OURS!
1490 TestlyxstringInvariant(this);
1491 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1495 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1496 size_type pos2, size_type n2) const
1498 Assert(pos <= rep->sz); // OURS!
1499 Assert(pos2 <= str.rep->sz); // OURS!
1500 TestlyxstringInvariant(this);
1501 return internal_compare(pos, n,
1503 str.rep->sz - pos2, n2);
1507 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1510 Assert(s && pos <= rep->sz); // OURS!
1511 TestlyxstringInvariant(this);
1512 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1520 // i = index, n = length
1521 lyxstring lyxstring::substr(size_type i, size_type n) const
1523 Assert(i <= rep->sz); // STD!
1524 TestlyxstringInvariant(this);
1526 return lyxstring(*this, i, n);
1530 /////////////////////////////////////////////
1531 // String operators, non member functions
1532 /////////////////////////////////////////////
1534 bool operator==(lyxstring const & a, lyxstring const & b)
1536 return a.compare(b) == 0;
1540 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1543 return b.compare(a) == 0;
1547 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1550 return a.compare(b) == 0;
1554 bool operator!=(lyxstring const & a, lyxstring const & b)
1556 return a.compare(b) != 0;
1560 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1563 return b.compare(a) != 0;
1567 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1570 return a.compare(b) != 0;
1574 bool operator>(lyxstring const & a, lyxstring const & b)
1576 return a.compare(b) > 0;
1580 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1583 return b.compare(a) < 0; // since we reverse the parameters
1587 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1590 return a.compare(b) > 0;
1594 bool operator<(lyxstring const & a, lyxstring const & b)
1596 return a.compare(b) < 0;
1600 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1603 return b.compare(a) > 0; // since we reverse the parameters
1607 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1610 return a.compare(b) < 0;
1614 bool operator>=(lyxstring const & a, lyxstring const & b)
1616 return a.compare(b) >= 0;
1620 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1623 return b.compare(a) <= 0; // since we reverse the parameters
1627 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1630 return a.compare(b) >= 0;
1634 bool operator<=(lyxstring const & a, lyxstring const & b)
1636 return a.compare(b) <= 0;
1640 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1643 return b.compare(a) >= 0; // since we reverse the parameters
1647 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1650 return a.compare(b) <= 0;
1654 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1662 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1671 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1680 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1689 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1697 void swap(lyxstring & str1, lyxstring & str2)
1705 istream & operator>>(istream & is, lyxstring & s)
1708 // very bad solution
1709 char * nome = new char[1024];
1711 lyxstring tmp(nome);
1713 if (!tmp.empty()) s = tmp;
1716 int w = is.widdth(0);
1720 if (isspace(c)) { is.putback(c); break; }
1722 if (--w == 1) break;
1724 if (s.empty()) is.setstate(ios::failbit);
1730 ostream & operator<<(ostream & o, lyxstring const & s)
1732 return o.write(s.data(), s.length());
1736 istream & getline(istream & is, lyxstring & s,
1737 lyxstring::value_type delim)
1739 // very bad solution