]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
Add string << operators for the other streams as well, and removes
[lyx.git] / src / support / lyxstring.C
index ae20c4d23c61bb6d0118cdc9a22c1ef878748131..4088c033c0c90bbff84d9725f0432f134d40e5c8 100644 (file)
@@ -1,20 +1,17 @@
 /* This file is part of
- * ======================================================
+ * ====================================================== 
  * 
  *           LyX, The Document Processor
  *      
- *         Copyright (C) 1995 Matthias Ettrich
- *          Copyright (C) 1995-1999 The LyX Team.
+ *         Copyright 1995 Matthias Ettrich
+ *          Copyright 1995-2000 The LyX Team.
  *
- * ======================================================*/
+ * ====================================================== */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 
-// If we use STL string, there is no need to compile this class.
-#ifdef USE_INCLUDED_STRING
-
 #ifdef __GNUG__
 #pragma implementation "lyxstring.h"
 #endif
 
 #include "LAssert.h"
 
+#include "debug.h"
+
 using std::min;
+using std::istream;
+using std::ostream;
+
+// This class is supposed to be functionaly equivalent to a
+// standard conformant string. This mean among others that we
+// are useing the same requirements. Before you change anything
+// in this file consult me and/or the standard to discover the
+// right behavior.
+
+// Asserts with a STD! are required by the standard.
+// Asserts with a OURS! are added by me.
+// Some asserts could still be missing and some of the existing
+// ones might be wrong or not needed.
 
 // Reference count has been checked, empty_rep removed and
 // introduced again in a similar guise. Where is empty_rep _really_
 // needed?
 
-// Insertion and replace is implemented, as far as I can see everything
-// works, but could perhaps be done smarter.
+// We are missing a couple of imporant things from the standard:
+// reverse iterators and methods taking InputIterators as paramters.
+// Also the methods returning iterators is returning the wrong value.
 
 // All the different find functions need a good look over.
 // I have so far not tested them extensively and would be
 // happy if others took the time to have a peek.
 
-#ifdef WITH_WARNINGS
-#warning temporarily here for debugging purposes only
-#endif
-lyxstring::size_type lyxstring::size() const
-{ 
-       return rep->sz;
-}
-
-
-//--------------------------------------------------------------------------
-// lyxstringInvariant
-#ifdef DEVEL_VERSION
-
-/** Testing of the lyxstring invariant
- * By creating an object that tests the lyxstring invariant during its
- * construction *and* its deconstruction we greatly simplify our code.
- * Calling TestlyxstringInvariant() upon entry to an lyxstring method 
- * will test the invariant upon entry to the code.  If the Asserts fail
- * then we know from the stack trace that the corruption occurred *before*
- * entry to this method.  We can also be sure it didn't happen in any of
- * the tested lyxstring methods.  It is therefore likely to be due to some
- * other external force.
- * Several lyxstring methods have multiple exit points which would otherwise
- * require us to insert a separate test before each return.  But since we
- * created an object its destructor will be called upon exit (any exit!).
- * We thus get testing at both start and end of a method with one line of
- * code at the head of a method.  More importantly,  we get good testing
- * everytime we run the code.
- * NOTE:  just because we test the invariant doesn't mean we can forget
- * about testing pre and post conditions specific to any given method.
- * This test simply proves that the lyxstring/Srep is in a valid state it
- * does *not* prove that the method did what it was supposed to.
- */
-class lyxstringInvariant
-{
-public:
-       lyxstringInvariant(lyxstring const *);
-       ~lyxstringInvariant();
-private:
-       void helper() const;
-       lyxstring const * object;
-};
-
-// To test if this scheme works "as advertised" uncomment the printf's in
-// the constructor and destructor below and then uncomment the printf and the
-// call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
-// The correct output when LyX has been recompiled and run is:
-//     lyxstringInvariant constructor
-//     lyxstring::operator=(char const *)
-//     lyxstringInvariant constructor
-//     lyxstringInvariant destructor completed
-//     lyxstringInvariant destructor completed
-// NOTE: The easiest way to catch this snippet of the output is to wait for
-//       the splash screen to disappear and then open and close Help->Credits
-//
-lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
-{
-//     printf("lyxstringInvariant constructor\n");
-       helper();
-}
+// Space allocation of string.
+// I have tried to do this very simple without using any special tricks.
+// Earlier we used a fixed value to enlarge the string with this would
+// cause a lot of reallocations with large strings (especially if
+// push_back was used) and wasting space for very small strings.
+// I have now changed the allocation to use a doubling of reserved
+// space until it is large enough. So far tests show a small speed
+// increase and a noticable memory saving.
 
-lyxstringInvariant::~lyxstringInvariant()
-{
-       helper();
-//     printf("lyxstringInvariant destructor completed\n");
-}
+// Lgb.
 
-void lyxstringInvariant::helper() const
-{
-       // Some of these tests might look pointless but they are
-       // all part of the invariant and if we want to make sure
-       // we have a bullet proof implementation then we need to
-       // test every last little thing we *know* should be true.
-       // I may have missed a test or two, so feel free to fill
-       // in the gaps.  ARRae.
-       // NOTE:  Don't put TestlyxstringInvariant() in any of the
-       // lyxstring methods used below otherwise you'll get an
-       // infinite recursion and a crash.
-       Assert(object);
-       Assert(object->rep);
-       Assert(object->rep->s);    // s is never 0
-       Assert(object->rep->res);  // always some space allocated
-       Assert(object->size() <= object->rep->res);
-       Assert(object->rep->ref >= 1);  // its in use so it must be referenced
-       Assert(object->rep->ref < (1 << 8*sizeof(lyxstring::Srep::ref)) - 1);
-       // if it does ever == then we should be generating a new copy
-       // and starting again.  (Is char always 8-bits?)
-}
-#define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
-#else
-#define TestlyxstringInvariant(s)
-#endif //DEVEL_VERSION
-//-------------------------------------------------------------------------
 
 ///////////////////////////////////////
