]> git.lyx.org Git - lyx.git/blob - src/support/lyxstring.C
remove commented HAVE_SSTREAM code
[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-2000 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         rep = rep->get_own_copy();
464         return rep->s;
465 }
466
467
468 lyxstring::const_iterator lyxstring::begin() const
469 {
470         return rep->s;
471 }
472
473
474 lyxstring::iterator lyxstring::end()
475 {
476         rep = rep->get_own_copy();
477         return rep->s + rep->sz;
478 }
479
480
481 lyxstring::const_iterator lyxstring::end() const
482 {
483         return rep->s + rep->sz;
484 }
485
486 #if 0
487 reverse_iterator lyxstring::rbegin()
488 {
489         return reverse_iterator( end() );
490 }
491
492
493 const_reverse_iterator lyxstring::rbegin() const
494 {
495         return const_reverse_iterator( end() );
496 }
497
498
499 reverse_iterator lyxstring::rend()
500 {
501         return reverse_iterator( begin() );
502 }
503
504
505 const_reverse_iterator lyxstring::rend() const
506 {
507         return const_reverse_iterator( begin() );
508 }
509 #endif
510
511
512 ///////////////////////
513 // Size and Capacity
514 ///////////////////////
515
516 lyxstring::size_type lyxstring::size() const
517
518         return rep->sz;
519 }
520
521
522 void lyxstring::resize(size_type n, value_type c)
523 {
524         Assert(n <= npos); // STD!
525         TestlyxstringInvariant(this);
526
527         // This resets sz to res_arg
528         rep = rep->get_own_copy();
529         rep->resize(n, c);
530 }
531
532
533 lyxstring::size_type lyxstring::capacity() const
534 {
535         return rep->res;
536 }
537
538
539 void lyxstring::reserve(size_type res_arg)
540 {
541         TestlyxstringInvariant(this);
542
543         rep = rep->get_own_copy();
544         rep->reserve(res_arg);
545 }
546
547
548 ////////////////
549 // Assignment
550 ////////////////
551
552 lyxstring & lyxstring::operator= (lyxstring const & x)
553 {
554         TestlyxstringInvariant(this);
555
556         return assign(x);
557 }
558
559
560 lyxstring & lyxstring::operator= (value_type const * s)
561 {
562         Assert(s); // OURS!
563         TestlyxstringInvariant(this);
564 //      printf("lyxstring::operator= (value_type const *)\n");
565
566         return assign(s);
567 }
568
569
570 lyxstring & lyxstring::operator=(value_type c)
571 {
572         TestlyxstringInvariant(this);
573
574         value_type s[1];
575         s[0] = c;
576         if (rep->ref == 1) // recycle rep
577                 rep->assign(1, s);
578         else {
579                 rep->ref--;
580                 rep = new Srep(1, s);
581         }
582         return *this;
583 }
584
585
586 lyxstring & lyxstring::assign(lyxstring const & x)
587 {
588         TestlyxstringInvariant(this);
589
590         x.rep->ref++; // protect against ``st = st''
591         if (--rep->ref == 0) delete rep;
592         rep = x.rep; // share representation
593         return *this;
594 }
595         
596
597 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
598 {
599         Assert(pos <= x.rep->sz); // STD!
600         TestlyxstringInvariant(this);
601
602         return assign(x.substr(pos, n));
603 }
604         
605
606 lyxstring & lyxstring::assign(value_type const * s, size_type n)
607 {
608         Assert(s); // OURS!
609         TestlyxstringInvariant(this);
610
611         n = min(strlen(s), n);
612         if (rep->ref == 1) // recycle rep
613                 rep->assign(n, s);
614         else {
615                 rep->ref--;
616                 rep = new Srep(n, s);
617         }
618         return *this;
619 }
620         
621
622 lyxstring & lyxstring::assign(value_type const * s)
623 {
624         Assert(s); // OURS!
625         TestlyxstringInvariant(this);
626
627         return assign(s, strlen(s));
628 }
629
630
631 lyxstring & lyxstring::assign(size_type n, value_type ch)
632 {
633         TestlyxstringInvariant(this);
634
635         rep = rep->get_own_copy();
636         rep->assign(n, ch);
637         return *this;
638 }
639
640
641 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
642 {
643         TestlyxstringInvariant(this);
644
645         rep = rep->get_own_copy();
646         rep->assign(last - first, first);
647         return *this;
648 }
649
650
651 ////////////////////
652 // Element Access
653 ////////////////////
654
655 lyxstring::const_reference lyxstring::operator[](size_type pos) const
656 {
657         Assert(pos <= rep->sz); // OURS!
658         static char helper = '\0';
659         return pos == rep->sz ? helper : rep->s[pos];
660 }
661
662
663 lyxstring::reference lyxstring::operator[](size_type pos)
664 {
665         Assert(pos < rep->sz); // OURS!
666         TestlyxstringInvariant(this);
667
668         rep = rep->get_own_copy();
669         return rep->s[pos];
670 }
671
672
673 lyxstring::const_reference lyxstring::at(size_type n) const
674 {
675         Assert(n < rep->sz); // STD!
676         return rep->s[n];
677 }
678
679
680 lyxstring::reference lyxstring::at(size_type n)
681 {
682         Assert(n < rep->sz); // STD!
683         TestlyxstringInvariant(this);
684
685         rep = rep->get_own_copy();
686         return rep->s[n];
687 }
688
689
690 /////////////
691 // Insert
692 /////////////
693
694 lyxstring & lyxstring::operator+=(lyxstring const & x)
695 {
696         TestlyxstringInvariant(this);
697
698         return append(x);
699 }
700
701
702 lyxstring & lyxstring::operator+=(value_type const * x)
703 {
704         Assert(x); // OURS!
705         TestlyxstringInvariant(this);
706
707         return append(x);
708 }
709
710
711 lyxstring & lyxstring::operator+=(value_type c)
712 {
713         TestlyxstringInvariant(this);
714
715         push_back(c);
716         return *this;
717 }
718
719
720 void lyxstring::push_back(value_type c)
721 {
722         TestlyxstringInvariant(this);
723
724         rep = rep->get_own_copy();
725         rep->push_back(c);
726 }
727
728
729 lyxstring & lyxstring::append(lyxstring const & x)
730 {
731         TestlyxstringInvariant(this);
732
733         if (x.empty()) return *this;
734         rep = rep->get_own_copy();
735         rep->append(x.length(), x.rep->s);
736         return *this;
737 }
738
739
740 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
741 {
742         Assert(pos <= x.rep->sz); // STD!
743         TestlyxstringInvariant(this);
744
745         return append(x.substr(pos, n));
746 }
747
748
749 lyxstring & lyxstring::append(value_type const * p, size_type n)
750 {
751         Assert(p); // OURS!
752         TestlyxstringInvariant(this);
753
754         if (!*p || !n) return *this;
755         rep = rep->get_own_copy();
756         rep->append(min(n, strlen(p)), p);
757         return *this;
758 }
759
760
761 lyxstring & lyxstring::append(value_type const * p)
762 {
763         Assert(p); // OURS!
764         TestlyxstringInvariant(this);
765
766         if (!*p) return *this;
767         rep = rep->get_own_copy();
768         rep->append(strlen(p), p);
769         return *this;
770 }
771
772
773 lyxstring & lyxstring::append(size_type n, value_type c)
774 {
775         TestlyxstringInvariant(this);
776
777         value_type * tmp = new value_type[n];
778         memset(tmp, c, n);
779         rep = rep->get_own_copy();
780         rep->append(n, tmp);
781         delete[] tmp;
782         return *this;
783 }
784
785
786 lyxstring & lyxstring::append(iterator first, iterator last)
787 {
788         TestlyxstringInvariant(this);
789
790         rep = rep->get_own_copy();
791         rep->append(last - first, first);
792         return *this;
793 }
794
795 // insert value_typeacters before (*this)[pos]
796
797 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
798 {
799         TestlyxstringInvariant(this);
800
801         return insert(pos, x, 0, x.rep->sz);
802 }
803
804
805 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
806                               size_type pos2, size_type n)
807 {
808         Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
809         TestlyxstringInvariant(this);
810
811         rep = rep->get_own_copy();
812         rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
813         return *this;
814 }
815
816
817 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
818 {
819         Assert(p); // OURS!
820         TestlyxstringInvariant(this);
821
822         if (*p && n) {
823                 // insert nothing and you change nothing
824                 rep = rep->get_own_copy();
825                 rep->insert(pos, p, min(n, strlen(p)));
826         }
827         return *this;
828 }
829
830
831 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
832 {
833         Assert(p); // OURS!
834         TestlyxstringInvariant(this);
835
836         if (*p) {
837                 // insert nothing and you change nothing
838                 rep = rep->get_own_copy();
839                 rep->insert(pos, p, strlen(p));
840         }
841         return *this;
842 }
843
844
845 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
846 {
847         TestlyxstringInvariant(this);
848
849         rep = rep->get_own_copy();
850         value_type * tmp = new value_type[n];
851         memset(tmp, c, n);
852         rep->insert(pos, tmp, n);
853         delete[] tmp;
854         return *this;
855 }
856
857
858 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
859 {
860         TestlyxstringInvariant(this);
861
862         // what iterator is this supposed to return??
863         size_type tmp = p - begin();
864         insert(p - begin(), 1, c);
865         return begin() + tmp + 1; // ??
866 }
867
868
869 void lyxstring::insert(iterator p, size_type n , value_type c)
870 {
871         TestlyxstringInvariant(this);
872
873         insert(p - begin(), n , c);
874 }
875
876
877 void lyxstring::insert(iterator p, iterator first, iterator last)
878 {
879         TestlyxstringInvariant(this);
880
881         insert(p - begin(), first, last - first);
882 }
883         
884
885 ////////////////
886 // Find
887 ////////////////
888  
889          // All the below find functions should be verified,
890          // it is very likely that I have mixed up or interpreted
891          // some of the parameters wrong, also some of the funcs can surely
892          // be written more effectively.
893
894 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
895 {
896         if (!rep->sz || i >= rep->sz) return npos;
897         
898         TestlyxstringInvariant(this);
899
900         for (size_type t = i; rep->sz - t >= a.length(); ++t) {
901                 // search until (*this)[i] == a[0]
902                 if (rep->s[t] == a[0]) {
903                         // check if the rest of the value_types match
904                         bool equal = true;
905                         for (size_type j = 0; j < a.length(); ++j) {
906                                 if (rep->s[t + j] != a[j]) {
907                                         equal = false;
908                                         break;
909                                 }
910                         }
911                         if (equal) return t;
912                 }
913         }
914         return npos;
915 }
916
917
918 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
919                                      size_type n) const
920 {
921         Assert(ptr); // OURS!
922         if (!rep->sz || !*ptr || i >= rep->sz) return npos;
923         
924         TestlyxstringInvariant(this);
925
926         // What is "n" here? is it the number of value_types to use in ptr
927         // or does "i" and "n" togeter form a substring to search
928         // for ptr in? For now I will assume that "n" tells the length
929         // of ptr. (Lgb)
930         n = min(n, strlen(ptr));
931         for (size_type t = i; rep->sz - t >= n; ++t) {
932                 // search until (*this)[i] == a[0]
933                 if (rep->s[t] == ptr[0]) {
934                         // check if the rest of the value_types match
935                         bool equal = true;
936                         for (size_type j = 0; j < n; ++j) {
937                                 if (rep->s[t + j] != ptr[j]) {
938                                         equal = false;
939                                         break;
940                                 }
941                         }
942                         if (equal) return t;
943                 }
944         }
945         return npos;
946 }
947
948
949 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
950 {
951         Assert(s); // OURS!
952         if (!rep->sz || i >= rep->sz) return npos;
953         
954         TestlyxstringInvariant(this);
955
956         if (!s || !*s) return npos;
957         return find(s, i, strlen(s));
958 }
959
960
961 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
962 {
963         if (!rep->sz || i >= rep->sz) return npos;
964
965         TestlyxstringInvariant(this);
966
967         for (size_type t = 0; t + i < rep->sz; ++t) {
968                 if (rep->s[t + i] == c) return t + i;
969         }
970         return npos;
971 }
972
973
974 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
975 {
976         TestlyxstringInvariant(this);
977
978         size_type ii = min(rep->sz - 1, i);
979         do {
980                 if (a[a.length() - 1] == rep->s[ii]) {
981                         int t = rep->sz - 2;
982                         size_type l = ii - 1;
983                         for (; t >= 0; --t, --l) {
984                                 if (a[t] != rep->s[l]) break;
985                         }
986                         if (a[t] == rep->s[l]) return l;
987                 }
988         } while(ii-- > 0);
989         return npos;
990 }
991
992
993 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
994                                       size_type n) const
995 {
996         Assert(ptr); // OURS!
997         TestlyxstringInvariant(this);
998         if (!*ptr) return npos;
999
1000         size_type ii = min(rep->sz - 1, i);
1001         do {
1002                 if (ptr[n - 1] == rep->s[ii]) {
1003                         int t = n - 2;
1004                         size_type l = ii - 1;
1005                         for (; t >= 0; --t, --l) {
1006                                 if (ptr[t] != rep->s[l]) break;
1007                         }
1008                         if (ptr[t] == rep->s[l]) return l;
1009                 }
1010         } while (ii-- > 0);
1011         return npos;
1012 }
1013
1014
1015 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1016                                       size_type i) const
1017 {
1018         Assert(ptr); // OURS!
1019         TestlyxstringInvariant(this);
1020         if (!*ptr) return npos;
1021
1022         size_type ii = min(rep->sz - 1, i);
1023         do {
1024                 if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
1025                         int t = strlen(ptr) - 2;
1026                         size_type l = ii - 1;
1027                         for (; t >= 0; --t, --l) {
1028                                 if (ptr[t] != rep->s[l]) break;
1029                         }
1030                         if (ptr[t] == rep->s[l]) return l;
1031                 }
1032         } while (ii-- > 0);
1033         return npos;
1034 }
1035
1036
1037 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1038 {
1039         TestlyxstringInvariant(this);
1040
1041         size_type ii = min(rep->sz - 1, i);
1042         for (size_type t = ii; t != 0; --t) {
1043                 if (rep->s[t] == c) return t;
1044         }
1045         return npos;
1046 }
1047
1048
1049 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1050                                               size_type i) const
1051 {
1052         Assert(i < rep->sz); // OURS!
1053         TestlyxstringInvariant(this);
1054
1055         for (size_type t = i; t < rep->sz; ++t) {
1056                 if (a.find(rep->s[t]) != npos) return t;
1057         }
1058         return npos;
1059 }
1060
1061
1062 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1063                                               size_type i,
1064                                               size_type n) const
1065 {
1066         Assert(ptr && i < rep->sz); // OURS!
1067         TestlyxstringInvariant(this);
1068         if (!n) return npos;
1069
1070         for (size_type t = i; t < rep->sz; ++t) {
1071                 if(memchr(ptr, rep->s[t], n) != 0) return t;
1072         }
1073         return npos;
1074 }
1075
1076
1077 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1078                                               size_type i) const
1079 {
1080         Assert(ptr && i < rep->sz); // OURS!
1081         TestlyxstringInvariant(this);
1082
1083         for (size_type t = i; t < rep->sz; ++t) {
1084                 if (strchr(ptr, rep->s[t]) != 0) return t;
1085         }
1086         return npos;
1087 }
1088
1089
1090 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1091 {
1092         Assert(i < rep->sz); // OURS!
1093         TestlyxstringInvariant(this);
1094
1095         for (size_type t = i; t < rep->sz; ++t) {
1096                 if (rep->s[t] == c) return t;
1097         }
1098         return npos;
1099 }
1100
1101
1102 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1103                                              size_type i) const
1104 {
1105         TestlyxstringInvariant(this);
1106
1107         size_type ii = min(rep->sz - 1, i);
1108         for (int t = ii; t >= 0; --t) {
1109                 if (a.find(rep->s[t]) != npos) return t;
1110         }
1111         return npos;
1112 }
1113
1114
1115 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1116                                              size_type i,
1117                                              size_type n) const
1118 {
1119         Assert(ptr); // OURS!
1120         TestlyxstringInvariant(this);
1121         if (!n) return npos;
1122
1123         size_type ii = min(rep->sz - 1, i);
1124         for (int t = ii; t >= 0; --t) {
1125                 if(memchr(ptr, rep->s[t], n) != 0) return t;
1126         }
1127         return npos;
1128 }
1129
1130
1131 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1132                                              size_type i) const
1133 {
1134         Assert(ptr); // OURS!
1135         TestlyxstringInvariant(this);
1136
1137         size_type ii = min(rep->sz - 1, i);
1138         for (int t = ii; t >= 0; --t) {
1139                 if (strchr(ptr, rep->s[t]) != 0) return t;
1140         }
1141         return npos;
1142 }
1143
1144
1145 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1146 {
1147         TestlyxstringInvariant(this);
1148
1149         if (!rep->sz) return npos;
1150         size_type ii = min(rep->sz - 1, i);
1151         for (int t = ii; t >= 0; --t) {
1152                 if (rep->s[t] == c) return t;
1153         }
1154         return npos;
1155 }
1156
1157
1158 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1159                                                   size_type i) const
1160 {
1161         TestlyxstringInvariant(this);
1162
1163         if (!rep->sz) return npos;
1164         Assert(i < rep->sz);
1165         for (size_type t = i; t < rep->sz; ++t) {
1166                 if (a.find(rep->s[t]) == npos) return t;
1167         }
1168         return npos;
1169 }
1170
1171
1172 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1173                                                   size_type i,
1174                                                   size_type n) const
1175 {
1176         Assert(ptr && i < rep->sz); // OURS!
1177         TestlyxstringInvariant(this);
1178
1179         if (!n) return (i < rep->sz) ? i : npos;
1180         for (size_type t = i; t < rep->sz; ++t) {
1181                 if(memchr(ptr, rep->s[t], n) == 0) return t;
1182         }
1183         return npos;
1184 }
1185
1186
1187 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1188                                                   size_type i) const
1189 {
1190         Assert(ptr && i < rep->sz); // OURS!
1191         TestlyxstringInvariant(this);
1192
1193         for (size_type t = i; t < rep->sz; ++t) {
1194                 if (strchr(ptr, rep->s[t]) == 0) return t;
1195         }
1196         return npos;
1197 }
1198
1199
1200 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1201                                                   size_type i) const
1202 {
1203         if (!rep->sz) return npos;
1204         Assert(i < rep->sz); // OURS!
1205         TestlyxstringInvariant(this);
1206
1207         for (size_type t = i; t < rep->sz; ++t) {
1208                 if (rep->s[t] != c) return t;
1209         }
1210         return npos;
1211 }
1212
1213
1214 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1215                                                  size_type i) const
1216 {
1217         TestlyxstringInvariant(this);
1218
1219         size_type ii = min(rep->sz - 1, i);
1220         for (int t = ii; t >= 0; --t) {
1221                 if (a.find(rep->s[t]) == npos) return t;
1222         }
1223         return npos;
1224 }
1225
1226
1227 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1228                                                  size_type i,
1229                                                  size_type n) const
1230 {
1231         Assert(ptr); // OURS!
1232         TestlyxstringInvariant(this);
1233
1234         if (!n) return npos;
1235         size_type ii = min(rep->sz - 1, i);
1236
1237         for (int t = ii; t >= 0; --t) {
1238                 if(memchr(ptr, rep->s[t], n) == 0) return t;
1239         }
1240         return npos;
1241 }
1242
1243
1244 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1245                                                  size_type i) const
1246 {
1247         Assert(ptr); // OURS!
1248         TestlyxstringInvariant(this);
1249
1250         size_type ii = min(rep->sz - 1, i);
1251         for (int t = ii; t >= 0; --t) {
1252                 if (strchr(ptr, rep->s[t]) == 0) return t;
1253         }
1254         return npos;
1255 }
1256
1257
1258 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1259                                                  size_type i) const
1260 {
1261         TestlyxstringInvariant(this);
1262
1263         size_type ii = min(rep->sz - 1, i);
1264         for (int t = ii; t >= 0; --t) {
1265                 if (rep->s[t] != c) return t;
1266         }
1267         return npos;
1268 }
1269
1270
1271 /////////////////
1272 // Replace
1273 /////////////////
1274
1275 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1276 {
1277         Assert(i <= rep->sz); // OURS!
1278         TestlyxstringInvariant(this);
1279
1280         return replace(i, n, x, 0, x.rep->sz);
1281 }
1282
1283
1284 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1285                                size_type i2, size_type n2)
1286 {
1287         Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1288         TestlyxstringInvariant(this);
1289
1290         rep = rep->get_own_copy();
1291         rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1292         return *this;
1293 }
1294
1295
1296 lyxstring & lyxstring::replace(size_type i, size_type n,
1297                                value_type const * p, size_type n2)
1298 {
1299         Assert(p && i < rep->sz); // OURS!
1300         TestlyxstringInvariant(this);
1301
1302         rep = rep->get_own_copy();
1303         rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1304         return *this;
1305 }
1306
1307
1308 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1309 {
1310         Assert(p && i < rep->sz); // OURS!
1311         TestlyxstringInvariant(this);
1312
1313         return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1314 }
1315
1316
1317 lyxstring & lyxstring::replace(size_type i, size_type n,
1318                                size_type n2, value_type c)
1319 {
1320         Assert(i <= rep->sz);  // OURS!
1321         TestlyxstringInvariant(this);
1322
1323         rep = rep->get_own_copy();
1324         value_type * tmp = new value_type[n2];
1325         memset(tmp, c, n2);
1326         rep->replace(i, min(n, rep->sz), tmp, n2);
1327         delete[] tmp;
1328         return *this;
1329 }
1330
1331
1332 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1333 {
1334         TestlyxstringInvariant(this);
1335
1336         return replace(i - begin(), i2 - i, str); 
1337 }
1338
1339
1340 lyxstring & lyxstring::replace(iterator i, iterator i2,
1341                                value_type const * p, size_type n)
1342 {
1343         Assert(p); // OURS!
1344         TestlyxstringInvariant(this);
1345
1346         return replace(i - begin(), i2 - i, p, n);
1347 }
1348
1349
1350 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1351 {
1352         Assert(p); // OURS!
1353         TestlyxstringInvariant(this);
1354
1355         return replace(i - begin(), i2 - i, p);
1356 }
1357
1358
1359 lyxstring & lyxstring::replace(iterator i, iterator i2,
1360                                size_type n , value_type c)
1361 {
1362         TestlyxstringInvariant(this);
1363
1364         return replace(i - begin(), i2 - i, n, c);
1365 }
1366         
1367
1368 lyxstring & lyxstring::replace(iterator i, iterator i2,
1369                                iterator j, iterator j2)
1370 {
1371         TestlyxstringInvariant(this);
1372
1373         return replace(i - begin(), i2 - i, j, j2 - j);
1374 }
1375
1376
1377 void lyxstring::swap(lyxstring & str)
1378 {
1379         if (rep == str.rep) return;
1380         Srep * tmp = str.rep;
1381         str.rep = rep;
1382         rep = tmp;
1383 }
1384
1385
1386 lyxstring & lyxstring::erase(size_type i, size_type n)
1387 {
1388         Assert(i <= rep->sz); // STD!
1389         TestlyxstringInvariant(this);
1390
1391         rep = rep->get_own_copy();
1392         if (i == 0 && n >= rep->sz) {
1393                 rep->sz = 0;
1394         } else {
1395                 n = min(n, rep->sz - i);
1396                 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1397                 rep->sz -= n;
1398         }
1399         return *this;
1400 }
1401
1402
1403 lyxstring::iterator lyxstring::erase(iterator i)
1404 {
1405         TestlyxstringInvariant(this);
1406
1407         // what iterator is this supposed to return?
1408         // the iterator after the one erased
1409         erase(i - begin(), 1);
1410         return begin(); // BUG
1411 }
1412
1413
1414 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1415 {
1416         TestlyxstringInvariant(this);
1417
1418         erase(first - begin(), last - first);
1419         return begin(); // BUG
1420 }
1421
1422
1423 /////////////////////////////////////
1424 // Conversion to C-style Strings
1425 /////////////////////////////////////
1426
1427 lyxstring::value_type const * lyxstring::c_str() const
1428 {
1429         rep->s[length()] = '\0';
1430         return rep->s;
1431 }
1432
1433
1434 lyxstring::value_type const * lyxstring::data() const
1435 {
1436         return rep->s;
1437 }
1438
1439
1440 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1441                                      size_type pos) const
1442 {
1443         Assert(buf); // OURS!
1444         Assert(pos <= rep->sz); // STD!
1445         TestlyxstringInvariant(this);
1446
1447         register int nn = min(len, length() - pos);
1448         memcpy(buf, &(rep->s[pos]), nn);
1449         return nn;
1450 }
1451
1452
1453 ////////////////////
1454 // Comparisons
1455 ////////////////////
1456
1457 // Compare funcs should be verified.
1458
1459 int lyxstring::internal_compare(size_type pos, size_type n,
1460                                 value_type const * s,
1461                                 size_type slen, size_type n2) const
1462 {
1463         if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1464         if (!*s) return 1;
1465         // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1466
1467         // remember that n can very well be a lot larger than rep->sz
1468         // so we have to ensure that n is no larger than rep->sz
1469         n = min(n, rep->sz);
1470         n2 = min(n2, slen);
1471         if (n == n2)
1472                 return memcmp(&(rep->s[pos]), s, n);
1473         int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1474         if (c)
1475                 return c;
1476         if (n < n2)
1477                 return -1;
1478         return 1;
1479 }
1480
1481
1482 int lyxstring::compare(lyxstring const & str) const
1483 {
1484         TestlyxstringInvariant(this);
1485         return internal_compare(0, rep->sz, str.rep->s,
1486                                 str.rep->sz, str.rep->sz);
1487 }
1488
1489
1490 int lyxstring::compare(value_type const * s) const
1491 {
1492         Assert(s); //OURS!
1493         TestlyxstringInvariant(this);
1494         int n = (!s) ? 0 : strlen(s);
1495         return internal_compare(0, rep->sz, s, n, n);
1496 }
1497
1498
1499 int lyxstring::compare(size_type pos, size_type n,
1500                        lyxstring const & str) const
1501 {
1502         Assert(pos <= rep->sz); // OURS!
1503         TestlyxstringInvariant(this);
1504         return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1505 }
1506
1507
1508 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1509                        size_type pos2, size_type n2) const
1510 {
1511         Assert(pos <= rep->sz); // OURS!
1512         Assert(pos2 <= str.rep->sz); // OURS!
1513         TestlyxstringInvariant(this);
1514         return internal_compare(pos, n,
1515                                 str.rep->s + pos2,
1516                                 str.rep->sz - pos2, n2);
1517 }
1518
1519
1520 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1521                        size_type n2) const
1522 {
1523         Assert(s && pos <= rep->sz); // OURS!
1524         TestlyxstringInvariant(this);
1525         return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1526 }
1527
1528
1529 /////////////////
1530 // Substrings
1531 /////////////////
1532
1533 // i = index, n = length
1534 lyxstring lyxstring::substr(size_type i, size_type n) const
1535 {
1536         Assert(i <= rep->sz); // STD!
1537         TestlyxstringInvariant(this);
1538
1539         return lyxstring(*this, i, n);
1540 }
1541
1542
1543 /////////////////////////////////////////////
1544 // String operators, non member functions
1545 /////////////////////////////////////////////
1546
1547 bool operator==(lyxstring const & a, lyxstring const & b)
1548 {
1549         return a.compare(b) == 0;
1550 }
1551
1552
1553 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1554 {
1555         Assert(a); // OURS!
1556         return b.compare(a) == 0;
1557 }
1558
1559
1560 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1561 {
1562         Assert(b); // OURS!
1563         return a.compare(b) == 0;
1564 }
1565
1566
1567 bool operator!=(lyxstring const & a, lyxstring const & b)
1568 {
1569         return a.compare(b) != 0;
1570 }
1571
1572
1573 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1574 {
1575         Assert(a); // OURS!
1576         return b.compare(a) != 0;
1577 }
1578
1579
1580 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1581 {
1582         Assert(b); // OURS!
1583         return a.compare(b) != 0;
1584 }
1585
1586
1587 bool operator>(lyxstring const & a, lyxstring const & b)
1588 {
1589         return a.compare(b) > 0;
1590 }
1591
1592
1593 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1594 {
1595         Assert(a); // OURS!
1596         return b.compare(a) < 0; // since we reverse the parameters
1597 }
1598
1599
1600 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1601 {
1602         Assert(b); // OURS!
1603         return a.compare(b) > 0;
1604 }
1605
1606
1607 bool operator<(lyxstring const & a, lyxstring const & b)
1608 {
1609         return a.compare(b) < 0;
1610 }
1611
1612
1613 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1614 {
1615         Assert(a); // OURS!
1616         return b.compare(a) > 0; // since we reverse the parameters
1617 }
1618
1619
1620 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1621 {
1622         Assert(b); // OURS!
1623         return a.compare(b) < 0;
1624 }
1625
1626
1627 bool operator>=(lyxstring const & a, lyxstring const & b)
1628 {
1629         return a.compare(b) >= 0;
1630 }
1631
1632
1633 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1634 {
1635         Assert(a); // OURS!
1636         return b.compare(a) <= 0; // since we reverse the parameters
1637 }
1638
1639
1640 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1641 {
1642         Assert(b); // OURS!
1643         return a.compare(b) >= 0;
1644 }
1645
1646
1647 bool operator<=(lyxstring const & a, lyxstring const & b)
1648 {
1649         return a.compare(b) <= 0;
1650 }
1651
1652
1653 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1654 {
1655         Assert(a); // OURS!
1656         return b.compare(a) >= 0; // since we reverse the parameters
1657 }
1658
1659
1660 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1661 {
1662         Assert(b); // OURS!
1663         return a.compare(b) <= 0;
1664 }
1665
1666
1667 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1668 {
1669         lyxstring tmp(a);
1670         tmp += b;
1671         return tmp;
1672 }
1673
1674
1675 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1676 {
1677         Assert(a); // OURS!
1678         lyxstring tmp(a);
1679         tmp += b;
1680         return tmp;
1681 }
1682
1683
1684 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1685 {
1686         lyxstring tmp;
1687         tmp += a;
1688         tmp += b;
1689         return tmp;
1690 }
1691
1692
1693 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1694 {
1695         Assert(b); // OURS!
1696         lyxstring tmp(a);
1697         tmp += b;
1698         return tmp;
1699 }
1700
1701
1702 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1703 {
1704         lyxstring tmp(a);
1705         tmp += b;
1706         return tmp;
1707 }
1708
1709
1710 void swap(lyxstring & str1, lyxstring & str2)
1711 {
1712         str1.swap(str2);
1713 }
1714
1715
1716 #include <iostream>
1717
1718 istream & operator>>(istream & is, lyxstring & s)
1719 {
1720 #if 1
1721         // very bad solution
1722         char * nome = new char[1024];
1723         is >> nome;
1724         lyxstring tmp(nome);
1725         delete [] nome;
1726         if (!tmp.empty()) s = tmp;
1727 #else
1728         // better solution
1729         int w = is.widdth(0);
1730         s.clear();
1731         char c = 0;
1732         while (is.get(c)) {
1733                 if (isspace(c)) { is.putback(c); break; }
1734                 s += c;
1735                 if (--w == 1) break;
1736         }
1737         if (s.empty()) is.setstate(ios::failbit);
1738 #endif
1739         return is;
1740 }
1741
1742
1743 ostream & operator<<(ostream & o, lyxstring const & s)
1744 {
1745         return o.write(s.data(), s.length());
1746 }
1747
1748
1749 istream & getline(istream & is, lyxstring & s,
1750                   lyxstring::value_type delim)
1751 {
1752         // very bad solution
1753         char tmp = 0;
1754         s.erase();
1755         while(is) {
1756                 is.get(tmp);
1757                 if (tmp != delim) {
1758                         s += tmp;
1759                 } else {
1760                         break;
1761                 }
1762         }
1763         return is;
1764 }