1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1999 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.
62 ///////////////////////////////////////
63 // The internal string representation
64 ///////////////////////////////////////
66 struct lyxstring::Srep {
71 /// The total amount of data reserved for this representaion
73 /// Data. At least 1 char for trailing null.
74 lyxstring::value_type * s;
77 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
79 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
81 ~Srep() { delete[] s; }
83 Srep * get_own_copy() {
84 if (ref == 1) return this;
86 return new Srep(sz, s);
90 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
92 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
94 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
96 void push_back(lyxstring::value_type c);
98 void insert(lyxstring::size_type pos,
99 const lyxstring::value_type * p,
100 lyxstring::size_type n);
102 void resize(lyxstring::size_type n, lyxstring::value_type c);
104 void reserve(lyxstring::size_type res_arg);
106 void replace(lyxstring::size_type i, lyxstring::size_type n,
107 lyxstring::value_type const * p, lyxstring::size_type n2);
110 Srep & operator=(const Srep &);
115 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
117 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
122 s = new value_type[res + 1]; // add space for terminator
124 // if sz = 0 nothing gets copied and we have an error
127 // possibly allows for large but empty string
128 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
151 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
153 // can be called with p == 0 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
175 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
181 s = new value_type[res + 1]; // add space for terminator
185 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
186 // allows for a large empty string
193 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
195 register unsigned int const len = sz + asz;
200 value_type * tmp = new value_type[res + 1];
202 memcpy(tmp + sz, p, asz);
207 memcpy(s + sz, p, asz);
214 void lyxstring::Srep::push_back(value_type c)
216 s[sz] = c; // it is always room to put a value_type at the end
222 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);
254 void lyxstring::Srep::resize(size_type n, value_type c)
256 // This resets sz to res_arg
257 res = min(n, npos - 2); // We keep no xtra when we resize
258 value_type * tmp = new value_type[res + 1];
259 memcpy(tmp, s, min(sz, res));
261 memset(tmp + sz, c, res - sz);
269 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
271 // This keeps the old sz, but
272 // increases res with res_arg
274 value_type * tmp = new value_type[res + 1];
282 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
283 value_type const * p, size_type n2)
285 // can be called with p= 0 and n2= 0
288 if (res >= n2 + sz) {
289 memmove(s + i + n2, &s[i + n], sz - i);
290 memcpy(s + i, p, n2);
295 } while (res < n2 + sz);
296 value_type * tmp = new value_type[res + 1];
298 memcpy(tmp + i, p, n2);
299 memcpy(tmp + i + n2, &s[i + n], sz - i);
307 ///////////////////////////////////////
308 // The lyxstring Invariant tester
309 ///////////////////////////////////////
312 /** Testing of the lyxstring invariant
313 * By creating an object that tests the lyxstring invariant during its
314 * construction *and* its deconstruction we greatly simplify our code.
315 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
316 * will test the invariant upon entry to the code. If the Asserts fail
317 * then we know from the stack trace that the corruption occurred *before*
318 * entry to this method. We can also be sure it didn't happen in any of
319 * the tested lyxstring methods. It is therefore likely to be due to some
320 * other external force.
321 * Several lyxstring methods have multiple exit points which would otherwise
322 * require us to insert a separate test before each return. But since we
323 * created an object its destructor will be called upon exit (any exit!).
324 * We thus get testing at both start and end of a method with one line of
325 * code at the head of a method. More importantly, we get good testing
326 * everytime we run the code.
327 * NOTE: just because we test the invariant doesn't mean we can forget
328 * about testing pre and post conditions specific to any given method.
329 * This test simply proves that the lyxstring/Srep is in a valid state it
330 * does *not* prove that the method did what it was supposed to.
332 class lyxstringInvariant {
334 lyxstringInvariant(lyxstring const *);
335 ~lyxstringInvariant();
338 lyxstring const * object;
342 // To test if this scheme works "as advertised" uncomment the printf's in
343 // the constructor and destructor below and then uncomment the printf and the
344 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
345 // The correct output when LyX has been recompiled and run is:
346 // lyxstringInvariant constructor
347 // lyxstring::operator=(char const *)
348 // lyxstringInvariant constructor
349 // lyxstringInvariant destructor completed
350 // lyxstringInvariant destructor completed
351 // NOTE: The easiest way to catch this snippet of the output is to wait for
352 // the splash screen to disappear and then open and close Help->Credits
355 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
357 // printf("lyxstringInvariant constructor\n");
363 lyxstringInvariant::~lyxstringInvariant()
366 // printf("lyxstringInvariant destructor completed\n");
371 void lyxstringInvariant::helper() const
373 // Some of these tests might look pointless but they are
374 // all part of the invariant and if we want to make sure
375 // we have a bullet proof implementation then we need to
376 // test every last little thing we *know* should be true.
377 // I may have missed a test or two, so feel free to fill
378 // in the gaps. ARRae.
381 Assert(object->rep->s); // s is never 0
382 Assert(object->rep->res); // res cannot be 0
383 Assert(object->rep->sz <= object->rep->res);
384 Assert(object->rep->ref >= 1); // its in use so it must be referenced
385 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
386 // if it does ever == then we should be generating a new copy
387 // and starting again. (Is char always 8-bits?)
389 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
391 #define TestlyxstringInvariant(s)
392 #endif //DEVEL_VERSION
395 ///////////////////////////////////////
396 // Constructors and Deconstructors.
397 ///////////////////////////////////////
399 lyxstring::size_type const lyxstring::npos =
400 static_cast<lyxstring::size_type>(-1);
404 lyxstring::lyxstring()
406 static Srep empty_rep(0, "");
413 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
415 Assert(pos <= x.rep->sz); // STD!
416 if (pos == 0 && n >= x.length()) { // this is the default
420 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
426 lyxstring::lyxstring(value_type const * s, size_type n)
428 Assert(s && n < npos); // STD!
429 static Srep empty_rep(0, "");
430 if (*s && n) { // s is not empty string and n > 0
431 rep = new Srep(min(strlen(s), n), s);
440 lyxstring::lyxstring(value_type const * s)
443 static Srep empty_rep(0, "");
444 if (*s) { // s is not empty string
445 rep = new Srep(strlen(s), s);
454 lyxstring::lyxstring(size_type n, value_type c)
456 Assert(n < npos); // STD!
457 rep = new Srep(n, c);
462 lyxstring::lyxstring(const_iterator first, const_iterator last)
464 rep = new Srep(last - first, first);
469 lyxstring::~lyxstring()
471 if (--rep->ref == 0) delete rep;
474 ///////////////////////
476 ///////////////////////
479 lyxstring::iterator lyxstring::begin()
486 lyxstring::const_iterator lyxstring::begin() const
493 lyxstring::iterator lyxstring::end()
495 return rep->s + rep->sz;
500 lyxstring::const_iterator lyxstring::end() const
502 return rep->s + rep->sz;
507 reverse_iterator lyxstring::rbegin()
509 return reverse_iterator( end() );
514 const_reverse_iterator lyxstring::rbegin() const
516 return const_reverse_iterator( end() );
521 reverse_iterator lyxstring::rend()
523 return reverse_iterator( begin() );
528 const_reverse_iterator lyxstring::rend() const
530 return const_reverse_iterator( begin() );
535 ///////////////////////
537 ///////////////////////
540 lyxstring::size_type lyxstring::size() const
547 void lyxstring::resize(size_type n, value_type c)
549 Assert(n <= npos); // STD!
550 TestlyxstringInvariant(this);
552 // This resets sz to res_arg
553 rep = rep->get_own_copy();
559 lyxstring::size_type lyxstring::capacity() const
566 void lyxstring::reserve(size_type res_arg)
568 TestlyxstringInvariant(this);
570 rep = rep->get_own_copy();
571 rep->reserve(res_arg);
580 lyxstring & lyxstring::operator= (lyxstring const & x)
582 TestlyxstringInvariant(this);
589 lyxstring & lyxstring::operator= (value_type const * s)
592 TestlyxstringInvariant(this);
593 // printf("lyxstring::operator= (value_type const *)\n");
600 lyxstring & lyxstring::operator=(value_type c)
602 TestlyxstringInvariant(this);
606 if (rep->ref == 1) // recycle rep
610 rep = new Srep(1, s);
617 lyxstring & lyxstring::assign(lyxstring const & x)
619 TestlyxstringInvariant(this);
621 x.rep->ref++; // protect against ``st = st''
622 if (--rep->ref == 0) delete rep;
623 rep = x.rep; // share representation
629 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
631 Assert(pos <= x.rep->sz); // STD!
632 TestlyxstringInvariant(this);
634 return assign(x.substr(pos, n));
639 lyxstring & lyxstring::assign(value_type const * s, size_type n)
642 TestlyxstringInvariant(this);
644 n = min(strlen(s), n);
645 if (rep->ref == 1) // recycle rep
649 rep = new Srep(n, s);
656 lyxstring & lyxstring::assign(value_type const * s)
659 TestlyxstringInvariant(this);
661 return assign(s, strlen(s));
666 lyxstring & lyxstring::assign(size_type n, value_type ch)
668 TestlyxstringInvariant(this);
670 rep = rep->get_own_copy();
677 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
679 TestlyxstringInvariant(this);
681 rep = rep->get_own_copy();
682 rep->assign(last - first, first);
692 lyxstring::const_reference lyxstring::operator[](size_type pos) const
694 Assert(pos <= rep->sz); // OURS!
695 static char helper = '\0';
696 return pos == rep->sz ? helper : rep->s[pos];
701 lyxstring::reference lyxstring::operator[](size_type pos)
703 Assert(pos < rep->sz); // OURS!
704 TestlyxstringInvariant(this);
706 rep = rep->get_own_copy();
712 lyxstring::const_reference lyxstring::at(size_type n) const
714 Assert(n < rep->sz); // STD!
720 lyxstring::reference lyxstring::at(size_type n)
722 Assert(n < rep->sz); // STD!
723 TestlyxstringInvariant(this);
725 rep = rep->get_own_copy();
735 lyxstring & lyxstring::operator+=(lyxstring const & x)
737 TestlyxstringInvariant(this);
744 lyxstring & lyxstring::operator+=(value_type const * x)
747 TestlyxstringInvariant(this);
754 lyxstring & lyxstring::operator+=(value_type c)
756 TestlyxstringInvariant(this);
764 void lyxstring::push_back(value_type c)
766 TestlyxstringInvariant(this);
768 rep = rep->get_own_copy();
774 lyxstring & lyxstring::append(lyxstring const & x)
776 TestlyxstringInvariant(this);
778 if (x.empty()) return *this;
779 rep = rep->get_own_copy();
780 rep->append(x.length(), x.rep->s);
786 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
788 Assert(pos <= x.rep->sz); // STD!
789 TestlyxstringInvariant(this);
791 return append(x.substr(pos, n));
796 lyxstring & lyxstring::append(value_type const * p, size_type n)
799 TestlyxstringInvariant(this);
801 if (!*p || !n) return *this;
802 rep = rep->get_own_copy();
803 rep->append(min(n, strlen(p)), p);
809 lyxstring & lyxstring::append(value_type const * p)
812 TestlyxstringInvariant(this);
814 if (!*p) return *this;
815 rep = rep->get_own_copy();
816 rep->append(strlen(p), p);
822 lyxstring & lyxstring::append(size_type n, value_type c)
824 TestlyxstringInvariant(this);
826 value_type * tmp = new value_type[n];
828 rep = rep->get_own_copy();
836 lyxstring & lyxstring::append(iterator first, iterator last)
838 TestlyxstringInvariant(this);
840 rep = rep->get_own_copy();
841 rep->append(last - first, first);
845 // insert value_typeacters before (*this)[pos]
848 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
850 TestlyxstringInvariant(this);
852 return insert(pos, x, 0, x.rep->sz);
857 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
858 size_type pos2, size_type n)
860 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
861 TestlyxstringInvariant(this);
863 rep = rep->get_own_copy();
864 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
870 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
873 TestlyxstringInvariant(this);
876 // insert nothing and you change nothing
877 rep = rep->get_own_copy();
878 rep->insert(pos, p, min(n, strlen(p)));
885 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
888 TestlyxstringInvariant(this);
891 // insert nothing and you change nothing
892 rep = rep->get_own_copy();
893 rep->insert(pos, p, strlen(p));
900 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
902 TestlyxstringInvariant(this);
904 rep = rep->get_own_copy();
905 value_type * tmp = new value_type[n];
907 rep->insert(pos, tmp, n);
914 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
916 TestlyxstringInvariant(this);
918 // what iterator is this supposed to return??
919 size_type tmp = p - begin();
920 insert(p - begin(), 1, c);
921 return begin() + tmp + 1; // ??
926 void lyxstring::insert(iterator p, size_type n , value_type c)
928 TestlyxstringInvariant(this);
930 insert(p - begin(), n , c);
935 void lyxstring::insert(iterator p, iterator first, iterator last)
937 TestlyxstringInvariant(this);
939 insert(p - begin(), first, last - first);
947 // All the below find functions should be verified,
948 // it is very likely that I have mixed up or interpreted
949 // some of the parameters wrong, also some of the funcs can surely
950 // be written more effectively.
953 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
955 if (!rep->sz || i >= rep->sz) return npos;
957 TestlyxstringInvariant(this);
959 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
960 // search until (*this)[i] == a[0]
961 if (rep->s[t] == a[0]) {
962 // check if the rest of the value_types match
964 for (size_type j = 0; j < a.length(); ++j) {
965 if (rep->s[t + j] != a[j]) {
978 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
981 Assert(ptr); // OURS!
982 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
984 TestlyxstringInvariant(this);
986 // What is "n" here? is it the number of value_types to use in ptr
987 // or does "i" and "n" togeter form a substring to search
988 // for ptr in? For now I will assume that "n" tells the length
990 n = min(n, strlen(ptr));
991 for (size_type t = i; rep->sz - t >= n; ++t) {
992 // search until (*this)[i] == a[0]
993 if (rep->s[t] == ptr[0]) {
994 // check if the rest of the value_types match
996 for (size_type j = 0; j < n; ++j) {
997 if (rep->s[t + j] != ptr[j]) {
1002 if (equal) return t;
1010 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
1013 if (!rep->sz || i >= rep->sz) return npos;
1015 TestlyxstringInvariant(this);
1017 if (!s || !*s) return npos;
1018 return find(s, i, strlen(s));
1023 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
1025 if (!rep->sz || i >= rep->sz) return npos;
1027 TestlyxstringInvariant(this);
1029 for (size_type t = 0; t + i < rep->sz; ++t) {
1030 if (rep->s[t + i] == c) return t + i;
1037 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
1039 TestlyxstringInvariant(this);
1041 size_type ii = min(rep->sz - 1, i);
1043 if (a[a.length() - 1] == rep->s[ii]) {
1044 int t = rep->sz - 2;
1045 size_type l = ii - 1;
1046 for (; t >= 0; --t, --l) {
1047 if (a[t] != rep->s[l]) break;
1049 if (a[t] == rep->s[l]) return l;
1057 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1060 Assert(ptr); // OURS!
1061 TestlyxstringInvariant(this);
1062 if (!*ptr) return npos;
1064 size_type ii = min(rep->sz - 1, i);
1066 if (ptr[n - 1] == rep->s[ii]) {
1068 size_type l = ii - 1;
1069 for (; t >= 0; --t, --l) {
1070 if (ptr[t] != rep->s[l]) break;
1072 if (ptr[t] == rep->s[l]) return l;
1080 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1083 Assert(ptr); // OURS!
1084 TestlyxstringInvariant(this);
1085 if (!*ptr) return npos;
1087 size_type ii = min(rep->sz - 1, i);
1089 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1090 int t = strlen(ptr) - 2;
1091 size_type l = ii - 1;
1092 for (; t >= 0; --t, --l) {
1093 if (ptr[t] != rep->s[l]) break;
1095 if (ptr[t] == rep->s[l]) return l;
1103 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1105 TestlyxstringInvariant(this);
1107 size_type ii = min(rep->sz - 1, i);
1108 for (size_type t = ii; t != 0; --t) {
1109 if (rep->s[t] == c) return t;
1116 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1119 Assert(i < rep->sz); // OURS!
1120 TestlyxstringInvariant(this);
1122 for (size_type t = i; t < rep->sz; ++t) {
1123 if (a.find(rep->s[t]) != npos) return t;
1130 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1134 Assert(ptr && i < rep->sz); // OURS!
1135 TestlyxstringInvariant(this);
1136 if (!n) return npos;
1138 for (size_type t = i; t < rep->sz; ++t) {
1139 if(memchr(ptr, rep->s[t], n) != 0) return t;
1146 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1149 Assert(ptr && i < rep->sz); // OURS!
1150 TestlyxstringInvariant(this);
1152 for (size_type t = i; t < rep->sz; ++t) {
1153 if (strchr(ptr, rep->s[t]) != 0) return t;
1160 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1162 Assert(i < rep->sz); // OURS!
1163 TestlyxstringInvariant(this);
1165 for (size_type t = i; t < rep->sz; ++t) {
1166 if (rep->s[t] == c) return t;
1173 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1176 TestlyxstringInvariant(this);
1178 size_type ii = min(rep->sz - 1, i);
1179 for (int t = ii; t >= 0; --t) {
1180 if (a.find(rep->s[t]) != npos) return t;
1187 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1191 Assert(ptr); // OURS!
1192 TestlyxstringInvariant(this);
1193 if (!n) return npos;
1195 size_type ii = min(rep->sz - 1, i);
1196 for (int t = ii; t >= 0; --t) {
1197 if(memchr(ptr, rep->s[t], n) != 0) return t;
1204 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1207 Assert(ptr); // OURS!
1208 TestlyxstringInvariant(this);
1210 size_type ii = min(rep->sz - 1, i);
1211 for (int t = ii; t >= 0; --t) {
1212 if (strchr(ptr, rep->s[t]) != 0) return t;
1219 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1221 TestlyxstringInvariant(this);
1223 if (!rep->sz) return npos;
1224 size_type ii = min(rep->sz - 1, i);
1225 for (int t = ii; t >= 0; --t) {
1226 if (rep->s[t] == c) return t;
1233 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1236 TestlyxstringInvariant(this);
1238 if (!rep->sz) return npos;
1239 Assert(i < rep->sz);
1240 for (size_type t = i; t < rep->sz; ++t) {
1241 if (a.find(rep->s[t]) == npos) return t;
1248 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1252 Assert(ptr && i < rep->sz); // OURS!
1253 TestlyxstringInvariant(this);
1255 if (!n) return (i < rep->sz) ? i : npos;
1256 for (size_type t = i; t < rep->sz; ++t) {
1257 if(memchr(ptr, rep->s[t], n) == 0) return t;
1264 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1267 Assert(ptr && i < rep->sz); // OURS!
1268 TestlyxstringInvariant(this);
1270 for (size_type t = i; t < rep->sz; ++t) {
1271 if (strchr(ptr, rep->s[t]) == 0) return t;
1278 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1281 if (!rep->sz) return npos;
1282 Assert(i < rep->sz); // OURS!
1283 TestlyxstringInvariant(this);
1285 for (size_type t = i; t < rep->sz; ++t) {
1286 if (rep->s[t] != c) return t;
1293 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1296 TestlyxstringInvariant(this);
1298 size_type ii = min(rep->sz - 1, i);
1299 for (int t = ii; t >= 0; --t) {
1300 if (a.find(rep->s[t]) == npos) return t;
1307 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1311 Assert(ptr); // OURS!
1312 TestlyxstringInvariant(this);
1314 if (!n) return npos;
1315 size_type ii = min(rep->sz - 1, i);
1317 for (int t = ii; t >= 0; --t) {
1318 if(memchr(ptr, rep->s[t], n) == 0) return t;
1325 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1328 Assert(ptr); // OURS!
1329 TestlyxstringInvariant(this);
1331 size_type ii = min(rep->sz - 1, i);
1332 for (int t = ii; t >= 0; --t) {
1333 if (strchr(ptr, rep->s[t]) == 0) return t;
1340 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1343 TestlyxstringInvariant(this);
1345 size_type ii = min(rep->sz - 1, i);
1346 for (int t = ii; t >= 0; --t) {
1347 if (rep->s[t] != c) return t;
1358 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1360 Assert(i <= rep->sz); // OURS!
1361 TestlyxstringInvariant(this);
1363 return replace(i, n, x, 0, x.rep->sz);
1368 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1369 size_type i2, size_type n2)
1371 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1372 TestlyxstringInvariant(this);
1374 rep = rep->get_own_copy();
1375 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1381 lyxstring & lyxstring::replace(size_type i, size_type n,
1382 value_type const * p, size_type n2)
1384 Assert(p && i < rep->sz); // OURS!
1385 TestlyxstringInvariant(this);
1387 rep = rep->get_own_copy();
1388 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1394 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1396 Assert(p && i < rep->sz); // OURS!
1397 TestlyxstringInvariant(this);
1399 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1404 lyxstring & lyxstring::replace(size_type i, size_type n,
1405 size_type n2, value_type c)
1407 Assert(i < rep->sz); // OURS!
1408 TestlyxstringInvariant(this);
1410 rep = rep->get_own_copy();
1411 value_type * tmp = new value_type[n2];
1413 rep->replace(i, min(n, rep->sz), tmp, n2);
1420 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1422 TestlyxstringInvariant(this);
1424 return replace(i - begin(), i2 - i, str);
1429 lyxstring & lyxstring::replace(iterator i, iterator i2,
1430 value_type const * p, size_type n)
1433 TestlyxstringInvariant(this);
1435 return replace(i - begin(), i2 - i, p, n);
1440 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1443 TestlyxstringInvariant(this);
1445 return replace(i - begin(), i2 - i, p);
1450 lyxstring & lyxstring::replace(iterator i, iterator i2,
1451 size_type n , value_type c)
1453 TestlyxstringInvariant(this);
1455 return replace(i - begin(), i2 - i, n, c);
1460 lyxstring & lyxstring::replace(iterator i, iterator i2,
1461 iterator j, iterator j2)
1463 TestlyxstringInvariant(this);
1465 return replace(i - begin(), i2 - i, j, j2 - j);
1470 void lyxstring::swap(lyxstring & str)
1472 if (rep == str.rep) return;
1473 Srep * tmp = str.rep;
1480 lyxstring & lyxstring::erase(size_type i, size_type n)
1482 Assert(i <= rep->sz); // STD!
1483 TestlyxstringInvariant(this);
1485 rep = rep->get_own_copy();
1486 if (i == 0 && n >= rep->sz) {
1489 n = min(n, rep->sz - i);
1490 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1498 lyxstring::iterator lyxstring::erase(iterator i)
1500 TestlyxstringInvariant(this);
1502 // what iterator is this supposed to return?
1503 // the iterator after the one erased
1504 erase(i - begin(), 1);
1505 return begin(); // BUG
1510 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1512 TestlyxstringInvariant(this);
1514 erase(first - begin(), last - first);
1515 return begin(); // BUG
1519 /////////////////////////////////////
1520 // Conversion to C-style Strings
1521 /////////////////////////////////////
1524 lyxstring::value_type const * lyxstring::c_str() const
1526 rep->s[length()] = '\0';
1532 lyxstring::value_type const * lyxstring::data() const
1539 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1540 size_type pos) const
1542 Assert(buf); // OURS!
1543 Assert(pos <= rep->sz); // STD!
1544 TestlyxstringInvariant(this);
1546 register int nn = min(len, length() - pos);
1547 memcpy(buf, &(rep->s[pos]), nn);
1552 ////////////////////
1554 ////////////////////
1556 // Compare funcs should be verified.
1559 int lyxstring::internal_compare(size_type pos, size_type n,
1560 value_type const * s,
1561 size_type slen, size_type n2) const
1563 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1565 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1567 // remember that n can very well be a lot larger than rep->sz
1568 // so we have to ensure that n is no larger than rep->sz
1569 n = min(n, rep->sz);
1572 return memcmp(&(rep->s[pos]), s, n);
1573 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1583 int lyxstring::compare(lyxstring const & str) const
1585 TestlyxstringInvariant(this);
1586 return internal_compare(0, rep->sz, str.rep->s,
1587 str.rep->sz, str.rep->sz);
1592 int lyxstring::compare(value_type const * s) const
1595 TestlyxstringInvariant(this);
1596 int n = (!s) ? 0 : strlen(s);
1597 return internal_compare(0, rep->sz, s, n, n);
1602 int lyxstring::compare(size_type pos, size_type n,
1603 lyxstring const & str) const
1605 Assert(pos <= rep->sz); // OURS!
1606 TestlyxstringInvariant(this);
1607 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1612 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1613 size_type pos2, size_type n2) const
1615 Assert(pos <= rep->sz); // OURS!
1616 Assert(pos2 <= str.rep->sz); // OURS!
1617 TestlyxstringInvariant(this);
1618 return internal_compare(pos, n,
1620 str.rep->sz - pos2, n2);
1625 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1628 Assert(s && pos <= rep->sz); // OURS!
1629 TestlyxstringInvariant(this);
1630 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1638 // i = index, n = length
1640 lyxstring lyxstring::substr(size_type i, size_type n) const
1642 Assert(i <= rep->sz); // STD!
1643 TestlyxstringInvariant(this);
1645 return lyxstring(*this, i, n);
1649 /////////////////////////////////////////////
1650 // String operators, non member functions
1651 /////////////////////////////////////////////
1653 bool operator==(lyxstring const & a, lyxstring const & b)
1655 return a.compare(b) == 0;
1659 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1662 return b.compare(a) == 0;
1666 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1669 return a.compare(b) == 0;
1673 bool operator!=(lyxstring const & a, lyxstring const & b)
1675 return a.compare(b) != 0;
1679 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1682 return b.compare(a) != 0;
1686 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1689 return a.compare(b) != 0;
1693 bool operator>(lyxstring const & a, lyxstring const & b)
1695 return a.compare(b) > 0;
1699 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1702 return b.compare(a) < 0; // since we reverse the parameters
1706 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1709 return a.compare(b) > 0;
1713 bool operator<(lyxstring const & a, lyxstring const & b)
1715 return a.compare(b) < 0;
1719 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1722 return b.compare(a) > 0; // since we reverse the parameters
1726 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1729 return a.compare(b) < 0;
1733 bool operator>=(lyxstring const & a, lyxstring const & b)
1735 return a.compare(b) >= 0;
1739 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1742 return b.compare(a) <= 0; // since we reverse the parameters
1746 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1749 return a.compare(b) >= 0;
1753 bool operator<=(lyxstring const & a, lyxstring const & b)
1755 return a.compare(b) <= 0;
1759 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1762 return b.compare(a) >= 0; // since we reverse the parameters
1766 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1769 return a.compare(b) <= 0;
1773 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1781 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1790 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1799 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1808 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1816 void swap(lyxstring & str1, lyxstring & str2)
1824 istream & operator>>(istream & is, lyxstring & s)
1827 // very bad solution
1828 char * nome = new char[1024];
1830 lyxstring tmp(nome);
1832 if (!tmp.empty()) s = tmp;
1835 int w = is.widdth(0);
1839 if (isspace(c)) { is.putback(c); break; }
1841 if (--w == 1) break;
1843 if (s.empty()) is.setstate(ios::failbit);
1849 ostream & operator<<(ostream & o, lyxstring const & s)
1851 return o.write(s.data(), s.length());
1855 istream & getline(istream & is, lyxstring & s,
1856 lyxstring::value_type delim)
1858 // very bad solution