-// Constructors and Deconstructors.
+// The internal string representation
 ///////////////////////////////////////
 
+struct lyxstring::Srep {
+       /// size
+       size_t sz;
+       /// Reference count
+       size_t ref;
+       /// The total amount of data reserved for this representaion
+       size_t res;
+       /// Data. At least 1 char for trailing null.
+       lyxstring::value_type * s;
+       
+       ///
+       Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
+       ///
+       Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
+       ///
+       ~Srep() { delete[] s; }
+       ///
+       Srep * get_own_copy() {
+               if (ref == 1) return this;
+               --ref;
+               return new Srep(sz, s);
+       }
+       
+       ///
+       void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
+       ///
+       void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
+       ///
+       void append(lyxstring::size_type asz, const lyxstring::value_type * p);
+       ///
+       void push_back(lyxstring::value_type c);
+       ///
+       void insert(lyxstring::size_type pos,
+                   const lyxstring::value_type * p,
+                   lyxstring::size_type n);
+       ///
+       void resize(lyxstring::size_type n, lyxstring::value_type c);
+       ///
+       void reserve(lyxstring::size_type res_arg);
+       ///
+       void replace(lyxstring::size_type i, lyxstring::size_type n,
+                    lyxstring::value_type const * p, lyxstring::size_type n2);
+private:
+       Srep(const Srep &);
+       Srep & operator=(const Srep &);
+};
+
 
 lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
 {
-// can be called with p==0 by lyxstring::assign(const value_type *, size_type)
+       // can be called with p == 0 by
+       // lyxstring::assign(const value_type *, size_type)
 
        sz = nsz;
        ref = 1;
-       res = sz + xtra;
+       res = sz ? sz : 1;
        s = new value_type[res + 1]; // add space for terminator
        if (p && sz) {
                // if sz = 0 nothing gets copied and we have an error
@@ -161,7 +140,7 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
 {
        sz = nsz;
        ref = 1;
-       res = sz + xtra;
+       res = sz ? sz : 1;
        s = new value_type[res + 1]; // add space for terminator
        memset(s, ch, sz);
        if (!ch) {
@@ -174,12 +153,13 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
 
 void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
 {
-// can be called with p==0 by lyxstring::assign(const value_type *, size_type)
+       // can be called with p == 0
+       // by lyxstring::assign(const value_type *, size_type)
 
        if (res < nsz) {
                delete[] s;
                sz = nsz;
-               res = sz + xtra;
+               res = sz ? sz : 1;
                s = new value_type[res + 1]; // add space for terminator
        } else {
                sz = nsz;
@@ -200,7 +180,7 @@ void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
        sz = nsz;
        if (res < nsz) {
                delete[] s;
-               res = sz + xtra;
+               res = sz ? sz : 1;
                s = new value_type[res + 1]; // add space for terminator
        }
        memset(s, ch, sz);
@@ -216,7 +196,9 @@ void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
 {
        register unsigned int const len = sz + asz;
        if (res < len) {
-               res = len + xtra;
+               do {
+                       res *= 2;
+               } while (res < len);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, sz);
                memcpy(tmp + sz, p, asz);
@@ -235,7 +217,9 @@ void lyxstring::Srep::push_back(value_type c)
        s[sz] = c; // it is always room to put a value_type at the end
        ++sz;
        if (res < sz) {
-               res = sz + xtra;
+               do {
+                       res *= 2;
+               } while (res < sz);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, sz);
                delete[] s;
@@ -245,20 +229,21 @@ void lyxstring::Srep::push_back(value_type c)
 
 
 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
-                          lyxstring::size_type n)
+                            lyxstring::size_type n)
 {
-       Assert(pos <= sz);
        if (res < n + sz) {
-               res = sz + n + xtra;
+               do {
+                       res *= 2;
+               } while (res < n + sz);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, pos);
                memcpy(tmp + pos, p, n);
-               memcpy(tmp + pos + n, & s[pos], sz - pos);
+               memcpy(tmp + pos + n, &s[pos], sz - pos);
                sz += n;
                delete[] s;
                s = tmp;
        } else {
-               memmove(s + pos + n, & s[pos], sz - pos);
+               memmove(s + pos + n, &s[pos], sz - pos);
                memcpy(s + pos, p, n);
                sz += n;
        }
@@ -267,7 +252,6 @@ void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
 
 void lyxstring::Srep::resize(size_type n, value_type c)
 {
-       Assert(n < npos);
        // This resets sz to res_arg
        res = min(n, npos - 2); // We keep no xtra when we resize
        value_type * tmp = new value_type[res + 1];
@@ -293,10 +277,9 @@ void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
 
 
 void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
-                           value_type const * p, size_type n2)
+                             value_type const * p, size_type n2)
 {
-// can be called with p=0 and n2=0
-       Assert(i < sz && ((!p && !n2) || p));
+// can be called with p= 0 and n2= 0
        n = min(sz - i, n);
        sz -= n;
        if (res >= n2 + sz) {
@@ -304,7 +287,9 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
                memcpy(s + i, p, n2);
                sz += n2;
        } else {
-               res = sz + n2 + xtra;
+               do {
+                       res *= 2;
+               } while (res < n2 + sz);
                value_type * tmp = new value_type[res + 1];
                memcpy(tmp, s, i);
                memcpy(tmp + i, p, n2);
@@ -316,7 +301,103 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
 }
 
 
-lyxstring::size_type const lyxstring::npos = static_cast<lyxstring::size_type>(-1);
+///////////////////////////////////////
+// The lyxstring Invariant tester
+///////////////////////////////////////
+// There are no know bugs in lyxstring now, and it have been
+// tested for a long time. so we disable the invariant checker. (Lgb)
+#undef ENABLE_ASSERTIONS
+#ifdef ENABLE_ASSERTIONS
+
+/** Testing of the lyxstring invariant
+ * By creating an object that tests the lyxstring invariant during its
+ * construction *and* its deconstruction we greatly simplify our code.
+ * Calling TestlyxstringInvariant() upon entry to an lyxstring method 
+ * will test the invariant upon entry to the code.  If the Asserts fail
+ * then we know from the stack trace that the corruption occurred *before*
+ * entry to this method.  We can also be sure it didn't happen in any of
+ * the tested lyxstring methods.  It is therefore likely to be due to some
+ * other external force.
+ * Several lyxstring methods have multiple exit points which would otherwise
+ * require us to insert a separate test before each return.  But since we
+ * created an object its destructor will be called upon exit (any exit!).
+ * We thus get testing at both start and end of a method with one line of
+ * code at the head of a method.  More importantly,  we get good testing
+ * everytime we run the code.
+ * NOTE:  just because we test the invariant doesn't mean we can forget
+ * about testing pre and post conditions specific to any given method.
+ * This test simply proves that the lyxstring/Srep is in a valid state it
+ * does *not* prove that the method did what it was supposed to.
+ */
+class lyxstringInvariant {
+public:
+       lyxstringInvariant(lyxstring const *);
+       ~lyxstringInvariant();
+private:
+       void helper() const;
+       lyxstring const * object;
+};
+
+
+// To test if this scheme works "as advertised" uncomment the printf's in
+// the constructor and destructor below and then uncomment the printf and the
+// call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
+// The correct output when LyX has been recompiled and run is:
+//     lyxstringInvariant constructor
+//     lyxstring::operator=(char const *)
+//     lyxstringInvariant constructor
+//     lyxstringInvariant destructor completed
+//     lyxstringInvariant destructor completed
+// NOTE: The easiest way to catch this snippet of the output is to wait for
+//       the splash screen to disappear and then open and close Help->Credits
+//
+lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
+{
+       // printf("lyxstringInvariant constructor\n");
+       helper();
+}
+
+
+lyxstringInvariant::~lyxstringInvariant()
+{
+       helper();
+       // printf("lyxstringInvariant destructor completed\n");
+}
+
+
+void lyxstringInvariant::helper() const
+{
+       // Some of these tests might look pointless but they are
+       // all part of the invariant and if we want to make sure
+       // we have a bullet proof implementation then we need to
+       // test every last little thing we *know* should be true.
+       // I may have missed a test or two, so feel free to fill
+       // in the gaps.  ARRae.
+       using lyx::Assert;
+       Assert(object);
+       Assert(object->rep);
+       Assert(object->rep->s);    // s is never 0
+       Assert(object->rep->res);  // res cannot be 0
+       Assert(object->rep->sz <= object->rep->res);
+       Assert(object->rep->ref >= 1);  // its in use so it must be referenced
+       Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
+       // if it does ever == then we should be generating a new copy
+       // and starting again.  (Is char always 8-bits?)
+}
+#define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
+#else
+#define TestlyxstringInvariant(s)
+#endif /* ENABLE_ASSERTIONS */
+
+
+///////////////////////////////////////
+// Constructors and Deconstructors.
+///////////////////////////////////////
+
+lyxstring::size_type const lyxstring::npos =
+static_cast<lyxstring::size_type>(-1);
+
 
 lyxstring::lyxstring()
 {
@@ -328,7 +409,7 @@ lyxstring::lyxstring()
 
 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
 {
-       Assert(pos < x.rep->sz || pos == 0);
+       lyx::Assert(pos <= x.rep->sz); // STD!
        if (pos == 0 && n >= x.length()) { // this is the default
                x.rep->ref++;
                rep = x.rep;
@@ -340,10 +421,10 @@ lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
 
 lyxstring::lyxstring(value_type const * s, size_type n)
 {
-       Assert(s); // we don't allow null pointers
+       lyx::Assert(s && n < npos); // STD!
        static Srep empty_rep(0, "");
-       if (s && *s && n) { // s is not empty string and n > 0
-               rep = new Srep(min(strlen(s), n), s);
+       if (n) { // n > 0
+               rep = new Srep(n, s);
        } else {
                ++empty_rep.ref;
                rep = &empty_rep;
@@ -353,10 +434,9 @@ lyxstring::lyxstring(value_type const * s, size_type n)
 
 lyxstring::lyxstring(value_type const * s)
 {
-       // yes we allow them just don't initalize them
-       // Assert(s); // we don't allow null pointers
+       lyx::Assert(s); // STD!
        static Srep empty_rep(0, "");
-       if (s && *s) { // s is not empty string
+       if (*s) { // s is not empty string
                rep = new Srep(strlen(s), s);
        } else {
                ++empty_rep.ref;
@@ -367,22 +447,29 @@ lyxstring::lyxstring(value_type const * s)
 
 lyxstring::lyxstring(size_type n, value_type c)
 {
+       lyx::Assert(n < npos); // STD!
        rep = new Srep(n, c);
 }
 
 
-lyxstring::lyxstring(iterator first, iterator last)
+lyxstring::lyxstring(const_iterator first, const_iterator last)
 {
        rep = new Srep(last - first, first);
 }
 
 
+lyxstring::~lyxstring()
+{
+       if (--rep->ref == 0) delete rep;
+}
+
 ///////////////////////
 // Iterators
 ///////////////////////
 
 lyxstring::iterator lyxstring::begin()
 {
+       rep = rep->get_own_copy();
        return rep->s;
 }
 
@@ -395,6 +482,7 @@ lyxstring::const_iterator lyxstring::begin() const
 
 lyxstring::iterator lyxstring::end()
 {
+       rep = rep->get_own_copy();
        return rep->s + rep->sz;
 }
 
@@ -429,12 +517,20 @@ const_reverse_iterator lyxstring::rend() const
 }
 #endif
 
+
 ///////////////////////
 // Size and Capacity
 ///////////////////////
 
+lyxstring::size_type lyxstring::size() const
+{ 
+       return rep->sz;
+}
+
+
 void lyxstring::resize(size_type n, value_type c)
 {
+       lyx::Assert(n <= npos); // STD!
        TestlyxstringInvariant(this);
 
        // This resets sz to res_arg
@@ -472,9 +568,9 @@ lyxstring & lyxstring::operator=(lyxstring const & x)
 
 lyxstring & lyxstring::operator=(value_type const * s)
 {
-       Assert(s);      
+       lyx::Assert(s); // OURS!
        TestlyxstringInvariant(this);
-//     printf("lyxstring::operator=(value_type const *)\n");
+//     printf("lyxstring::operator= (value_type const *)\n");
 
        return assign(s);
 }
@@ -509,6 +605,7 @@ lyxstring & lyxstring::assign(lyxstring const & x)
 
 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
 {
+       lyx::Assert(pos <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        return assign(x.substr(pos, n));
@@ -517,14 +614,13 @@ lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
 
 lyxstring & lyxstring::assign(value_type const * s, size_type n)
 {
-       Assert(s);
+       lyx::Assert(s && n < npos); // STD!
        TestlyxstringInvariant(this);
 
-       n = min(strlen(s), n);
        if (rep->ref == 1) // recycle rep
                rep->assign(n, s);
        else {
-               rep->ref--;
+               --rep->ref;
                rep = new Srep(n, s);
        }
        return *this;
@@ -533,7 +629,7 @@ lyxstring & lyxstring::assign(value_type const * s, size_type n)
 
 lyxstring & lyxstring::assign(value_type const * s)
 {
-       Assert(s);
+       lyx::Assert(s); // OURS!
        TestlyxstringInvariant(this);
 
        return assign(s, strlen(s));
@@ -550,7 +646,7 @@ lyxstring & lyxstring::assign(size_type n, value_type ch)
 }
 
 
-lyxstring & lyxstring::assign(iterator first, iterator last)
+lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
 {
        TestlyxstringInvariant(this);
 
@@ -566,15 +662,20 @@ lyxstring & lyxstring::assign(iterator first, iterator last)
 
 lyxstring::const_reference lyxstring::operator[](size_type pos) const
 {
-       Assert(pos < rep->sz);
-
+#if 0
+       lyx::Assert(pos <= rep->sz); // OURS!
+       static char helper = '\0';
+       return pos == rep->sz ? helper : rep->s[pos];
+#else
+       lyx::Assert(pos < rep->sz); // OURS!
        return rep->s[pos];
+#endif
 }
 
 
 lyxstring::reference lyxstring::operator[](size_type pos)
 {
-       Assert(pos < rep->sz);
+       lyx::Assert(pos < rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -584,15 +685,14 @@ lyxstring::reference lyxstring::operator[](size_type pos)
 
 lyxstring::const_reference lyxstring::at(size_type n) const
 {
-       Assert(n < rep->sz);
-       
+       lyx::Assert(n < rep->sz); // STD!
        return rep->s[n];
 }
 
 
 lyxstring::reference lyxstring::at(size_type n)
 {
-       Assert(n < rep->sz);
+       lyx::Assert(n < rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -614,7 +714,7 @@ lyxstring & lyxstring::operator+=(lyxstring const & x)
 
 lyxstring & lyxstring::operator+=(value_type const * x)
 {
-       Assert(x);
+       lyx::Assert(x); // OURS!
        TestlyxstringInvariant(this);
 
        return append(x);
@@ -652,6 +752,7 @@ lyxstring & lyxstring::append(lyxstring const & x)
 
 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
 {
+       lyx::Assert(pos <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        return append(x.substr(pos, n));
@@ -660,25 +761,20 @@ lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
 
 lyxstring & lyxstring::append(value_type const * p, size_type n)
 {
-       Assert(p);
+       lyx::Assert(p); // OURS!
        TestlyxstringInvariant(this);
 
        if (!*p || !n) return *this;
        rep = rep->get_own_copy();
-       rep->append(min(n, strlen(p)), p);
+       rep->append(n, p);
        return *this;
 }
 
 
 lyxstring & lyxstring::append(value_type const * p)
 {
-       Assert(p);
-       TestlyxstringInvariant(this);
-
-       if (!*p) return *this;
-       rep = rep->get_own_copy();
-       rep->append(strlen(p), p);
-       return *this;
+       lyx::Assert(p); // OURS!
+       return append(p, strlen(p));
 }
 
 
@@ -689,7 +785,7 @@ lyxstring & lyxstring::append(size_type n, value_type c)
        value_type * tmp = new value_type[n];
        memset(tmp, c, n);
        rep = rep->get_own_copy();
-       rep->assign(n, tmp);
+       rep->append(n, tmp);
        delete[] tmp;
        return *this;
 }
@@ -704,7 +800,7 @@ lyxstring & lyxstring::append(iterator first, iterator last)
        return *this;
 }
 
-// insert value_typeacters before (*this)[pos]
+// insert characters before (*this)[pos]
 
 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
 {
@@ -715,8 +811,9 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
 
 
 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
-                         size_type pos2, size_type n)
+                             size_type pos2, size_type n)
 {
+       lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -727,13 +824,13 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
 
 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
 {
-       Assert(p);
+       lyx::Assert(p); // OURS!
        TestlyxstringInvariant(this);
 
        if (*p && n) {
                // insert nothing and you change nothing
                rep = rep->get_own_copy();
-               rep->insert(pos, p, min(n, strlen(p)));
+               rep->insert(pos, p, n);
        }
        return *this;
 }
@@ -741,15 +838,8 @@ lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
 
 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
 {
-       Assert(p);
-       TestlyxstringInvariant(this);
-
-       if (*p) {
-               // insert nothing and you change nothing
-               rep = rep->get_own_copy();
-               rep->insert(pos, p, strlen(p));
-       }
-       return *this;
+       lyx::Assert(p); // OURS!
+       return insert(pos, p, strlen(p));
 }
 
 
@@ -804,17 +894,18 @@ void lyxstring::insert(iterator p, iterator first, iterator last)
 
 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
 {
-       if (!rep->sz) return npos;
+       if (!rep->sz || i >= rep->sz) return npos;
        
-       Assert(i < rep->sz);
        TestlyxstringInvariant(this);
 
-       for (size_type t = i; rep->sz - t >= a.length(); ++t) {
+       size_type n = a.length();
+       if (!n) return npos;
+       for (size_type t = i; rep->sz - t >= n; ++t) {
                // search until (*this)[i] == a[0]
                if (rep->s[t] == a[0]) {
                        // check if the rest of the value_types match
                        bool equal = true;
-                       for (size_type j = 0; j < a.length(); ++j) {
+                       for (size_type j = 1; j < n; ++j) {
                                if (rep->s[t + j] != a[j]) {
                                        equal = false;
                                        break;
@@ -828,12 +919,11 @@ lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
 
 
 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
-                                size_type n) const
+                                    size_type n) const
 {
-       Assert(ptr);
-       if (!rep->sz || !*ptr) return npos;
+       lyx::Assert(ptr); // OURS!
+       if (!rep->sz || !*ptr || i >= rep->sz) return npos;
        
-       Assert(i < rep->sz);
        TestlyxstringInvariant(this);
 
        // What is "n" here? is it the number of value_types to use in ptr
@@ -841,12 +931,13 @@ lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
        // for ptr in? For now I will assume that "n" tells the length
        // of ptr. (Lgb)
        n = min(n, strlen(ptr));
-       for (size_type t = i; length() - t >= n; ++t) {
+       if (!n) return npos;
+       for (size_type t = i; rep->sz - t >= n; ++t) {
                // search until (*this)[i] == a[0]
                if (rep->s[t] == ptr[0]) {
                        // check if the rest of the value_types match
                        bool equal = true;
-                       for (size_type j = 0; j < n; ++j) {
+                       for (size_type j = 1; j < n; ++j) {
                                if (rep->s[t + j] != ptr[j]) {
                                        equal = false;
                                        break;
@@ -861,10 +952,9 @@ lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
 
 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
 {
-       Assert(s);
-       if (!rep->sz) return npos;
+       lyx::Assert(s); // OURS!
+       if (!rep->sz || i >= rep->sz) return npos;
        
-       Assert(i < rep->sz);
        TestlyxstringInvariant(this);
 
        if (!s || !*s) return npos;
@@ -874,12 +964,11 @@ lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
 
 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
 {
-       if (!rep->sz) return npos;
+       if (!rep->sz || i >= rep->sz) return npos;
 
-       Assert(i < rep->sz);
        TestlyxstringInvariant(this);
 
-        for (size_type t = 0; t + i < length(); ++t) {
+        for (size_type t = 0; t + i < rep->sz; ++t) {
                if (rep->s[t + i] == c) return t + i;
        }
         return npos;
@@ -890,61 +979,63 @@ lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
+       size_type n = a.length();
+       if (!n || rep->sz < n)
+               return npos;
+
+       size_type t = min(rep->sz - n, i);
        do {
-               if (a[a.length() - 1] == rep->s[ii]) {
-                       int t = length() - 2;
-                       size_type l = ii - 1;
-                       for (; t >= 0; --t, --l) {
-                               if (a[t] != rep->s[l]) break;
+               if (rep->s[t] == a[0]) {
+                       // check if the rest of the value_types match
+                       bool equal = true;
+                       for (size_type j = 1; j < n; ++j) {
+                               if (rep->s[t + j] != a[j]) {
+                                       equal = false;
+                                       break;
+                               }
                        }
-                       if (a[t] == rep->s[l]) return l;
+                       if (equal) return t;
                }
-       } while(ii-- > 0);
+       } while (t-- > 0);
        return npos;
 }
 
 
 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
-                                 size_type n) const
+                                     size_type n) const
 {
-       Assert(ptr);
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
-       if (!*ptr) return npos;
 
-       size_type ii = min(length() - 1, i);
+       n = min(n, strlen(ptr));
+       if (!n || rep->sz < n)
+               return npos;
+
+       size_type t = min(rep->sz - n, i);
        do {
-               if (ptr[n - 1] == rep->s[ii]) {
-                       int t = n - 2;
-                       size_type l = ii - 1;
-                       for (; t >= 0; --t, --l) {
-                               if (ptr[t] != rep->s[l]) break;
+               if (rep->s[t] == ptr[0]) {
+                       // check if the rest of the value_types match
+                       bool equal = true;
+                       for (size_type j = 1; j < n; ++j) {
+                               if (rep->s[t + j] != ptr[j]) {
+                                       equal = false;
+                                       break;
+                               }
                        }
-                       if (ptr[t] == rep->s[l]) return l;
+                       if (equal) return t;
                }
-       } while (ii-- > 0);
+       } while (t-- > 0);
        return npos;
 }
 
 
-lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const
+lyxstring::size_type lyxstring::rfind(value_type const * ptr,
+                                     size_type i) const
 {
-       Assert(ptr);
-       TestlyxstringInvariant(this);
-       if (!*ptr) return npos;
+       lyx::Assert(ptr); // OURS!
 
-       size_type ii = min(length() - 1, i);
-       do {
-               if (ptr[strlen(ptr) - 1] == rep->s[ii]) {
-                       int t = strlen(ptr) - 2;
-                       size_type l = ii - 1;
-                       for (; t >= 0; --t, --l) {
-                               if (ptr[t] != rep->s[l]) break;
-                       }
-                       if (ptr[t] == rep->s[l]) return l;
-               }
-       } while (ii-- > 0);
-       return npos;
+       if (!ptr || !*ptr) return npos;
+       return rfind(ptr, i, strlen(ptr));
 }
 
 
@@ -952,48 +1043,51 @@ lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
-        for (size_type t = ii; t != 0; --t) {
-               if (rep->s[t] == c) return t;
-       }
-        return npos;
+       size_type const sz = rep->sz;
+       if (sz < 1) return npos;
+       size_type ii = min(sz - 1, i);
+       do {
+               if (rep->s[ii] == c) return ii;
+       } while (ii-- > 0);
+       return npos;
 }
 
 
 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
-                                         size_type i) const
+                                             size_type i) const
 {
-       Assert(i < rep->sz);
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       for (size_type t = i; t < length(); ++t) {
+       for (size_type t = i; t < rep->sz; ++t) {
                if (a.find(rep->s[t]) != npos) return t;
        }
        return npos;
 }
 
 
-lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
-                                         size_type n) const
+lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
+                                             size_type i,
+                                             size_type n) const
 {
-       Assert(ptr && i < rep->sz);
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
        if (!n) return npos;
 
-       for (size_type t = i; t < length(); ++t) {
-               if(memchr(ptr, rep->s[t], n) != 0) return t;
+       for (size_type t = i; t < rep->sz; ++t) {
+               if (memchr(ptr, rep->s[t], n) != 0) return t;
        }
        return npos;
 }
 
 
 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
-                                         size_type i) const
+                                             size_type i) const
 {
-       Assert(ptr && i < rep->sz);
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       for (size_type t = i; t < length(); ++t) {
+       for (size_type t = i; t < rep->sz; ++t) {
                if (strchr(ptr, rep->s[t]) != 0) return t;
        }
        return npos;
@@ -1002,10 +1096,10 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
 
 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
 {
-       Assert(i < rep->sz);
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       for (size_type t = i; t < length(); ++t) {
+       for (size_type t = i; t < rep->sz; ++t) {
                if (rep->s[t] == c) return t;
        }
        return npos;
@@ -1013,39 +1107,41 @@ lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
 
 
 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
-                                        size_type i) const
+                                            size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
                if (a.find(rep->s[t]) != npos) return t;
        }
        return npos;
 }
 
-lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
-                                        size_type n) const
+
+lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
+                                            size_type i,
+                                            size_type n) const
 {
-       Assert(ptr);
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
        if (!n) return npos;
 
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
-               if(memchr(ptr, rep->s[t], n) != 0) return t;
+               if (memchr(ptr, rep->s[t], n) != 0) return t;
        }
        return npos;
 }
 
 
 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
-                                        size_type i) const
+                                            size_type i) const
 {
-       Assert(ptr);
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
                if (strchr(ptr, rep->s[t]) != 0) return t;
        }
@@ -1058,7 +1154,7 @@ lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
        TestlyxstringInvariant(this);
 
        if (!rep->sz) return npos;
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
                if (rep->s[t] == c) return t;
        }
@@ -1067,53 +1163,55 @@ lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
 
 
 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
-                                             size_type i) const
+                                                 size_type i) const
 {
        TestlyxstringInvariant(this);
 
        if (!rep->sz) return npos;
-       Assert(i < rep->sz);
-       for (size_type t = i; t < length(); ++t) {
+       lyx::Assert(i <= rep->sz);
+       for (size_type t = i; t < rep->sz; ++t) {
                if (a.find(rep->s[t]) == npos) return t;
        }
        return npos;
 }
 
 
-lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i,
-                                             size_type n) const
+lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
+                                                 size_type i,
+                                                 size_type n) const
 {
-       Assert(ptr && i < rep->sz);
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       if (!n) return (i < length()) ? i : npos;
-       for (size_type t = i; t < length(); ++t) {
-               if(memchr(ptr, rep->s[t], n) == 0) return t;
+       if (!n) return (i < rep->sz) ? i : npos;
+       for (size_type t = i; t < rep->sz; ++t) {
+               if (memchr(ptr, rep->s[t], n) == 0) return t;
        }
        return npos;
 }
 
 
 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
-                                             size_type i) const
+                                                 size_type i) const
 {
-       Assert(ptr && i < rep->sz);
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       for (size_type t = i; t < length(); ++t) {
+       for (size_type t = i; t < rep->sz; ++t) {
                if (strchr(ptr, rep->s[t]) == 0) return t;
        }
        return npos;
 }
 
 
-lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const
+lyxstring::size_type lyxstring::find_first_not_of(value_type c,
+                                                 size_type i) const
 {
        if (!rep->sz) return npos;
-       Assert(i < rep->sz);
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       for (size_type t = i; t < length(); ++t) {
+       for (size_type t = i; t < rep->sz; ++t) {
                if (rep->s[t] != c) return t;
        }
        return npos;
@@ -1121,11 +1219,11 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) con
 
 
 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
-                                            size_type i) const
+                                                size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
                if (a.find(rep->s[t]) == npos) return t;
        }
@@ -1133,28 +1231,30 @@ lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
 }
 
 
-lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, size_type i,
-                                            size_type n) const
+lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
+                                                size_type i,
+                                                size_type n) const
 {
-       Assert(ptr);
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
-       if (!n) return (ii >= 0) ? ii : npos;
+       if (!n) return npos;
+       size_type ii = min(rep->sz - 1, i);
+
        for (int t = ii; t >= 0; --t) {
-               if(memchr(ptr, rep->s[t], n) == 0) return t;
+               if (memchr(ptr, rep->s[t], n) == 0) return t;
        }
        return npos;
 }
 
 
 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
-                                            size_type i) const
+                                                size_type i) const
 {
-       Assert(ptr);
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
                if (strchr(ptr, rep->s[t]) == 0) return t;
        }
@@ -1162,11 +1262,12 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
 }
 
 
-lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) const
+lyxstring::size_type lyxstring::find_last_not_of(value_type c,
+                                                size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(length() - 1, i);
+       size_type ii = min(rep->sz - 1, i);
        for (int t = ii; t >= 0; --t) {
                if (rep->s[t] != c) return t;
        }
@@ -1180,17 +1281,17 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type c, size_type i) cons
 
 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
 {
-       Assert(i < rep->sz || i == 0);
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
-       return replace(i, n, x, 0, x.length());
+       return replace(i, n, x, 0, x.rep->sz);
 }
 
 
-lyxstring &  lyxstring::replace(size_type i,size_type n, lyxstring const & x,
-                           size_type i2, size_type n2)
+lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
+                              size_type i2, size_type n2)
 {
-       Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0));
+       lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1199,10 +1300,10 @@ lyxstring &  lyxstring::replace(size_type i,size_type n, lyxstring const & x,
 }
 
 
-lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
-                          size_type n2)
+lyxstring & lyxstring::replace(size_type i, size_type n,
+                              value_type const * p, size_type n2)
 {
-       Assert(p && i < rep->sz);
+       lyx::Assert(p && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1213,16 +1314,17 @@ lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p,
 
 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
 {
-       Assert(p && i < rep->sz);
+       lyx::Assert(p && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
 }
 
 
-lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c)
+lyxstring & lyxstring::replace(size_type i, size_type n,
+                              size_type n2, value_type c)
 {
-       Assert(i < rep->sz);
+       lyx::Assert(i <= rep->sz);  // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1234,6 +1336,13 @@ lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_typ
 }
 
 
+/// FY! FY! FY! go away !
+lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
+{
+       return replace(i, n, 1, c);
+}
+
+
 lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
 {
        TestlyxstringInvariant(this);
@@ -1243,9 +1352,9 @@ lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
 
 
 lyxstring & lyxstring::replace(iterator i, iterator i2,
-                          value_type const * p, size_type n)
+                              value_type const * p, size_type n)
 {
-       Assert(p);
+       lyx::Assert(p); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i - begin(), i2 - i, p, n);
@@ -1254,14 +1363,15 @@ lyxstring & lyxstring::replace(iterator i, iterator i2,
 
 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
 {
-       Assert(p);
+       lyx::Assert(p); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i - begin(), i2 - i, p);
 }
 
 
-lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type c)
+lyxstring & lyxstring::replace(iterator i, iterator i2,
+                              size_type n , value_type c)
 {
        TestlyxstringInvariant(this);
 
@@ -1269,7 +1379,8 @@ lyxstring & lyxstring::replace(iterator i, iterator i2, size_type n , value_type
 }
        
 
-lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
+lyxstring & lyxstring::replace(iterator i, iterator i2,
+                              iterator j, iterator j2)
 {
        TestlyxstringInvariant(this);
 
@@ -1277,9 +1388,18 @@ lyxstring & lyxstring::replace(iterator i, iterator i2, iterator j, iterator j2)
 }
 
 
+void lyxstring::swap(lyxstring & str)
+{
+       if (rep == str.rep) return;
+       Srep * tmp = str.rep;
+       str.rep = rep;
+       rep = tmp;
+}
+
+
 lyxstring & lyxstring::erase(size_type i, size_type n)
 {
-       Assert(i < rep->sz || i == 0);
+       lyx::Assert(i <= rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1331,9 +1451,11 @@ lyxstring::value_type const * lyxstring::data() const
 }
 
 
-lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const
+lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
+                                    size_type pos) const
 {
-       Assert(buf);
+       lyx::Assert(buf); // OURS!
+       lyx::Assert(pos <= rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        register int nn = min(len, length() - pos);
@@ -1347,65 +1469,74 @@ lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type
 ////////////////////
 
 // Compare funcs should be verified.
-// Should we try to make them work with '\0' value_types?
-// An STL string can usually contain '\0' value_types.
+
+int lyxstring::internal_compare(size_type pos, size_type n,
+                               value_type const * s,
+                               size_type slen, size_type n2) const
+{
+       if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
+       if (!*s) return 1;
+       // since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
+
+        // remember that n can very well be a lot larger than rep->sz
+        // so we have to ensure that n is no larger than rep->sz
+        n = min(n, rep->sz);
+       n2 = min(n2, slen);
+        if (n == n2)
+               return memcmp(&(rep->s[pos]), s, n);
+       int c = memcmp(&(rep->s[pos]), s, min(n, n2));
+       if (c)
+               return c;
+       if (n < n2)
+               return -1;
+       return 1;
+}
+
 
 int lyxstring::compare(lyxstring const & str) const
 {
        TestlyxstringInvariant(this);
-
-       return compare(0, rep->sz, str.rep->s, str.rep->sz);
+       return internal_compare(0, rep->sz, str.rep->s,
+                               str.rep->sz, str.rep->sz);
 }
 
 
 int lyxstring::compare(value_type const * s) const
 {
-       Assert(s);
+       lyx::Assert(s); //OURS!
        TestlyxstringInvariant(this);
-
-       return compare(0, rep->sz, s, (!s) ? 0 : strlen(s));
+       int n = (!s) ? 0 : strlen(s);
+       return internal_compare(0, rep->sz, s, n, n);
 }
 
 
-int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
+int lyxstring::compare(size_type pos, size_type n,
+                      lyxstring const & str) const
 {
+       lyx::Assert(pos <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
-
-       return compare(pos, n, str.rep->s, str.rep->sz);
+       return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
 }
 
 
 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
-                    size_type pos2, size_type n2) const
+                      size_type pos2, size_type n2) const
 {
+       lyx::Assert(pos <= rep->sz); // OURS!
+       lyx::Assert(pos2 <= str.rep->sz); // OURS!
        TestlyxstringInvariant(this);
-
-       return compare(pos, n, str.rep->s + pos2, n2);
+       return internal_compare(pos, n,
+                               str.rep->s + pos2,
+                               str.rep->sz - pos2, n2);
 }
 
 
 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
-                    size_type n2) const
+                      size_type n2) const
 {
-       Assert(s && (pos < rep->sz || pos == 0));
+       lyx::Assert(s && pos <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
-
-       if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
-       if (!*s) return 1;
-       // since n > n2, min(n,n2) == 0, c == 0 (stops segfault also)
-
-        // remember that n can very well be a lot larger than rep->sz
-        // so we have to ensure that n is no larger than rep->sz
-        n = min(n, rep->sz);
-       n2 = min(n2, strlen(s));
-        if (n == n2)
-               return memcmp(&(rep->s[pos]), s, n);
-       int c = memcmp(&(rep->s[pos]), s, min(n,n2));
-       if (c)
-               return c;
-       if (n < n2)
-               return -1;
-       return 1;
+       return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
 }
 
 
@@ -1416,14 +1547,13 @@ int lyxstring::compare(size_type pos, size_type n, value_type const * s,
 // i = index, n = length
 lyxstring lyxstring::substr(size_type i, size_type n) const
 {
-       Assert(i < rep->sz || i == 0);
+       lyx::Assert(i <= rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        return lyxstring(*this, i, n);
 }
 
 
-
 /////////////////////////////////////////////
 // String operators, non member functions
 /////////////////////////////////////////////
@@ -1436,14 +1566,14 @@ bool operator==(lyxstring const & a, lyxstring const & b)
 
 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        return b.compare(a) == 0;
 }
 
 
 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        return a.compare(b) == 0;
 }
 
@@ -1456,14 +1586,14 @@ bool operator!=(lyxstring const & a, lyxstring const & b)
 
 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        return b.compare(a) != 0;
 }
 
 
 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        return a.compare(b) != 0;
 }
 
@@ -1476,14 +1606,14 @@ bool operator>(lyxstring const & a, lyxstring const & b)
 
 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        return b.compare(a) < 0; // since we reverse the parameters
 }
 
 
 bool operator>(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        return a.compare(b) > 0;
 }
 
@@ -1496,14 +1626,14 @@ bool operator<(lyxstring const & a, lyxstring const & b)
 
 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        return b.compare(a) > 0; // since we reverse the parameters
 }
 
 
 bool operator<(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        return a.compare(b) < 0;
 }
 
@@ -1516,14 +1646,14 @@ bool operator>=(lyxstring const & a, lyxstring const & b)
 
 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        return b.compare(a) <= 0; // since we reverse the parameters
 }
 
 
 bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        return a.compare(b) >= 0;
 }
 
@@ -1536,14 +1666,14 @@ bool operator<=(lyxstring const & a, lyxstring const & b)
 
 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        return b.compare(a) >= 0; // since we reverse the parameters
 }
 
 
 bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        return a.compare(b) <= 0;
 }
 
@@ -1558,7 +1688,7 @@ lyxstring operator+(lyxstring const & a, lyxstring const & b)
 
 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a);
+       lyx::Assert(a); // OURS!
        lyxstring tmp(a);
        tmp += b;
        return tmp;
@@ -1576,7 +1706,7 @@ lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
 
 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b);
+       lyx::Assert(b); // OURS!
        lyxstring tmp(a);
        tmp += b;
        return tmp;
@@ -1590,31 +1720,54 @@ lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
        return tmp;
 }
 
+
+void swap(lyxstring & str1, lyxstring & str2)
+{
+       str1.swap(str2);
+}
+
+
 #include <iostream>
 
 istream & operator>>(istream & is, lyxstring & s)
 {
+#if 1
        // very bad solution
-       char nome[1024];
+       char * nome = new char[1024];
        is >> nome;
        lyxstring tmp(nome);
+       delete [] nome;
        if (!tmp.empty()) s = tmp;
+#else
+       // better solution
+       int w = is.widdth(0);
+       s.clear();
+       char c = 0;
+       while (is.get(c)) {
+               if (isspace(c)) { is.putback(c); break; }
+               s += c;
+               if (--w == 1) break;
+       }
+       if (s.empty()) is.setstate(ios::failbit);
+#endif
        return is;
 }
 
+
 ostream & operator<<(ostream & o, lyxstring const & s)
 {
        return o.write(s.data(), s.length());
 }
 
+
 istream & getline(istream & is, lyxstring & s,
                  lyxstring::value_type delim)
 {
        // very bad solution
-       char tmp;
+       char tmp = 0;
        s.erase();
-       while(is) {
-               is >> tmp;
+       while (is) {
+               is.get(tmp);
                if (tmp != delim) {
                        s += tmp;
                } else {
@@ -1624,4 +1777,11 @@ istream & getline(istream & is, lyxstring & s,
        return is;
 }
 
-#endif /* USE_INCLUDED_STRING */
+#ifdef TEST_MAIN
+int main() {
+       lyxstring a = "abcac";
+       cout << a.rfind("ab") << endl;
+       cout << a.rfind("c") << endl;
+       cout << a.rfind("d") << endl;
+}
+#endif