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