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