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