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