1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1999 The LyX Team.
9 * ====================================================== */
16 #pragma implementation "lyxstring.h"
19 //#include "LString.h"
20 #include "lyxstring.h"
29 // This class is supposed to be functionaly equivalent to a
30 // standard conformant string. This mean among others that we
31 // are useing the same requirements. Before you change anything
32 // in this file consult me and/or the standard to discover the
35 // Asserts with a STD! are required by the standard.
36 // Asserts with a OURS! are added by me.
37 // Some asserts could still be missing and some of the existing
38 // ones might be wrong or not needed.
40 // Reference count has been checked, empty_rep removed and
41 // introduced again in a similar guise. Where is empty_rep _really_
44 // We are missing a couple of imporant things from the standard:
45 // reverse iterators and methods taking InputIterators as paramters.
46 // Also the methods returning iterators is returning the wrong value.
48 // All the different find functions need a good look over.
49 // I have so far not tested them extensively and would be
50 // happy if others took the time to have a peek.
52 // Space allocation of string.
53 // I have tried to do this very simple without using any special tricks.
54 // Earlier we used a fixed value to enlarge the string with this would
55 // cause a lot of reallocations with large strings (especially if
56 // push_back was used) and wasting space for very small strings.
57 // I have now changed the allocation to use a doubling of reserved
58 // space until it is large enough. So far tests show a small speed
59 // increase and a noticable memory saving.
65 // I have no clue why this function is needed at all, it is static and
66 // confined to this filescope. How can any other file/fuction/module
67 // get access to it at all?? Perhaps the forward declaration in LString.h
68 // is enough. ARRae, any clues? Lgb.
70 #ifdef HAVE_STL_STRING_FWD_H
71 // SGI's STL > 3.13 expects string to provide __get_c_string.
72 // Due to a clash with SGI's forward declaration of string we have
73 // to provide this ourselves and block their string declarations
74 // as best we can. ARRae.
76 static char const * __get_c_string(string const & s)
84 ///////////////////////////////////////
85 // The internal string representation
86 ///////////////////////////////////////
88 struct lyxstring::Srep {
93 /// The total amount of data reserved for this representaion
95 /// Data. At least 1 char for trailing null.
96 lyxstring::value_type * s;
99 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
101 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
103 ~Srep() { delete[] s; }
105 Srep * get_own_copy() {
106 if (ref == 1) return this;
108 return new Srep(sz, s);
112 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
114 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
116 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
118 void push_back(lyxstring::value_type c);
120 void insert(lyxstring::size_type pos,
121 const lyxstring::value_type * p,
122 lyxstring::size_type n);
124 void resize(lyxstring::size_type n, lyxstring::value_type c);
126 void reserve(lyxstring::size_type res_arg);
128 void replace(lyxstring::size_type i, lyxstring::size_type n,
129 lyxstring::value_type const * p, lyxstring::size_type n2);
132 Srep & operator=(const Srep &);
136 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
138 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
143 s = new value_type[res + 1]; // add space for terminator
145 // if sz = 0 nothing gets copied and we have an error
148 // possibly allows for large but empty string
149 sz = 0; // this line should be redundant
155 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
160 s = new value_type[res + 1]; // add space for terminator
163 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
164 // allows for large but empty string
170 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
172 // can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
178 s = new value_type[res + 1]; // add space for terminator
183 // if sz = 0 nothing gets copied and we have an error
187 sz = 0; // this line should be redundant
193 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
199 s = new value_type[res + 1]; // add space for terminator
203 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
204 // allows for a large empty string
210 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
212 register unsigned int const len = sz + asz;
217 value_type * tmp = new value_type[res + 1];
219 memcpy(tmp + sz, p, asz);
224 memcpy(s + sz, p, asz);
230 void lyxstring::Srep::push_back(value_type c)
232 s[sz] = c; // it is always room to put a value_type at the end
238 value_type * tmp = new value_type[res + 1];
246 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
247 lyxstring::size_type n)
252 } while (res < n + sz);
253 value_type * tmp = new value_type[res + 1];
255 memcpy(tmp + pos, p, n);
256 memcpy(tmp + pos + n, &s[pos], sz - pos);
261 memmove(s + pos + n, &s[pos], sz - pos);
262 memcpy(s + pos, p, n);
268 void lyxstring::Srep::resize(size_type n, value_type c)
270 // This resets sz to res_arg
271 res = min(n, npos - 2); // We keep no xtra when we resize
272 value_type * tmp = new value_type[res + 1];
273 memcpy(tmp, s, min(sz, res));
275 memset(tmp + sz, c, res - sz);
282 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
284 // This keeps the old sz, but
285 // increases res with res_arg
287 value_type * tmp = new value_type[res + 1];
294 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
295 value_type const * p, size_type n2)
297 // can be called with p= 0 and n2= 0
300 if (res >= n2 + sz) {
301 memmove(s + i + n2, &s[i + n], sz - i);
302 memcpy(s + i, p, n2);
307 } while (res < n2 + sz);
308 value_type * tmp = new value_type[res + 1];
310 memcpy(tmp + i, p, n2);
311 memcpy(tmp + i + n2, &s[i + n], sz - i);
319 ///////////////////////////////////////
320 // The lyxstring Invariant tester
321 ///////////////////////////////////////
324 /** Testing of the lyxstring invariant
325 * By creating an object that tests the lyxstring invariant during its
326 * construction *and* its deconstruction we greatly simplify our code.
327 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
328 * will test the invariant upon entry to the code. If the Asserts fail
329 * then we know from the stack trace that the corruption occurred *before*
330 * entry to this method. We can also be sure it didn't happen in any of
331 * the tested lyxstring methods. It is therefore likely to be due to some
332 * other external force.
333 * Several lyxstring methods have multiple exit points which would otherwise
334 * require us to insert a separate test before each return. But since we
335 * created an object its destructor will be called upon exit (any exit!).
336 * We thus get testing at both start and end of a method with one line of
337 * code at the head of a method. More importantly, we get good testing
338 * everytime we run the code.
339 * NOTE: just because we test the invariant doesn't mean we can forget
340 * about testing pre and post conditions specific to any given method.
341 * This test simply proves that the lyxstring/Srep is in a valid state it
342 * does *not* prove that the method did what it was supposed to.
344 class lyxstringInvariant {
346 lyxstringInvariant(lyxstring const *);
347 ~lyxstringInvariant();
350 lyxstring const * object;
354 // To test if this scheme works "as advertised" uncomment the printf's in
355 // the constructor and destructor below and then uncomment the printf and the
356 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
357 // The correct output when LyX has been recompiled and run is:
358 // lyxstringInvariant constructor
359 // lyxstring::operator=(char const *)
360 // lyxstringInvariant constructor
361 // lyxstringInvariant destructor completed
362 // lyxstringInvariant destructor completed
363 // NOTE: The easiest way to catch this snippet of the output is to wait for
364 // the splash screen to disappear and then open and close Help->Credits
366 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
368 // printf("lyxstringInvariant constructor\n");
373 lyxstringInvariant::~lyxstringInvariant()
376 // printf("lyxstringInvariant destructor completed\n");
380 void lyxstringInvariant::helper() const
382 // Some of these tests might look pointless but they are
383 // all part of the invariant and if we want to make sure
384 // we have a bullet proof implementation then we need to
385 // test every last little thing we *know* should be true.
386 // I may have missed a test or two, so feel free to fill
387 // in the gaps. ARRae.
390 Assert(object->rep->s); // s is never 0
391 Assert(object->rep->res); // res cannot be 0
392 Assert(object->rep->sz <= object->rep->res);
393 Assert(object->rep->ref >= 1); // its in use so it must be referenced
394 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
395 // if it does ever == then we should be generating a new copy
396 // and starting again. (Is char always 8-bits?)
398 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
400 #define TestlyxstringInvariant(s)
401 #endif //DEVEL_VERSION
404 ///////////////////////////////////////
405 // Constructors and Deconstructors.
406 ///////////////////////////////////////
408 lyxstring::size_type const lyxstring::npos =
409 static_cast<lyxstring::size_type>(-1);
412 lyxstring::lyxstring()
414 static Srep empty_rep(0, "");
420 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
422 Assert(pos <= x.rep->sz); // STD!
423 if (pos == 0 && n >= x.length()) { // this is the default
427 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
432 lyxstring::lyxstring(value_type const * s, size_type n)
434 Assert(s && n < npos); // STD!
435 static Srep empty_rep(0, "");
436 if (*s && n) { // s is not empty string and n > 0
437 rep = new Srep(min(strlen(s), n), s);
445 lyxstring::lyxstring(value_type const * s)
448 static Srep empty_rep(0, "");
449 if (*s) { // s is not empty string
450 rep = new Srep(strlen(s), s);
458 lyxstring::lyxstring(size_type n, value_type c)
460 Assert(n < npos); // STD!
461 rep = new Srep(n, c);
465 lyxstring::lyxstring(const_iterator first, const_iterator last)
467 rep = new Srep(last - first, first);
471 lyxstring::~lyxstring()
473 if (--rep->ref == 0) delete rep;
476 ///////////////////////
478 ///////////////////////
480 lyxstring::iterator lyxstring::begin()
486 lyxstring::const_iterator lyxstring::begin() const
492 lyxstring::iterator lyxstring::end()
494 return rep->s + rep->sz;
498 lyxstring::const_iterator lyxstring::end() const
500 return rep->s + rep->sz;
504 reverse_iterator lyxstring::rbegin()
506 return reverse_iterator( end() );
510 const_reverse_iterator lyxstring::rbegin() const
512 return const_reverse_iterator( end() );
516 reverse_iterator lyxstring::rend()
518 return reverse_iterator( begin() );
522 const_reverse_iterator lyxstring::rend() const
524 return const_reverse_iterator( begin() );
529 ///////////////////////
531 ///////////////////////
533 lyxstring::size_type lyxstring::size() const
539 void lyxstring::resize(size_type n, value_type c)
541 Assert(n <= npos); // STD!
542 TestlyxstringInvariant(this);
544 // This resets sz to res_arg
545 rep = rep->get_own_copy();
550 lyxstring::size_type lyxstring::capacity() const
556 void lyxstring::reserve(size_type res_arg)
558 TestlyxstringInvariant(this);
560 rep = rep->get_own_copy();
561 rep->reserve(res_arg);
569 lyxstring & lyxstring::operator= (lyxstring const & x)
571 TestlyxstringInvariant(this);
577 lyxstring & lyxstring::operator= (value_type const * s)
580 TestlyxstringInvariant(this);
581 // printf("lyxstring::operator= (value_type const *)\n");
587 lyxstring & lyxstring::operator=(value_type c)
589 TestlyxstringInvariant(this);
593 if (rep->ref == 1) // recycle rep
597 rep = new Srep(1, s);
603 lyxstring & lyxstring::assign(lyxstring const & x)
605 TestlyxstringInvariant(this);
607 x.rep->ref++; // protect against ``st = st''
608 if (--rep->ref == 0) delete rep;
609 rep = x.rep; // share representation
614 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
616 Assert(pos <= x.rep->sz); // STD!
617 TestlyxstringInvariant(this);
619 return assign(x.substr(pos, n));
623 lyxstring & lyxstring::assign(value_type const * s, size_type n)
626 TestlyxstringInvariant(this);
628 n = min(strlen(s), n);
629 if (rep->ref == 1) // recycle rep
633 rep = new Srep(n, s);
639 lyxstring & lyxstring::assign(value_type const * s)
642 TestlyxstringInvariant(this);
644 return assign(s, strlen(s));
648 lyxstring & lyxstring::assign(size_type n, value_type ch)
650 TestlyxstringInvariant(this);
652 rep = rep->get_own_copy();
658 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
660 TestlyxstringInvariant(this);
662 rep = rep->get_own_copy();
663 rep->assign(last - first, first);
672 lyxstring::const_reference lyxstring::operator[](size_type pos) const
674 Assert(pos <= rep->sz); // OURS!
675 static char helper = '\0';
676 return pos == rep->sz ? helper : rep->s[pos];
680 lyxstring::reference lyxstring::operator[](size_type pos)
682 Assert(pos < rep->sz); // OURS!
683 TestlyxstringInvariant(this);
685 rep = rep->get_own_copy();
690 lyxstring::const_reference lyxstring::at(size_type n) const
692 Assert(n < rep->sz); // STD!
697 lyxstring::reference lyxstring::at(size_type n)
699 Assert(n < rep->sz); // STD!
700 TestlyxstringInvariant(this);
702 rep = rep->get_own_copy();
711 lyxstring & lyxstring::operator+=(lyxstring const & x)
713 TestlyxstringInvariant(this);
719 lyxstring & lyxstring::operator+=(value_type const * x)
722 TestlyxstringInvariant(this);
728 lyxstring & lyxstring::operator+=(value_type c)
730 TestlyxstringInvariant(this);
737 void lyxstring::push_back(value_type c)
739 TestlyxstringInvariant(this);
741 rep = rep->get_own_copy();
746 lyxstring & lyxstring::append(lyxstring const & x)
748 TestlyxstringInvariant(this);
750 if (x.empty()) return *this;
751 rep = rep->get_own_copy();
752 rep->append(x.length(), x.rep->s);
757 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
759 Assert(pos <= x.rep->sz); // STD!
760 TestlyxstringInvariant(this);
762 return append(x.substr(pos, n));
766 lyxstring & lyxstring::append(value_type const * p, size_type n)
769 TestlyxstringInvariant(this);
771 if (!*p || !n) return *this;
772 rep = rep->get_own_copy();
773 rep->append(min(n, strlen(p)), p);
778 lyxstring & lyxstring::append(value_type const * p)
781 TestlyxstringInvariant(this);
783 if (!*p) return *this;
784 rep = rep->get_own_copy();
785 rep->append(strlen(p), p);
790 lyxstring & lyxstring::append(size_type n, value_type c)
792 TestlyxstringInvariant(this);
794 value_type * tmp = new value_type[n];
796 rep = rep->get_own_copy();
803 lyxstring & lyxstring::append(iterator first, iterator last)
805 TestlyxstringInvariant(this);
807 rep = rep->get_own_copy();
808 rep->append(last - first, first);
812 // insert value_typeacters before (*this)[pos]
814 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
816 TestlyxstringInvariant(this);
818 return insert(pos, x, 0, x.rep->sz);
822 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
823 size_type pos2, size_type n)
825 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
826 TestlyxstringInvariant(this);
828 rep = rep->get_own_copy();
829 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
834 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
837 TestlyxstringInvariant(this);
840 // insert nothing and you change nothing
841 rep = rep->get_own_copy();
842 rep->insert(pos, p, min(n, strlen(p)));
848 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
851 TestlyxstringInvariant(this);
854 // insert nothing and you change nothing
855 rep = rep->get_own_copy();
856 rep->insert(pos, p, strlen(p));
862 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
864 TestlyxstringInvariant(this);
866 rep = rep->get_own_copy();
867 value_type * tmp = new value_type[n];
869 rep->insert(pos, tmp, n);
875 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
877 TestlyxstringInvariant(this);
879 // what iterator is this supposed to return??
880 size_type tmp = p - begin();
881 insert(p - begin(), 1, c);
882 return begin() + tmp + 1; // ??
886 void lyxstring::insert(iterator p, size_type n , value_type c)
888 TestlyxstringInvariant(this);
890 insert(p - begin(), n , c);
894 void lyxstring::insert(iterator p, iterator first, iterator last)
896 TestlyxstringInvariant(this);
898 insert(p - begin(), first, last - first);
906 // All the below find functions should be verified,
907 // it is very likely that I have mixed up or interpreted
908 // some of the parameters wrong, also some of the funcs can surely
909 // be written more effectively.
911 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
913 if (!rep->sz || i >= rep->sz) return npos;
915 TestlyxstringInvariant(this);
917 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
918 // search until (*this)[i] == a[0]
919 if (rep->s[t] == a[0]) {
920 // check if the rest of the value_types match
922 for (size_type j = 0; j < a.length(); ++j) {
923 if (rep->s[t + j] != a[j]) {
935 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
938 Assert(ptr); // OURS!
939 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
941 TestlyxstringInvariant(this);
943 // What is "n" here? is it the number of value_types to use in ptr
944 // or does "i" and "n" togeter form a substring to search
945 // for ptr in? For now I will assume that "n" tells the length
947 n = min(n, strlen(ptr));
948 for (size_type t = i; rep->sz - t >= n; ++t) {
949 // search until (*this)[i] == a[0]
950 if (rep->s[t] == ptr[0]) {
951 // check if the rest of the value_types match
953 for (size_type j = 0; j < n; ++j) {
954 if (rep->s[t + j] != ptr[j]) {
966 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
969 if (!rep->sz || i >= rep->sz) return npos;
971 TestlyxstringInvariant(this);
973 if (!s || !*s) return npos;
974 return find(s, i, strlen(s));
978 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
980 if (!rep->sz || i >= rep->sz) return npos;
982 TestlyxstringInvariant(this);
984 for (size_type t = 0; t + i < rep->sz; ++t) {
985 if (rep->s[t + i] == c) return t + i;
991 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
993 TestlyxstringInvariant(this);
995 size_type ii = min(rep->sz - 1, i);
997 if (a[a.length() - 1] == rep->s[ii]) {
999 size_type l = ii - 1;
1000 for (; t >= 0; --t, --l) {
1001 if (a[t] != rep->s[l]) break;
1003 if (a[t] == rep->s[l]) return l;
1010 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1013 Assert(ptr); // OURS!
1014 TestlyxstringInvariant(this);
1015 if (!*ptr) return npos;
1017 size_type ii = min(rep->sz - 1, i);
1019 if (ptr[n - 1] == rep->s[ii]) {
1021 size_type l = ii - 1;
1022 for (; t >= 0; --t, --l) {
1023 if (ptr[t] != rep->s[l]) break;
1025 if (ptr[t] == rep->s[l]) return l;
1032 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1035 Assert(ptr); // OURS!
1036 TestlyxstringInvariant(this);
1037 if (!*ptr) return npos;
1039 size_type ii = min(rep->sz - 1, i);
1041 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1042 int t = strlen(ptr) - 2;
1043 size_type l = ii - 1;
1044 for (; t >= 0; --t, --l) {
1045 if (ptr[t] != rep->s[l]) break;
1047 if (ptr[t] == rep->s[l]) return l;
1054 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1056 TestlyxstringInvariant(this);
1058 size_type ii = min(rep->sz - 1, i);
1059 for (size_type t = ii; t != 0; --t) {
1060 if (rep->s[t] == c) return t;
1066 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1069 Assert(i < rep->sz); // OURS!
1070 TestlyxstringInvariant(this);
1072 for (size_type t = i; t < rep->sz; ++t) {
1073 if (a.find(rep->s[t]) != npos) return t;
1079 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1083 Assert(ptr && i < rep->sz); // OURS!
1084 TestlyxstringInvariant(this);
1085 if (!n) return npos;
1087 for (size_type t = i; t < rep->sz; ++t) {
1088 if(memchr(ptr, rep->s[t], n) != 0) return t;
1094 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1097 Assert(ptr && i < rep->sz); // OURS!
1098 TestlyxstringInvariant(this);
1100 for (size_type t = i; t < rep->sz; ++t) {
1101 if (strchr(ptr, rep->s[t]) != 0) return t;
1107 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1109 Assert(i < rep->sz); // OURS!
1110 TestlyxstringInvariant(this);
1112 for (size_type t = i; t < rep->sz; ++t) {
1113 if (rep->s[t] == c) return t;
1119 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1122 TestlyxstringInvariant(this);
1124 size_type ii = min(rep->sz - 1, i);
1125 for (int t = ii; t >= 0; --t) {
1126 if (a.find(rep->s[t]) != npos) return t;
1132 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1136 Assert(ptr); // OURS!
1137 TestlyxstringInvariant(this);
1138 if (!n) return npos;
1140 size_type ii = min(rep->sz - 1, i);
1141 for (int t = ii; t >= 0; --t) {
1142 if(memchr(ptr, rep->s[t], n) != 0) return t;
1148 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1151 Assert(ptr); // OURS!
1152 TestlyxstringInvariant(this);
1154 size_type ii = min(rep->sz - 1, i);
1155 for (int t = ii; t >= 0; --t) {
1156 if (strchr(ptr, rep->s[t]) != 0) return t;
1162 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1164 TestlyxstringInvariant(this);
1166 if (!rep->sz) return npos;
1167 size_type ii = min(rep->sz - 1, i);
1168 for (int t = ii; t >= 0; --t) {
1169 if (rep->s[t] == c) return t;
1175 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1178 TestlyxstringInvariant(this);
1180 if (!rep->sz) return npos;
1181 Assert(i < rep->sz);
1182 for (size_type t = i; t < rep->sz; ++t) {
1183 if (a.find(rep->s[t]) == npos) return t;
1189 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1193 Assert(ptr && i < rep->sz); // OURS!
1194 TestlyxstringInvariant(this);
1196 if (!n) return (i < rep->sz) ? i : npos;
1197 for (size_type t = i; t < rep->sz; ++t) {
1198 if(memchr(ptr, rep->s[t], n) == 0) return t;
1204 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1207 Assert(ptr && i < rep->sz); // OURS!
1208 TestlyxstringInvariant(this);
1210 for (size_type t = i; t < rep->sz; ++t) {
1211 if (strchr(ptr, rep->s[t]) == 0) return t;
1217 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1220 if (!rep->sz) return npos;
1221 Assert(i < rep->sz); // OURS!
1222 TestlyxstringInvariant(this);
1224 for (size_type t = i; t < rep->sz; ++t) {
1225 if (rep->s[t] != c) return t;
1231 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1234 TestlyxstringInvariant(this);
1236 size_type ii = min(rep->sz - 1, i);
1237 for (int t = ii; t >= 0; --t) {
1238 if (a.find(rep->s[t]) == npos) return t;
1244 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1248 Assert(ptr); // OURS!
1249 TestlyxstringInvariant(this);
1251 if (!n) return npos;
1252 size_type ii = min(rep->sz - 1, i);
1254 for (int t = ii; t >= 0; --t) {
1255 if(memchr(ptr, rep->s[t], n) == 0) return t;
1261 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1264 Assert(ptr); // OURS!
1265 TestlyxstringInvariant(this);
1267 size_type ii = min(rep->sz - 1, i);
1268 for (int t = ii; t >= 0; --t) {
1269 if (strchr(ptr, rep->s[t]) == 0) return t;
1275 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1278 TestlyxstringInvariant(this);
1280 size_type ii = min(rep->sz - 1, i);
1281 for (int t = ii; t >= 0; --t) {
1282 if (rep->s[t] != c) return t;
1292 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1294 Assert(i <= rep->sz); // OURS!
1295 TestlyxstringInvariant(this);
1297 return replace(i, n, x, 0, x.rep->sz);
1301 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1302 size_type i2, size_type n2)
1304 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1305 TestlyxstringInvariant(this);
1307 rep = rep->get_own_copy();
1308 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1313 lyxstring & lyxstring::replace(size_type i, size_type n,
1314 value_type const * p, size_type n2)
1316 Assert(p && i < rep->sz); // OURS!
1317 TestlyxstringInvariant(this);
1319 rep = rep->get_own_copy();
1320 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1325 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1327 Assert(p && i < rep->sz); // OURS!
1328 TestlyxstringInvariant(this);
1330 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1334 lyxstring & lyxstring::replace(size_type i, size_type n,
1335 size_type n2, value_type c)
1337 Assert(i < rep->sz); // OURS!
1338 TestlyxstringInvariant(this);
1340 rep = rep->get_own_copy();
1341 value_type * tmp = new value_type[n2];
1343 rep->replace(i, min(n, rep->sz), tmp, n2);
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)
1361 TestlyxstringInvariant(this);
1363 return replace(i - begin(), i2 - i, p, n);
1367 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
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 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 Assert(buf); // OURS!
1461 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
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 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 Assert(pos <= rep->sz); // OURS!
1529 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 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 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)
1573 return b.compare(a) == 0;
1577 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
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)
1593 return b.compare(a) != 0;
1597 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
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)
1613 return b.compare(a) < 0; // since we reverse the parameters
1617 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
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)
1633 return b.compare(a) > 0; // since we reverse the parameters
1637 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
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)
1653 return b.compare(a) <= 0; // since we reverse the parameters
1657 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
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)
1673 return b.compare(a) >= 0; // since we reverse the parameters
1677 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
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)
1701 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1710 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
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.widdth(0);
1750 if (isspace(c)) { is.putback(c); break; }
1752 if (--w == 1) break;
1754 if (s.empty()) is.setstate(ios::failbit);
1760 ostream & operator<<(ostream & o, lyxstring const & s)
1762 return o.write(s.data(), s.length());
1766 istream & getline(istream & is, lyxstring & s,
1767 lyxstring::value_type delim)
1769 // very bad solution