1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
16 #pragma implementation "lyxstring.h"
19 #include "lyxstring.h"
32 // This class is supposed to be functionaly equivalent to a
33 // standard conformant string. This mean among others that we
34 // are useing the same requirements. Before you change anything
35 // in this file consult me and/or the standard to discover the
38 // Asserts with a STD! are required by the standard.
39 // Asserts with a OURS! are added by me.
40 // Some asserts could still be missing and some of the existing
41 // ones might be wrong or not needed.
43 // Reference count has been checked, empty_rep removed and
44 // introduced again in a similar guise. Where is empty_rep _really_
47 // We are missing a couple of imporant things from the standard:
48 // reverse iterators and methods taking InputIterators as paramters.
49 // Also the methods returning iterators is returning the wrong value.
51 // All the different find functions need a good look over.
52 // I have so far not tested them extensively and would be
53 // happy if others took the time to have a peek.
55 // Space allocation of string.
56 // I have tried to do this very simple without using any special tricks.
57 // Earlier we used a fixed value to enlarge the string with this would
58 // cause a lot of reallocations with large strings (especially if
59 // push_back was used) and wasting space for very small strings.
60 // I have now changed the allocation to use a doubling of reserved
61 // space until it is large enough. So far tests show a small speed
62 // increase and a noticable memory saving.
67 ///////////////////////////////////////
68 // The internal string representation
69 ///////////////////////////////////////
71 struct lyxstring::Srep {
76 /// The total amount of data reserved for this representaion
78 /// Data. At least 1 char for trailing null.
79 lyxstring::value_type * s;
82 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
84 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
86 ~Srep() { delete[] s; }
88 Srep * get_own_copy() {
89 if (ref == 1) return this;
91 return new Srep(sz, s);
95 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
97 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
99 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
101 void push_back(lyxstring::value_type c);
103 void insert(lyxstring::size_type pos,
104 const lyxstring::value_type * p,
105 lyxstring::size_type n);
107 void resize(lyxstring::size_type n, lyxstring::value_type c);
109 void reserve(lyxstring::size_type res_arg);
111 void replace(lyxstring::size_type i, lyxstring::size_type n,
112 lyxstring::value_type const * p, lyxstring::size_type n2);
115 Srep & operator=(const Srep &);
119 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
121 // can be called with p == 0 by
122 // lyxstring::assign(const value_type *, size_type)
127 s = new value_type[res + 1]; // add space for terminator
129 // if sz = 0 nothing gets copied and we have an error
132 // possibly allows for large but empty string
133 sz = 0; // this line should be redundant
139 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
144 s = new value_type[res + 1]; // add space for terminator
147 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
148 // allows for large but empty string
154 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
156 // can be called with p == 0
157 // by lyxstring::assign(const value_type *, size_type)
163 s = new value_type[res + 1]; // add space for terminator
168 // if sz = 0 nothing gets copied and we have an error
172 sz = 0; // this line should be redundant
178 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
184 s = new value_type[res + 1]; // add space for terminator
188 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
189 // allows for a large empty string
195 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
197 register unsigned int const len = sz + asz;
202 value_type * tmp = new value_type[res + 1];
204 memcpy(tmp + sz, p, asz);
209 memcpy(s + sz, p, asz);
215 void lyxstring::Srep::push_back(value_type c)
217 s[sz] = c; // it is always room to put a value_type at the end
223 value_type * tmp = new value_type[res + 1];
231 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
232 lyxstring::size_type n)
237 } while (res < n + sz);
238 value_type * tmp = new value_type[res + 1];
240 memcpy(tmp + pos, p, n);
241 memcpy(tmp + pos + n, &s[pos], sz - pos);
246 memmove(s + pos + n, &s[pos], sz - pos);
247 memcpy(s + pos, p, n);
253 void lyxstring::Srep::resize(size_type n, value_type c)
255 // This resets sz to res_arg
256 res = min(n, npos - 2); // We keep no xtra when we resize
257 value_type * tmp = new value_type[res + 1];
258 memcpy(tmp, s, min(sz, res));
260 memset(tmp + sz, c, res - sz);
267 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
269 // This keeps the old sz, but
270 // increases res with res_arg
272 value_type * tmp = new value_type[res + 1];
279 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
280 value_type const * p, size_type n2)
282 // can be called with p= 0 and n2= 0
285 if (res >= n2 + sz) {
286 memmove(s + i + n2, &s[i + n], sz - i);
287 memcpy(s + i, p, n2);
292 } while (res < n2 + sz);
293 value_type * tmp = new value_type[res + 1];
295 memcpy(tmp + i, p, n2);
296 memcpy(tmp + i + n2, &s[i + n], sz - i);
304 ///////////////////////////////////////
305 // The lyxstring Invariant tester
306 ///////////////////////////////////////
308 // There are no know bugs in lyxstring now, and it have been
309 // tested for a long time. so we disable the invariant checker. (Lgb)
310 #undef ENABLE_ASSERTIONS
311 #ifdef ENABLE_ASSERTIONS
313 /** Testing of the lyxstring invariant
314 * By creating an object that tests the lyxstring invariant during its
315 * construction *and* its deconstruction we greatly simplify our code.
316 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
317 * will test the invariant upon entry to the code. If the Asserts fail
318 * then we know from the stack trace that the corruption occurred *before*
319 * entry to this method. We can also be sure it didn't happen in any of
320 * the tested lyxstring methods. It is therefore likely to be due to some
321 * other external force.
322 * Several lyxstring methods have multiple exit points which would otherwise
323 * require us to insert a separate test before each return. But since we
324 * created an object its destructor will be called upon exit (any exit!).
325 * We thus get testing at both start and end of a method with one line of
326 * code at the head of a method. More importantly, we get good testing
327 * everytime we run the code.
328 * NOTE: just because we test the invariant doesn't mean we can forget
329 * about testing pre and post conditions specific to any given method.
330 * This test simply proves that the lyxstring/Srep is in a valid state it
331 * does *not* prove that the method did what it was supposed to.
333 class lyxstringInvariant {
335 lyxstringInvariant(lyxstring const *);
336 ~lyxstringInvariant();
339 lyxstring const * object;
343 // To test if this scheme works "as advertised" uncomment the printf's in
344 // the constructor and destructor below and then uncomment the printf and the
345 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
346 // The correct output when LyX has been recompiled and run is:
347 // lyxstringInvariant constructor
348 // lyxstring::operator=(char const *)
349 // lyxstringInvariant constructor
350 // lyxstringInvariant destructor completed
351 // lyxstringInvariant destructor completed
352 // NOTE: The easiest way to catch this snippet of the output is to wait for
353 // 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");
362 lyxstringInvariant::~lyxstringInvariant()
365 // printf("lyxstringInvariant destructor completed\n");
369 void lyxstringInvariant::helper() const
371 // Some of these tests might look pointless but they are
372 // all part of the invariant and if we want to make sure
373 // we have a bullet proof implementation then we need to
374 // test every last little thing we *know* should be true.
375 // I may have missed a test or two, so feel free to fill
376 // in the gaps. ARRae.
380 Assert(object->rep->s); // s is never 0
381 Assert(object->rep->res); // res cannot be 0
382 Assert(object->rep->sz <= object->rep->res);
383 Assert(object->rep->ref >= 1); // its in use so it must be referenced
384 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
385 // if it does ever == then we should be generating a new copy
386 // and starting again. (Is char always 8-bits?)
388 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
390 #define TestlyxstringInvariant(s)
391 #endif /* ENABLE_ASSERTIONS */
394 ///////////////////////////////////////
395 // Constructors and Deconstructors.
396 ///////////////////////////////////////
398 lyxstring::size_type const lyxstring::npos =
399 static_cast<lyxstring::size_type>(-1);
402 lyxstring::lyxstring()
404 static Srep empty_rep(0, "");
410 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
412 lyx::Assert(pos <= x.rep->sz); // STD!
413 if (pos == 0 && n >= x.length()) { // this is the default
417 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
422 lyxstring::lyxstring(value_type const * s, size_type n)
424 lyx::Assert(s && n < npos); // STD!
425 static Srep empty_rep(0, "");
427 rep = new Srep(n, s);
435 lyxstring::lyxstring(value_type const * s)
437 lyx::Assert(s); // STD!
438 static Srep empty_rep(0, "");
439 if (*s) { // s is not empty string
440 rep = new Srep(strlen(s), s);
448 lyxstring::lyxstring(size_type n, value_type c)
450 lyx::Assert(n < npos); // STD!
451 rep = new Srep(n, c);
455 lyxstring::lyxstring(const_iterator first, const_iterator last)
457 rep = new Srep(last - first, first);
461 lyxstring::~lyxstring()
463 if (--rep->ref == 0) delete rep;
466 ///////////////////////
468 ///////////////////////
470 lyxstring::iterator lyxstring::begin()
472 rep = rep->get_own_copy();
477 lyxstring::const_iterator lyxstring::begin() const
483 lyxstring::iterator lyxstring::end()
485 rep = rep->get_own_copy();
486 return rep->s + rep->sz;
490 lyxstring::const_iterator lyxstring::end() const
492 return rep->s + rep->sz;
496 reverse_iterator lyxstring::rbegin()
498 return reverse_iterator( end() );
502 const_reverse_iterator lyxstring::rbegin() const
504 return const_reverse_iterator( end() );
508 reverse_iterator lyxstring::rend()
510 return reverse_iterator( begin() );
514 const_reverse_iterator lyxstring::rend() const
516 return const_reverse_iterator( begin() );
521 ///////////////////////
523 ///////////////////////
525 lyxstring::size_type lyxstring::size() const
531 void lyxstring::resize(size_type n, value_type c)
533 lyx::Assert(n <= npos); // STD!
534 TestlyxstringInvariant(this);
536 // This resets sz to res_arg
537 rep = rep->get_own_copy();
542 lyxstring::size_type lyxstring::capacity() const
548 void lyxstring::reserve(size_type res_arg)
550 TestlyxstringInvariant(this);
552 rep = rep->get_own_copy();
553 rep->reserve(res_arg);
561 lyxstring & lyxstring::operator=(lyxstring const & x)
563 TestlyxstringInvariant(this);
569 lyxstring & lyxstring::operator=(value_type const * s)
571 lyx::Assert(s); // OURS!
572 TestlyxstringInvariant(this);
573 // printf("lyxstring::operator= (value_type const *)\n");
579 lyxstring & lyxstring::operator=(value_type c)
581 TestlyxstringInvariant(this);
585 if (rep->ref == 1) // recycle rep
589 rep = new Srep(1, s);
595 lyxstring & lyxstring::assign(lyxstring const & x)
597 TestlyxstringInvariant(this);
599 x.rep->ref++; // protect against ``st = st''
600 if (--rep->ref == 0) delete rep;
601 rep = x.rep; // share representation
606 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
608 lyx::Assert(pos <= x.rep->sz); // STD!
609 TestlyxstringInvariant(this);
611 return assign(x.substr(pos, n));
615 lyxstring & lyxstring::assign(value_type const * s, size_type n)
617 lyx::Assert(s && n < npos); // STD!
618 TestlyxstringInvariant(this);
620 if (rep->ref == 1) // recycle rep
624 rep = new Srep(n, s);
630 lyxstring & lyxstring::assign(value_type const * s)
632 lyx::Assert(s); // OURS!
633 TestlyxstringInvariant(this);
635 return assign(s, strlen(s));
639 lyxstring & lyxstring::assign(size_type n, value_type ch)
641 TestlyxstringInvariant(this);
643 rep = rep->get_own_copy();
649 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
651 TestlyxstringInvariant(this);
653 rep = rep->get_own_copy();
654 rep->assign(last - first, first);
663 lyxstring::const_reference lyxstring::operator[](size_type pos) const
666 lyx::Assert(pos <= rep->sz); // OURS!
667 static char helper = '\0';
668 return pos == rep->sz ? helper : rep->s[pos];
670 lyx::Assert(pos < rep->sz); // OURS!
676 lyxstring::reference lyxstring::operator[](size_type pos)
678 lyx::Assert(pos < rep->sz); // OURS!
679 TestlyxstringInvariant(this);
681 rep = rep->get_own_copy();
686 lyxstring::const_reference lyxstring::at(size_type n) const
688 lyx::Assert(n < rep->sz); // STD!
693 lyxstring::reference lyxstring::at(size_type n)
695 lyx::Assert(n < rep->sz); // STD!
696 TestlyxstringInvariant(this);
698 rep = rep->get_own_copy();
707 lyxstring & lyxstring::operator+=(lyxstring const & x)
709 TestlyxstringInvariant(this);
715 lyxstring & lyxstring::operator+=(value_type const * x)
717 lyx::Assert(x); // OURS!
718 TestlyxstringInvariant(this);
724 lyxstring & lyxstring::operator+=(value_type c)
726 TestlyxstringInvariant(this);
733 void lyxstring::push_back(value_type c)
735 TestlyxstringInvariant(this);
737 rep = rep->get_own_copy();
742 lyxstring & lyxstring::append(lyxstring const & x)
744 TestlyxstringInvariant(this);
746 if (x.empty()) return *this;
747 rep = rep->get_own_copy();
748 rep->append(x.length(), x.rep->s);
753 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
755 lyx::Assert(pos <= x.rep->sz); // STD!
756 TestlyxstringInvariant(this);
758 return append(x.substr(pos, n));
762 lyxstring & lyxstring::append(value_type const * p, size_type n)
764 lyx::Assert(p); // OURS!
765 TestlyxstringInvariant(this);
767 if (!*p || !n) return *this;
768 rep = rep->get_own_copy();
774 lyxstring & lyxstring::append(value_type const * p)
776 lyx::Assert(p); // OURS!
777 return append(p, strlen(p));
781 lyxstring & lyxstring::append(size_type n, value_type c)
783 TestlyxstringInvariant(this);
785 value_type * tmp = new value_type[n];
787 rep = rep->get_own_copy();
794 lyxstring & lyxstring::append(iterator first, iterator last)
796 TestlyxstringInvariant(this);
798 rep = rep->get_own_copy();
799 rep->append(last - first, first);
803 // insert characters before (*this)[pos]
805 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
807 TestlyxstringInvariant(this);
809 return insert(pos, x, 0, x.rep->sz);
813 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
814 size_type pos2, size_type n)
816 lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
817 TestlyxstringInvariant(this);
819 rep = rep->get_own_copy();
820 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
825 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
827 lyx::Assert(p); // OURS!
828 TestlyxstringInvariant(this);
831 // insert nothing and you change nothing
832 rep = rep->get_own_copy();
833 rep->insert(pos, p, n);
839 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
841 lyx::Assert(p); // OURS!
842 return insert(pos, p, strlen(p));
846 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
848 TestlyxstringInvariant(this);
850 rep = rep->get_own_copy();
851 value_type * tmp = new value_type[n];
853 rep->insert(pos, tmp, n);
859 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
861 TestlyxstringInvariant(this);
863 // what iterator is this supposed to return??
864 size_type tmp = p - begin();
865 insert(p - begin(), 1, c);
866 return begin() + tmp + 1; // ??
870 void lyxstring::insert(iterator p, size_type n , value_type c)
872 TestlyxstringInvariant(this);
874 insert(p - begin(), n , c);
878 void lyxstring::insert(iterator p, iterator first, iterator last)
880 TestlyxstringInvariant(this);
882 insert(p - begin(), first, last - first);
890 // All the below find functions should be verified,
891 // it is very likely that I have mixed up or interpreted
892 // some of the parameters wrong, also some of the funcs can surely
893 // be written more effectively.
895 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
897 if (!rep->sz || i >= rep->sz) return npos;
899 TestlyxstringInvariant(this);
901 size_type n = a.length();
903 for (size_type t = i; rep->sz - t >= n; ++t) {
904 // search until (*this)[i] == a[0]
905 if (rep->s[t] == a[0]) {
906 // check if the rest of the value_types match
908 for (size_type j = 1; j < n; ++j) {
909 if (rep->s[t + j] != a[j]) {
921 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
924 lyx::Assert(ptr); // OURS!
925 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
927 TestlyxstringInvariant(this);
929 // What is "n" here? is it the number of value_types to use in ptr
930 // or does "i" and "n" togeter form a substring to search
931 // for ptr in? For now I will assume that "n" tells the length
933 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 = 1; 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
955 lyx::Assert(s); // OURS!
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 n = a.length();
983 if (!n || rep->sz < n)
986 size_type t = min(rep->sz - n, i);
988 if (rep->s[t] == a[0]) {
989 // check if the rest of the value_types match
991 for (size_type j = 1; j < n; ++j) {
992 if (rep->s[t + j] != a[j]) {
1004 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1007 lyx::Assert(ptr); // OURS!
1008 TestlyxstringInvariant(this);
1010 n = min(n, strlen(ptr));
1011 if (!n || rep->sz < n)
1014 size_type t = min(rep->sz - n, i);
1016 if (rep->s[t] == ptr[0]) {
1017 // check if the rest of the value_types match
1019 for (size_type j = 1; j < n; ++j) {
1020 if (rep->s[t + j] != ptr[j]) {
1025 if (equal) return t;
1032 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1035 lyx::Assert(ptr); // OURS!
1037 if (!ptr || !*ptr) return npos;
1038 return rfind(ptr, i, strlen(ptr));
1042 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1044 TestlyxstringInvariant(this);
1046 size_type const sz = rep->sz;
1047 if (sz < 1) return npos;
1048 size_type ii = min(sz - 1, i);
1050 if (rep->s[ii] == c) return ii;
1056 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1059 lyx::Assert(i <= rep->sz); // OURS!
1060 TestlyxstringInvariant(this);
1062 for (size_type t = i; t < rep->sz; ++t) {
1063 if (a.find(rep->s[t]) != npos) return t;
1069 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1073 lyx::Assert(ptr && i <= rep->sz); // OURS!
1074 TestlyxstringInvariant(this);
1075 if (!n) return npos;
1077 for (size_type t = i; t < rep->sz; ++t) {
1078 if (memchr(ptr, rep->s[t], n) != 0) return t;
1084 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1087 lyx::Assert(ptr && i <= rep->sz); // OURS!
1088 TestlyxstringInvariant(this);
1090 for (size_type t = i; t < rep->sz; ++t) {
1091 if (strchr(ptr, rep->s[t]) != 0) return t;
1097 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1099 lyx::Assert(i <= rep->sz); // OURS!
1100 TestlyxstringInvariant(this);
1102 for (size_type t = i; t < rep->sz; ++t) {
1103 if (rep->s[t] == c) return t;
1109 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1112 TestlyxstringInvariant(this);
1114 size_type ii = min(rep->sz - 1, i);
1115 for (int t = ii; t >= 0; --t) {
1116 if (a.find(rep->s[t]) != npos) return t;
1122 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1126 lyx::Assert(ptr); // OURS!
1127 TestlyxstringInvariant(this);
1128 if (!n) return npos;
1130 size_type ii = min(rep->sz - 1, i);
1131 for (int t = ii; t >= 0; --t) {
1132 if (memchr(ptr, rep->s[t], n) != 0) return t;
1138 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1141 lyx::Assert(ptr); // OURS!
1142 TestlyxstringInvariant(this);
1144 size_type ii = min(rep->sz - 1, i);
1145 for (int t = ii; t >= 0; --t) {
1146 if (strchr(ptr, rep->s[t]) != 0) return t;
1152 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1154 TestlyxstringInvariant(this);
1156 if (!rep->sz) return npos;
1157 size_type ii = min(rep->sz - 1, i);
1158 for (int t = ii; t >= 0; --t) {
1159 if (rep->s[t] == c) return t;
1165 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1168 TestlyxstringInvariant(this);
1170 if (!rep->sz) return npos;
1171 lyx::Assert(i <= rep->sz);
1172 for (size_type t = i; t < rep->sz; ++t) {
1173 if (a.find(rep->s[t]) == npos) return t;
1179 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1183 lyx::Assert(ptr && i <= rep->sz); // OURS!
1184 TestlyxstringInvariant(this);
1186 if (!n) return (i < rep->sz) ? i : npos;
1187 for (size_type t = i; t < rep->sz; ++t) {
1188 if (memchr(ptr, rep->s[t], n) == 0) return t;
1194 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1197 lyx::Assert(ptr && i <= rep->sz); // OURS!
1198 TestlyxstringInvariant(this);
1200 for (size_type t = i; t < rep->sz; ++t) {
1201 if (strchr(ptr, rep->s[t]) == 0) return t;
1207 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1210 if (!rep->sz) return npos;
1211 lyx::Assert(i <= rep->sz); // OURS!
1212 TestlyxstringInvariant(this);
1214 for (size_type t = i; t < rep->sz; ++t) {
1215 if (rep->s[t] != c) return t;
1221 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1224 TestlyxstringInvariant(this);
1226 size_type ii = min(rep->sz - 1, i);
1227 for (int t = ii; t >= 0; --t) {
1228 if (a.find(rep->s[t]) == npos) return t;
1234 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1238 lyx::Assert(ptr); // OURS!
1239 TestlyxstringInvariant(this);
1241 if (!n) return npos;
1242 size_type ii = min(rep->sz - 1, i);
1244 for (int t = ii; t >= 0; --t) {
1245 if (memchr(ptr, rep->s[t], n) == 0) return t;
1251 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1254 lyx::Assert(ptr); // OURS!
1255 TestlyxstringInvariant(this);
1257 size_type ii = min(rep->sz - 1, i);
1258 for (int t = ii; t >= 0; --t) {
1259 if (strchr(ptr, rep->s[t]) == 0) return t;
1265 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1268 TestlyxstringInvariant(this);
1270 size_type ii = min(rep->sz - 1, i);
1271 for (int t = ii; t >= 0; --t) {
1272 if (rep->s[t] != c) return t;
1282 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1284 lyx::Assert(i <= rep->sz); // OURS!
1285 TestlyxstringInvariant(this);
1287 return replace(i, n, x, 0, x.rep->sz);
1291 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1292 size_type i2, size_type n2)
1294 lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1295 TestlyxstringInvariant(this);
1297 rep = rep->get_own_copy();
1298 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1303 lyxstring & lyxstring::replace(size_type i, size_type n,
1304 value_type const * p, size_type n2)
1306 lyx::Assert(p && i <= rep->sz); // OURS!
1307 TestlyxstringInvariant(this);
1309 rep = rep->get_own_copy();
1310 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1315 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1317 lyx::Assert(p && i <= rep->sz); // OURS!
1318 TestlyxstringInvariant(this);
1320 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1324 lyxstring & lyxstring::replace(size_type i, size_type n,
1325 size_type n2, value_type c)
1327 lyx::Assert(i <= rep->sz); // OURS!
1328 TestlyxstringInvariant(this);
1330 rep = rep->get_own_copy();
1331 value_type * tmp = new value_type[n2];
1333 rep->replace(i, min(n, rep->sz), tmp, n2);
1339 /// FY! FY! FY! go away !
1340 lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
1342 return replace(i, n, 1, c);
1346 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1348 TestlyxstringInvariant(this);
1350 return replace(i - begin(), i2 - i, str);
1354 lyxstring & lyxstring::replace(iterator i, iterator i2,
1355 value_type const * p, size_type n)
1357 lyx::Assert(p); // OURS!
1358 TestlyxstringInvariant(this);
1360 return replace(i - begin(), i2 - i, p, n);
1364 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1366 lyx::Assert(p); // OURS!
1367 TestlyxstringInvariant(this);
1369 return replace(i - begin(), i2 - i, p);
1373 lyxstring & lyxstring::replace(iterator i, iterator i2,
1374 size_type n , value_type c)
1376 TestlyxstringInvariant(this);
1378 return replace(i - begin(), i2 - i, n, c);
1382 lyxstring & lyxstring::replace(iterator i, iterator i2,
1383 iterator j, iterator j2)
1385 TestlyxstringInvariant(this);
1387 return replace(i - begin(), i2 - i, j, j2 - j);
1391 void lyxstring::swap(lyxstring & str)
1393 if (rep == str.rep) return;
1394 Srep * tmp = str.rep;
1400 lyxstring & lyxstring::erase(size_type i, size_type n)
1402 lyx::Assert(i <= rep->sz); // STD!
1403 TestlyxstringInvariant(this);
1405 rep = rep->get_own_copy();
1406 if (i == 0 && n >= rep->sz) {
1409 n = min(n, rep->sz - i);
1410 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1417 lyxstring::iterator lyxstring::erase(iterator i)
1419 TestlyxstringInvariant(this);
1421 // what iterator is this supposed to return?
1422 // the iterator after the one erased
1423 erase(i - begin(), 1);
1424 return begin(); // BUG
1428 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1430 TestlyxstringInvariant(this);
1432 erase(first - begin(), last - first);
1433 return begin(); // BUG
1437 /////////////////////////////////////
1438 // Conversion to C-style Strings
1439 /////////////////////////////////////
1441 lyxstring::value_type const * lyxstring::c_str() const
1443 rep->s[length()] = '\0';
1448 lyxstring::value_type const * lyxstring::data() const
1454 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1455 size_type pos) const
1457 lyx::Assert(buf); // OURS!
1458 lyx::Assert(pos <= rep->sz); // STD!
1459 TestlyxstringInvariant(this);
1461 register int nn = min(len, length() - pos);
1462 memcpy(buf, &(rep->s[pos]), nn);
1467 ////////////////////
1469 ////////////////////
1471 // Compare funcs should be verified.
1473 int lyxstring::internal_compare(size_type pos, size_type n,
1474 value_type const * s,
1475 size_type slen, size_type n2) const
1477 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1479 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1481 // remember that n can very well be a lot larger than rep->sz
1482 // so we have to ensure that n is no larger than rep->sz
1483 n = min(n, rep->sz);
1486 return memcmp(&(rep->s[pos]), s, n);
1487 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1496 int lyxstring::compare(lyxstring const & str) const
1498 TestlyxstringInvariant(this);
1499 return internal_compare(0, rep->sz, str.rep->s,
1500 str.rep->sz, str.rep->sz);
1504 int lyxstring::compare(value_type const * s) const
1506 lyx::Assert(s); //OURS!
1507 TestlyxstringInvariant(this);
1508 int n = (!s) ? 0 : strlen(s);
1509 return internal_compare(0, rep->sz, s, n, n);
1513 int lyxstring::compare(size_type pos, size_type n,
1514 lyxstring const & str) const
1516 lyx::Assert(pos <= rep->sz); // OURS!
1517 TestlyxstringInvariant(this);
1518 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1522 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1523 size_type pos2, size_type n2) const
1525 lyx::Assert(pos <= rep->sz); // OURS!
1526 lyx::Assert(pos2 <= str.rep->sz); // OURS!
1527 TestlyxstringInvariant(this);
1528 return internal_compare(pos, n,
1530 str.rep->sz - pos2, n2);
1534 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1537 lyx::Assert(s && pos <= rep->sz); // OURS!
1538 TestlyxstringInvariant(this);
1539 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1547 // i = index, n = length
1548 lyxstring lyxstring::substr(size_type i, size_type n) const
1550 lyx::Assert(i <= rep->sz); // STD!
1551 TestlyxstringInvariant(this);
1553 return lyxstring(*this, i, n);
1557 /////////////////////////////////////////////
1558 // String operators, non member functions
1559 /////////////////////////////////////////////
1561 bool operator==(lyxstring const & a, lyxstring const & b)
1563 return a.compare(b) == 0;
1567 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1569 lyx::Assert(a); // OURS!
1570 return b.compare(a) == 0;
1574 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1576 lyx::Assert(b); // OURS!
1577 return a.compare(b) == 0;
1581 bool operator!=(lyxstring const & a, lyxstring const & b)
1583 return a.compare(b) != 0;
1587 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1589 lyx::Assert(a); // OURS!
1590 return b.compare(a) != 0;
1594 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1596 lyx::Assert(b); // OURS!
1597 return a.compare(b) != 0;
1601 bool operator>(lyxstring const & a, lyxstring const & b)
1603 return a.compare(b) > 0;
1607 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1609 lyx::Assert(a); // OURS!
1610 return b.compare(a) < 0; // since we reverse the parameters
1614 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1616 lyx::Assert(b); // OURS!
1617 return a.compare(b) > 0;
1621 bool operator<(lyxstring const & a, lyxstring const & b)
1623 return a.compare(b) < 0;
1627 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1629 lyx::Assert(a); // OURS!
1630 return b.compare(a) > 0; // since we reverse the parameters
1634 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1636 lyx::Assert(b); // OURS!
1637 return a.compare(b) < 0;
1641 bool operator>=(lyxstring const & a, lyxstring const & b)
1643 return a.compare(b) >= 0;
1647 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1649 lyx::Assert(a); // OURS!
1650 return b.compare(a) <= 0; // since we reverse the parameters
1654 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1656 lyx::Assert(b); // OURS!
1657 return a.compare(b) >= 0;
1661 bool operator<=(lyxstring const & a, lyxstring const & b)
1663 return a.compare(b) <= 0;
1667 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1669 lyx::Assert(a); // OURS!
1670 return b.compare(a) >= 0; // since we reverse the parameters
1674 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1676 lyx::Assert(b); // OURS!
1677 return a.compare(b) <= 0;
1681 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1689 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1691 lyx::Assert(a); // OURS!
1698 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1707 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1709 lyx::Assert(b); // OURS!
1716 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1724 void swap(lyxstring & str1, lyxstring & str2)
1732 istream & operator>>(istream & is, lyxstring & s)
1735 // very bad solution
1736 char * nome = new char[1024];
1738 lyxstring tmp(nome);
1740 if (!tmp.empty()) s = tmp;
1743 int w = is.widdth(0);
1747 if (isspace(c)) { is.putback(c); break; }
1749 if (--w == 1) break;
1751 if (s.empty()) is.setstate(ios::failbit);
1757 ostream & operator<<(ostream & o, lyxstring const & s)
1759 return o.write(s.data(), s.length());
1763 istream & getline(istream & is, lyxstring & s,
1764 lyxstring::value_type delim)
1766 // very bad solution
1782 lyxstring a = "abcac";
1783 cout << a.rfind("ab") << endl;
1784 cout << a.rfind("c") << endl;
1785 cout << a.rfind("d") << endl;