]> git.lyx.org Git - lyx.git/blob - src/support/lyxstring.C
hopefully solve some of the lyxstring problems.
[lyx.git] / src / support / lyxstring.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright (C) 1995 Matthias Ettrich
7  *          Copyright (C) 1995-1999 The LyX Team.
8  *
9  * ======================================================*/
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #ifdef __GNUG__
16 #pragma implementation "lyxstring.h"
17 #endif
18
19 #include "lyxstring.h"
20 #include <cstdlib>
21 #include <cctype>
22 #include <algorithm>
23
24 #include "LAssert.h"
25
26 using std::min;
27
28 // Reference count has been checked, empty_rep removed and
29 // introduced again in a similar guise. Where is empty_rep _really_
30 // needed?
31
32 // Insertion and replace is implemented, as far as I can see everything
33 // works, but could perhaps be done smarter.
34
35 // All the different find functions need a good look over.
36 // I have so far not tested them extensively and would be
37 // happy if others took the time to have a peek.
38
39 #ifdef WITH_WARNINGS
40 #warning temporarily here for debugging purposes only
41 #endif
42 lyxstring::size_type lyxstring::size() const
43
44         return rep->sz;
45 }
46
47
48 //--------------------------------------------------------------------------
49 // lyxstringInvariant
50 #ifdef DEVEL_VERSION
51
52 /** Testing of the lyxstring invariant
53  * By creating an object that tests the lyxstring invariant during its
54  * construction *and* its deconstruction we greatly simplify our code.
55  * Calling TestlyxstringInvariant() upon entry to an lyxstring method 
56  * will test the invariant upon entry to the code.  If the Asserts fail
57  * then we know from the stack trace that the corruption occurred *before*
58  * entry to this method.  We can also be sure it didn't happen in any of
59  * the tested lyxstring methods.  It is therefore likely to be due to some
60  * other external force.
61  * Several lyxstring methods have multiple exit points which would otherwise
62  * require us to insert a separate test before each return.  But since we
63  * created an object its destructor will be called upon exit (any exit!).
64  * We thus get testing at both start and end of a method with one line of
65  * code at the head of a method.  More importantly,  we get good testing
66  * everytime we run the code.
67  * NOTE:  just because we test the invariant doesn't mean we can forget
68  * about testing pre and post conditions specific to any given method.
69  * This test simply proves that the lyxstring/Srep is in a valid state it
70  * does *not* prove that the method did what it was supposed to.
71  */
72 class lyxstringInvariant
73 {
74 public:
75         lyxstringInvariant(lyxstring const *);
76         ~lyxstringInvariant();
77 private:
78         void helper() const;
79         lyxstring const * object;
80 };
81
82 // To test if this scheme works "as advertised" uncomment the printf's in
83 // the constructor and destructor below and then uncomment the printf and the
84 // call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
85 // The correct output when LyX has been recompiled and run is:
86 //     lyxstringInvariant constructor
87 //     lyxstring::operator=(char const *)
88 //     lyxstringInvariant constructor
89 //     lyxstringInvariant destructor completed
90 //     lyxstringInvariant destructor completed
91 // NOTE: The easiest way to catch this snippet of the output is to wait for
92 //       the splash screen to disappear and then open and close Help->Credits
93 //
94 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
95 {
96 //      printf("lyxstringInvariant constructor\n");
97         helper();
98 }
99
100 lyxstringInvariant::~lyxstringInvariant()
101 {
102         helper();
103 //      printf("lyxstringInvariant destructor completed\n");
104 }
105
106 void lyxstringInvariant::helper() const
107 {
108         // Some of these tests might look pointless but they are
109         // all part of the invariant and if we want to make sure
110         // we have a bullet proof implementation then we need to
111         // test every last little thing we *know* should be true.
112         // I may have missed a test or two, so feel free to fill
113         // in the gaps.  ARRae.
114         // NOTE:  Don't put TestlyxstringInvariant() in any of the
115         // lyxstring methods used below otherwise you'll get an
116         // infinite recursion and a crash.
117         Assert(object);
118         Assert(object->rep);
119         Assert(object->rep->s);    // s is never 0
120         Assert(object->rep->res);  // always some space allocated
121         Assert(object->size() <= object->rep->res);
122         Assert(object->rep->ref >= 1);  // its in use so it must be referenced
123         Assert(object->rep->ref < (1 << 8*sizeof(object->rep->ref)) - 1);
124         // if it does ever == then we should be generating a new copy
125         // and starting again.  (Is char always 8-bits?)
126 }
127 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
128 #else
129 #define TestlyxstringInvariant(s)
130 #endif //DEVEL_VERSION
131 //-------------------------------------------------------------------------
132
133 ///////////////////////////////////////
134 // Constructors and Deconstructors.
135 ///////////////////////////////////////
136
137
138 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
139 {
140 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
141
142         sz = nsz;
143         ref = 1;
144         res = sz + xtra;
145         s = new value_type[res + 1]; // add space for terminator
146         if (p && sz) {
147                 // if sz = 0 nothing gets copied and we have an error
148                 memcpy(s, p, sz);
149         } else {
150                 // possibly allows for large but empty string
151                 sz = 0;  // this line should be redundant
152                 s[0] = '\0';
153         }
154 }
155
156
157 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
158 {
159         sz = nsz;
160         ref = 1;
161         res = sz + xtra;
162         s = new value_type[res + 1]; // add space for terminator
163         memset(s, ch, sz);
164         if (!ch) {
165                 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
166                 // allows for large but empty string
167                 sz = 0;
168         }
169 }
170         
171
172 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
173 {
174 // can be called with p==0 by lyxstring::assign(const value_type *, size_type)
175
176         if (res < nsz) {
177                 delete[] s;
178                 sz = nsz;
179                 res = sz + xtra;
180                 s = new value_type[res + 1]; // add space for terminator
181         } else {
182                 sz = nsz;
183         }
184         if (p && sz) {
185                 // if sz = 0 nothing gets copied and we have an error
186                 memcpy(s, p, sz);
187         } else {
188                 // stops segfaults
189                 sz = 0;  // this line should be redundant
190                 s[0] = '\0';
191         }
192 }
193
194
195 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
196 {
197         sz = nsz;
198         if (res < nsz) {
199                 delete[] s;
200                 res = sz + xtra;
201                 s = new value_type[res + 1]; // add space for terminator
202         }
203         memset(s, ch, sz);
204         if (!ch) {
205                 // if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
206                 // allows for a large empty string
207                 sz = 0;
208         }
209 }
210
211
212 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
213 {
214         register unsigned int const len = sz + asz;
215         if (res < len) {
216                 res = len + xtra;
217                 value_type * tmp = new value_type[res + 1];
218                 memcpy(tmp, s, sz);
219                 memcpy(tmp + sz, p, asz);
220                 sz += asz;
221                 delete[] s;
222                 s = tmp;
223         } else {
224                 memcpy(s + sz, p, asz);
225                 sz += asz;
226         }
227 }
228
229
230 void lyxstring::Srep::push_back(value_type c)
231 {
232         s[sz] = c; // it is always room to put a value_type at the end
233         ++sz;
234         if (res < sz) {
235                 res = sz + xtra;
236                 value_type * tmp = new value_type[res + 1];
237                 memcpy(tmp, s, sz);
238                 delete[] s;
239                 s = tmp;
240         }
241 }
242
243
244 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
245                            lyxstring::size_type n)
246 {
247         Assert(pos <= sz);
248         if (res < n + sz) {
249                 res = sz + n + xtra;
250                 value_type * tmp = new value_type[res + 1];
251                 memcpy(tmp, s, pos);
252                 memcpy(tmp + pos, p, n);
253                 memcpy(tmp + pos + n, & s[pos], sz - pos);
254                 sz += n;
255                 delete[] s;
256                 s = tmp;
257         } else {
258                 memmove(s + pos + n, & s[pos], sz - pos);
259                 memcpy(s + pos, p, n);
260                 sz += n;
261         }
262 }
263
264
265 void lyxstring::Srep::resize(size_type n, value_type c)
266 {
267         Assert(n < npos);
268         // This resets sz to res_arg
269         res = min(n, npos - 2); // We keep no xtra when we resize
270         value_type * tmp = new value_type[res + 1];
271         memcpy(tmp, s, min(sz, res));
272         if (res > sz)
273                 memset(tmp + sz, c, res - sz);
274         delete[] s;
275         sz = res;
276         s = tmp;
277 }
278
279
280 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
281 {
282         // This keeps the old sz, but
283         // increases res with res_arg
284         res += res_arg;
285         value_type * tmp = new value_type[res + 1];
286         memcpy(tmp, s, sz);
287         delete[] s;
288         s = tmp;
289 }
290
291
292 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
293                             value_type const * p, size_type n2)
294 {
295 // can be called with p=0 and n2=0
296         Assert(i < sz && ((!p && !n2) || p));
297         n = min(sz - i, n);
298         sz -= n;
299         if (res >= n2 + sz) {
300                 memmove(s + i + n2, &s[i + n], sz - i);
301                 memcpy(s + i, p, n2);
302                 sz += n2;
303         } else {
304                 res = sz + n2 + xtra;
305                 value_type * tmp = new value_type[res + 1];
306                 memcpy(tmp, s, i);
307                 memcpy(tmp + i, p, n2);
308                 memcpy(tmp + i + n2, &s[i + n], sz - i);
309                 delete[] s;
310                 s = tmp;
311                 sz += n2; 
312         }
313 }
314
315
316 lyxstring::size_type const lyxstring::npos = static_cast<lyxstring::size_type>(-1);
317
318 lyxstring::lyxstring()
319 {
320         static Srep empty_rep(0, "");
321         ++empty_rep.ref;
322         rep = &empty_rep;
323 }
324
325
326 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
327 {
328         Assert(pos < x.rep->sz || pos == 0);
329         if (pos == 0 && n >= x.length()) { // this is the default
330                 x.rep->ref++;
331                 rep = x.rep;
332         } else {
333                 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
334         }
335 }
336
337
338 lyxstring::lyxstring(value_type const * s, size_type n)
339 {
340         Assert(s); // we don't allow null pointers
341         static Srep empty_rep(0, "");
342         if (*s && n) { // s is not empty string and n > 0
343                 rep = new Srep(min(strlen(s), n), s);
344         } else {
345                 ++empty_rep.ref;
346                 rep = &empty_rep;
347         }
348 }
349
350
351 lyxstring::lyxstring(value_type const * s)
352 {
353         Assert(s); // we don't allow null pointers
354         static Srep empty_rep(0, "");
355         if (*s) { // s is not empty string
356                 rep = new Srep(strlen(s), s);
357         } else {
358                 ++empty_rep.ref;
359                 rep = &empty_rep;
360         }
361 }
362
363
364 lyxstring::lyxstring(size_type n, value_type c)
365 {
366         rep = new Srep(n, c);
367 }
368
369
370 lyxstring::lyxstring(iterator first, iterator last)
371 {
372         rep = new Srep(last - first, first);
373 }
374
375
376 ///////////////////////
377 // Iterators
378 ///////////////////////
379
380 lyxstring::iterator lyxstring::begin()
381 {
382         return rep->s;
383 }
384
385
386 lyxstring::const_iterator lyxstring::begin() const
387 {
388         return rep->s;
389 }
390
391
392 lyxstring::iterator lyxstring::end()
393 {
394         return rep->s + rep->sz;
395 }
396
397
398 lyxstring::const_iterator lyxstring::end() const
399 {
400         return rep->s + rep->sz;
401 }
402
403 #if 0
404 reverse_iterator lyxstring::rbegin()
405 {
406         return reverse_iterator( end() );
407 }
408
409
410 const_reverse_iterator lyxstring::rbegin() const
411 {
412         return const_reverse_iterator( end() );
413 }
414
415
416 reverse_iterator lyxstring::rend()
417 {
418         return reverse_iterator( begin() );
419 }
420
421
422 const_reverse_iterator lyxstring::rend() const
423 {
424         return const_reverse_iterator( begin() );
425 }
426 #endif
427
428 ///////////////////////
429 // Size and Capacity
430 ///////////////////////
431
432 void lyxstring::resize(size_type n, value_type c)
433 {
434         TestlyxstringInvariant(this);
435
436         // This resets sz to res_arg
437         rep = rep->get_own_copy();
438         rep->resize(n, c);
439 }
440
441
442 lyxstring::size_type lyxstring::capacity() const
443 {
444         return rep->res;
445 }
446
447
448 void lyxstring::reserve(size_type res_arg)
449 {
450         TestlyxstringInvariant(this);
451
452         rep = rep->get_own_copy();
453         rep->reserve(res_arg);
454 }
455
456
457 ////////////////
458 // Assignment
459 ////////////////
460
461 lyxstring & lyxstring::operator=(lyxstring const & x)
462 {
463         TestlyxstringInvariant(this);
464
465         return assign(x);
466 }
467
468
469 lyxstring & lyxstring::operator=(value_type const * s)
470 {
471         Assert(s);      
472         TestlyxstringInvariant(this);
473 //      printf("lyxstring::operator=(value_type const *)\n");
474
475         return assign(s);
476 }
477
478
479 lyxstring & lyxstring::operator=(value_type c)
480 {
481         TestlyxstringInvariant(this);
482
483         value_type s[1];
484         s[0] = c;
485         if (rep->ref == 1) // recycle rep
486                 rep->assign(1, s);
487         else {
488                 rep->ref--;
489                 rep = new Srep(1, s);
490         }
491         return *this;
492 }
493
494
495 lyxstring & lyxstring::assign(lyxstring const & x)
496 {
497         TestlyxstringInvariant(this);
498
499         x.rep->ref++; // protect against ``st = st''
500         if (--rep->ref == 0) delete rep;
501         rep = x.rep; // share representation
502         return *this;
503 }
504         
505
506 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
507 {
508         TestlyxstringInvariant(this);
509
510         return assign(x.substr(pos, n));
511 }
512         
513
514 lyxstring & lyxstring::assign(value_type const * s, size_type n)
515 {
516         Assert(s);
517         TestlyxstringInvariant(this);
518
519         n = min(strlen(s), n);
520         if (rep->ref == 1) // recycle rep
521                 rep->assign(n, s);
522         else {
523                 rep->ref--;
524                 rep = new Srep(n, s);
525         }
526         return *this;
527 }
528         
529
530 lyxstring & lyxstring::assign(value_type const * s)
531 {
532         Assert(s);
533         TestlyxstringInvariant(this);
534
535         return assign(s, strlen(s));
536 }
537
538
539 lyxstring & lyxstring::assign(size_type n, value_type ch)
540 {
541         TestlyxstringInvariant(this);
542
543         rep = rep->get_own_copy();
544         rep->assign(n, ch);
545         return *this;
546 }
547
548
549 lyxstring & lyxstring::assign(iterator first, iterator last)
550 {
551         TestlyxstringInvariant(this);
552
553         rep = rep->get_own_copy();
554         rep->assign(last - first, first);
555         return *this;
556 }
557
558
559 ////////////////////
560 // Element Access
561 ////////////////////
562
563 lyxstring::const_reference lyxstring::operator[](size_type pos) const
564 {
565         Assert(pos < rep->sz);
566
567         return rep->s[pos];
568 }
569
570
571 lyxstring::reference lyxstring::operator[](size_type pos)
572 {
573         Assert(pos < rep->sz);
574         TestlyxstringInvariant(this);
575
576         rep = rep->get_own_copy();
577         return rep->s[pos];
578 }
579
580
581 lyxstring::const_reference lyxstring::at(size_type n) const
582 {
583         Assert(n < rep->sz);
584         
585         return rep->s[n];
586 }
587
588
589 lyxstring::reference lyxstring::at(size_type n)
590 {
591         Assert(n < rep->sz);
592         TestlyxstringInvariant(this);
593
594         rep = rep->get_own_copy();
595         return rep->s[n];
596 }
597
598
599 /////////////
600 // Insert
601 /////////////
602
603 lyxstring & lyxstring::operator+=(lyxstring const & x)
604 {
605         TestlyxstringInvariant(this);
606
607         return append(x);
608 }
609
610
611 lyxstring & lyxstring::operator+=(value_type const * x)
612 {
613         Assert(x);
614         TestlyxstringInvariant(this);
615
616         return append(x);
617 }
618
619
620 lyxstring & lyxstring::operator+=(value_type c)
621 {
622         TestlyxstringInvariant(this);
623
624         push_back(c);
625         return *this;
626 }
627
628
629 void lyxstring::push_back(value_type c)
630 {
631         TestlyxstringInvariant(this);
632
633         rep = rep->get_own_copy();
634         rep->push_back(c);
635 }
636
637
638 lyxstring & lyxstring::append(lyxstring const & x)
639 {
640         TestlyxstringInvariant(this);
641
642         if (x.empty()) return *this;
643         rep = rep->get_own_copy();
644         rep->append(x.length(), x.rep->s);
645         return *this;
646 }
647
648
649 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
650 {
651         TestlyxstringInvariant(this);
652
653         return append(x.substr(pos, n));
654 }
655
656
657 lyxstring & lyxstring::append(value_type const * p, size_type n)
658 {
659         Assert(p);
660         TestlyxstringInvariant(this);
661
662         if (!*p || !n) return *this;
663         rep = rep->get_own_copy();
664         rep->append(min(n, strlen(p)), p);
665         return *this;
666 }
667
668
669 lyxstring & lyxstring::append(value_type const * p)
670 {
671         Assert(p);
672         TestlyxstringInvariant(this);
673
674         if (!*p) return *this;
675         rep = rep->get_own_copy();
676         rep->append(strlen(p), p);
677         return *this;
678 }
679
680
681 lyxstring & lyxstring::append(size_type n, value_type c)
682 {
683         TestlyxstringInvariant(this);
684
685         value_type * tmp = new value_type[n];
686         memset(tmp, c, n);
687         rep = rep->get_own_copy();
688         rep->assign(n, tmp);
689         delete[] tmp;
690         return *this;
691 }
692
693
694 lyxstring & lyxstring::append(iterator first, iterator last)
695 {
696         TestlyxstringInvariant(this);
697
698         rep = rep->get_own_copy();
699         rep->append(last - first, first);
700         return *this;
701 }
702
703 // insert value_typeacters before (*this)[pos]
704
705 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
706 {
707         TestlyxstringInvariant(this);
708
709         return insert(pos, x, 0, x.rep->sz);
710 }
711
712
713 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
714                           size_type pos2, size_type n)
715 {
716         TestlyxstringInvariant(this);
717
718         rep = rep->get_own_copy();
719         rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
720         return *this;
721 }
722
723
724 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
725 {
726         Assert(p);
727         TestlyxstringInvariant(this);
728
729         if (*p && n) {
730                 // insert nothing and you change nothing
731                 rep = rep->get_own_copy();
732                 rep->insert(pos, p, min(n, strlen(p)));
733         }
734         return *this;
735 }
736
737
738 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
739 {
740         Assert(p);
741         TestlyxstringInvariant(this);
742
743         if (*p) {
744                 // insert nothing and you change nothing
745                 rep = rep->get_own_copy();
746                 rep->insert(pos, p, strlen(p));
747         }
748         return *this;
749 }
750
751
752 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
753 {
754         TestlyxstringInvariant(this);
755
756         rep = rep->get_own_copy();
757         value_type * tmp = new value_type[n];
758         memset(tmp, c, n);
759         rep->insert(pos, tmp, n);
760         delete[] tmp;
761         return *this;
762 }
763
764
765 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
766 {
767         TestlyxstringInvariant(this);
768
769         // what iterator is this supposed to return??
770         size_type tmp = p - begin();
771         insert(p - begin(), 1, c);
772         return begin() + tmp + 1; // ??
773 }
774
775
776 void lyxstring::insert(iterator p, size_type n , value_type c)
777 {
778         TestlyxstringInvariant(this);
779
780         insert(p - begin(), n , c);
781 }
782
783
784 void lyxstring::insert(iterator p, iterator first, iterator last)
785 {
786         TestlyxstringInvariant(this);
787
788         insert(p - begin(), first, last - first);
789 }
790         
791
792 ////////////////
793 // Find
794 ////////////////
795  
796          // All the below find functions should be verified,
797          // it is very likely that I have mixed up or interpreted
798          // some of the parameters wrong, also some of the funcs can surely
799          // be written more effectively.
800
801 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
802 {
803         if (!rep->sz) return npos;
804         
805         Assert(i < rep->sz);
806         TestlyxstringInvariant(this);
807
808         for (size_type t = i; rep->sz - t >= a.length(); ++t) {
809                 // search until (*this)[i] == a[0]
810                 if (rep->s[t] == a[0]) {
811                         // check if the rest of the value_types match
812                         bool equal = true;
813                         for (size_type j = 0; j < a.length(); ++j) {
814                                 if (rep->s[t + j] != a[j]) {
815                                         equal = false;
816                                         break;
817                                 }
818                         }
819                         if (equal) return t;
820                 }
821         }
822         return npos;
823 }
824
825
826 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
827                                  size_type n) const
828 {
829         Assert(ptr);
830         if (!rep->sz || !*ptr) return npos;
831         
832         Assert(i < rep->sz);
833         TestlyxstringInvariant(this);
834
835         // What is "n" here? is it the number of value_types to use in ptr
836         // or does "i" and "n" togeter form a substring to search
837         // for ptr in? For now I will assume that "n" tells the length
838         // of ptr. (Lgb)
839         n = min(n, strlen(ptr));
840         for (size_type t = i; length() - t >= n; ++t) {
841                 // search until (*this)[i] == a[0]
842                 if (rep->s[t] == ptr[0]) {
843                         // check if the rest of the value_types match
844                         bool equal = true;
845                         for (size_type j = 0; j < n; ++j) {
846                                 if (rep->s[t + j] != ptr[j]) {
847                                         equal = false;
848                                         break;
849                                 }
850                         }
851                         if (equal) return t;
852                 }
853         }
854         return npos;
855 }
856
857
858 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
859 {
860         Assert(s);
861         if (!rep->sz) return npos;
862         
863         Assert(i < rep->sz);
864         TestlyxstringInvariant(this);
865
866         if (!s || !*s) return npos;
867         return find(s, i, strlen(s));
868 }
869
870
871 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
872 {
873         if (!rep->sz) return npos;
874
875         Assert(i < rep->sz);
876         TestlyxstringInvariant(this);
877
878         for (size_type t = 0; t + i < length(); ++t) {
879                 if (rep->s[t + i] == c) return t + i;
880         }
881         return npos;
882 }
883
884
885 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
886 {
887         TestlyxstringInvariant(this);
888
889         size_type ii = min(length() - 1, i);
890         do {
891                 if (a[a.length() - 1] == rep->s[ii]) {
892                         int t = length() - 2;
893                         size_type l = ii - 1;
894                         for (; t >= 0; --t, --l) {
895                                 if (a[t] != rep->s[l]) break;
896                         }
897                         if (a[t] == rep->s[l]) return l;
898                 }
899         } while(ii-- > 0);
900         return npos;
901 }
902
903
904 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
905                                   size_type n) const
906 {
907         Assert(ptr);
908         TestlyxstringInvariant(this);
909         if (!*ptr) return npos;
910
911         size_type ii = min(length() - 1, i);
912         do {
913                 if (ptr[n - 1] == rep->s[ii]) {
914                         int t = n - 2;
915                         size_type l = ii - 1;
916                         for (; t >= 0; --t, --l) {
917                                 if (ptr[t] != rep->s[l]) break;
918                         }
919                         if (ptr[t] == rep->s[l]) return l;
920                 }
921         } while (ii-- > 0);
922         return npos;
923 }
924
925
926 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
927 {
928         Assert(ptr);
929         TestlyxstringInvariant(this);
930         if (!*ptr) return npos;
931
932         size_type ii = min(length() - 1, i);
933         do {
934                 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
935                         int t = strlen(ptr) - 2;
936                         size_type l = ii - 1;
937                         for (; t >= 0; --t, --l) {
938                                 if (ptr[t] != rep->s[l]) break;
939                         }
940                         if (ptr[t] == rep->s[l]) return l;
941                 }
942         } while (ii-- > 0);
943         return npos;
944 }
945
946
947 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
948 {
949         TestlyxstringInvariant(this);
950
951         size_type ii = min(length() - 1, i);
952         for (size_type t = ii; t != 0; --t) {
953                 if (rep->s[t] == c) return t;
954         }
955         return npos;
956 }
957
958
959 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
960                                           size_type i) const
961 {
962         Assert(i < rep->sz);
963         TestlyxstringInvariant(this);
964
965         for (size_type t = i; t < length(); ++t) {
966                 if (a.find(rep->s[t]) != npos) return t;
967         }
968         return npos;
969 }
970
971
972 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
973                                           size_type n) const
974 {
975         Assert(ptr && i < rep->sz);
976         TestlyxstringInvariant(this);
977         if (!n) return npos;
978
979         for (size_type t = i; t < length(); ++t) {
980                 if(memchr(ptr, rep->s[t], n) != 0) return t;
981         }
982         return npos;
983 }
984
985
986 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
987                                           size_type i) const
988 {
989         Assert(ptr && i < rep->sz);
990         TestlyxstringInvariant(this);
991
992         for (size_type t = i; t < length(); ++t) {
993                 if (strchr(ptr, rep->s[t]) != 0) return t;
994         }
995         return npos;
996 }
997
998
999 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1000 {
1001         Assert(i < rep->sz);
1002         TestlyxstringInvariant(this);
1003
1004         for (size_type t = i; t < length(); ++t) {
1005                 if (rep->s[t] == c) return t;
1006         }
1007         return npos;
1008 }
1009
1010
1011 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1012                                          size_type i) const
1013 {
1014         TestlyxstringInvariant(this);
1015
1016         size_type ii = min(length() - 1, i);
1017         for (int t = ii; t >= 0; --t) {
1018                 if (a.find(rep->s[t]) != npos) return t;
1019         }
1020         return npos;
1021 }
1022
1023 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1024                                          size_type n) const
1025 {
1026         Assert(ptr);
1027         TestlyxstringInvariant(this);
1028         if (!n) return npos;
1029
1030         size_type ii = min(length() - 1, i);
1031         for (int t = ii; t >= 0; --t) {
1032                 if(memchr(ptr, rep->s[t], n) != 0) return t;
1033         }
1034         return npos;
1035 }
1036
1037
1038 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1039                                          size_type i) const
1040 {
1041         Assert(ptr);
1042         TestlyxstringInvariant(this);
1043
1044         size_type ii = min(length() - 1, i);
1045         for (int t = ii; t >= 0; --t) {
1046                 if (strchr(ptr, rep->s[t]) != 0) return t;
1047         }
1048         return npos;
1049 }
1050
1051
1052 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1053 {
1054         TestlyxstringInvariant(this);
1055
1056         if (!rep->sz) return npos;
1057         size_type ii = min(length() - 1, i);
1058         for (int t = ii; t >= 0; --t) {
1059                 if (rep->s[t] == c) return t;
1060         }
1061         return npos;
1062 }
1063
1064
1065 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1066                                               size_type i) const
1067 {
1068         TestlyxstringInvariant(this);
1069
1070         if (!rep->sz) return npos;
1071         Assert(i < rep->sz);
1072         for (size_type t = i; t < length(); ++t) {
1073                 if (a.find(rep->s[t]) == npos) return t;
1074         }
1075         return npos;
1076 }
1077
1078
1079 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1080                                               size_type n) const
1081 {
1082         Assert(ptr && i < rep->sz);
1083         TestlyxstringInvariant(this);
1084
1085         if (!n) return (i < length()) ? i : npos;
1086         for (size_type t = i; t < length(); ++t) {
1087                 if(memchr(ptr, rep->s[t], n) == 0) return t;
1088         }
1089         return npos;
1090 }
1091
1092
1093 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1094                                               size_type i) const
1095 {
1096         Assert(ptr && i < rep->sz);
1097         TestlyxstringInvariant(this);
1098
1099         for (size_type t = i; t < length(); ++t) {
1100                 if (strchr(ptr, rep->s[t]) == 0) return t;
1101         }
1102         return npos;
1103 }
1104
1105
1106 lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const
1107 {
1108         if (!rep->sz) return npos;
1109         Assert(i < rep->sz);
1110         TestlyxstringInvariant(this);
1111
1112         for (size_type t = i; t < length(); ++t) {
1113                 if (rep->s[t] != c) return t;
1114         }
1115         return npos;
1116 }
1117
1118
1119 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1120                                              size_type i) const
1121 {
1122         TestlyxstringInvariant(this);
1123
1124         size_type ii = min(length() - 1, i);
1125         for (int t = ii; t >= 0; --t) {
1126                 if (a.find(rep->s[t]) == npos) return t;
1127         }
1128         return npos;
1129 }
1130
1131
1132 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1133                                                  size_type i,
1134                                                  size_type n) const
1135 {
1136         Assert(ptr);
1137         TestlyxstringInvariant(this);
1138
1139         if (!n) return npos;
1140         size_type ii = min(length() - 1, i);
1141         //if (!n) return (ii >= 0) ? ii : npos;
1142         for (int t = ii; t >= 0; --t) {
1143                 if(memchr(ptr, rep->s[t], n) == 0) return t;
1144         }
1145         return npos;
1146 }
1147
1148
1149 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1150                                              size_type i) const
1151 {
1152         Assert(ptr);
1153         TestlyxstringInvariant(this);
1154
1155         size_type ii = min(length() - 1, i);
1156         for (int t = ii; t >= 0; --t) {
1157                 if (strchr(ptr, rep->s[t]) == 0) return t;
1158         }
1159         return npos;
1160 }
1161
1162
1163 lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) const
1164 {
1165         TestlyxstringInvariant(this);
1166
1167         size_type ii = min(length() - 1, i);
1168         for (int t = ii; t >= 0; --t) {
1169                 if (rep->s[t] != c) return t;
1170         }
1171         return npos;
1172 }
1173
1174
1175 /////////////////
1176 // Replace
1177 /////////////////
1178
1179 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1180 {
1181         Assert(i < rep->sz || i == 0);
1182         TestlyxstringInvariant(this);
1183
1184         return replace(i, n, x, 0, x.length());
1185 }
1186
1187
1188 lyxstring &  lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1189                             size_type i2, size_type n2)
1190 {
1191         Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1192         TestlyxstringInvariant(this);
1193
1194         rep = rep->get_own_copy();
1195         rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1196         return *this;
1197 }
1198
1199
1200 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
1201                            size_type n2)
1202 {
1203         Assert(p && i < rep->sz);
1204         TestlyxstringInvariant(this);
1205
1206         rep = rep->get_own_copy();
1207         rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1208         return *this;
1209 }
1210
1211
1212 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1213 {
1214         Assert(p && i < rep->sz);
1215         TestlyxstringInvariant(this);
1216
1217         return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1218 }
1219
1220
1221 lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c)
1222 {
1223         Assert(i < rep->sz);
1224         TestlyxstringInvariant(this);
1225
1226         rep = rep->get_own_copy();
1227         value_type * tmp = new value_type[n2];
1228         memset(tmp, c, n2);
1229         rep->replace(i, min(n, rep->sz), tmp, n2);
1230         delete[] tmp;
1231         return *this;
1232 }
1233
1234
1235 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1236 {
1237         TestlyxstringInvariant(this);
1238
1239         return replace(i - begin(), i2 - i, str); 
1240 }
1241
1242
1243 lyxstring & lyxstring::replace(iterator i, iterator i2,
1244                            value_type const * p, size_type n)
1245 {
1246         Assert(p);
1247         TestlyxstringInvariant(this);
1248
1249         return replace(i - begin(), i2 - i, p, n);
1250 }
1251
1252
1253 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1254 {
1255         Assert(p);
1256         TestlyxstringInvariant(this);
1257
1258         return replace(i - begin(), i2 - i, p);
1259 }
1260
1261
1262 lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type c)
1263 {
1264         TestlyxstringInvariant(this);
1265
1266         return replace(i - begin(), i2 - i, n, c);
1267 }
1268         
1269
1270 lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
1271 {
1272         TestlyxstringInvariant(this);
1273
1274         return replace(i - begin(), i2 - i, j, j2 - j);
1275 }
1276
1277
1278 lyxstring & lyxstring::erase(size_type i, size_type n)
1279 {
1280         Assert(i < rep->sz || i == 0);
1281         TestlyxstringInvariant(this);
1282
1283         rep = rep->get_own_copy();
1284         if (i == 0 && n >= rep->sz) {
1285                 rep->sz = 0;
1286         } else {
1287                 n = min(n, rep->sz - i);
1288                 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1289                 rep->sz -= n;
1290         }
1291         return *this;
1292 }
1293
1294
1295 lyxstring::iterator lyxstring::erase(iterator i)
1296 {
1297         TestlyxstringInvariant(this);
1298
1299         // what iterator is this supposed to return?
1300         // the iterator after the one erased
1301         erase(i - begin(), 1);
1302         return begin(); // BUG
1303 }
1304
1305
1306 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1307 {
1308         TestlyxstringInvariant(this);
1309
1310         erase(first - begin(), last - first);
1311         return begin(); // BUG
1312 }
1313
1314
1315 /////////////////////////////////////
1316 // Conversion to C-style Strings
1317 /////////////////////////////////////
1318
1319 lyxstring::value_type const * lyxstring::c_str() const
1320 {
1321         rep->s[length()] = '\0';
1322         return rep->s;
1323 }
1324
1325
1326 lyxstring::value_type const * lyxstring::data() const
1327 {
1328         return rep->s;
1329 }
1330
1331
1332 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const
1333 {
1334         Assert(buf);
1335         TestlyxstringInvariant(this);
1336
1337         register int nn = min(len, length() - pos);
1338         memcpy(buf, &(rep->s[pos]), nn);
1339         return nn;
1340 }
1341
1342
1343 ////////////////////
1344 // Comparisons
1345 ////////////////////
1346
1347 // Compare funcs should be verified.
1348 // Should we try to make them work with '\0' value_types?
1349 // An STL string can usually contain '\0' value_types.
1350
1351 int lyxstring::compare(lyxstring const & str) const
1352 {
1353         TestlyxstringInvariant(this);
1354
1355         return compare(0, rep->sz, str.rep->s, str.rep->sz);
1356 }
1357
1358
1359 int lyxstring::compare(value_type const * s) const
1360 {
1361         Assert(s);
1362         TestlyxstringInvariant(this);
1363
1364         return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1365 }
1366
1367
1368 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1369 {
1370         TestlyxstringInvariant(this);
1371
1372         return compare(pos, n, str.rep->s, str.rep->sz);
1373 }
1374
1375
1376 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1377                      size_type pos2, size_type n2) const
1378 {
1379         TestlyxstringInvariant(this);
1380
1381         return compare(pos, n, str.rep->s + pos2, n2);
1382 }
1383
1384
1385 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1386                      size_type n2) const
1387 {
1388         Assert(s && (pos < rep->sz || pos == 0));
1389         TestlyxstringInvariant(this);
1390
1391         if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1392         if (!*s) return 1;
1393         // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1394
1395         // remember that n can very well be a lot larger than rep->sz
1396         // so we have to ensure that n is no larger than rep->sz
1397         n = min(n, rep->sz);
1398         n2 = min(n2, strlen(s));
1399         if (n == n2)
1400                 return memcmp(&(rep->s[pos]), s, n);
1401         int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1402         if (c)
1403                 return c;
1404         if (n < n2)
1405                 return -1;
1406         return 1;
1407 }
1408
1409
1410 /////////////////
1411 // Substrings
1412 /////////////////
1413
1414 // i = index, n = length
1415 lyxstring lyxstring::substr(size_type i, size_type n) const
1416 {
1417         Assert(i < rep->sz || i == 0);
1418         TestlyxstringInvariant(this);
1419
1420         return lyxstring(*this, i, n);
1421 }
1422
1423
1424
1425 /////////////////////////////////////////////
1426 // String operators, non member functions
1427 /////////////////////////////////////////////
1428
1429 bool operator==(lyxstring const & a, lyxstring const & b)
1430 {
1431         return a.compare(b) == 0;
1432 }
1433
1434
1435 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1436 {
1437         Assert(a);
1438         return b.compare(a) == 0;
1439 }
1440
1441
1442 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1443 {
1444         Assert(b);
1445         return a.compare(b) == 0;
1446 }
1447
1448
1449 bool operator!=(lyxstring const & a, lyxstring const & b)
1450 {
1451         return a.compare(b) != 0;
1452 }
1453
1454
1455 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1456 {
1457         Assert(a);
1458         return b.compare(a) != 0;
1459 }
1460
1461
1462 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1463 {
1464         Assert(b);
1465         return a.compare(b) != 0;
1466 }
1467
1468
1469 bool operator>(lyxstring const & a, lyxstring const & b)
1470 {
1471         return a.compare(b) > 0;
1472 }
1473
1474
1475 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1476 {
1477         Assert(a);
1478         return b.compare(a) < 0; // since we reverse the parameters
1479 }
1480
1481
1482 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1483 {
1484         Assert(b);
1485         return a.compare(b) > 0;
1486 }
1487
1488
1489 bool operator<(lyxstring const & a, lyxstring const & b)
1490 {
1491         return a.compare(b) < 0;
1492 }
1493
1494
1495 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1496 {
1497         Assert(a);
1498         return b.compare(a) > 0; // since we reverse the parameters
1499 }
1500
1501
1502 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1503 {
1504         Assert(b);
1505         return a.compare(b) < 0;
1506 }
1507
1508
1509 bool operator>=(lyxstring const & a, lyxstring const & b)
1510 {
1511         return a.compare(b) >= 0;
1512 }
1513
1514
1515 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1516 {
1517         Assert(a);
1518         return b.compare(a) <= 0; // since we reverse the parameters
1519 }
1520
1521
1522 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1523 {
1524         Assert(b);
1525         return a.compare(b) >= 0;
1526 }
1527
1528
1529 bool operator<=(lyxstring const & a, lyxstring const & b)
1530 {
1531         return a.compare(b) <= 0;
1532 }
1533
1534
1535 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1536 {
1537         Assert(a);
1538         return b.compare(a) >= 0; // since we reverse the parameters
1539 }
1540
1541
1542 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1543 {
1544         Assert(b);
1545         return a.compare(b) <= 0;
1546 }
1547
1548
1549 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1550 {
1551         lyxstring tmp(a);
1552         tmp += b;
1553         return tmp;
1554 }
1555
1556
1557 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1558 {
1559         Assert(a);
1560         lyxstring tmp(a);
1561         tmp += b;
1562         return tmp;
1563 }
1564
1565
1566 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1567 {
1568         lyxstring tmp;
1569         tmp += a;
1570         tmp += b;
1571         return tmp;
1572 }
1573
1574
1575 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1576 {
1577         Assert(b);
1578         lyxstring tmp(a);
1579         tmp += b;
1580         return tmp;
1581 }
1582
1583
1584 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1585 {
1586         lyxstring tmp(a);
1587         tmp += b;
1588         return tmp;
1589 }
1590
1591 #include <iostream>
1592
1593 istream & operator>>(istream & is, lyxstring & s)
1594 {
1595         // very bad solution
1596         char * nome = new char[1024];
1597         is >> nome;
1598         lyxstring tmp(nome);
1599         delete [] nome;
1600         if (!tmp.empty()) s = tmp;
1601         return is;
1602 }
1603
1604 ostream & operator<<(ostream & o, lyxstring const & s)
1605 {
1606         return o.write(s.data(), s.length());
1607 }
1608
1609 istream & getline(istream & is, lyxstring & s,
1610                   lyxstring::value_type delim)
1611 {
1612         // very bad solution
1613         char tmp;
1614         s.erase();
1615         while(is) {
1616                 is >> tmp;
1617                 if (tmp != delim) {
1618                         s += tmp;
1619                 } else {
1620                         break;
1621                 }
1622         }
1623         return is;
1624 }