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 // This is actually what the standard requires,
667 lyx::Assert(pos <= rep->sz); // OURS!
668 static char helper = '\0';
669 return pos == rep->sz ? helper : rep->s[pos];
671 // but we use this one since it is stricter
672 // and more according to the real intent of std::string.
673 lyx::Assert(pos < rep->sz); // OURS!
679 lyxstring::reference lyxstring::operator[](size_type pos)
681 lyx::Assert(pos < rep->sz); // OURS!
682 TestlyxstringInvariant(this);
684 rep = rep->get_own_copy();
689 lyxstring::const_reference lyxstring::at(size_type n) const
691 lyx::Assert(n < rep->sz); // STD!
696 lyxstring::reference lyxstring::at(size_type n)
698 lyx::Assert(n < rep->sz); // STD!
699 TestlyxstringInvariant(this);
701 rep = rep->get_own_copy();
710 lyxstring & lyxstring::operator+=(lyxstring const & x)
712 TestlyxstringInvariant(this);
718 lyxstring & lyxstring::operator+=(value_type const * x)
720 lyx::Assert(x); // OURS!
721 TestlyxstringInvariant(this);
727 lyxstring & lyxstring::operator+=(value_type c)
729 TestlyxstringInvariant(this);
736 void lyxstring::push_back(value_type c)
738 TestlyxstringInvariant(this);
740 rep = rep->get_own_copy();
745 lyxstring & lyxstring::append(lyxstring const & x)
747 TestlyxstringInvariant(this);
749 if (x.empty()) return *this;
750 rep = rep->get_own_copy();
751 rep->append(x.length(), x.rep->s);
756 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
758 lyx::Assert(pos <= x.rep->sz); // STD!
759 TestlyxstringInvariant(this);
761 return append(x.substr(pos, n));
765 lyxstring & lyxstring::append(value_type const * p, size_type n)
767 lyx::Assert(p); // OURS!
768 TestlyxstringInvariant(this);
770 if (!*p || !n) return *this;
771 rep = rep->get_own_copy();
777 lyxstring & lyxstring::append(value_type const * p)
779 lyx::Assert(p); // OURS!
780 return append(p, strlen(p));
784 lyxstring & lyxstring::append(size_type n, value_type c)
786 TestlyxstringInvariant(this);
788 value_type * tmp = new value_type[n];
790 rep = rep->get_own_copy();
797 lyxstring & lyxstring::append(iterator first, iterator last)
799 TestlyxstringInvariant(this);
801 rep = rep->get_own_copy();
802 rep->append(last - first, first);
806 // insert characters before (*this)[pos]
808 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
810 TestlyxstringInvariant(this);
812 return insert(pos, x, 0, x.rep->sz);
816 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
817 size_type pos2, size_type n)
819 lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
820 TestlyxstringInvariant(this);
822 rep = rep->get_own_copy();
823 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
828 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
830 lyx::Assert(p); // OURS!
831 TestlyxstringInvariant(this);
834 // insert nothing and you change nothing
835 rep = rep->get_own_copy();
836 rep->insert(pos, p, n);
842 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
844 lyx::Assert(p); // OURS!
845 return insert(pos, p, strlen(p));
849 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
851 TestlyxstringInvariant(this);
853 rep = rep->get_own_copy();
854 value_type * tmp = new value_type[n];
856 rep->insert(pos, tmp, n);
862 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
864 TestlyxstringInvariant(this);
866 // what iterator is this supposed to return??
867 size_type tmp = p - begin();
868 insert(p - begin(), 1, c);
869 return begin() + tmp + 1; // ??
873 void lyxstring::insert(iterator p, size_type n , value_type c)
875 TestlyxstringInvariant(this);
877 insert(p - begin(), n , c);
881 void lyxstring::insert(iterator p, iterator first, iterator last)
883 TestlyxstringInvariant(this);
885 insert(p - begin(), first, last - first);
893 // All the below find functions should be verified,
894 // it is very likely that I have mixed up or interpreted
895 // some of the parameters wrong, also some of the funcs can surely
896 // be written more effectively.
898 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
900 if (!rep->sz || i >= rep->sz) return npos;
902 TestlyxstringInvariant(this);
904 size_type n = a.length();
906 for (size_type t = i; rep->sz - t >= n; ++t) {
907 // search until (*this)[i] == a[0]
908 if (rep->s[t] == a[0]) {
909 // check if the rest of the value_types match
911 for (size_type j = 1; j < n; ++j) {
912 if (rep->s[t + j] != a[j]) {
924 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
927 lyx::Assert(ptr); // OURS!
928 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
930 TestlyxstringInvariant(this);
932 // What is "n" here? is it the number of value_types to use in ptr
933 // or does "i" and "n" togeter form a substring to search
934 // for ptr in? For now I will assume that "n" tells the length
936 n = min(n, strlen(ptr));
938 for (size_type t = i; rep->sz - t >= n; ++t) {
939 // search until (*this)[i] == a[0]
940 if (rep->s[t] == ptr[0]) {
941 // check if the rest of the value_types match
943 for (size_type j = 1; j < n; ++j) {
944 if (rep->s[t + j] != ptr[j]) {
956 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
958 lyx::Assert(s); // OURS!
959 if (!rep->sz || i >= rep->sz) return npos;
961 TestlyxstringInvariant(this);
963 if (!s || !*s) return npos;
964 return find(s, i, strlen(s));
968 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
970 if (!rep->sz || i >= rep->sz) return npos;
972 TestlyxstringInvariant(this);
974 for (size_type t = 0; t + i < rep->sz; ++t) {
975 if (rep->s[t + i] == c) return t + i;
981 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
983 TestlyxstringInvariant(this);
985 size_type n = a.length();
986 if (!n || rep->sz < n)
989 size_type t = min(rep->sz - n, i);
991 if (rep->s[t] == a[0]) {
992 // check if the rest of the value_types match
994 for (size_type j = 1; j < n; ++j) {
995 if (rep->s[t + j] != a[j]) {
1000 if (equal) return t;
1007 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1010 lyx::Assert(ptr); // OURS!
1011 TestlyxstringInvariant(this);
1013 n = min(n, strlen(ptr));
1014 if (!n || rep->sz < n)
1017 size_type t = min(rep->sz - n, i);
1019 if (rep->s[t] == ptr[0]) {
1020 // check if the rest of the value_types match
1022 for (size_type j = 1; j < n; ++j) {
1023 if (rep->s[t + j] != ptr[j]) {
1028 if (equal) return t;
1035 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1038 lyx::Assert(ptr); // OURS!
1040 if (!ptr || !*ptr) return npos;
1041 return rfind(ptr, i, strlen(ptr));
1045 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1047 TestlyxstringInvariant(this);
1049 size_type const sz = rep->sz;
1050 if (sz < 1) return npos;
1051 size_type ii = min(sz - 1, i);
1053 if (rep->s[ii] == c) return ii;
1059 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1062 lyx::Assert(i <= rep->sz); // OURS!
1063 TestlyxstringInvariant(this);
1065 for (size_type t = i; t < rep->sz; ++t) {
1066 if (a.find(rep->s[t]) != npos) return t;
1072 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1076 lyx::Assert(ptr && i <= rep->sz); // OURS!
1077 TestlyxstringInvariant(this);
1078 if (!n) return npos;
1080 for (size_type t = i; t < rep->sz; ++t) {
1081 if (memchr(ptr, rep->s[t], n) != 0) return t;
1087 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1090 lyx::Assert(ptr && i <= rep->sz); // OURS!
1091 TestlyxstringInvariant(this);
1093 for (size_type t = i; t < rep->sz; ++t) {
1094 if (strchr(ptr, rep->s[t]) != 0) return t;
1100 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1102 lyx::Assert(i <= rep->sz); // OURS!
1103 TestlyxstringInvariant(this);
1105 for (size_type t = i; t < rep->sz; ++t) {
1106 if (rep->s[t] == c) return t;
1112 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1115 TestlyxstringInvariant(this);
1117 size_type ii = min(rep->sz - 1, i);
1118 for (int t = ii; t >= 0; --t) {
1119 if (a.find(rep->s[t]) != npos) return t;
1125 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1129 lyx::Assert(ptr); // OURS!
1130 TestlyxstringInvariant(this);
1131 if (!n) return npos;
1133 size_type ii = min(rep->sz - 1, i);
1134 for (int t = ii; t >= 0; --t) {
1135 if (memchr(ptr, rep->s[t], n) != 0) return t;
1141 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1144 lyx::Assert(ptr); // OURS!
1145 TestlyxstringInvariant(this);
1147 size_type ii = min(rep->sz - 1, i);
1148 for (int t = ii; t >= 0; --t) {
1149 if (strchr(ptr, rep->s[t]) != 0) return t;
1155 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1157 TestlyxstringInvariant(this);
1159 if (!rep->sz) return npos;
1160 size_type ii = min(rep->sz - 1, i);
1161 for (int t = ii; t >= 0; --t) {
1162 if (rep->s[t] == c) return t;
1168 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1171 TestlyxstringInvariant(this);
1173 if (!rep->sz) return npos;
1174 lyx::Assert(i <= rep->sz);
1175 for (size_type t = i; t < rep->sz; ++t) {
1176 if (a.find(rep->s[t]) == npos) return t;
1182 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1186 lyx::Assert(ptr && i <= rep->sz); // OURS!
1187 TestlyxstringInvariant(this);
1189 if (!n) return (i < rep->sz) ? i : npos;
1190 for (size_type t = i; t < rep->sz; ++t) {
1191 if (memchr(ptr, rep->s[t], n) == 0) return t;
1197 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1200 lyx::Assert(ptr && i <= rep->sz); // OURS!
1201 TestlyxstringInvariant(this);
1203 for (size_type t = i; t < rep->sz; ++t) {
1204 if (strchr(ptr, rep->s[t]) == 0) return t;
1210 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1213 if (!rep->sz) return npos;
1214 lyx::Assert(i <= rep->sz); // OURS!
1215 TestlyxstringInvariant(this);
1217 for (size_type t = i; t < rep->sz; ++t) {
1218 if (rep->s[t] != c) return t;
1224 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1227 TestlyxstringInvariant(this);
1229 size_type ii = min(rep->sz - 1, i);
1230 for (int t = ii; t >= 0; --t) {
1231 if (a.find(rep->s[t]) == npos) return t;
1237 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1241 lyx::Assert(ptr); // OURS!
1242 TestlyxstringInvariant(this);
1244 if (!n) return npos;
1245 size_type ii = min(rep->sz - 1, i);
1247 for (int t = ii; t >= 0; --t) {
1248 if (memchr(ptr, rep->s[t], n) == 0) return t;
1254 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1257 lyx::Assert(ptr); // OURS!
1258 TestlyxstringInvariant(this);
1260 size_type ii = min(rep->sz - 1, i);
1261 for (int t = ii; t >= 0; --t) {
1262 if (strchr(ptr, rep->s[t]) == 0) return t;
1268 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1271 TestlyxstringInvariant(this);
1273 size_type ii = min(rep->sz - 1, i);
1274 for (int t = ii; t >= 0; --t) {
1275 if (rep->s[t] != c) return t;
1285 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1287 lyx::Assert(i <= rep->sz); // OURS!
1288 TestlyxstringInvariant(this);
1290 return replace(i, n, x, 0, x.rep->sz);
1294 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1295 size_type i2, size_type n2)
1297 lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1298 TestlyxstringInvariant(this);
1300 rep = rep->get_own_copy();
1301 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1306 lyxstring & lyxstring::replace(size_type i, size_type n,
1307 value_type const * p, size_type n2)
1309 lyx::Assert(p && i <= rep->sz); // OURS!
1310 TestlyxstringInvariant(this);
1312 rep = rep->get_own_copy();
1313 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1318 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1320 lyx::Assert(p && i <= rep->sz); // OURS!
1321 TestlyxstringInvariant(this);
1323 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1327 lyxstring & lyxstring::replace(size_type i, size_type n,
1328 size_type n2, value_type c)
1330 lyx::Assert(i <= rep->sz); // OURS!
1331 TestlyxstringInvariant(this);
1333 rep = rep->get_own_copy();
1334 value_type * tmp = new value_type[n2];
1336 rep->replace(i, min(n, rep->sz), tmp, n2);
1342 /// FY! FY! FY! go away !
1343 lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
1345 return replace(i, n, 1, c);
1349 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1351 TestlyxstringInvariant(this);
1353 return replace(i - begin(), i2 - i, str);
1357 lyxstring & lyxstring::replace(iterator i, iterator i2,
1358 value_type const * p, size_type n)
1360 lyx::Assert(p); // OURS!
1361 TestlyxstringInvariant(this);
1363 return replace(i - begin(), i2 - i, p, n);
1367 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1369 lyx::Assert(p); // OURS!
1370 TestlyxstringInvariant(this);
1372 return replace(i - begin(), i2 - i, p);
1376 lyxstring & lyxstring::replace(iterator i, iterator i2,
1377 size_type n , value_type c)
1379 TestlyxstringInvariant(this);
1381 return replace(i - begin(), i2 - i, n, c);
1385 lyxstring & lyxstring::replace(iterator i, iterator i2,
1386 iterator j, iterator j2)
1388 TestlyxstringInvariant(this);
1390 return replace(i - begin(), i2 - i, j, j2 - j);
1394 void lyxstring::swap(lyxstring & str)
1396 if (rep == str.rep) return;
1397 Srep * tmp = str.rep;
1403 lyxstring & lyxstring::erase(size_type i, size_type n)
1405 lyx::Assert(i <= rep->sz); // STD!
1406 TestlyxstringInvariant(this);
1408 rep = rep->get_own_copy();
1409 if (i == 0 && n >= rep->sz) {
1412 n = min(n, rep->sz - i);
1413 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1420 lyxstring::iterator lyxstring::erase(iterator i)
1422 TestlyxstringInvariant(this);
1424 // what iterator is this supposed to return?
1425 // the iterator after the one erased
1426 erase(i - begin(), 1);
1427 return begin(); // BUG
1431 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1433 TestlyxstringInvariant(this);
1435 erase(first - begin(), last - first);
1436 return begin(); // BUG
1440 /////////////////////////////////////
1441 // Conversion to C-style Strings
1442 /////////////////////////////////////
1444 lyxstring::value_type const * lyxstring::c_str() const
1446 rep->s[length()] = '\0';
1451 lyxstring::value_type const * lyxstring::data() const
1457 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1458 size_type pos) const
1460 lyx::Assert(buf); // OURS!
1461 lyx::Assert(pos <= rep->sz); // STD!
1462 TestlyxstringInvariant(this);
1464 register int nn = min(len, length() - pos);
1465 memcpy(buf, &(rep->s[pos]), nn);
1470 ////////////////////
1472 ////////////////////
1474 // Compare funcs should be verified.
1476 int lyxstring::internal_compare(size_type pos, size_type n,
1477 value_type const * s,
1478 size_type slen, size_type n2) const
1480 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1482 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1484 // remember that n can very well be a lot larger than rep->sz
1485 // so we have to ensure that n is no larger than rep->sz
1486 n = min(n, rep->sz);
1489 return memcmp(&(rep->s[pos]), s, n);
1490 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1499 int lyxstring::compare(lyxstring const & str) const
1501 TestlyxstringInvariant(this);
1502 return internal_compare(0, rep->sz, str.rep->s,
1503 str.rep->sz, str.rep->sz);
1507 int lyxstring::compare(value_type const * s) const
1509 lyx::Assert(s); //OURS!
1510 TestlyxstringInvariant(this);
1511 int n = (!s) ? 0 : strlen(s);
1512 return internal_compare(0, rep->sz, s, n, n);
1516 int lyxstring::compare(size_type pos, size_type n,
1517 lyxstring const & str) const
1519 lyx::Assert(pos <= rep->sz); // OURS!
1520 TestlyxstringInvariant(this);
1521 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1525 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1526 size_type pos2, size_type n2) const
1528 lyx::Assert(pos <= rep->sz); // OURS!
1529 lyx::Assert(pos2 <= str.rep->sz); // OURS!
1530 TestlyxstringInvariant(this);
1531 return internal_compare(pos, n,
1533 str.rep->sz - pos2, n2);
1537 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1540 lyx::Assert(s && pos <= rep->sz); // OURS!
1541 TestlyxstringInvariant(this);
1542 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1550 // i = index, n = length
1551 lyxstring lyxstring::substr(size_type i, size_type n) const
1553 lyx::Assert(i <= rep->sz); // STD!
1554 TestlyxstringInvariant(this);
1556 return lyxstring(*this, i, n);
1560 /////////////////////////////////////////////
1561 // String operators, non member functions
1562 /////////////////////////////////////////////
1564 bool operator==(lyxstring const & a, lyxstring const & b)
1566 return a.compare(b) == 0;
1570 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1572 lyx::Assert(a); // OURS!
1573 return b.compare(a) == 0;
1577 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1579 lyx::Assert(b); // OURS!
1580 return a.compare(b) == 0;
1584 bool operator!=(lyxstring const & a, lyxstring const & b)
1586 return a.compare(b) != 0;
1590 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1592 lyx::Assert(a); // OURS!
1593 return b.compare(a) != 0;
1597 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1599 lyx::Assert(b); // OURS!
1600 return a.compare(b) != 0;
1604 bool operator>(lyxstring const & a, lyxstring const & b)
1606 return a.compare(b) > 0;
1610 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1612 lyx::Assert(a); // OURS!
1613 return b.compare(a) < 0; // since we reverse the parameters
1617 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1619 lyx::Assert(b); // OURS!
1620 return a.compare(b) > 0;
1624 bool operator<(lyxstring const & a, lyxstring const & b)
1626 return a.compare(b) < 0;
1630 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1632 lyx::Assert(a); // OURS!
1633 return b.compare(a) > 0; // since we reverse the parameters
1637 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1639 lyx::Assert(b); // OURS!
1640 return a.compare(b) < 0;
1644 bool operator>=(lyxstring const & a, lyxstring const & b)
1646 return a.compare(b) >= 0;
1650 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1652 lyx::Assert(a); // OURS!
1653 return b.compare(a) <= 0; // since we reverse the parameters
1657 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1659 lyx::Assert(b); // OURS!
1660 return a.compare(b) >= 0;
1664 bool operator<=(lyxstring const & a, lyxstring const & b)
1666 return a.compare(b) <= 0;
1670 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1672 lyx::Assert(a); // OURS!
1673 return b.compare(a) >= 0; // since we reverse the parameters
1677 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1679 lyx::Assert(b); // OURS!
1680 return a.compare(b) <= 0;
1684 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1692 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1694 lyx::Assert(a); // OURS!
1701 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1710 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1712 lyx::Assert(b); // OURS!
1719 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1727 void swap(lyxstring & str1, lyxstring & str2)
1735 istream & operator>>(istream & is, lyxstring & s)
1738 // very bad solution
1739 char * nome = new char[1024];
1741 lyxstring tmp(nome);
1743 if (!tmp.empty()) s = tmp;
1746 int w = is.width(0);
1749 bool skipspace = true;
1760 if (--w == 1) break;
1762 if (s.empty()) is.setstate(std::ios::failbit);
1768 ostream & operator<<(ostream & o, lyxstring const & s)
1770 return o.write(s.data(), s.length());
1774 istream & getline(istream & is, lyxstring & s,
1775 lyxstring::value_type delim)
1777 // very bad solution
1793 lyxstring a = "abcac";
1794 cout << a.rfind("ab") << endl;
1795 cout << a.rfind("c") << endl;
1796 cout << a.rfind("d") << endl;