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