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 ///////////////////////////////////////
307 #ifdef ENABLE_ASSERTIONS
309 /** Testing of the lyxstring invariant
310 * By creating an object that tests the lyxstring invariant during its
311 * construction *and* its deconstruction we greatly simplify our code.
312 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
313 * will test the invariant upon entry to the code. If the Asserts fail
314 * then we know from the stack trace that the corruption occurred *before*
315 * entry to this method. We can also be sure it didn't happen in any of
316 * the tested lyxstring methods. It is therefore likely to be due to some
317 * other external force.
318 * Several lyxstring methods have multiple exit points which would otherwise
319 * require us to insert a separate test before each return. But since we
320 * created an object its destructor will be called upon exit (any exit!).
321 * We thus get testing at both start and end of a method with one line of
322 * code at the head of a method. More importantly, we get good testing
323 * everytime we run the code.
324 * NOTE: just because we test the invariant doesn't mean we can forget
325 * about testing pre and post conditions specific to any given method.
326 * This test simply proves that the lyxstring/Srep is in a valid state it
327 * does *not* prove that the method did what it was supposed to.
329 class lyxstringInvariant {
331 lyxstringInvariant(lyxstring const *);
332 ~lyxstringInvariant();
335 lyxstring const * object;
339 // To test if this scheme works "as advertised" uncomment the printf's in
340 // the constructor and destructor below and then uncomment the printf and the
341 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
342 // The correct output when LyX has been recompiled and run is:
343 // lyxstringInvariant constructor
344 // lyxstring::operator=(char const *)
345 // lyxstringInvariant constructor
346 // lyxstringInvariant destructor completed
347 // lyxstringInvariant destructor completed
348 // NOTE: The easiest way to catch this snippet of the output is to wait for
349 // the splash screen to disappear and then open and close Help->Credits
351 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
353 // printf("lyxstringInvariant constructor\n");
358 lyxstringInvariant::~lyxstringInvariant()
361 // printf("lyxstringInvariant destructor completed\n");
365 void lyxstringInvariant::helper() const
367 // Some of these tests might look pointless but they are
368 // all part of the invariant and if we want to make sure
369 // we have a bullet proof implementation then we need to
370 // test every last little thing we *know* should be true.
371 // I may have missed a test or two, so feel free to fill
372 // in the gaps. ARRae.
376 Assert(object->rep->s); // s is never 0
377 Assert(object->rep->res); // res cannot be 0
378 Assert(object->rep->sz <= object->rep->res);
379 Assert(object->rep->ref >= 1); // its in use so it must be referenced
380 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
381 // if it does ever == then we should be generating a new copy
382 // and starting again. (Is char always 8-bits?)
384 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
386 #define TestlyxstringInvariant(s)
387 #endif /* ENABLE_ASSERTIONS */
390 ///////////////////////////////////////
391 // Constructors and Deconstructors.
392 ///////////////////////////////////////
394 lyxstring::size_type const lyxstring::npos =
395 static_cast<lyxstring::size_type>(-1);
398 lyxstring::lyxstring()
400 static Srep empty_rep(0, "");
406 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
408 lyx::Assert(pos <= x.rep->sz); // STD!
409 if (pos == 0 && n >= x.length()) { // this is the default
413 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
418 lyxstring::lyxstring(value_type const * s, size_type n)
420 lyx::Assert(s && n < npos); // STD!
421 static Srep empty_rep(0, "");
423 rep = new Srep(n, s);
431 lyxstring::lyxstring(value_type const * s)
433 lyx::Assert(s); // STD!
434 static Srep empty_rep(0, "");
435 if (*s) { // s is not empty string
436 rep = new Srep(strlen(s), s);
444 lyxstring::lyxstring(size_type n, value_type c)
446 lyx::Assert(n < npos); // STD!
447 rep = new Srep(n, c);
451 lyxstring::lyxstring(const_iterator first, const_iterator last)
453 rep = new Srep(last - first, first);
457 lyxstring::~lyxstring()
459 if (--rep->ref == 0) delete rep;
462 ///////////////////////
464 ///////////////////////
466 lyxstring::iterator lyxstring::begin()
468 rep = rep->get_own_copy();
473 lyxstring::const_iterator lyxstring::begin() const
479 lyxstring::iterator lyxstring::end()
481 rep = rep->get_own_copy();
482 return rep->s + rep->sz;
486 lyxstring::const_iterator lyxstring::end() const
488 return rep->s + rep->sz;
492 reverse_iterator lyxstring::rbegin()
494 return reverse_iterator( end() );
498 const_reverse_iterator lyxstring::rbegin() const
500 return const_reverse_iterator( end() );
504 reverse_iterator lyxstring::rend()
506 return reverse_iterator( begin() );
510 const_reverse_iterator lyxstring::rend() const
512 return const_reverse_iterator( begin() );
517 ///////////////////////
519 ///////////////////////
521 lyxstring::size_type lyxstring::size() const
527 void lyxstring::resize(size_type n, value_type c)
529 lyx::Assert(n <= npos); // STD!
530 TestlyxstringInvariant(this);
532 // This resets sz to res_arg
533 rep = rep->get_own_copy();
538 lyxstring::size_type lyxstring::capacity() const
544 void lyxstring::reserve(size_type res_arg)
546 TestlyxstringInvariant(this);
548 rep = rep->get_own_copy();
549 rep->reserve(res_arg);
557 lyxstring & lyxstring::operator=(lyxstring const & x)
559 TestlyxstringInvariant(this);
565 lyxstring & lyxstring::operator=(value_type const * s)
567 lyx::Assert(s); // OURS!
568 TestlyxstringInvariant(this);
569 // printf("lyxstring::operator= (value_type const *)\n");
575 lyxstring & lyxstring::operator=(value_type c)
577 TestlyxstringInvariant(this);
581 if (rep->ref == 1) // recycle rep
585 rep = new Srep(1, s);
591 lyxstring & lyxstring::assign(lyxstring const & x)
593 TestlyxstringInvariant(this);
595 x.rep->ref++; // protect against ``st = st''
596 if (--rep->ref == 0) delete rep;
597 rep = x.rep; // share representation
602 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
604 lyx::Assert(pos <= x.rep->sz); // STD!
605 TestlyxstringInvariant(this);
607 return assign(x.substr(pos, n));
611 lyxstring & lyxstring::assign(value_type const * s, size_type n)
613 lyx::Assert(s && n < npos); // STD!
614 TestlyxstringInvariant(this);
616 if (rep->ref == 1) // recycle rep
620 rep = new Srep(n, s);
626 lyxstring & lyxstring::assign(value_type const * s)
628 lyx::Assert(s); // OURS!
629 TestlyxstringInvariant(this);
631 return assign(s, strlen(s));
635 lyxstring & lyxstring::assign(size_type n, value_type ch)
637 TestlyxstringInvariant(this);
639 rep = rep->get_own_copy();
645 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
647 TestlyxstringInvariant(this);
649 rep = rep->get_own_copy();
650 rep->assign(last - first, first);
659 lyxstring::const_reference lyxstring::operator[](size_type pos) const
662 lyx::Assert(pos <= rep->sz); // OURS!
663 static char helper = '\0';
664 return pos == rep->sz ? helper : rep->s[pos];
666 lyx::Assert(pos < rep->sz); // OURS!
672 lyxstring::reference lyxstring::operator[](size_type pos)
674 lyx::Assert(pos < rep->sz); // OURS!
675 TestlyxstringInvariant(this);
677 rep = rep->get_own_copy();
682 lyxstring::const_reference lyxstring::at(size_type n) const
684 lyx::Assert(n < rep->sz); // STD!
689 lyxstring::reference lyxstring::at(size_type n)
691 lyx::Assert(n < rep->sz); // STD!
692 TestlyxstringInvariant(this);
694 rep = rep->get_own_copy();
703 lyxstring & lyxstring::operator+=(lyxstring const & x)
705 TestlyxstringInvariant(this);
711 lyxstring & lyxstring::operator+=(value_type const * x)
713 lyx::Assert(x); // OURS!
714 TestlyxstringInvariant(this);
720 lyxstring & lyxstring::operator+=(value_type c)
722 TestlyxstringInvariant(this);
729 void lyxstring::push_back(value_type c)
731 TestlyxstringInvariant(this);
733 rep = rep->get_own_copy();
738 lyxstring & lyxstring::append(lyxstring const & x)
740 TestlyxstringInvariant(this);
742 if (x.empty()) return *this;
743 rep = rep->get_own_copy();
744 rep->append(x.length(), x.rep->s);
749 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
751 lyx::Assert(pos <= x.rep->sz); // STD!
752 TestlyxstringInvariant(this);
754 return append(x.substr(pos, n));
758 lyxstring & lyxstring::append(value_type const * p, size_type n)
760 lyx::Assert(p); // OURS!
761 TestlyxstringInvariant(this);
763 if (!*p || !n) return *this;
764 rep = rep->get_own_copy();
770 lyxstring & lyxstring::append(value_type const * p)
772 lyx::Assert(p); // OURS!
773 return append(p, strlen(p));
777 lyxstring & lyxstring::append(size_type n, value_type c)
779 TestlyxstringInvariant(this);
781 value_type * tmp = new value_type[n];
783 rep = rep->get_own_copy();
790 lyxstring & lyxstring::append(iterator first, iterator last)
792 TestlyxstringInvariant(this);
794 rep = rep->get_own_copy();
795 rep->append(last - first, first);
799 // insert characters before (*this)[pos]
801 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
803 TestlyxstringInvariant(this);
805 return insert(pos, x, 0, x.rep->sz);
809 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
810 size_type pos2, size_type n)
812 lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
813 TestlyxstringInvariant(this);
815 rep = rep->get_own_copy();
816 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
821 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
823 lyx::Assert(p); // OURS!
824 TestlyxstringInvariant(this);
827 // insert nothing and you change nothing
828 rep = rep->get_own_copy();
829 rep->insert(pos, p, n);
835 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
837 lyx::Assert(p); // OURS!
838 return insert(pos, p, strlen(p));
842 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
844 TestlyxstringInvariant(this);
846 rep = rep->get_own_copy();
847 value_type * tmp = new value_type[n];
849 rep->insert(pos, tmp, n);
855 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
857 TestlyxstringInvariant(this);
859 // what iterator is this supposed to return??
860 size_type tmp = p - begin();
861 insert(p - begin(), 1, c);
862 return begin() + tmp + 1; // ??
866 void lyxstring::insert(iterator p, size_type n , value_type c)
868 TestlyxstringInvariant(this);
870 insert(p - begin(), n , c);
874 void lyxstring::insert(iterator p, iterator first, iterator last)
876 TestlyxstringInvariant(this);
878 insert(p - begin(), first, last - first);
886 // All the below find functions should be verified,
887 // it is very likely that I have mixed up or interpreted
888 // some of the parameters wrong, also some of the funcs can surely
889 // be written more effectively.
891 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
893 if (!rep->sz || i >= rep->sz) return npos;
895 TestlyxstringInvariant(this);
897 size_type n = a.length();
899 for (size_type t = i; rep->sz - t >= n; ++t) {
900 // search until (*this)[i] == a[0]
901 if (rep->s[t] == a[0]) {
902 // check if the rest of the value_types match
904 for (size_type j = 1; j < n; ++j) {
905 if (rep->s[t + j] != a[j]) {
917 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
920 lyx::Assert(ptr); // OURS!
921 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
923 TestlyxstringInvariant(this);
925 // What is "n" here? is it the number of value_types to use in ptr
926 // or does "i" and "n" togeter form a substring to search
927 // for ptr in? For now I will assume that "n" tells the length
929 n = min(n, strlen(ptr));
931 for (size_type t = i; rep->sz - t >= n; ++t) {
932 // search until (*this)[i] == a[0]
933 if (rep->s[t] == ptr[0]) {
934 // check if the rest of the value_types match
936 for (size_type j = 1; j < n; ++j) {
937 if (rep->s[t + j] != ptr[j]) {
949 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
951 lyx::Assert(s); // OURS!
952 if (!rep->sz || i >= rep->sz) return npos;
954 TestlyxstringInvariant(this);
956 if (!s || !*s) return npos;
957 return find(s, i, strlen(s));
961 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
963 if (!rep->sz || i >= rep->sz) return npos;
965 TestlyxstringInvariant(this);
967 for (size_type t = 0; t + i < rep->sz; ++t) {
968 if (rep->s[t + i] == c) return t + i;
974 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
976 TestlyxstringInvariant(this);
978 size_type n = a.length();
979 if (!n || rep->sz < n)
982 size_type t = min(rep->sz - n, i);
984 if (rep->s[t] == a[0]) {
985 // check if the rest of the value_types match
987 for (size_type j = 1; j < n; ++j) {
988 if (rep->s[t + j] != a[j]) {
1000 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1003 lyx::Assert(ptr); // OURS!
1004 TestlyxstringInvariant(this);
1006 n = min(n, strlen(ptr));
1007 if (!n || rep->sz < n)
1010 size_type t = min(rep->sz - n, i);
1012 if (rep->s[t] == ptr[0]) {
1013 // check if the rest of the value_types match
1015 for (size_type j = 1; j < n; ++j) {
1016 if (rep->s[t + j] != ptr[j]) {
1021 if (equal) return t;
1028 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1031 lyx::Assert(ptr); // OURS!
1033 if (!ptr || !*ptr) return npos;
1034 return rfind(ptr, i, strlen(ptr));
1038 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1040 TestlyxstringInvariant(this);
1042 size_type const sz = rep->sz;
1043 if (sz < 1) return npos;
1044 size_type ii = min(sz - 1, i);
1046 if (rep->s[ii] == c) return ii;
1052 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1055 lyx::Assert(i <= rep->sz); // OURS!
1056 TestlyxstringInvariant(this);
1058 for (size_type t = i; t < rep->sz; ++t) {
1059 if (a.find(rep->s[t]) != npos) return t;
1065 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1069 lyx::Assert(ptr && i <= rep->sz); // OURS!
1070 TestlyxstringInvariant(this);
1071 if (!n) return npos;
1073 for (size_type t = i; t < rep->sz; ++t) {
1074 if (memchr(ptr, rep->s[t], n) != 0) return t;
1080 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1083 lyx::Assert(ptr && i <= rep->sz); // OURS!
1084 TestlyxstringInvariant(this);
1086 for (size_type t = i; t < rep->sz; ++t) {
1087 if (strchr(ptr, rep->s[t]) != 0) return t;
1093 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1095 lyx::Assert(i <= rep->sz); // OURS!
1096 TestlyxstringInvariant(this);
1098 for (size_type t = i; t < rep->sz; ++t) {
1099 if (rep->s[t] == c) return t;
1105 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1108 TestlyxstringInvariant(this);
1110 size_type ii = min(rep->sz - 1, i);
1111 for (int t = ii; t >= 0; --t) {
1112 if (a.find(rep->s[t]) != npos) return t;
1118 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1122 lyx::Assert(ptr); // OURS!
1123 TestlyxstringInvariant(this);
1124 if (!n) return npos;
1126 size_type ii = min(rep->sz - 1, i);
1127 for (int t = ii; t >= 0; --t) {
1128 if (memchr(ptr, rep->s[t], n) != 0) return t;
1134 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1137 lyx::Assert(ptr); // OURS!
1138 TestlyxstringInvariant(this);
1140 size_type ii = min(rep->sz - 1, i);
1141 for (int t = ii; t >= 0; --t) {
1142 if (strchr(ptr, rep->s[t]) != 0) return t;
1148 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1150 TestlyxstringInvariant(this);
1152 if (!rep->sz) return npos;
1153 size_type ii = min(rep->sz - 1, i);
1154 for (int t = ii; t >= 0; --t) {
1155 if (rep->s[t] == c) return t;
1161 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1164 TestlyxstringInvariant(this);
1166 if (!rep->sz) return npos;
1167 lyx::Assert(i <= rep->sz);
1168 for (size_type t = i; t < rep->sz; ++t) {
1169 if (a.find(rep->s[t]) == npos) return t;
1175 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1179 lyx::Assert(ptr && i <= rep->sz); // OURS!
1180 TestlyxstringInvariant(this);
1182 if (!n) return (i < rep->sz) ? i : npos;
1183 for (size_type t = i; t < rep->sz; ++t) {
1184 if (memchr(ptr, rep->s[t], n) == 0) return t;
1190 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1193 lyx::Assert(ptr && i <= rep->sz); // OURS!
1194 TestlyxstringInvariant(this);
1196 for (size_type t = i; t < rep->sz; ++t) {
1197 if (strchr(ptr, rep->s[t]) == 0) return t;
1203 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1206 if (!rep->sz) return npos;
1207 lyx::Assert(i <= rep->sz); // OURS!
1208 TestlyxstringInvariant(this);
1210 for (size_type t = i; t < rep->sz; ++t) {
1211 if (rep->s[t] != c) return t;
1217 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1220 TestlyxstringInvariant(this);
1222 size_type ii = min(rep->sz - 1, i);
1223 for (int t = ii; t >= 0; --t) {
1224 if (a.find(rep->s[t]) == npos) return t;
1230 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1234 lyx::Assert(ptr); // OURS!
1235 TestlyxstringInvariant(this);
1237 if (!n) return npos;
1238 size_type ii = min(rep->sz - 1, i);
1240 for (int t = ii; t >= 0; --t) {
1241 if (memchr(ptr, rep->s[t], n) == 0) return t;
1247 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1250 lyx::Assert(ptr); // OURS!
1251 TestlyxstringInvariant(this);
1253 size_type ii = min(rep->sz - 1, i);
1254 for (int t = ii; t >= 0; --t) {
1255 if (strchr(ptr, rep->s[t]) == 0) return t;
1261 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1264 TestlyxstringInvariant(this);
1266 size_type ii = min(rep->sz - 1, i);
1267 for (int t = ii; t >= 0; --t) {
1268 if (rep->s[t] != c) return t;
1278 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1280 lyx::Assert(i <= rep->sz); // OURS!
1281 TestlyxstringInvariant(this);
1283 return replace(i, n, x, 0, x.rep->sz);
1287 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1288 size_type i2, size_type n2)
1290 lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1291 TestlyxstringInvariant(this);
1293 rep = rep->get_own_copy();
1294 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1299 lyxstring & lyxstring::replace(size_type i, size_type n,
1300 value_type const * p, size_type n2)
1302 lyx::Assert(p && i <= rep->sz); // OURS!
1303 TestlyxstringInvariant(this);
1305 rep = rep->get_own_copy();
1306 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1311 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1313 lyx::Assert(p && i <= rep->sz); // OURS!
1314 TestlyxstringInvariant(this);
1316 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1320 lyxstring & lyxstring::replace(size_type i, size_type n,
1321 size_type n2, value_type c)
1323 lyx::Assert(i <= rep->sz); // OURS!
1324 TestlyxstringInvariant(this);
1326 rep = rep->get_own_copy();
1327 value_type * tmp = new value_type[n2];
1329 rep->replace(i, min(n, rep->sz), tmp, n2);
1335 /// FY! FY! FY! go away !
1336 lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
1338 return replace(i, n, 1, c);
1342 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1344 TestlyxstringInvariant(this);
1346 return replace(i - begin(), i2 - i, str);
1350 lyxstring & lyxstring::replace(iterator i, iterator i2,
1351 value_type const * p, size_type n)
1353 lyx::Assert(p); // OURS!
1354 TestlyxstringInvariant(this);
1356 return replace(i - begin(), i2 - i, p, n);
1360 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1362 lyx::Assert(p); // OURS!
1363 TestlyxstringInvariant(this);
1365 return replace(i - begin(), i2 - i, p);
1369 lyxstring & lyxstring::replace(iterator i, iterator i2,
1370 size_type n , value_type c)
1372 TestlyxstringInvariant(this);
1374 return replace(i - begin(), i2 - i, n, c);
1378 lyxstring & lyxstring::replace(iterator i, iterator i2,
1379 iterator j, iterator j2)
1381 TestlyxstringInvariant(this);
1383 return replace(i - begin(), i2 - i, j, j2 - j);
1387 void lyxstring::swap(lyxstring & str)
1389 if (rep == str.rep) return;
1390 Srep * tmp = str.rep;
1396 lyxstring & lyxstring::erase(size_type i, size_type n)
1398 lyx::Assert(i <= rep->sz); // STD!
1399 TestlyxstringInvariant(this);
1401 rep = rep->get_own_copy();
1402 if (i == 0 && n >= rep->sz) {
1405 n = min(n, rep->sz - i);
1406 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1413 lyxstring::iterator lyxstring::erase(iterator i)
1415 TestlyxstringInvariant(this);
1417 // what iterator is this supposed to return?
1418 // the iterator after the one erased
1419 erase(i - begin(), 1);
1420 return begin(); // BUG
1424 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1426 TestlyxstringInvariant(this);
1428 erase(first - begin(), last - first);
1429 return begin(); // BUG
1433 /////////////////////////////////////
1434 // Conversion to C-style Strings
1435 /////////////////////////////////////
1437 lyxstring::value_type const * lyxstring::c_str() const
1439 rep->s[length()] = '\0';
1444 lyxstring::value_type const * lyxstring::data() const
1450 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1451 size_type pos) const
1453 lyx::Assert(buf); // OURS!
1454 lyx::Assert(pos <= rep->sz); // STD!
1455 TestlyxstringInvariant(this);
1457 register int nn = min(len, length() - pos);
1458 memcpy(buf, &(rep->s[pos]), nn);
1463 ////////////////////
1465 ////////////////////
1467 // Compare funcs should be verified.
1469 int lyxstring::internal_compare(size_type pos, size_type n,
1470 value_type const * s,
1471 size_type slen, size_type n2) const
1473 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1475 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1477 // remember that n can very well be a lot larger than rep->sz
1478 // so we have to ensure that n is no larger than rep->sz
1479 n = min(n, rep->sz);
1482 return memcmp(&(rep->s[pos]), s, n);
1483 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1492 int lyxstring::compare(lyxstring const & str) const
1494 TestlyxstringInvariant(this);
1495 return internal_compare(0, rep->sz, str.rep->s,
1496 str.rep->sz, str.rep->sz);
1500 int lyxstring::compare(value_type const * s) const
1502 lyx::Assert(s); //OURS!
1503 TestlyxstringInvariant(this);
1504 int n = (!s) ? 0 : strlen(s);
1505 return internal_compare(0, rep->sz, s, n, n);
1509 int lyxstring::compare(size_type pos, size_type n,
1510 lyxstring const & str) const
1512 lyx::Assert(pos <= rep->sz); // OURS!
1513 TestlyxstringInvariant(this);
1514 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1518 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1519 size_type pos2, size_type n2) const
1521 lyx::Assert(pos <= rep->sz); // OURS!
1522 lyx::Assert(pos2 <= str.rep->sz); // OURS!
1523 TestlyxstringInvariant(this);
1524 return internal_compare(pos, n,
1526 str.rep->sz - pos2, n2);
1530 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1533 lyx::Assert(s && pos <= rep->sz); // OURS!
1534 TestlyxstringInvariant(this);
1535 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1543 // i = index, n = length
1544 lyxstring lyxstring::substr(size_type i, size_type n) const
1546 lyx::Assert(i <= rep->sz); // STD!
1547 TestlyxstringInvariant(this);
1549 return lyxstring(*this, i, n);
1553 /////////////////////////////////////////////
1554 // String operators, non member functions
1555 /////////////////////////////////////////////
1557 bool operator==(lyxstring const & a, lyxstring const & b)
1559 return a.compare(b) == 0;
1563 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1565 lyx::Assert(a); // OURS!
1566 return b.compare(a) == 0;
1570 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1572 lyx::Assert(b); // OURS!
1573 return a.compare(b) == 0;
1577 bool operator!=(lyxstring const & a, lyxstring const & b)
1579 return a.compare(b) != 0;
1583 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1585 lyx::Assert(a); // OURS!
1586 return b.compare(a) != 0;
1590 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1592 lyx::Assert(b); // OURS!
1593 return a.compare(b) != 0;
1597 bool operator>(lyxstring const & a, lyxstring const & b)
1599 return a.compare(b) > 0;
1603 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1605 lyx::Assert(a); // OURS!
1606 return b.compare(a) < 0; // since we reverse the parameters
1610 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1612 lyx::Assert(b); // OURS!
1613 return a.compare(b) > 0;
1617 bool operator<(lyxstring const & a, lyxstring const & b)
1619 return a.compare(b) < 0;
1623 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1625 lyx::Assert(a); // OURS!
1626 return b.compare(a) > 0; // since we reverse the parameters
1630 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1632 lyx::Assert(b); // OURS!
1633 return a.compare(b) < 0;
1637 bool operator>=(lyxstring const & a, lyxstring const & b)
1639 return a.compare(b) >= 0;
1643 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1645 lyx::Assert(a); // OURS!
1646 return b.compare(a) <= 0; // since we reverse the parameters
1650 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1652 lyx::Assert(b); // OURS!
1653 return a.compare(b) >= 0;
1657 bool operator<=(lyxstring const & a, lyxstring const & b)
1659 return a.compare(b) <= 0;
1663 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1665 lyx::Assert(a); // OURS!
1666 return b.compare(a) >= 0; // since we reverse the parameters
1670 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1672 lyx::Assert(b); // OURS!
1673 return a.compare(b) <= 0;
1677 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1685 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1687 lyx::Assert(a); // OURS!
1694 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1703 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1705 lyx::Assert(b); // OURS!
1712 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1720 void swap(lyxstring & str1, lyxstring & str2)
1728 istream & operator>>(istream & is, lyxstring & s)
1731 // very bad solution
1732 char * nome = new char[1024];
1734 lyxstring tmp(nome);
1736 if (!tmp.empty()) s = tmp;
1739 int w = is.widdth(0);
1743 if (isspace(c)) { is.putback(c); break; }
1745 if (--w == 1) break;
1747 if (s.empty()) is.setstate(ios::failbit);
1753 ostream & operator<<(ostream & o, lyxstring const & s)
1755 return o.write(s.data(), s.length());
1759 istream & getline(istream & is, lyxstring & s,
1760 lyxstring::value_type delim)
1762 // very bad solution
1778 lyxstring a = "abcac";
1779 cout << a.rfind("ab") << endl;
1780 cout << a.rfind("c") << endl;
1781 cout << a.rfind("d") << endl;