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