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