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