]> git.lyx.org Git - lyx.git/blob - src/support/lyxstring.C
small changes read changelog
[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 #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         lyx::Assert(pos <= rep->sz); // OURS!
663         static char helper = '\0';
664         return pos == rep->sz ? helper : rep->s[pos];
665 #else
666         lyx::Assert(pos < rep->sz); // OURS!
667         return rep->s[pos];
668 #endif
669 }
670
671
672 lyxstring::reference lyxstring::operator[](size_type pos)
673 {
674         lyx::Assert(pos < rep->sz); // OURS!
675         TestlyxstringInvariant(this);
676
677         rep = rep->get_own_copy();
678         return rep->s[pos];
679 }
680
681
682 lyxstring::const_reference lyxstring::at(size_type n) const
683 {
684         lyx::Assert(n < rep->sz); // STD!
685         return rep->s[n];
686 }
687
688
689 lyxstring::reference lyxstring::at(size_type n)
690 {
691         lyx::Assert(n < rep->sz); // STD!
692         TestlyxstringInvariant(this);
693
694         rep = rep->get_own_copy();
695         return rep->s[n];
696 }
697
698
699 /////////////
700 // Insert
701 /////////////
702
703 lyxstring & lyxstring::operator+=(lyxstring const & x)
704 {
705         TestlyxstringInvariant(this);
706
707         return append(x);
708 }
709
710
711 lyxstring & lyxstring::operator+=(value_type const * x)
712 {
713         lyx::Assert(x); // OURS!
714         TestlyxstringInvariant(this);
715
716         return append(x);
717 }
718
719
720 lyxstring & lyxstring::operator+=(value_type c)
721 {
722         TestlyxstringInvariant(this);
723
724         push_back(c);
725         return *this;
726 }
727
728
729 void lyxstring::push_back(value_type c)
730 {
731         TestlyxstringInvariant(this);
732
733         rep = rep->get_own_copy();
734         rep->push_back(c);
735 }
736
737
738 lyxstring & lyxstring::append(lyxstring const & x)
739 {
740         TestlyxstringInvariant(this);
741
742         if (x.empty()) return *this;
743         rep = rep->get_own_copy();
744         rep->append(x.length(), x.rep->s);
745         return *this;
746 }
747
748
749 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
750 {
751         lyx::Assert(pos <= x.rep->sz); // STD!
752         TestlyxstringInvariant(this);
753
754         return append(x.substr(pos, n));
755 }
756
757
758 lyxstring & lyxstring::append(value_type const * p, size_type n)
759 {
760         lyx::Assert(p); // OURS!
761         TestlyxstringInvariant(this);
762
763         if (!*p || !n) return *this;
764         rep = rep->get_own_copy();
765         rep->append(n, p);
766         return *this;
767 }
768
769
770 lyxstring & lyxstring::append(value_type const * p)
771 {
772         lyx::Assert(p); // OURS!
773         return append(p, strlen(p));
774 }
775
776
777 lyxstring & lyxstring::append(size_type n, value_type c)
778 {
779         TestlyxstringInvariant(this);
780
781         value_type * tmp = new value_type[n];
782         memset(tmp, c, n);
783         rep = rep->get_own_copy();
784         rep->append(n, tmp);
785         delete[] tmp;
786         return *this;
787 }
788
789
790 lyxstring & lyxstring::append(iterator first, iterator last)
791 {
792         TestlyxstringInvariant(this);
793
794         rep = rep->get_own_copy();
795         rep->append(last - first, first);
796         return *this;
797 }
798
799 // insert characters before (*this)[pos]
800
801 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
802 {
803         TestlyxstringInvariant(this);
804
805         return insert(pos, x, 0, x.rep->sz);
806 }
807
808
809 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
810                               size_type pos2, size_type n)
811 {
812         lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
813         TestlyxstringInvariant(this);
814
815         rep = rep->get_own_copy();
816         rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
817         return *this;
818 }
819
820
821 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
822 {
823         lyx::Assert(p); // OURS!
824         TestlyxstringInvariant(this);
825
826         if (*p && n) {
827                 // insert nothing and you change nothing
828                 rep = rep->get_own_copy();
829                 rep->insert(pos, p, n);
830         }
831         return *this;
832 }
833
834
835 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
836 {
837         lyx::Assert(p); // OURS!
838         return insert(pos, p, strlen(p));
839 }
840
841
842 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
843 {
844         TestlyxstringInvariant(this);
845
846         rep = rep->get_own_copy();
847         value_type * tmp = new value_type[n];
848         memset(tmp, c, n);
849         rep->insert(pos, tmp, n);
850         delete[] tmp;
851         return *this;
852 }
853
854
855 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
856 {
857         TestlyxstringInvariant(this);
858
859         // what iterator is this supposed to return??
860         size_type tmp = p - begin();
861         insert(p - begin(), 1, c);
862         return begin() + tmp + 1; // ??
863 }
864
865
866 void lyxstring::insert(iterator p, size_type n , value_type c)
867 {
868         TestlyxstringInvariant(this);
869
870         insert(p - begin(), n , c);
871 }
872
873
874 void lyxstring::insert(iterator p, iterator first, iterator last)
875 {
876         TestlyxstringInvariant(this);
877
878         insert(p - begin(), first, last - first);
879 }
880         
881
882 ////////////////
883 // Find
884 ////////////////
885  
886          // All the below find functions should be verified,
887          // it is very likely that I have mixed up or interpreted
888          // some of the parameters wrong, also some of the funcs can surely
889          // be written more effectively.
890
891 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
892 {
893         if (!rep->sz || i >= rep->sz) return npos;
894         
895         TestlyxstringInvariant(this);
896
897         size_type n = a.length();
898         if (!n) return npos;
899         for (size_type t = i; rep->sz - t >= n; ++t) {
900                 // search until (*this)[i] == a[0]
901                 if (rep->s[t] == a[0]) {
902                         // check if the rest of the value_types match
903                         bool equal = true;
904                         for (size_type j = 1; j < n; ++j) {
905                                 if (rep->s[t + j] != a[j]) {
906                                         equal = false;
907                                         break;
908                                 }
909                         }
910                         if (equal) return t;
911                 }
912         }
913         return npos;
914 }
915
916
917 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
918                                      size_type n) const
919 {
920         lyx::Assert(ptr); // OURS!
921         if (!rep->sz || !*ptr || i >= rep->sz) return npos;
922         
923         TestlyxstringInvariant(this);
924
925         // What is "n" here? is it the number of value_types to use in ptr
926         // or does "i" and "n" togeter form a substring to search
927         // for ptr in? For now I will assume that "n" tells the length
928         // of ptr. (Lgb)
929         n = min(n, strlen(ptr));
930         if (!n) return npos;
931         for (size_type t = i; rep->sz - t >= n; ++t) {
932                 // search until (*this)[i] == a[0]
933                 if (rep->s[t] == ptr[0]) {
934                         // check if the rest of the value_types match
935                         bool equal = true;
936                         for (size_type j = 1; j < n; ++j) {
937                                 if (rep->s[t + j] != ptr[j]) {
938                                         equal = false;
939                                         break;
940                                 }
941                         }
942                         if (equal) return t;
943                 }
944         }
945         return npos;
946 }
947
948
949 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
950 {
951         lyx::Assert(s); // OURS!
952         if (!rep->sz || i >= rep->sz) return npos;
953         
954         TestlyxstringInvariant(this);
955
956         if (!s || !*s) return npos;
957         return find(s, i, strlen(s));
958 }
959
960
961 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
962 {
963         if (!rep->sz || i >= rep->sz) return npos;
964
965         TestlyxstringInvariant(this);
966
967         for (size_type t = 0; t + i < rep->sz; ++t) {
968                 if (rep->s[t + i] == c) return t + i;
969         }
970         return npos;
971 }
972
973
974 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
975 {
976         TestlyxstringInvariant(this);
977
978         size_type n = a.length();
979         if (!n || rep->sz < n)
980                 return npos;
981
982         size_type t = min(rep->sz - n, i);
983         do {
984                 if (rep->s[t] == a[0]) {
985                         // check if the rest of the value_types match
986                         bool equal = true;
987                         for (size_type j = 1; j < n; ++j) {
988                                 if (rep->s[t + j] != a[j]) {
989                                         equal = false;
990                                         break;
991                                 }
992                         }
993                         if (equal) return t;
994                 }
995         } while(t-- > 0);
996         return npos;
997 }
998
999
1000 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
1001                                       size_type n) const
1002 {
1003         lyx::Assert(ptr); // OURS!
1004         TestlyxstringInvariant(this);
1005
1006         n = min(n, strlen(ptr));
1007         if (!n || rep->sz < n)
1008                 return npos;
1009
1010         size_type t = min(rep->sz - n, i);
1011         do {
1012                 if (rep->s[t] == ptr[0]) {
1013                         // check if the rest of the value_types match
1014                         bool equal = true;
1015                         for (size_type j = 1; j < n; ++j) {
1016                                 if (rep->s[t + j] != ptr[j]) {
1017                                         equal = false;
1018                                         break;
1019                                 }
1020                         }
1021                         if (equal) return t;
1022                 }
1023         } while (t-- > 0);
1024         return npos;
1025 }
1026
1027
1028 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
1029                                       size_type i) const
1030 {
1031         lyx::Assert(ptr); // OURS!
1032
1033         if (!ptr || !*ptr) return npos;
1034         return rfind(ptr, i, strlen(ptr));
1035 }
1036
1037
1038 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
1039 {
1040         TestlyxstringInvariant(this);
1041
1042         size_type const sz = rep->sz;
1043         if (sz < 1) return npos;
1044         size_type ii = min(sz - 1, i);
1045         do {
1046                 if (rep->s[ii] == c) return ii;
1047         } while (ii-- > 0);
1048         return npos;
1049 }
1050
1051
1052 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
1053                                               size_type i) const
1054 {
1055         lyx::Assert(i <= rep->sz); // OURS!
1056         TestlyxstringInvariant(this);
1057
1058         for (size_type t = i; t < rep->sz; ++t) {
1059                 if (a.find(rep->s[t]) != npos) return t;
1060         }
1061         return npos;
1062 }
1063
1064
1065 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1066                                               size_type i,
1067                                               size_type n) const
1068 {
1069         lyx::Assert(ptr && i <= rep->sz); // OURS!
1070         TestlyxstringInvariant(this);
1071         if (!n) return npos;
1072
1073         for (size_type t = i; t < rep->sz; ++t) {
1074                 if (memchr(ptr, rep->s[t], n) != 0) return t;
1075         }
1076         return npos;
1077 }
1078
1079
1080 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
1081                                               size_type i) const
1082 {
1083         lyx::Assert(ptr && i <= rep->sz); // OURS!
1084         TestlyxstringInvariant(this);
1085
1086         for (size_type t = i; t < rep->sz; ++t) {
1087                 if (strchr(ptr, rep->s[t]) != 0) return t;
1088         }
1089         return npos;
1090 }
1091
1092
1093 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
1094 {
1095         lyx::Assert(i <= rep->sz); // OURS!
1096         TestlyxstringInvariant(this);
1097
1098         for (size_type t = i; t < rep->sz; ++t) {
1099                 if (rep->s[t] == c) return t;
1100         }
1101         return npos;
1102 }
1103
1104
1105 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
1106                                              size_type i) const
1107 {
1108         TestlyxstringInvariant(this);
1109
1110         size_type ii = min(rep->sz - 1, i);
1111         for (int t = ii; t >= 0; --t) {
1112                 if (a.find(rep->s[t]) != npos) return t;
1113         }
1114         return npos;
1115 }
1116
1117
1118 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1119                                              size_type i,
1120                                              size_type n) const
1121 {
1122         lyx::Assert(ptr); // OURS!
1123         TestlyxstringInvariant(this);
1124         if (!n) return npos;
1125
1126         size_type ii = min(rep->sz - 1, i);
1127         for (int t = ii; t >= 0; --t) {
1128                 if (memchr(ptr, rep->s[t], n) != 0) return t;
1129         }
1130         return npos;
1131 }
1132
1133
1134 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
1135                                              size_type i) const
1136 {
1137         lyx::Assert(ptr); // OURS!
1138         TestlyxstringInvariant(this);
1139
1140         size_type ii = min(rep->sz - 1, i);
1141         for (int t = ii; t >= 0; --t) {
1142                 if (strchr(ptr, rep->s[t]) != 0) return t;
1143         }
1144         return npos;
1145 }
1146
1147
1148 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
1149 {
1150         TestlyxstringInvariant(this);
1151
1152         if (!rep->sz) return npos;
1153         size_type ii = min(rep->sz - 1, i);
1154         for (int t = ii; t >= 0; --t) {
1155                 if (rep->s[t] == c) return t;
1156         }
1157         return npos;
1158 }
1159
1160
1161 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
1162                                                   size_type i) const
1163 {
1164         TestlyxstringInvariant(this);
1165
1166         if (!rep->sz) return npos;
1167         lyx::Assert(i <= rep->sz);
1168         for (size_type t = i; t < rep->sz; ++t) {
1169                 if (a.find(rep->s[t]) == npos) return t;
1170         }
1171         return npos;
1172 }
1173
1174
1175 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1176                                                   size_type i,
1177                                                   size_type n) const
1178 {
1179         lyx::Assert(ptr && i <= rep->sz); // OURS!
1180         TestlyxstringInvariant(this);
1181
1182         if (!n) return (i < rep->sz) ? i : npos;
1183         for (size_type t = i; t < rep->sz; ++t) {
1184                 if (memchr(ptr, rep->s[t], n) == 0) return t;
1185         }
1186         return npos;
1187 }
1188
1189
1190 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
1191                                                   size_type i) const
1192 {
1193         lyx::Assert(ptr && i <= rep->sz); // OURS!
1194         TestlyxstringInvariant(this);
1195
1196         for (size_type t = i; t < rep->sz; ++t) {
1197                 if (strchr(ptr, rep->s[t]) == 0) return t;
1198         }
1199         return npos;
1200 }
1201
1202
1203 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
1204                                                   size_type i) const
1205 {
1206         if (!rep->sz) return npos;
1207         lyx::Assert(i <= rep->sz); // OURS!
1208         TestlyxstringInvariant(this);
1209
1210         for (size_type t = i; t < rep->sz; ++t) {
1211                 if (rep->s[t] != c) return t;
1212         }
1213         return npos;
1214 }
1215
1216
1217 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
1218                                                  size_type i) const
1219 {
1220         TestlyxstringInvariant(this);
1221
1222         size_type ii = min(rep->sz - 1, i);
1223         for (int t = ii; t >= 0; --t) {
1224                 if (a.find(rep->s[t]) == npos) return t;
1225         }
1226         return npos;
1227 }
1228
1229
1230 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1231                                                  size_type i,
1232                                                  size_type n) const
1233 {
1234         lyx::Assert(ptr); // OURS!
1235         TestlyxstringInvariant(this);
1236
1237         if (!n) return npos;
1238         size_type ii = min(rep->sz - 1, i);
1239
1240         for (int t = ii; t >= 0; --t) {
1241                 if (memchr(ptr, rep->s[t], n) == 0) return t;
1242         }
1243         return npos;
1244 }
1245
1246
1247 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
1248                                                  size_type i) const
1249 {
1250         lyx::Assert(ptr); // OURS!
1251         TestlyxstringInvariant(this);
1252
1253         size_type ii = min(rep->sz - 1, i);
1254         for (int t = ii; t >= 0; --t) {
1255                 if (strchr(ptr, rep->s[t]) == 0) return t;
1256         }
1257         return npos;
1258 }
1259
1260
1261 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
1262                                                  size_type i) const
1263 {
1264         TestlyxstringInvariant(this);
1265
1266         size_type ii = min(rep->sz - 1, i);
1267         for (int t = ii; t >= 0; --t) {
1268                 if (rep->s[t] != c) return t;
1269         }
1270         return npos;
1271 }
1272
1273
1274 /////////////////
1275 // Replace
1276 /////////////////
1277
1278 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
1279 {
1280         lyx::Assert(i <= rep->sz); // OURS!
1281         TestlyxstringInvariant(this);
1282
1283         return replace(i, n, x, 0, x.rep->sz);
1284 }
1285
1286
1287 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
1288                                size_type i2, size_type n2)
1289 {
1290         lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
1291         TestlyxstringInvariant(this);
1292
1293         rep = rep->get_own_copy();
1294         rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
1295         return *this;
1296 }
1297
1298
1299 lyxstring & lyxstring::replace(size_type i, size_type n,
1300                                value_type const * p, size_type n2)
1301 {
1302         lyx::Assert(p && i <= rep->sz); // OURS!
1303         TestlyxstringInvariant(this);
1304
1305         rep = rep->get_own_copy();
1306         rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
1307         return *this;
1308 }
1309
1310
1311 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
1312 {
1313         lyx::Assert(p && i <= rep->sz); // OURS!
1314         TestlyxstringInvariant(this);
1315
1316         return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
1317 }
1318
1319
1320 lyxstring & lyxstring::replace(size_type i, size_type n,
1321                                size_type n2, value_type c)
1322 {
1323         lyx::Assert(i <= rep->sz);  // OURS!
1324         TestlyxstringInvariant(this);
1325
1326         rep = rep->get_own_copy();
1327         value_type * tmp = new value_type[n2];
1328         memset(tmp, c, n2);
1329         rep->replace(i, min(n, rep->sz), tmp, n2);
1330         delete[] tmp;
1331         return *this;
1332 }
1333
1334
1335 /// FY! FY! FY! go away !
1336 lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
1337 {
1338         return replace(i, n, 1, c);
1339 }
1340
1341
1342 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
1343 {
1344         TestlyxstringInvariant(this);
1345
1346         return replace(i - begin(), i2 - i, str); 
1347 }
1348
1349
1350 lyxstring & lyxstring::replace(iterator i, iterator i2,
1351                                value_type const * p, size_type n)
1352 {
1353         lyx::Assert(p); // OURS!
1354         TestlyxstringInvariant(this);
1355
1356         return replace(i - begin(), i2 - i, p, n);
1357 }
1358
1359
1360 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
1361 {
1362         lyx::Assert(p); // OURS!
1363         TestlyxstringInvariant(this);
1364
1365         return replace(i - begin(), i2 - i, p);
1366 }
1367
1368
1369 lyxstring & lyxstring::replace(iterator i, iterator i2,
1370                                size_type n , value_type c)
1371 {
1372         TestlyxstringInvariant(this);
1373
1374         return replace(i - begin(), i2 - i, n, c);
1375 }
1376         
1377
1378 lyxstring & lyxstring::replace(iterator i, iterator i2,
1379                                iterator j, iterator j2)
1380 {
1381         TestlyxstringInvariant(this);
1382
1383         return replace(i - begin(), i2 - i, j, j2 - j);
1384 }
1385
1386
1387 void lyxstring::swap(lyxstring & str)
1388 {
1389         if (rep == str.rep) return;
1390         Srep * tmp = str.rep;
1391         str.rep = rep;
1392         rep = tmp;
1393 }
1394
1395
1396 lyxstring & lyxstring::erase(size_type i, size_type n)
1397 {
1398         lyx::Assert(i <= rep->sz); // STD!
1399         TestlyxstringInvariant(this);
1400
1401         rep = rep->get_own_copy();
1402         if (i == 0 && n >= rep->sz) {
1403                 rep->sz = 0;
1404         } else {
1405                 n = min(n, rep->sz - i);
1406                 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
1407                 rep->sz -= n;
1408         }
1409         return *this;
1410 }
1411
1412
1413 lyxstring::iterator lyxstring::erase(iterator i)
1414 {
1415         TestlyxstringInvariant(this);
1416
1417         // what iterator is this supposed to return?
1418         // the iterator after the one erased
1419         erase(i - begin(), 1);
1420         return begin(); // BUG
1421 }
1422
1423
1424 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
1425 {
1426         TestlyxstringInvariant(this);
1427
1428         erase(first - begin(), last - first);
1429         return begin(); // BUG
1430 }
1431
1432
1433 /////////////////////////////////////
1434 // Conversion to C-style Strings
1435 /////////////////////////////////////
1436
1437 lyxstring::value_type const * lyxstring::c_str() const
1438 {
1439         rep->s[length()] = '\0';
1440         return rep->s;
1441 }
1442
1443
1444 lyxstring::value_type const * lyxstring::data() const
1445 {
1446         return rep->s;
1447 }
1448
1449
1450 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
1451                                      size_type pos) const
1452 {
1453         lyx::Assert(buf); // OURS!
1454         lyx::Assert(pos <= rep->sz); // STD!
1455         TestlyxstringInvariant(this);
1456
1457         register int nn = min(len, length() - pos);
1458         memcpy(buf, &(rep->s[pos]), nn);
1459         return nn;
1460 }
1461
1462
1463 ////////////////////
1464 // Comparisons
1465 ////////////////////
1466
1467 // Compare funcs should be verified.
1468
1469 int lyxstring::internal_compare(size_type pos, size_type n,
1470                                 value_type const * s,
1471                                 size_type slen, size_type n2) const
1472 {
1473         if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
1474         if (!*s) return 1;
1475         // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
1476
1477         // remember that n can very well be a lot larger than rep->sz
1478         // so we have to ensure that n is no larger than rep->sz
1479         n = min(n, rep->sz);
1480         n2 = min(n2, slen);
1481         if (n == n2)
1482                 return memcmp(&(rep->s[pos]), s, n);
1483         int c = memcmp(&(rep->s[pos]), s, min(n, n2));
1484         if (c)
1485                 return c;
1486         if (n < n2)
1487                 return -1;
1488         return 1;
1489 }
1490
1491
1492 int lyxstring::compare(lyxstring const & str) const
1493 {
1494         TestlyxstringInvariant(this);
1495         return internal_compare(0, rep->sz, str.rep->s,
1496                                 str.rep->sz, str.rep->sz);
1497 }
1498
1499
1500 int lyxstring::compare(value_type const * s) const
1501 {
1502         lyx::Assert(s); //OURS!
1503         TestlyxstringInvariant(this);
1504         int n = (!s) ? 0 : strlen(s);
1505         return internal_compare(0, rep->sz, s, n, n);
1506 }
1507
1508
1509 int lyxstring::compare(size_type pos, size_type n,
1510                        lyxstring const & str) const
1511 {
1512         lyx::Assert(pos <= rep->sz); // OURS!
1513         TestlyxstringInvariant(this);
1514         return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
1515 }
1516
1517
1518 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
1519                        size_type pos2, size_type n2) const
1520 {
1521         lyx::Assert(pos <= rep->sz); // OURS!
1522         lyx::Assert(pos2 <= str.rep->sz); // OURS!
1523         TestlyxstringInvariant(this);
1524         return internal_compare(pos, n,
1525                                 str.rep->s + pos2,
1526                                 str.rep->sz - pos2, n2);
1527 }
1528
1529
1530 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
1531                        size_type n2) const
1532 {
1533         lyx::Assert(s && pos <= rep->sz); // OURS!
1534         TestlyxstringInvariant(this);
1535         return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
1536 }
1537
1538
1539 /////////////////
1540 // Substrings
1541 /////////////////
1542
1543 // i = index, n = length
1544 lyxstring lyxstring::substr(size_type i, size_type n) const
1545 {
1546         lyx::Assert(i <= rep->sz); // STD!
1547         TestlyxstringInvariant(this);
1548
1549         return lyxstring(*this, i, n);
1550 }
1551
1552
1553 /////////////////////////////////////////////
1554 // String operators, non member functions
1555 /////////////////////////////////////////////
1556
1557 bool operator==(lyxstring const & a, lyxstring const & b)
1558 {
1559         return a.compare(b) == 0;
1560 }
1561
1562
1563 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
1564 {
1565         lyx::Assert(a); // OURS!
1566         return b.compare(a) == 0;
1567 }
1568
1569
1570 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
1571 {
1572         lyx::Assert(b); // OURS!
1573         return a.compare(b) == 0;
1574 }
1575
1576
1577 bool operator!=(lyxstring const & a, lyxstring const & b)
1578 {
1579         return a.compare(b) != 0;
1580 }
1581
1582
1583 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
1584 {
1585         lyx::Assert(a); // OURS!
1586         return b.compare(a) != 0;
1587 }
1588
1589
1590 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
1591 {
1592         lyx::Assert(b); // OURS!
1593         return a.compare(b) != 0;
1594 }
1595
1596
1597 bool operator>(lyxstring const & a, lyxstring const & b)
1598 {
1599         return a.compare(b) > 0;
1600 }
1601
1602
1603 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
1604 {
1605         lyx::Assert(a); // OURS!
1606         return b.compare(a) < 0; // since we reverse the parameters
1607 }
1608
1609
1610 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
1611 {
1612         lyx::Assert(b); // OURS!
1613         return a.compare(b) > 0;
1614 }
1615
1616
1617 bool operator<(lyxstring const & a, lyxstring const & b)
1618 {
1619         return a.compare(b) < 0;
1620 }
1621
1622
1623 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
1624 {
1625         lyx::Assert(a); // OURS!
1626         return b.compare(a) > 0; // since we reverse the parameters
1627 }
1628
1629
1630 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
1631 {
1632         lyx::Assert(b); // OURS!
1633         return a.compare(b) < 0;
1634 }
1635
1636
1637 bool operator>=(lyxstring const & a, lyxstring const & b)
1638 {
1639         return a.compare(b) >= 0;
1640 }
1641
1642
1643 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
1644 {
1645         lyx::Assert(a); // OURS!
1646         return b.compare(a) <= 0; // since we reverse the parameters
1647 }
1648
1649
1650 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
1651 {
1652         lyx::Assert(b); // OURS!
1653         return a.compare(b) >= 0;
1654 }
1655
1656
1657 bool operator<=(lyxstring const & a, lyxstring const & b)
1658 {
1659         return a.compare(b) <= 0;
1660 }
1661
1662
1663 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
1664 {
1665         lyx::Assert(a); // OURS!
1666         return b.compare(a) >= 0; // since we reverse the parameters
1667 }
1668
1669
1670 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
1671 {
1672         lyx::Assert(b); // OURS!
1673         return a.compare(b) <= 0;
1674 }
1675
1676
1677 lyxstring operator+(lyxstring const & a, lyxstring const & b)
1678 {
1679         lyxstring tmp(a);
1680         tmp += b;
1681         return tmp;
1682 }
1683
1684
1685 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
1686 {
1687         lyx::Assert(a); // OURS!
1688         lyxstring tmp(a);
1689         tmp += b;
1690         return tmp;
1691 }
1692
1693
1694 lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
1695 {
1696         lyxstring tmp;
1697         tmp += a;
1698         tmp += b;
1699         return tmp;
1700 }
1701
1702
1703 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
1704 {
1705         lyx::Assert(b); // OURS!
1706         lyxstring tmp(a);
1707         tmp += b;
1708         return tmp;
1709 }
1710
1711
1712 lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
1713 {
1714         lyxstring tmp(a);
1715         tmp += b;
1716         return tmp;
1717 }
1718
1719
1720 void swap(lyxstring & str1, lyxstring & str2)
1721 {
1722         str1.swap(str2);
1723 }
1724
1725
1726 #include <iostream>
1727
1728 istream & operator>>(istream & is, lyxstring & s)
1729 {
1730 #if 1
1731         // very bad solution
1732         char * nome = new char[1024];
1733         is >> nome;
1734         lyxstring tmp(nome);
1735         delete [] nome;
1736         if (!tmp.empty()) s = tmp;
1737 #else
1738         // better solution
1739         int w = is.widdth(0);
1740         s.clear();
1741         char c = 0;
1742         while (is.get(c)) {
1743                 if (isspace(c)) { is.putback(c); break; }
1744                 s += c;
1745                 if (--w == 1) break;
1746         }
1747         if (s.empty()) is.setstate(ios::failbit);
1748 #endif
1749         return is;
1750 }
1751
1752
1753 ostream & operator<<(ostream & o, lyxstring const & s)
1754 {
1755         return o.write(s.data(), s.length());
1756 }
1757
1758
1759 istream & getline(istream & is, lyxstring & s,
1760                   lyxstring::value_type delim)
1761 {
1762         // very bad solution
1763         char tmp = 0;
1764         s.erase();
1765         while(is) {
1766                 is.get(tmp);
1767                 if (tmp != delim) {
1768                         s += tmp;
1769                 } else {
1770                         break;
1771                 }
1772         }
1773         return is;
1774 }
1775
1776 #ifdef TEST_MAIN
1777 int main() {
1778         lyxstring a = "abcac";
1779         cout << a.rfind("ab") << endl;
1780         cout << a.rfind("c") << endl;
1781         cout << a.rfind("d") << endl;
1782 }
1783 #endif