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