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"
28 // This class is supposed to be functionaly equivalent to a
29 // standard conformant string. This mean among others that we
30 // are useing the same requirements. Before you change anything
31 // in this file consult me and/or the standard to discover the
34 // Asserts with a STD! are required by the standard.
35 // Asserts with a OURS! are added by me.
36 // Some asserts could still be missing and some of the existing
37 // ones might be wrong or not needed.
39 // Reference count has been checked, empty_rep removed and
40 // introduced again in a similar guise. Where is empty_rep _really_
43 // We are missing a couple of imporant things from the standard:
44 // reverse iterators and methods taking InputIterators as paramters.
45 // Also the methods returning iterators is returning the wrong value.
47 // All the different find functions need a good look over.
48 // I have so far not tested them extensively and would be
49 // happy if others took the time to have a peek.
51 // Space allocation of string.
52 // I have tried to do this very simple without using any special tricks.
53 // Earlier we used a fixed value to enlarge the string with this would
54 // cause a lot of reallocations with large strings (especially if
55 // push_back was used) and wasting space for very small strings.
56 // I have now changed the allocation to use a doubling of reserved
57 // space until it is large enough. So far tests show a small speed
58 // increase and a noticable memory saving.
63 ///////////////////////////////////////
64 // The internal string representation
65 ///////////////////////////////////////
67 struct lyxstring::Srep {
72 /// The total amount of data reserved for this representaion
74 /// Data. At least 1 char for trailing null.
75 lyxstring::value_type * s;
78 Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
80 Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
82 ~Srep() { delete[] s; }
84 Srep * get_own_copy() {
85 if (ref == 1) return this;
87 return new Srep(sz, s);
91 void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
93 void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
95 void append(lyxstring::size_type asz, const lyxstring::value_type * p);
97 void push_back(lyxstring::value_type c);
99 void insert(lyxstring::size_type pos,
100 const lyxstring::value_type * p,
101 lyxstring::size_type n);
103 void resize(lyxstring::size_type n, lyxstring::value_type c);
105 void reserve(lyxstring::size_type res_arg);
107 void replace(lyxstring::size_type i, lyxstring::size_type n,
108 lyxstring::value_type const * p, lyxstring::size_type n2);
111 Srep & operator=(const Srep &);
115 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
117 // can be called with p == 0 by
118 // lyxstring::assign(const value_type *, size_type)
123 s = new value_type[res + 1]; // add space for terminator
125 // if sz = 0 nothing gets copied and we have an error
128 // possibly allows for large but empty string
129 sz = 0; // this line should be redundant
135 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
140 s = new value_type[res + 1]; // add space for terminator
143 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
144 // allows for large but empty string
150 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
152 // can be called with p == 0
153 // by lyxstring::assign(const value_type *, size_type)
159 s = new value_type[res + 1]; // add space for terminator
164 // if sz = 0 nothing gets copied and we have an error
168 sz = 0; // this line should be redundant
174 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
180 s = new value_type[res + 1]; // add space for terminator
184 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
185 // allows for a large empty string
191 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
193 register unsigned int const len = sz + asz;
198 value_type * tmp = new value_type[res + 1];
200 memcpy(tmp + sz, p, asz);
205 memcpy(s + sz, p, asz);
211 void lyxstring::Srep::push_back(value_type c)
213 s[sz] = c; // it is always room to put a value_type at the end
219 value_type * tmp = new value_type[res + 1];
227 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
228 lyxstring::size_type n)
233 } while (res < n + sz);
234 value_type * tmp = new value_type[res + 1];
236 memcpy(tmp + pos, p, n);
237 memcpy(tmp + pos + n, &s[pos], sz - pos);
242 memmove(s + pos + n, &s[pos], sz - pos);
243 memcpy(s + pos, p, n);
249 void lyxstring::Srep::resize(size_type n, value_type c)
251 // This resets sz to res_arg
252 res = min(n, npos - 2); // We keep no xtra when we resize
253 value_type * tmp = new value_type[res + 1];
254 memcpy(tmp, s, min(sz, res));
256 memset(tmp + sz, c, res - sz);
263 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
265 // This keeps the old sz, but
266 // increases res with res_arg
268 value_type * tmp = new value_type[res + 1];
275 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
276 value_type const * p, size_type n2)
278 // can be called with p= 0 and n2= 0
281 if (res >= n2 + sz) {
282 memmove(s + i + n2, &s[i + n], sz - i);
283 memcpy(s + i, p, n2);
288 } while (res < n2 + sz);
289 value_type * tmp = new value_type[res + 1];
291 memcpy(tmp + i, p, n2);
292 memcpy(tmp + i + n2, &s[i + n], sz - i);
300 ///////////////////////////////////////
301 // The lyxstring Invariant tester
302 ///////////////////////////////////////
303 #ifdef ENABLE_ASSERTIONS
305 /** Testing of the lyxstring invariant
306 * By creating an object that tests the lyxstring invariant during its
307 * construction *and* its deconstruction we greatly simplify our code.
308 * Calling TestlyxstringInvariant() upon entry to an lyxstring method
309 * will test the invariant upon entry to the code. If the Asserts fail
310 * then we know from the stack trace that the corruption occurred *before*
311 * entry to this method. We can also be sure it didn't happen in any of
312 * the tested lyxstring methods. It is therefore likely to be due to some
313 * other external force.
314 * Several lyxstring methods have multiple exit points which would otherwise
315 * require us to insert a separate test before each return. But since we
316 * created an object its destructor will be called upon exit (any exit!).
317 * We thus get testing at both start and end of a method with one line of
318 * code at the head of a method. More importantly, we get good testing
319 * everytime we run the code.
320 * NOTE: just because we test the invariant doesn't mean we can forget
321 * about testing pre and post conditions specific to any given method.
322 * This test simply proves that the lyxstring/Srep is in a valid state it
323 * does *not* prove that the method did what it was supposed to.
325 class lyxstringInvariant {
327 lyxstringInvariant(lyxstring const *);
328 ~lyxstringInvariant();
331 lyxstring const * object;
335 // To test if this scheme works "as advertised" uncomment the printf's in
336 // the constructor and destructor below and then uncomment the printf and the
337 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
338 // The correct output when LyX has been recompiled and run is:
339 // lyxstringInvariant constructor
340 // lyxstring::operator=(char const *)
341 // lyxstringInvariant constructor
342 // lyxstringInvariant destructor completed
343 // lyxstringInvariant destructor completed
344 // NOTE: The easiest way to catch this snippet of the output is to wait for
345 // the splash screen to disappear and then open and close Help->Credits
347 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
349 // printf("lyxstringInvariant constructor\n");
354 lyxstringInvariant::~lyxstringInvariant()
357 // printf("lyxstringInvariant destructor completed\n");
361 void lyxstringInvariant::helper() const
363 // Some of these tests might look pointless but they are
364 // all part of the invariant and if we want to make sure
365 // we have a bullet proof implementation then we need to
366 // test every last little thing we *know* should be true.
367 // I may have missed a test or two, so feel free to fill
368 // in the gaps. ARRae.
371 Assert(object->rep->s); // s is never 0
372 Assert(object->rep->res); // res cannot be 0
373 Assert(object->rep->sz <= object->rep->res);
374 Assert(object->rep->ref >= 1); // its in use so it must be referenced
375 Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
376 // if it does ever == then we should be generating a new copy
377 // and starting again. (Is char always 8-bits?)
379 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
381 #define TestlyxstringInvariant(s)
382 #endif /* ENABLE_ASSERTIONS */
385 ///////////////////////////////////////
386 // Constructors and Deconstructors.
387 ///////////////////////////////////////
389 lyxstring::size_type const lyxstring::npos =
390 static_cast<lyxstring::size_type>(-1);
393 lyxstring::lyxstring()
395 static Srep empty_rep(0, "");
401 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
403 Assert(pos <= x.rep->sz); // STD!
404 if (pos == 0 && n >= x.length()) { // this is the default
408 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
413 lyxstring::lyxstring(value_type const * s, size_type n)
415 Assert(s && n < npos); // STD!
416 static Srep empty_rep(0, "");
417 if (*s && n) { // s is not empty string and n > 0
418 rep = new Srep(min(strlen(s), n), s);
426 lyxstring::lyxstring(value_type const * s)
429 static Srep empty_rep(0, "");
430 if (*s) { // s is not empty string
431 rep = new Srep(strlen(s), s);
439 lyxstring::lyxstring(size_type n, value_type c)
441 Assert(n < npos); // STD!
442 rep = new Srep(n, c);
446 lyxstring::lyxstring(const_iterator first, const_iterator last)
448 rep = new Srep(last - first, first);
452 lyxstring::~lyxstring()
454 if (--rep->ref == 0) delete rep;
457 ///////////////////////
459 ///////////////////////
461 lyxstring::iterator lyxstring::begin()
463 rep = rep->get_own_copy();
468 lyxstring::const_iterator lyxstring::begin() const
474 lyxstring::iterator lyxstring::end()
476 rep = rep->get_own_copy();
477 return rep->s + rep->sz;
481 lyxstring::const_iterator lyxstring::end() const
483 return rep->s + rep->sz;
487 reverse_iterator lyxstring::rbegin()
489 return reverse_iterator( end() );
493 const_reverse_iterator lyxstring::rbegin() const
495 return const_reverse_iterator( end() );
499 reverse_iterator lyxstring::rend()
501 return reverse_iterator( begin() );
505 const_reverse_iterator lyxstring::rend() const
507 return const_reverse_iterator( begin() );
512 ///////////////////////
514 ///////////////////////
516 lyxstring::size_type lyxstring::size() const
522 void lyxstring::resize(size_type n, value_type c)
524 Assert(n <= npos); // STD!
525 TestlyxstringInvariant(this);
527 // This resets sz to res_arg
528 rep = rep->get_own_copy();
533 lyxstring::size_type lyxstring::capacity() const
539 void lyxstring::reserve(size_type res_arg)
541 TestlyxstringInvariant(this);
543 rep = rep->get_own_copy();
544 rep->reserve(res_arg);
552 lyxstring & lyxstring::operator= (lyxstring const & x)
554 TestlyxstringInvariant(this);
560 lyxstring & lyxstring::operator= (value_type const * s)
563 TestlyxstringInvariant(this);
564 // printf("lyxstring::operator= (value_type const *)\n");
570 lyxstring & lyxstring::operator=(value_type c)
572 TestlyxstringInvariant(this);
576 if (rep->ref == 1) // recycle rep
580 rep = new Srep(1, s);
586 lyxstring & lyxstring::assign(lyxstring const & x)
588 TestlyxstringInvariant(this);
590 x.rep->ref++; // protect against ``st = st''
591 if (--rep->ref == 0) delete rep;
592 rep = x.rep; // share representation
597 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
599 Assert(pos <= x.rep->sz); // STD!
600 TestlyxstringInvariant(this);
602 return assign(x.substr(pos, n));
606 lyxstring & lyxstring::assign(value_type const * s, size_type n)
609 TestlyxstringInvariant(this);
611 n = min(strlen(s), n);
612 if (rep->ref == 1) // recycle rep
616 rep = new Srep(n, s);
622 lyxstring & lyxstring::assign(value_type const * s)
625 TestlyxstringInvariant(this);
627 return assign(s, strlen(s));
631 lyxstring & lyxstring::assign(size_type n, value_type ch)
633 TestlyxstringInvariant(this);
635 rep = rep->get_own_copy();
641 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
643 TestlyxstringInvariant(this);
645 rep = rep->get_own_copy();
646 rep->assign(last - first, first);
655 lyxstring::const_reference lyxstring::operator[](size_type pos) const
657 Assert(pos <= rep->sz); // OURS!
658 static char helper = '\0';
659 return pos == rep->sz ? helper : rep->s[pos];
663 lyxstring::reference lyxstring::operator[](size_type pos)
665 Assert(pos < rep->sz); // OURS!
666 TestlyxstringInvariant(this);
668 rep = rep->get_own_copy();
673 lyxstring::const_reference lyxstring::at(size_type n) const
675 Assert(n < rep->sz); // STD!
680 lyxstring::reference lyxstring::at(size_type n)
682 Assert(n < rep->sz); // STD!
683 TestlyxstringInvariant(this);
685 rep = rep->get_own_copy();
694 lyxstring & lyxstring::operator+=(lyxstring const & x)
696 TestlyxstringInvariant(this);
702 lyxstring & lyxstring::operator+=(value_type const * x)
705 TestlyxstringInvariant(this);
711 lyxstring & lyxstring::operator+=(value_type c)
713 TestlyxstringInvariant(this);
720 void lyxstring::push_back(value_type c)
722 TestlyxstringInvariant(this);
724 rep = rep->get_own_copy();
729 lyxstring & lyxstring::append(lyxstring const & x)
731 TestlyxstringInvariant(this);
733 if (x.empty()) return *this;
734 rep = rep->get_own_copy();
735 rep->append(x.length(), x.rep->s);
740 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
742 Assert(pos <= x.rep->sz); // STD!
743 TestlyxstringInvariant(this);
745 return append(x.substr(pos, n));
749 lyxstring & lyxstring::append(value_type const * p, size_type n)
752 TestlyxstringInvariant(this);
754 if (!*p || !n) return *this;
755 rep = rep->get_own_copy();
756 rep->append(min(n, strlen(p)), p);
761 lyxstring & lyxstring::append(value_type const * p)
764 TestlyxstringInvariant(this);
766 if (!*p) return *this;
767 rep = rep->get_own_copy();
768 rep->append(strlen(p), p);
773 lyxstring & lyxstring::append(size_type n, value_type c)
775 TestlyxstringInvariant(this);
777 value_type * tmp = new value_type[n];
779 rep = rep->get_own_copy();
786 lyxstring & lyxstring::append(iterator first, iterator last)
788 TestlyxstringInvariant(this);
790 rep = rep->get_own_copy();
791 rep->append(last - first, first);
795 // insert value_typeacters before (*this)[pos]
797 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
799 TestlyxstringInvariant(this);
801 return insert(pos, x, 0, x.rep->sz);
805 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
806 size_type pos2, size_type n)
808 Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
809 TestlyxstringInvariant(this);
811 rep = rep->get_own_copy();
812 rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
817 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
820 TestlyxstringInvariant(this);
823 // insert nothing and you change nothing
824 rep = rep->get_own_copy();
825 rep->insert(pos, p, min(n, strlen(p)));
831 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
834 TestlyxstringInvariant(this);
837 // insert nothing and you change nothing
838 rep = rep->get_own_copy();
839 rep->insert(pos, p, strlen(p));
845 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
847 TestlyxstringInvariant(this);
849 rep = rep->get_own_copy();
850 value_type * tmp = new value_type[n];
852 rep->insert(pos, tmp, n);
858 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
860 TestlyxstringInvariant(this);
862 // what iterator is this supposed to return??
863 size_type tmp = p - begin();
864 insert(p - begin(), 1, c);
865 return begin() + tmp + 1; // ??
869 void lyxstring::insert(iterator p, size_type n , value_type c)
871 TestlyxstringInvariant(this);
873 insert(p - begin(), n , c);
877 void lyxstring::insert(iterator p, iterator first, iterator last)
879 TestlyxstringInvariant(this);
881 insert(p - begin(), first, last - first);
889 // All the below find functions should be verified,
890 // it is very likely that I have mixed up or interpreted
891 // some of the parameters wrong, also some of the funcs can surely
892 // be written more effectively.
894 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
896 if (!rep->sz || i >= rep->sz) return npos;
898 TestlyxstringInvariant(this);
900 for (size_type t = i; rep->sz - t >= a.length(); ++t) {
901 // search until (*this)[i] == a[0]
902 if (rep->s[t] == a[0]) {
903 // check if the rest of the value_types match
905 for (size_type j = 0; j < a.length(); ++j) {
906 if (rep->s[t + j] != a[j]) {
918 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
921 Assert(ptr); // OURS!
922 if (!rep->sz || !*ptr || i >= rep->sz) return npos;
924 TestlyxstringInvariant(this);
926 // What is "n" here? is it the number of value_types to use in ptr
927 // or does "i" and "n" togeter form a substring to search
928 // for ptr in? For now I will assume that "n" tells the length
930 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 = 0; 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
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 ii = min(rep->sz - 1, i);
980 if (a[a.length() - 1] == rep->s[ii]) {
982 size_type l = ii - 1;
983 for (; t >= 0; --t, --l) {
984 if (a[t] != rep->s[l]) break;
986 if (a[t] == rep->s[l]) return l;
993 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
996 Assert(ptr); // OURS!
997 TestlyxstringInvariant(this);
998 if (!*ptr) return npos;
1000 size_type ii = min(rep->sz - 1, i);
1002 if (ptr[n - 1] == rep->s[ii]) {
1004 size_type l = ii - 1;
1005 for (; t >= 0; --t, --l) {
1006 if (ptr[t] != rep->s[l]) break;
1008 if (ptr[t] == rep->s[l]) return l;
1015 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1018 Assert(ptr); // OURS!
1019 TestlyxstringInvariant(this);
1020 if (!*ptr) return npos;
1022 size_type ii = min(rep->sz - 1, i);
1024 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1025 int t = strlen(ptr) - 2;
1026 size_type l = ii - 1;
1027 for (; t >= 0; --t, --l) {
1028 if (ptr[t] != rep->s[l]) break;
1030 if (ptr[t] == rep->s[l]) return l;
1037 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1039 TestlyxstringInvariant(this);
1041 size_type ii = min(rep->sz - 1, i);
1042 for (size_type t = ii; t != 0; --t) {
1043 if (rep->s[t] == c) return t;
1049 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1052 Assert(i < rep->sz); // OURS!
1053 TestlyxstringInvariant(this);
1055 for (size_type t = i; t < rep->sz; ++t) {
1056 if (a.find(rep->s[t]) != npos) return t;
1062 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1066 Assert(ptr && i < rep->sz); // OURS!
1067 TestlyxstringInvariant(this);
1068 if (!n) return npos;
1070 for (size_type t = i; t < rep->sz; ++t) {
1071 if(memchr(ptr, rep->s[t], n) != 0) return t;
1077 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1080 Assert(ptr && i < rep->sz); // OURS!
1081 TestlyxstringInvariant(this);
1083 for (size_type t = i; t < rep->sz; ++t) {
1084 if (strchr(ptr, rep->s[t]) != 0) return t;
1090 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1092 Assert(i < rep->sz); // OURS!
1093 TestlyxstringInvariant(this);
1095 for (size_type t = i; t < rep->sz; ++t) {
1096 if (rep->s[t] == c) return t;
1102 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1105 TestlyxstringInvariant(this);
1107 size_type ii = min(rep->sz - 1, i);
1108 for (int t = ii; t >= 0; --t) {
1109 if (a.find(rep->s[t]) != npos) return t;
1115 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1119 Assert(ptr); // OURS!
1120 TestlyxstringInvariant(this);
1121 if (!n) return npos;
1123 size_type ii = min(rep->sz - 1, i);
1124 for (int t = ii; t >= 0; --t) {
1125 if(memchr(ptr, rep->s[t], n) != 0) return t;
1131 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1134 Assert(ptr); // OURS!
1135 TestlyxstringInvariant(this);
1137 size_type ii = min(rep->sz - 1, i);
1138 for (int t = ii; t >= 0; --t) {
1139 if (strchr(ptr, rep->s[t]) != 0) return t;
1145 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1147 TestlyxstringInvariant(this);
1149 if (!rep->sz) return npos;
1150 size_type ii = min(rep->sz - 1, i);
1151 for (int t = ii; t >= 0; --t) {
1152 if (rep->s[t] == c) return t;
1158 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1161 TestlyxstringInvariant(this);
1163 if (!rep->sz) return npos;
1164 Assert(i < rep->sz);
1165 for (size_type t = i; t < rep->sz; ++t) {
1166 if (a.find(rep->s[t]) == npos) return t;
1172 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1176 Assert(ptr && i < rep->sz); // OURS!
1177 TestlyxstringInvariant(this);
1179 if (!n) return (i < rep->sz) ? i : npos;
1180 for (size_type t = i; t < rep->sz; ++t) {
1181 if(memchr(ptr, rep->s[t], n) == 0) return t;
1187 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1190 Assert(ptr && i < rep->sz); // OURS!
1191 TestlyxstringInvariant(this);
1193 for (size_type t = i; t < rep->sz; ++t) {
1194 if (strchr(ptr, rep->s[t]) == 0) return t;
1200 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1203 if (!rep->sz) return npos;
1204 Assert(i < rep->sz); // OURS!
1205 TestlyxstringInvariant(this);
1207 for (size_type t = i; t < rep->sz; ++t) {
1208 if (rep->s[t] != c) return t;
1214 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1217 TestlyxstringInvariant(this);
1219 size_type ii = min(rep->sz - 1, i);
1220 for (int t = ii; t >= 0; --t) {
1221 if (a.find(rep->s[t]) == npos) return t;
1227 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1231 Assert(ptr); // OURS!
1232 TestlyxstringInvariant(this);
1234 if (!n) return npos;
1235 size_type ii = min(rep->sz - 1, i);
1237 for (int t = ii; t >= 0; --t) {
1238 if(memchr(ptr, rep->s[t], n) == 0) return t;
1244 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1247 Assert(ptr); // OURS!
1248 TestlyxstringInvariant(this);
1250 size_type ii = min(rep->sz - 1, i);
1251 for (int t = ii; t >= 0; --t) {
1252 if (strchr(ptr, rep->s[t]) == 0) return t;
1258 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1261 TestlyxstringInvariant(this);
1263 size_type ii = min(rep->sz - 1, i);
1264 for (int t = ii; t >= 0; --t) {
1265 if (rep->s[t] != c) return t;
1275 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1277 Assert(i <= rep->sz); // OURS!
1278 TestlyxstringInvariant(this);
1280 return replace(i, n, x, 0, x.rep->sz);
1284 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1285 size_type i2, size_type n2)
1287 Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1288 TestlyxstringInvariant(this);
1290 rep = rep->get_own_copy();
1291 rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1296 lyxstring & lyxstring::replace(size_type i, size_type n,
1297 value_type const * p, size_type n2)
1299 Assert(p && i < rep->sz); // OURS!
1300 TestlyxstringInvariant(this);
1302 rep = rep->get_own_copy();
1303 rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1308 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1310 Assert(p && i < rep->sz); // OURS!
1311 TestlyxstringInvariant(this);
1313 return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1317 lyxstring & lyxstring::replace(size_type i, size_type n,
1318 size_type n2, value_type c)
1320 Assert(i < rep->sz); // OURS!
1321 TestlyxstringInvariant(this);
1323 rep = rep->get_own_copy();
1324 value_type * tmp = new value_type[n2];
1326 rep->replace(i, min(n, rep->sz), tmp, n2);
1332 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1334 TestlyxstringInvariant(this);
1336 return replace(i - begin(), i2 - i, str);
1340 lyxstring & lyxstring::replace(iterator i, iterator i2,
1341 value_type const * p, size_type n)
1344 TestlyxstringInvariant(this);
1346 return replace(i - begin(), i2 - i, p, n);
1350 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1353 TestlyxstringInvariant(this);
1355 return replace(i - begin(), i2 - i, p);
1359 lyxstring & lyxstring::replace(iterator i, iterator i2,
1360 size_type n , value_type c)
1362 TestlyxstringInvariant(this);
1364 return replace(i - begin(), i2 - i, n, c);
1368 lyxstring & lyxstring::replace(iterator i, iterator i2,
1369 iterator j, iterator j2)
1371 TestlyxstringInvariant(this);
1373 return replace(i - begin(), i2 - i, j, j2 - j);
1377 void lyxstring::swap(lyxstring & str)
1379 if (rep == str.rep) return;
1380 Srep * tmp = str.rep;
1386 lyxstring & lyxstring::erase(size_type i, size_type n)
1388 Assert(i <= rep->sz); // STD!
1389 TestlyxstringInvariant(this);
1391 rep = rep->get_own_copy();
1392 if (i == 0 && n >= rep->sz) {
1395 n = min(n, rep->sz - i);
1396 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1403 lyxstring::iterator lyxstring::erase(iterator i)
1405 TestlyxstringInvariant(this);
1407 // what iterator is this supposed to return?
1408 // the iterator after the one erased
1409 erase(i - begin(), 1);
1410 return begin(); // BUG
1414 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1416 TestlyxstringInvariant(this);
1418 erase(first - begin(), last - first);
1419 return begin(); // BUG
1423 /////////////////////////////////////
1424 // Conversion to C-style Strings
1425 /////////////////////////////////////
1427 lyxstring::value_type const * lyxstring::c_str() const
1429 rep->s[length()] = '\0';
1434 lyxstring::value_type const * lyxstring::data() const
1440 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1441 size_type pos) const
1443 Assert(buf); // OURS!
1444 Assert(pos <= rep->sz); // STD!
1445 TestlyxstringInvariant(this);
1447 register int nn = min(len, length() - pos);
1448 memcpy(buf, &(rep->s[pos]), nn);
1453 ////////////////////
1455 ////////////////////
1457 // Compare funcs should be verified.
1459 int lyxstring::internal_compare(size_type pos, size_type n,
1460 value_type const * s,
1461 size_type slen, size_type n2) const
1463 if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1465 // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1467 // remember that n can very well be a lot larger than rep->sz
1468 // so we have to ensure that n is no larger than rep->sz
1469 n = min(n, rep->sz);
1472 return memcmp(&(rep->s[pos]), s, n);
1473 int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1482 int lyxstring::compare(lyxstring const & str) const
1484 TestlyxstringInvariant(this);
1485 return internal_compare(0, rep->sz, str.rep->s,
1486 str.rep->sz, str.rep->sz);
1490 int lyxstring::compare(value_type const * s) const
1493 TestlyxstringInvariant(this);
1494 int n = (!s) ? 0 : strlen(s);
1495 return internal_compare(0, rep->sz, s, n, n);
1499 int lyxstring::compare(size_type pos, size_type n,
1500 lyxstring const & str) const
1502 Assert(pos <= rep->sz); // OURS!
1503 TestlyxstringInvariant(this);
1504 return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1508 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1509 size_type pos2, size_type n2) const
1511 Assert(pos <= rep->sz); // OURS!
1512 Assert(pos2 <= str.rep->sz); // OURS!
1513 TestlyxstringInvariant(this);
1514 return internal_compare(pos, n,
1516 str.rep->sz - pos2, n2);
1520 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1523 Assert(s && pos <= rep->sz); // OURS!
1524 TestlyxstringInvariant(this);
1525 return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1533 // i = index, n = length
1534 lyxstring lyxstring::substr(size_type i, size_type n) const
1536 Assert(i <= rep->sz); // STD!
1537 TestlyxstringInvariant(this);
1539 return lyxstring(*this, i, n);
1543 /////////////////////////////////////////////
1544 // String operators, non member functions
1545 /////////////////////////////////////////////
1547 bool operator==(lyxstring const & a, lyxstring const & b)
1549 return a.compare(b) == 0;
1553 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1556 return b.compare(a) == 0;
1560 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1563 return a.compare(b) == 0;
1567 bool operator!=(lyxstring const & a, lyxstring const & b)
1569 return a.compare(b) != 0;
1573 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1576 return b.compare(a) != 0;
1580 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1583 return a.compare(b) != 0;
1587 bool operator>(lyxstring const & a, lyxstring const & b)
1589 return a.compare(b) > 0;
1593 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1596 return b.compare(a) < 0; // since we reverse the parameters
1600 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1603 return a.compare(b) > 0;
1607 bool operator<(lyxstring const & a, lyxstring const & b)
1609 return a.compare(b) < 0;
1613 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1616 return b.compare(a) > 0; // since we reverse the parameters
1620 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1623 return a.compare(b) < 0;
1627 bool operator>=(lyxstring const & a, lyxstring const & b)
1629 return a.compare(b) >= 0;
1633 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1636 return b.compare(a) <= 0; // since we reverse the parameters
1640 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1643 return a.compare(b) >= 0;
1647 bool operator<=(lyxstring const & a, lyxstring const & b)
1649 return a.compare(b) <= 0;
1653 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1656 return b.compare(a) >= 0; // since we reverse the parameters
1660 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1663 return a.compare(b) <= 0;
1667 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1675 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1684 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1693 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1702 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1710 void swap(lyxstring & str1, lyxstring & str2)
1718 istream & operator>>(istream & is, lyxstring & s)
1721 // very bad solution
1722 char * nome = new char[1024];
1724 lyxstring tmp(nome);
1726 if (!tmp.empty()) s = tmp;
1729 int w = is.widdth(0);
1733 if (isspace(c)) { is.putback(c); break; }
1735 if (--w == 1) break;
1737 if (s.empty()) is.setstate(ios::failbit);
1743 ostream & operator<<(ostream & o, lyxstring const & s)
1745 return o.write(s.data(), s.length());
1749 istream & getline(istream & is, lyxstring & s,
1750 lyxstring::value_type delim)
1752 // very bad solution