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