3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
15 #include "lyxstring.h"
30 // This class is supposed to be functionaly equivalent to a
31 // standard conformant string. This mean among others that we
32 // are useing the same requirements. Before you change anything
33 // in this file consult me and/or the standard to discover the
36 // Asserts with a STD! are required by the standard.
37 // Asserts with a OURS! are added by me.
38 // Some asserts could still be missing and some of the existing
39 // ones might be wrong or not needed.
41 // Reference count has been checked, empty_rep removed and
42 // introduced again in a similar guise. Where is empty_rep _really_
45 // We are missing a couple of imporant things from the standard:
46 // reverse iterators and methods taking InputIterators as paramters.
47 // Also the methods returning iterators is returning the wrong value.
49 // All the different find functions need a good look over.
50 // I have so far not tested them extensively and would be
51 // happy if others took the time to have a peek.
53 // Space allocation of string.
54 // I have tried to do this very simple without using any special tricks.
55 // Earlier we used a fixed value to enlarge the string with this would
56 // cause a lot of reallocations with large strings (especially if
57 // push_back was used) and wasting space for very small strings.
58 // I have now changed the allocation to use a doubling of reserved
59 // space until it is large enough. So far tests show a small speed
60 // increase and a noticable memory saving.
66 using support::Assert;
68 ///////////////////////////////////////
69 // The internal string representation
70 ///////////////////////////////////////
77 /// The total amount of data reserved for this representaion
79 /// Data. At least 1 char for trailing null.
80 string::value_type * s;
83 Srep(string::size_type nsz, const string::value_type * p);
85 Srep(string::size_type nsz, string::value_type ch);
87 ~Srep() { delete[] s; }
89 Srep * get_own_copy() {
90 if (ref == 1) return this;
92 return new Srep(sz, s);
96 void assign(string::size_type nsz, const string::value_type * p);
98 void assign(string::size_type nsz, string::value_type ch);
100 void append(string::size_type asz, const string::value_type * p);
102 void push_back(string::value_type c);
104 void insert(string::size_type pos,
105 const string::value_type * p,
106 string::size_type n);
108 void resize(string::size_type n, string::value_type c);
110 void reserve(string::size_type res_arg);
112 void replace(string::size_type i, string::size_type n,
113 string::value_type const * p, string::size_type n2);
116 Srep & operator=(const Srep &);
120 string::Srep::Srep(string::size_type nsz, const value_type * p)
122 // can be called with p == 0 by
123 // string::assign(const value_type *, size_type)
128 s = new value_type[res + 1]; // add space for terminator
130 // if sz = 0 nothing gets copied and we have an error
133 // possibly allows for large but empty string
134 sz = 0; // this line should be redundant
140 string::Srep::Srep(string::size_type nsz, value_type ch)
145 s = new value_type[res + 1]; // add space for terminator
148 // if ch == '\0' strlen(string.c_str()) == 0 so sz = 0
149 // allows for large but empty string
155 void string::Srep::assign(string::size_type nsz, const value_type * p)
157 // can be called with p == 0
158 // by string::assign(const value_type *, size_type)
164 s = new value_type[res + 1]; // add space for terminator
169 // if sz = 0 nothing gets copied and we have an error
173 sz = 0; // this line should be redundant
179 void string::Srep::assign(string::size_type nsz, value_type ch)
185 s = new value_type[res + 1]; // add space for terminator
189 // if ch == '\0' strlen(string.c_str()) == 0 so sz = 0
190 // allows for a large empty string
196 void string::Srep::append(string::size_type asz, const value_type * p)
198 register unsigned int const len = sz + asz;
203 value_type * tmp = new value_type[res + 1];
205 memcpy(tmp + sz, p, asz);
210 memcpy(s + sz, p, asz);
216 void string::Srep::push_back(value_type c)
218 s[sz] = c; // it is always room to put a value_type at the end
224 value_type * tmp = new value_type[res + 1];
232 void string::Srep::insert(string::size_type pos, const value_type * p,
238 } while (res < n + sz);
239 value_type * tmp = new value_type[res + 1];
241 memcpy(tmp + pos, p, n);
242 memcpy(tmp + pos + n, &s[pos], sz - pos);
247 memmove(s + pos + n, &s[pos], sz - pos);
248 memcpy(s + pos, p, n);
254 void string::Srep::resize(size_type n, value_type c)
256 // This resets sz to res_arg
257 res = min(n, npos - 2); // We keep no xtra when we resize
258 value_type * tmp = new value_type[res + 1];
259 memcpy(tmp, s, min(sz, res));
261 memset(tmp + sz, c, res - sz);
268 void string::Srep::reserve(string::size_type res_arg)
270 // This keeps the old sz, but
271 // increases res with res_arg
273 value_type * tmp = new value_type[res + 1];
280 void string::Srep::replace(string::size_type i, string::size_type n,
281 value_type const * p, size_type n2)
283 // can be called with p= 0 and n2= 0
286 if (res >= n2 + sz) {
287 memmove(s + i + n2, &s[i + n], sz - i);
288 memcpy(s + i, p, n2);
293 } while (res < n2 + sz);
294 value_type * tmp = new value_type[res + 1];
296 memcpy(tmp + i, p, n2);
297 memcpy(tmp + i + n2, &s[i + n], sz - i);
305 ///////////////////////////////////////
306 // The string Invariant tester
307 ///////////////////////////////////////
309 // There are no know bugs in string now, and it have been
310 // tested for a long time. so we disable the invariant checker. (Lgb)
311 #undef ENABLE_ASSERTIONS
312 #ifdef ENABLE_ASSERTIONS
314 /** Testing of the string invariant
315 * By creating an object that tests the string invariant during its
316 * construction *and* its deconstruction we greatly simplify our code.
317 * Calling TeststringInvariant() upon entry to an string method
318 * will test the invariant upon entry to the code. If the Asserts fail
319 * then we know from the stack trace that the corruption occurred *before*
320 * entry to this method. We can also be sure it didn't happen in any of
321 * the tested string methods. It is therefore likely to be due to some
322 * other external force.
323 * Several string methods have multiple exit points which would otherwise
324 * require us to insert a separate test before each return. But since we
325 * created an object its destructor will be called upon exit (any exit!).
326 * We thus get testing at both start and end of a method with one line of
327 * code at the head of a method. More importantly, we get good testing
328 * everytime we run the code.
329 * NOTE: just because we test the invariant doesn't mean we can forget
330 * about testing pre and post conditions specific to any given method.
331 * This test simply proves that the string/Srep is in a valid state it
332 * does *not* prove that the method did what it was supposed to.
334 class stringInvariant {
336 stringInvariant(string const *);
340 string const * object;
344 // To test if this scheme works "as advertised" uncomment the printf's in
345 // the constructor and destructor below and then uncomment the printf and the
346 // call to TestlyxstringInvariant() in string::operator=(char const *).
347 // The correct output when LyX has been recompiled and run is:
348 // lyxstringInvariant constructor
349 // string::operator=(char const *)
350 // lyxstringInvariant constructor
351 // lyxstringInvariant destructor completed
352 // lyxstringInvariant destructor completed
353 // NOTE: The easiest way to catch this snippet of the output is to wait for
354 // the splash screen to disappear and then open and close Help->Credits
356 stringInvariant::stringInvariant(string const * ls) : object(ls)
358 // printf("stringInvariant constructor\n");
363 stringInvariant::~stringInvariant()
366 // printf("stringInvariant destructor completed\n");
370 void stringInvariant::helper() const
372 // Some of these tests might look pointless but they are
373 // all part of the invariant and if we want to make sure
374 // we have a bullet proof implementation then we need to
375 // test every last little thing we *know* should be true.
376 // I may have missed a test or two, so feel free to fill
377 // 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 TeststringInvariant(s) stringInvariant string_invariant(s);
390 #define TeststringInvariant(s)
391 #endif /* ENABLE_ASSERTIONS */
394 ///////////////////////////////////////
395 // Constructors and Deconstructors.
396 ///////////////////////////////////////
398 string::size_type const string::npos =
399 static_cast<string::size_type>(-1);
404 static Srep empty_rep(0, "");
410 string::string(string const & x, size_type pos, size_type n)
412 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 string::string(value_type const * s, size_type n)
424 Assert(s && n < npos); // STD!
425 static Srep empty_rep(0, "");
427 rep = new Srep(n, s);
435 string::string(value_type const * s)
438 static Srep empty_rep(0, "");
439 if (*s) { // s is not empty string
440 rep = new Srep(strlen(s), s);
448 string::string(size_type n, value_type c)
450 Assert(n < npos); // STD!
451 rep = new Srep(n, c);
455 #warning string user, have a look here. (Lgb)
457 // Commented out to avoid warnings from doxygen. (Lgb)
458 string::string(const_iterator first, const_iterator last)
460 rep = new Srep(last - first, first);
467 if (--rep->ref == 0) delete rep;
470 ///////////////////////
472 ///////////////////////
474 string::iterator string::begin()
476 rep = rep->get_own_copy();
481 string::const_iterator string::begin() const
487 string::iterator string::end()
489 rep = rep->get_own_copy();
490 return rep->s + rep->sz;
494 string::const_iterator string::end() const
496 return rep->s + rep->sz;
500 reverse_iterator string::rbegin()
502 return reverse_iterator( end() );
506 const_reverse_iterator string::rbegin() const
508 return const_reverse_iterator( end() );
512 reverse_iterator string::rend()
514 return reverse_iterator( begin() );
518 const_reverse_iterator string::rend() const
520 return const_reverse_iterator( begin() );
525 ///////////////////////
527 ///////////////////////
529 string::size_type string::size() const
535 void string::resize(size_type n, value_type c)
537 Assert(n <= npos); // STD!
538 TeststringInvariant(this);
540 // This resets sz to res_arg
541 rep = rep->get_own_copy();
546 string::size_type string::capacity() const
552 void string::reserve(size_type res_arg)
554 TeststringInvariant(this);
556 rep = rep->get_own_copy();
557 rep->reserve(res_arg);
565 string & string::operator=(string const & x)
567 TeststringInvariant(this);
573 string & string::operator=(value_type const * s)
576 TeststringInvariant(this);
577 // printf("string::operator= (value_type const *)\n");
583 string & string::operator=(value_type c)
585 TeststringInvariant(this);
589 if (rep->ref == 1) // recycle rep
593 rep = new Srep(1, s);
599 string & string::assign(string const & x)
601 TeststringInvariant(this);
603 x.rep->ref++; // protect against ``st = st''
604 if (--rep->ref == 0) delete rep;
605 rep = x.rep; // share representation
610 string & string::assign(string const & x, size_type pos, size_type n)
612 Assert(pos <= x.rep->sz); // STD!
613 TeststringInvariant(this);
615 return assign(x.substr(pos, n));
619 string & string::assign(value_type const * s, size_type n)
621 Assert(s && n < npos); // STD!
622 TeststringInvariant(this);
624 if (rep->ref == 1) // recycle rep
628 rep = new Srep(n, s);
634 string & string::assign(value_type const * s)
637 TeststringInvariant(this);
639 return assign(s, strlen(s));
643 string & string::assign(size_type n, value_type ch)
645 TeststringInvariant(this);
647 rep = rep->get_own_copy();
653 string & string::assign(const_iterator first, const_iterator last)
655 TeststringInvariant(this);
657 rep = rep->get_own_copy();
658 rep->assign(last - first, first);
667 string::const_reference string::operator[](size_type pos) const
670 // This is actually what the standard requires,
671 Assert(pos <= rep->sz); // OURS!
672 static char const helper = '\0';
673 return pos == rep->sz ? helper : rep->s[pos];
675 // but we use this one since it is stricter
676 // and more according to the real intent of std::string.
677 Assert(pos < rep->sz); // OURS!
683 string::reference string::operator[](size_type pos)
685 Assert(pos < rep->sz); // OURS!
686 TeststringInvariant(this);
688 rep = rep->get_own_copy();
693 string::const_reference string::at(size_type n) const
695 Assert(n < rep->sz); // STD!
700 string::reference string::at(size_type n)
702 Assert(n < rep->sz); // STD!
703 TeststringInvariant(this);
705 rep = rep->get_own_copy();
714 string & string::operator+=(string const & x)
716 TeststringInvariant(this);
722 string & string::operator+=(value_type const * x)
725 TeststringInvariant(this);
731 string & string::operator+=(value_type c)
733 TeststringInvariant(this);
740 void string::push_back(value_type c)
742 TeststringInvariant(this);
744 rep = rep->get_own_copy();
749 string & string::append(string const & x)
751 TeststringInvariant(this);
753 if (x.empty()) return *this;
754 rep = rep->get_own_copy();
755 rep->append(x.length(), x.rep->s);
760 string & string::append(string const & x, size_type pos, size_type n)
762 Assert(pos <= x.rep->sz); // STD!
763 TeststringInvariant(this);
765 return append(x.substr(pos, n));
769 string & string::append(value_type const * p, size_type n)
772 TeststringInvariant(this);
774 if (!*p || !n) return *this;
775 rep = rep->get_own_copy();
781 string & string::append(value_type const * p)
784 return append(p, strlen(p));
788 string & string::append(size_type n, value_type c)
790 TeststringInvariant(this);
792 value_type * tmp = new value_type[n];
794 rep = rep->get_own_copy();
801 string & string::append(iterator first, iterator last)
803 TeststringInvariant(this);
805 rep = rep->get_own_copy();
806 rep->append(last - first, first);
810 // insert characters before (*this)[pos]
812 string & string::insert(size_type pos, string const & x)
814 TeststringInvariant(this);
816 return insert(pos, x, 0, x.rep->sz);
820 string & string::insert(size_type pos, string const & x,
821 size_type pos2, size_type n)
823 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
824 TeststringInvariant(this);
826 rep = rep->get_own_copy();
827 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
832 string & string::insert(size_type pos, value_type const * p, size_type n)
835 TeststringInvariant(this);
838 // insert nothing and you change nothing
839 rep = rep->get_own_copy();
840 rep->insert(pos, p, n);
846 string & string::insert(size_type pos, value_type const * p)
849 return insert(pos, p, strlen(p));
853 string & string::insert(size_type pos, size_type n, value_type c)
855 TeststringInvariant(this);
857 rep = rep->get_own_copy();
858 value_type * tmp = new value_type[n];
860 rep->insert(pos, tmp, n);
866 string::iterator string::insert(iterator p, value_type c)
868 TeststringInvariant(this);
870 // what iterator is this supposed to return??
871 size_type tmp = p - begin();
872 insert(p - begin(), 1, c);
873 return begin() + tmp + 1; // ??
877 void string::insert(iterator p, size_type n , value_type c)
879 TeststringInvariant(this);
881 insert(p - begin(), n , c);
885 void string::insert(iterator p, iterator first, iterator last)
887 TeststringInvariant(this);
889 insert(p - begin(), first, last - first);
897 // All the below find functions should be verified,
898 // it is very likely that I have mixed up or interpreted
899 // some of the parameters wrong, also some of the funcs can surely
900 // be written more effectively.
902 string::size_type string::find(string const & a, size_type i) const
904 if (!rep->sz || i >= rep->sz) return npos;
906 TeststringInvariant(this);
908 size_type n = a.length();
910 for (size_type t = i; rep->sz - t >= n; ++t) {
911 // search until (*this)[i] == a[0]
912 if (rep->s[t] == a[0]) {
913 // check if the rest of the value_types match
915 for (size_type j = 1; j < n; ++j) {
916 if (rep->s[t + j] != a[j]) {
928 string::size_type string::find(value_type const * ptr, size_type i,
931 Assert(ptr); // OURS!
932 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
934 TeststringInvariant(this);
936 // What is "n" here? is it the number of value_types to use in ptr
937 // or does "i" and "n" togeter form a substring to search
938 // for ptr in? For now I will assume that "n" tells the length
940 n = min(n, strlen(ptr));
942 for (size_type t = i; rep->sz - t >= n; ++t) {
943 // search until (*this)[i] == a[0]
944 if (rep->s[t] == ptr[0]) {
945 // check if the rest of the value_types match
947 for (size_type j = 1; j < n; ++j) {
948 if (rep->s[t + j] != ptr[j]) {
960 string::size_type string::find(value_type const * s, size_type i) const
963 if (!rep->sz || i >= rep->sz) return npos;
965 TeststringInvariant(this);
967 if (!s || !*s) return npos;
968 return find(s, i, strlen(s));
972 string::size_type string::find(value_type c, size_type i) const
974 if (!rep->sz || i >= rep->sz) return npos;
976 TeststringInvariant(this);
978 for (size_type t = 0; t + i < rep->sz; ++t) {
979 if (rep->s[t + i] == c) return t + i;
985 string::size_type string::rfind(string const & a, size_type i) const
987 TeststringInvariant(this);
989 size_type n = a.length();
990 if (!n || rep->sz < n)
993 size_type t = min(rep->sz - n, i);
995 if (rep->s[t] == a[0]) {
996 // check if the rest of the value_types match
998 for (size_type j = 1; j < n; ++j) {
999 if (rep->s[t + j] != a[j]) {
1004 if (equal) return t;
1011 string::size_type string::rfind(value_type const * ptr, size_type i,
1014 Assert(ptr); // OURS!
1015 TeststringInvariant(this);
1017 n = min(n, strlen(ptr));
1018 if (!n || rep->sz < n)
1021 size_type t = min(rep->sz - n, i);
1023 if (rep->s[t] == ptr[0]) {
1024 // check if the rest of the value_types match
1026 for (size_type j = 1; j < n; ++j) {
1027 if (rep->s[t + j] != ptr[j]) {
1032 if (equal) return t;
1039 string::size_type string::rfind(value_type const * ptr,
1042 Assert(ptr); // OURS!
1044 if (!ptr || !*ptr) return npos;
1045 return rfind(ptr, i, strlen(ptr));
1049 string::size_type string::rfind(value_type c, size_type i) const
1051 TeststringInvariant(this);
1053 size_type const sz = rep->sz;
1054 if (sz < 1) return npos;
1055 size_type ii = min(sz - 1, i);
1057 if (rep->s[ii] == c) return ii;
1063 string::size_type string::find_first_of(string const & a,
1066 Assert(i <= rep->sz); // OURS!
1067 TeststringInvariant(this);
1069 for (size_type t = i; t < rep->sz; ++t) {
1070 if (a.find(rep->s[t]) != npos) return t;
1076 string::size_type string::find_first_of(value_type const * ptr,
1080 Assert(ptr && i <= rep->sz); // OURS!
1081 TeststringInvariant(this);
1082 if (!n) return npos;
1084 for (size_type t = i; t < rep->sz; ++t) {
1085 if (memchr(ptr, rep->s[t], n) != 0) return t;
1091 string::size_type string::find_first_of(value_type const * ptr,
1094 Assert(ptr && i <= rep->sz); // OURS!
1095 TeststringInvariant(this);
1097 for (size_type t = i; t < rep->sz; ++t) {
1098 if (strchr(ptr, rep->s[t]) != 0) return t;
1104 string::size_type string::find_first_of(value_type c, size_type i) const
1106 Assert(i <= rep->sz); // OURS!
1107 TeststringInvariant(this);
1109 for (size_type t = i; t < rep->sz; ++t) {
1110 if (rep->s[t] == c) return t;
1116 string::size_type string::find_last_of(string const & a,
1119 TeststringInvariant(this);
1121 size_type ii = min(rep->sz - 1, i);
1122 for (int t = ii; t >= 0; --t) {
1123 if (a.find(rep->s[t]) != npos) return t;
1129 string::size_type string::find_last_of(value_type const * ptr,
1133 Assert(ptr); // OURS!
1134 TeststringInvariant(this);
1135 if (!n) return npos;
1137 size_type ii = min(rep->sz - 1, i);
1138 for (int t = ii; t >= 0; --t) {
1139 if (memchr(ptr, rep->s[t], n) != 0) return t;
1145 string::size_type string::find_last_of(value_type const * ptr,
1148 Assert(ptr); // OURS!
1149 TeststringInvariant(this);
1151 size_type ii = min(rep->sz - 1, i);
1152 for (int t = ii; t >= 0; --t) {
1153 if (strchr(ptr, rep->s[t]) != 0) return t;
1159 string::size_type string::find_last_of(value_type c, size_type i) const
1161 TeststringInvariant(this);
1163 if (!rep->sz) return npos;
1164 size_type ii = min(rep->sz - 1, i);
1165 for (int t = ii; t >= 0; --t) {
1166 if (rep->s[t] == c) return t;
1172 string::size_type string::find_first_not_of(string const & a,
1175 TeststringInvariant(this);
1177 if (!rep->sz) return npos;
1178 Assert(i <= rep->sz);
1179 for (size_type t = i; t < rep->sz; ++t) {
1180 if (a.find(rep->s[t]) == npos) return t;
1186 string::size_type string::find_first_not_of(value_type const * ptr,
1190 Assert(ptr && i <= rep->sz); // OURS!
1191 TeststringInvariant(this);
1193 if (!n) return (i < rep->sz) ? i : npos;
1194 for (size_type t = i; t < rep->sz; ++t) {
1195 if (memchr(ptr, rep->s[t], n) == 0) return t;
1201 string::size_type string::find_first_not_of(value_type const * ptr,
1204 Assert(ptr && i <= rep->sz); // OURS!
1205 TeststringInvariant(this);
1207 for (size_type t = i; t < rep->sz; ++t) {
1208 if (strchr(ptr, rep->s[t]) == 0) return t;
1214 string::size_type string::find_first_not_of(value_type c,
1217 if (!rep->sz) return npos;
1218 Assert(i <= rep->sz); // OURS!
1219 TeststringInvariant(this);
1221 for (size_type t = i; t < rep->sz; ++t) {
1222 if (rep->s[t] != c) return t;
1228 string::size_type string::find_last_not_of(string const & a,
1231 TeststringInvariant(this);
1233 size_type ii = min(rep->sz - 1, i);
1234 for (int t = ii; t >= 0; --t) {
1235 if (a.find(rep->s[t]) == npos) return t;
1241 string::size_type string::find_last_not_of(value_type const * ptr,
1245 Assert(ptr); // OURS!
1246 TeststringInvariant(this);
1248 if (!n) return npos;
1249 size_type ii = min(rep->sz - 1, i);
1251 for (int t = ii; t >= 0; --t) {
1252 if (memchr(ptr, rep->s[t], n) == 0) return t;
1258 string::size_type string::find_last_not_of(value_type const * ptr,
1261 Assert(ptr); // OURS!
1262 TeststringInvariant(this);
1264 size_type ii = min(rep->sz - 1, i);
1265 for (int t = ii; t >= 0; --t) {
1266 if (strchr(ptr, rep->s[t]) == 0) return t;
1272 string::size_type string::find_last_not_of(value_type c,
1275 TeststringInvariant(this);
1277 size_type ii = min(rep->sz - 1, i);
1278 for (int t = ii; t >= 0; --t) {
1279 if (rep->s[t] != c) return t;
1289 string & string::replace(size_type i, size_type n, string const & x)
1291 Assert(i <= rep->sz); // OURS!
1292 TeststringInvariant(this);
1294 return replace(i, n, x, 0, x.rep->sz);
1298 string & string::replace(size_type i, size_type n, string const & x,
1299 size_type i2, size_type n2)
1301 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1302 TeststringInvariant(this);
1304 rep = rep->get_own_copy();
1305 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1310 string & string::replace(size_type i, size_type n,
1311 value_type const * p, size_type n2)
1313 Assert(p && i <= rep->sz); // OURS!
1314 TeststringInvariant(this);
1316 rep = rep->get_own_copy();
1317 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1322 string & string::replace(size_type i, size_type n, value_type const * p)
1324 Assert(p && i <= rep->sz); // OURS!
1325 TeststringInvariant(this);
1327 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1331 string & string::replace(size_type i, size_type n,
1332 size_type n2, value_type c)
1334 Assert(i <= rep->sz); // OURS!
1335 TeststringInvariant(this);
1337 rep = rep->get_own_copy();
1338 value_type * tmp = new value_type[n2];
1340 rep->replace(i, min(n, rep->sz), tmp, n2);
1346 /// FY! FY! FY! go away !
1347 string & string::replace(size_type i, size_type n, value_type c)
1349 return replace(i, n, 1, c);
1353 string & string::replace(iterator i, iterator i2, const string & str)
1355 TeststringInvariant(this);
1357 return replace(i - begin(), i2 - i, str);
1361 string & string::replace(iterator i, iterator i2,
1362 value_type const * p, size_type n)
1365 TeststringInvariant(this);
1367 return replace(i - begin(), i2 - i, p, n);
1371 string & string::replace(iterator i, iterator i2, value_type const * p)
1374 TeststringInvariant(this);
1376 return replace(i - begin(), i2 - i, p);
1380 string & string::replace(iterator i, iterator i2,
1381 size_type n , value_type c)
1383 TeststringInvariant(this);
1385 return replace(i - begin(), i2 - i, n, c);
1389 string & string::replace(iterator i, iterator i2,
1390 iterator j, iterator j2)
1392 TeststringInvariant(this);
1394 return replace(i - begin(), i2 - i, j, j2 - j);
1398 void string::swap(string & str)
1400 if (rep == str.rep) return;
1401 Srep * tmp = str.rep;
1407 string & string::erase(size_type i, size_type n)
1409 Assert(i <= rep->sz); // STD!
1410 TeststringInvariant(this);
1412 rep = rep->get_own_copy();
1413 if (i == 0 && n >= rep->sz) {
1416 n = min(n, rep->sz - i);
1417 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1424 string::iterator string::erase(iterator i)
1426 TeststringInvariant(this);
1428 // what iterator is this supposed to return?
1429 // the iterator after the one erased
1430 erase(i - begin(), 1);
1431 return begin(); // BUG
1435 string::iterator string::erase(iterator first, iterator last)
1437 TeststringInvariant(this);
1439 erase(first - begin(), last - first);
1440 return begin(); // BUG
1444 /////////////////////////////////////
1445 // Conversion to C-style Strings
1446 /////////////////////////////////////
1448 string::value_type const * string::c_str() const
1450 rep->s[length()] = '\0';
1455 string::value_type const * string::data() const
1461 string::size_type string::copy(value_type * buf, size_type len,
1462 size_type pos) const
1464 Assert(buf); // OURS!
1465 Assert(pos <= rep->sz); // STD!
1466 TeststringInvariant(this);
1468 register int nn = min(len, length() - pos);
1469 memcpy(buf, &(rep->s[pos]), nn);
1474 ////////////////////
1476 ////////////////////
1478 // Compare funcs should be verified.
1480 int string::internal_compare(size_type pos, size_type n,
1481 value_type const * s,
1482 size_type slen, size_type n2) const
1484 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1486 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1488 // remember that n can very well be a lot larger than rep->sz
1489 // so we have to ensure that n is no larger than rep->sz
1490 n = min(n, rep->sz);
1493 return memcmp(&(rep->s[pos]), s, n);
1494 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1503 int string::compare(string const & str) const
1505 TeststringInvariant(this);
1506 return internal_compare(0, rep->sz, str.rep->s,
1507 str.rep->sz, str.rep->sz);
1511 int string::compare(value_type const * s) const
1514 TeststringInvariant(this);
1515 int n = (!s) ? 0 : strlen(s);
1516 return internal_compare(0, rep->sz, s, n, n);
1520 int string::compare(size_type pos, size_type n,
1521 string const & str) const
1523 Assert(pos <= rep->sz); // OURS!
1524 TeststringInvariant(this);
1525 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1529 int string::compare(size_type pos, size_type n, string const & str,
1530 size_type pos2, size_type n2) const
1532 Assert(pos <= rep->sz); // OURS!
1533 Assert(pos2 <= str.rep->sz); // OURS!
1534 TeststringInvariant(this);
1535 return internal_compare(pos, n,
1537 str.rep->sz - pos2, n2);
1541 int string::compare(size_type pos, size_type n, value_type const * s,
1544 Assert(s && pos <= rep->sz); // OURS!
1545 TeststringInvariant(this);
1546 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1554 // i = index, n = length
1555 string string::substr(size_type i, size_type n) const
1557 Assert(i <= rep->sz); // STD!
1558 TeststringInvariant(this);
1560 return string(*this, i, n);
1564 /////////////////////////////////////////////
1565 // String operators, non member functions
1566 /////////////////////////////////////////////
1568 bool operator==(string const & a, string const & b)
1570 return a.compare(b) == 0;
1574 bool operator==(string::value_type const * a, string const & b)
1577 return b.compare(a) == 0;
1581 bool operator==(string const & a, string::value_type const * b)
1584 return a.compare(b) == 0;
1588 bool operator!=(string const & a, string const & b)
1590 return a.compare(b) != 0;
1594 bool operator!=(string::value_type const * a, string const & b)
1597 return b.compare(a) != 0;
1601 bool operator!=(string const & a, string::value_type const * b)
1604 return a.compare(b) != 0;
1608 bool operator>(string const & a, string const & b)
1610 return a.compare(b) > 0;
1614 bool operator>(string::value_type const * a, string const & b)
1617 return b.compare(a) < 0; // since we reverse the parameters
1621 bool operator>(string const & a, string::value_type const * b)
1624 return a.compare(b) > 0;
1628 bool operator<(string const & a, string const & b)
1630 return a.compare(b) < 0;
1634 bool operator<(string::value_type const * a, string const & b)
1637 return b.compare(a) > 0; // since we reverse the parameters
1641 bool operator<(string const & a, string::value_type const * b)
1644 return a.compare(b) < 0;
1648 bool operator>=(string const & a, string const & b)
1650 return a.compare(b) >= 0;
1654 bool operator>=(string::value_type const * a, string const & b)
1657 return b.compare(a) <= 0; // since we reverse the parameters
1661 bool operator>=(string const & a, string::value_type const * b)
1664 return a.compare(b) >= 0;
1668 bool operator<=(string const & a, string const & b)
1670 return a.compare(b) <= 0;
1674 bool operator<=(string::value_type const * a, string const & b)
1677 return b.compare(a) >= 0; // since we reverse the parameters
1681 bool operator<=(string const & a, string::value_type const * b)
1684 return a.compare(b) <= 0;
1688 string operator+(string const & a, string const & b)
1696 string operator+(string::value_type const * a, string const & b)
1705 string operator+(string::value_type a, string const & b)
1714 string operator+(string const & a, string::value_type const * b)
1723 string operator+(string const & a, string::value_type b)
1731 void swap(string & str1, string & str2)
1737 istream & operator>>(istream & is, string & s)
1740 // very bad solution
1741 char * nome = new char[1024];
1745 if (!tmp.empty()) s = tmp;
1748 int w = is.width(0);
1751 bool skipspace = true;
1762 if (--w == 1) break;
1764 if (s.empty()) is.setstate(std::ios::failbit);
1770 ostream & operator<<(ostream & o, string const & s)
1772 return o.write(s.data(), s.length());
1776 istream & getline(istream & is, string & s,
1777 string::value_type delim)
1779 // very bad solution
1797 lyx::string a = "abcac";
1798 cout << a.rfind("ab") << endl;
1799 cout << a.rfind("c") << endl;
1800 cout << a.rfind("d") << endl;