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