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