]> git.lyx.org Git - lyx.git/blob - src/support/lyxstring.C
removed a warning from screen and added CFLAGS in lyx.spec.in.
[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) return npos;
868         
869         Assert(i < rep->sz);
870         TestlyxstringInvariant(this);
871
872         for (size_type t = i; rep->sz - t >= a.length(); ++t) {
873                 // search until (*this)[i] == a[0]
874                 if (rep->s[t] == a[0]) {
875                         // check if the rest of the value_types match
876                         bool equal = true;
877                         for (size_type j = 0; j < a.length(); ++j) {
878                                 if (rep->s[t + j] != a[j]) {
879                                         equal = false;
880                                         break;
881                                 }
882                         }
883                         if (equal) return t;
884                 }
885         }
886         return npos;
887 }
888
889
890 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
891                                  size_type n) const
892 {
893         Assert(ptr);
894         if (!rep->sz || !*ptr) return npos;
895         
896         Assert(i < rep->sz);
897         TestlyxstringInvariant(this);
898
899         // What is "n" here? is it the number of value_types to use in ptr
900         // or does "i" and "n" togeter form a substring to search
901         // for ptr in? For now I will assume that "n" tells the length
902         // of ptr. (Lgb)
903         n = min(n, strlen(ptr));
904         for (size_type t = i; length() - t >= n; ++t) {
905                 // search until (*this)[i] == a[0]
906                 if (rep->s[t] == ptr[0]) {
907                         // check if the rest of the value_types match
908                         bool equal = true;
909                         for (size_type j = 0; j < n; ++j) {
910                                 if (rep->s[t + j] != ptr[j]) {
911                                         equal = false;
912                                         break;
913                                 }
914                         }
915                         if (equal) return t;
916                 }
917         }
918         return npos;
919 }
920
921
922 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
923 {
924         Assert(s);
925         if (!rep->sz) return npos;
926         
927         Assert(i < rep->sz);
928         TestlyxstringInvariant(this);
929
930         if (!s || !*s) return npos;
931         return find(s, i, strlen(s));
932 }
933
934
935 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
936 {
937         if (!rep->sz) return npos;
938
939         Assert(i < rep->sz);
940         TestlyxstringInvariant(this);
941
942         for (size_type t = 0; t + i < length(); ++t) {
943                 if (rep->s[t + i] == c) return t + i;
944         }
945         return npos;
946 }
947
948
949 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
950 {
951         TestlyxstringInvariant(this);
952
953         size_type ii = min(length() - 1, i);
954         do {
955                 if (a[a.length() - 1] == rep->s[ii]) {
956                         int t = length() - 2;
957                         size_type l = ii - 1;
958                         for (; t >= 0; --t, --l) {
959                                 if (a[t] != rep->s[l]) break;
960                         }
961                         if (a[t] == rep->s[l]) return l;
962                 }
963         } while(ii-- > 0);
964         return npos;
965 }
966
967
968 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
969                                   size_type n) const
970 {
971         Assert(ptr);
972         TestlyxstringInvariant(this);
973         if (!*ptr) return npos;
974
975         size_type ii = min(length() - 1, i);
976         do {
977                 if (ptr[n - 1] == rep->s[ii]) {
978                         int t = n - 2;
979                         size_type l = ii - 1;
980                         for (; t >= 0; --t, --l) {
981                                 if (ptr[t] != rep->s[l]) break;
982                         }
983                         if (ptr[t] == rep->s[l]) return l;
984                 }
985         } while (ii-- > 0);
986         return npos;
987 }
988
989
990 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
991 {
992         Assert(ptr);
993         TestlyxstringInvariant(this);
994         if (!*ptr) return npos;
995
996         size_type ii = min(length() - 1, i);
997         do {
998                 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
999                         int t = strlen(ptr) - 2;
1000                         size_type l = ii - 1;
1001                         for (; t >= 0; --t, --l) {
1002                                 if (ptr[t] != rep->s[l]) break;
1003                         }
1004                         if (ptr[t] == rep->s[l]) return l;
1005                 }
1006         } while (ii-- > 0);
1007         return npos;
1008 }
1009
1010
1011 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1012 {
1013         TestlyxstringInvariant(this);
1014
1015         size_type ii = min(length() - 1, i);
1016         for (size_type t = ii; t != 0; --t) {
1017                 if (rep->s[t] == c) return t;
1018         }
1019         return npos;
1020 }
1021
1022
1023 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1024                                           size_type i) const
1025 {
1026         Assert(i < rep->sz);
1027         TestlyxstringInvariant(this);
1028
1029         for (size_type t = i; t < length(); ++t) {
1030                 if (a.find(rep->s[t]) != npos) return t;
1031         }
1032         return npos;
1033 }
1034
1035
1036 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
1037                                           size_type n) const
1038 {
1039         Assert(ptr && i < rep->sz);
1040         TestlyxstringInvariant(this);
1041         if (!n) return npos;
1042
1043         for (size_type t = i; t < length(); ++t) {
1044                 if(memchr(ptr, rep->s[t], n) != 0) return t;
1045         }
1046         return npos;
1047 }
1048
1049
1050 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1051                                           size_type i) const
1052 {
1053         Assert(ptr && i < rep->sz);
1054         TestlyxstringInvariant(this);
1055
1056         for (size_type t = i; t < length(); ++t) {
1057                 if (strchr(ptr, rep->s[t]) != 0) return t;
1058         }
1059         return npos;
1060 }
1061
1062
1063 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1064 {
1065         Assert(i < rep->sz);
1066         TestlyxstringInvariant(this);
1067
1068         for (size_type t = i; t < length(); ++t) {
1069                 if (rep->s[t] == c) return t;
1070         }
1071         return npos;
1072 }
1073
1074
1075 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1076                                          size_type i) const
1077 {
1078         TestlyxstringInvariant(this);
1079
1080         size_type ii = min(length() - 1, i);
1081         for (int t = ii; t >= 0; --t) {
1082                 if (a.find(rep->s[t]) != npos) return t;
1083         }
1084         return npos;
1085 }
1086
1087 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
1088                                          size_type n) const
1089 {
1090         Assert(ptr);
1091         TestlyxstringInvariant(this);
1092         if (!n) return npos;
1093
1094         size_type ii = min(length() - 1, i);
1095         for (int t = ii; t >= 0; --t) {
1096                 if(memchr(ptr, rep->s[t], n) != 0) return t;
1097         }
1098         return npos;
1099 }
1100
1101
1102 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1103                                          size_type i) const
1104 {
1105         Assert(ptr);
1106         TestlyxstringInvariant(this);
1107
1108         size_type ii = min(length() - 1, i);
1109         for (int t = ii; t >= 0; --t) {
1110                 if (strchr(ptr, rep->s[t]) != 0) return t;
1111         }
1112         return npos;
1113 }
1114
1115
1116 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1117 {
1118         TestlyxstringInvariant(this);
1119
1120         if (!rep->sz) return npos;
1121         size_type ii = min(length() - 1, i);
1122         for (int t = ii; t >= 0; --t) {
1123                 if (rep->s[t] == c) return t;
1124         }
1125         return npos;
1126 }
1127
1128
1129 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1130                                               size_type i) const
1131 {
1132         TestlyxstringInvariant(this);
1133
1134         if (!rep->sz) return npos;
1135         Assert(i < rep->sz);
1136         for (size_type t = i; t < length(); ++t) {
1137                 if (a.find(rep->s[t]) == npos) return t;
1138         }
1139         return npos;
1140 }
1141
1142
1143 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
1144                                               size_type n) const
1145 {
1146         Assert(ptr && i < rep->sz);
1147         TestlyxstringInvariant(this);
1148
1149         if (!n) return (i < length()) ? i : npos;
1150         for (size_type t = i; t < length(); ++t) {
1151                 if(memchr(ptr, rep->s[t], n) == 0) return t;
1152         }
1153         return npos;
1154 }
1155
1156
1157 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1158                                               size_type i) const
1159 {
1160         Assert(ptr && i < rep->sz);
1161         TestlyxstringInvariant(this);
1162
1163         for (size_type t = i; t < length(); ++t) {
1164                 if (strchr(ptr, rep->s[t]) == 0) return t;
1165         }
1166         return npos;
1167 }
1168
1169
1170 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1171                                                   size_type i) const
1172 {
1173         if (!rep->sz) return npos;
1174         Assert(i < rep->sz);
1175         TestlyxstringInvariant(this);
1176
1177         for (size_type t = i; t < length(); ++t) {
1178                 if (rep->s[t] != c) return t;
1179         }
1180         return npos;
1181 }
1182
1183
1184 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1185                                                  size_type i) const
1186 {
1187         TestlyxstringInvariant(this);
1188
1189         size_type ii = min(length() - 1, i);
1190         for (int t = ii; t >= 0; --t) {
1191                 if (a.find(rep->s[t]) == npos) return t;
1192         }
1193         return npos;
1194 }
1195
1196
1197 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1198                                                  size_type i,
1199                                                  size_type n) const
1200 {
1201         Assert(ptr);
1202         TestlyxstringInvariant(this);
1203
1204         if (!n) return npos;
1205         size_type ii = min(length() - 1, i);
1206         //if (!n) return (ii >= 0) ? ii : npos;
1207         for (int t = ii; t >= 0; --t) {
1208                 if(memchr(ptr, rep->s[t], n) == 0) return t;
1209         }
1210         return npos;
1211 }
1212
1213
1214 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1215                                                  size_type i) const
1216 {
1217         Assert(ptr);
1218         TestlyxstringInvariant(this);
1219
1220         size_type ii = min(length() - 1, i);
1221         for (int t = ii; t >= 0; --t) {
1222                 if (strchr(ptr, rep->s[t]) == 0) return t;
1223         }
1224         return npos;
1225 }
1226
1227
1228 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1229                                                  size_type i) const
1230 {
1231         TestlyxstringInvariant(this);
1232
1233         size_type ii = min(length() - 1, i);
1234         for (int t = ii; t >= 0; --t) {
1235                 if (rep->s[t] != c) return t;
1236         }
1237         return npos;
1238 }
1239
1240
1241 /////////////////
1242 // Replace
1243 /////////////////
1244
1245 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1246 {
1247         Assert(i < rep->sz || i == 0);
1248         TestlyxstringInvariant(this);
1249
1250         return replace(i, n, x, 0, x.length());
1251 }
1252
1253
1254 lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x,
1255                                size_type i2, size_type n2)
1256 {
1257         Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
1258         TestlyxstringInvariant(this);
1259
1260         rep = rep->get_own_copy();
1261         rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1262         return *this;
1263 }
1264
1265
1266 lyxstring & lyxstring::replace(size_type i, size_type n,
1267                                value_type const * p, size_type n2)
1268 {
1269         Assert(p && i < rep->sz);
1270         TestlyxstringInvariant(this);
1271
1272         rep = rep->get_own_copy();
1273         rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1274         return *this;
1275 }
1276
1277
1278 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1279 {
1280         Assert(p && i < rep->sz);
1281         TestlyxstringInvariant(this);
1282
1283         return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1284 }
1285
1286
1287 lyxstring & lyxstring::replace(size_type i, size_type n,
1288                                size_type n2, value_type c)
1289 {
1290         Assert(i < rep->sz);
1291         TestlyxstringInvariant(this);
1292
1293         rep = rep->get_own_copy();
1294         value_type * tmp = new value_type[n2];
1295         memset(tmp, c, n2);
1296         rep->replace(i, min(n, rep->sz), tmp, n2);
1297         delete[] tmp;
1298         return *this;
1299 }
1300
1301
1302 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1303 {
1304         TestlyxstringInvariant(this);
1305
1306         return replace(i - begin(), i2 - i, str); 
1307 }
1308
1309
1310 lyxstring & lyxstring::replace(iterator i, iterator i2,
1311                                value_type const * p, size_type n)
1312 {
1313         Assert(p);
1314         TestlyxstringInvariant(this);
1315
1316         return replace(i - begin(), i2 - i, p, n);
1317 }
1318
1319
1320 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1321 {
1322         Assert(p);
1323         TestlyxstringInvariant(this);
1324
1325         return replace(i - begin(), i2 - i, p);
1326 }
1327
1328
1329 lyxstring & lyxstring::replace(iterator i, iterator i2,
1330                                size_type n , value_type c)
1331 {
1332         TestlyxstringInvariant(this);
1333
1334         return replace(i - begin(), i2 - i, n, c);
1335 }
1336         
1337
1338 lyxstring & lyxstring::replace(iterator i, iterator i2,
1339                                iterator j, iterator j2)
1340 {
1341         TestlyxstringInvariant(this);
1342
1343         return replace(i - begin(), i2 - i, j, j2 - j);
1344 }
1345
1346
1347 lyxstring & lyxstring::erase(size_type i, size_type n)
1348 {
1349         Assert(i < rep->sz || i == 0);
1350         TestlyxstringInvariant(this);
1351
1352         rep = rep->get_own_copy();
1353         if (i == 0 && n >= rep->sz) {
1354                 rep->sz = 0;
1355         } else {
1356                 n = min(n, rep->sz - i);
1357                 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1358                 rep->sz -= n;
1359         }
1360         return *this;
1361 }
1362
1363
1364 lyxstring::iterator lyxstring::erase(iterator i)
1365 {
1366         TestlyxstringInvariant(this);
1367
1368         // what iterator is this supposed to return?
1369         // the iterator after the one erased
1370         erase(i - begin(), 1);
1371         return begin(); // BUG
1372 }
1373
1374
1375 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1376 {
1377         TestlyxstringInvariant(this);
1378
1379         erase(first - begin(), last - first);
1380         return begin(); // BUG
1381 }
1382
1383
1384 /////////////////////////////////////
1385 // Conversion to C-style Strings
1386 /////////////////////////////////////
1387
1388 lyxstring::value_type const * lyxstring::c_str() const
1389 {
1390         rep->s[length()] = '\0';
1391         return rep->s;
1392 }
1393
1394
1395 lyxstring::value_type const * lyxstring::data() const
1396 {
1397         return rep->s;
1398 }
1399
1400
1401 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1402                                      size_type pos) const
1403 {
1404         Assert(buf);
1405         TestlyxstringInvariant(this);
1406
1407         register int nn = min(len, length() - pos);
1408         memcpy(buf, &(rep->s[pos]), nn);
1409         return nn;
1410 }
1411
1412
1413 ////////////////////
1414 // Comparisons
1415 ////////////////////
1416
1417 // Compare funcs should be verified.
1418 // Should we try to make them work with '\0' value_types?
1419 // An STL string can usually contain '\0' value_types.
1420
1421 int lyxstring::compare(lyxstring const & str) const
1422 {
1423         TestlyxstringInvariant(this);
1424
1425         return compare(0, rep->sz, str.rep->s, str.rep->sz);
1426 }
1427
1428
1429 int lyxstring::compare(value_type const * s) const
1430 {
1431         Assert(s);
1432         TestlyxstringInvariant(this);
1433
1434         return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
1435 }
1436
1437
1438 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
1439 {
1440         TestlyxstringInvariant(this);
1441
1442         return compare(pos, n, str.rep->s, str.rep->sz);
1443 }
1444
1445
1446 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1447                      size_type pos2, size_type n2) const
1448 {
1449         TestlyxstringInvariant(this);
1450
1451         return compare(pos, n, str.rep->s + pos2, n2);
1452 }
1453
1454
1455 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1456                      size_type n2) const
1457 {
1458         Assert(s && (pos < rep->sz || pos == 0));
1459         TestlyxstringInvariant(this);
1460
1461         if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1462         if (!*s) return 1;
1463         // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
1464
1465         // remember that n can very well be a lot larger than rep->sz
1466         // so we have to ensure that n is no larger than rep->sz
1467         n = min(n, rep->sz);
1468         n2 = min(n2, strlen(s));
1469         if (n == n2)
1470                 return memcmp(&(rep->s[pos]), s, n);
1471         int c = memcmp(&(rep->s[pos]), s, min(n,n2));
1472         if (c)
1473                 return c;
1474         if (n < n2)
1475                 return -1;
1476         return 1;
1477 }
1478
1479
1480 /////////////////
1481 // Substrings
1482 /////////////////
1483
1484 // i = index, n = length
1485 lyxstring lyxstring::substr(size_type i, size_type n) const
1486 {
1487         Assert(i < rep->sz || i == 0);
1488         TestlyxstringInvariant(this);
1489
1490         return lyxstring(*this, i, n);
1491 }
1492
1493
1494 /////////////////////////////////////////////
1495 // String operators, non member functions
1496 /////////////////////////////////////////////
1497
1498 bool operator==(lyxstring const & a, lyxstring const & b)
1499 {
1500         return a.compare(b) == 0;
1501 }
1502
1503
1504 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1505 {
1506         Assert(a);
1507         return b.compare(a) == 0;
1508 }
1509
1510
1511 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1512 {
1513         Assert(b);
1514         return a.compare(b) == 0;
1515 }
1516
1517
1518 bool operator!=(lyxstring const & a, lyxstring const & b)
1519 {
1520         return a.compare(b) != 0;
1521 }
1522
1523
1524 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1525 {
1526         Assert(a);
1527         return b.compare(a) != 0;
1528 }
1529
1530
1531 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1532 {
1533         Assert(b);
1534         return a.compare(b) != 0;
1535 }
1536
1537
1538 bool operator>(lyxstring const & a, lyxstring const & b)
1539 {
1540         return a.compare(b) > 0;
1541 }
1542
1543
1544 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1545 {
1546         Assert(a);
1547         return b.compare(a) < 0; // since we reverse the parameters
1548 }
1549
1550
1551 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1552 {
1553         Assert(b);
1554         return a.compare(b) > 0;
1555 }
1556
1557
1558 bool operator<(lyxstring const & a, lyxstring const & b)
1559 {
1560         return a.compare(b) < 0;
1561 }
1562
1563
1564 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1565 {
1566         Assert(a);
1567         return b.compare(a) > 0; // since we reverse the parameters
1568 }
1569
1570
1571 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1572 {
1573         Assert(b);
1574         return a.compare(b) < 0;
1575 }
1576
1577
1578 bool operator>=(lyxstring const & a, lyxstring const & b)
1579 {
1580         return a.compare(b) >= 0;
1581 }
1582
1583
1584 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1585 {
1586         Assert(a);
1587         return b.compare(a) <= 0; // since we reverse the parameters
1588 }
1589
1590
1591 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1592 {
1593         Assert(b);
1594         return a.compare(b) >= 0;
1595 }
1596
1597
1598 bool operator<=(lyxstring const & a, lyxstring const & b)
1599 {
1600         return a.compare(b) <= 0;
1601 }
1602
1603
1604 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1605 {
1606         Assert(a);
1607         return b.compare(a) >= 0; // since we reverse the parameters
1608 }
1609
1610
1611 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1612 {
1613         Assert(b);
1614         return a.compare(b) <= 0;
1615 }
1616
1617
1618 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1619 {
1620         lyxstring tmp(a);
1621         tmp += b;
1622         return tmp;
1623 }
1624
1625
1626 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1627 {
1628         Assert(a);
1629         lyxstring tmp(a);
1630         tmp += b;
1631         return tmp;
1632 }
1633
1634
1635 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1636 {
1637         lyxstring tmp;
1638         tmp += a;
1639         tmp += b;
1640         return tmp;
1641 }
1642
1643
1644 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1645 {
1646         Assert(b);
1647         lyxstring tmp(a);
1648         tmp += b;
1649         return tmp;
1650 }
1651
1652
1653 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1654 {
1655         lyxstring tmp(a);
1656         tmp += b;
1657         return tmp;
1658 }
1659
1660 #include <iostream>
1661
1662 istream & operator>>(istream & is, lyxstring & s)
1663 {
1664         // very bad solution
1665         char * nome = new char[1024];
1666         is >> nome;
1667         lyxstring tmp(nome);
1668         delete [] nome;
1669         if (!tmp.empty()) s = tmp;
1670         return is;
1671 }
1672
1673 ostream & operator<<(ostream & o, lyxstring const & s)
1674 {
1675         return o.write(s.data(), s.length());
1676 }
1677
1678 istream & getline(istream & is, lyxstring & s,
1679                   lyxstring::value_type delim)
1680 {
1681         // very bad solution
1682         char tmp;
1683         s.erase();
1684         while(is) {
1685                 is >> tmp;
1686                 if (tmp != delim) {
1687                         s += tmp;
1688                 } else {
1689                         break;
1690                 }
1691         }
1692         return is;
1693 }