]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
fix typo that put too many include paths for most people
[lyx.git] / src / support / lyxstring.C
index 65ef99a00fc92f681dee3cb5414f61a8591e18e8..42a9811cf5ea0926c3bee88ab64aea9c5979efb5 100644 (file)
@@ -1,10 +1,10 @@
 /* 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.
  *
  * ====================================================== */
 
 
 #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
@@ -59,6 +63,7 @@ using std::min;
 
 // Lgb.
 
+
 ///////////////////////////////////////
 // The internal string representation
 ///////////////////////////////////////
@@ -72,7 +77,7 @@ struct lyxstring::Srep {
        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);
        ///
@@ -85,7 +90,7 @@ struct lyxstring::Srep {
                --ref;
                return new Srep(sz, s);
        }
-       
+
        ///
        void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
        ///
@@ -111,10 +116,10 @@ private:
 };
 
 
-inline
 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;
@@ -131,7 +136,6 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
 }
 
 
-inline
 lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
 {
        sz = nsz;
@@ -145,12 +149,12 @@ lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
                sz = 0;
        }
 }
-       
 
-inline
+
 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;
@@ -171,7 +175,6 @@ void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
 }
 
 
-inline
 void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
 {
        sz = nsz;
@@ -189,7 +192,6 @@ void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
 }
 
 
-inline
 void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
 {
        register unsigned int const len = sz + asz;
@@ -210,7 +212,6 @@ void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
 }
 
 
-inline
 void lyxstring::Srep::push_back(value_type c)
 {
        s[sz] = c; // it is always room to put a value_type at the end
@@ -227,9 +228,8 @@ void lyxstring::Srep::push_back(value_type c)
 }
 
 
-inline
 void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
-                          lyxstring::size_type n)
+                            lyxstring::size_type n)
 {
        if (res < n + sz) {
                do {
@@ -250,7 +250,6 @@ void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
 }
 
 
-inline
 void lyxstring::Srep::resize(size_type n, value_type c)
 {
        // This resets sz to res_arg
@@ -265,7 +264,6 @@ void lyxstring::Srep::resize(size_type n, value_type c)
 }
 
 
-inline
 void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
 {
        // This keeps the old sz, but
@@ -278,9 +276,8 @@ void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
 }
 
 
-inline
 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
        n = min(sz - i, n);
@@ -299,7 +296,7 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
                memcpy(tmp + i + n2, &s[i + n], sz - i);
                delete[] s;
                s = tmp;
-               sz += n2; 
+               sz += n2;
        }
 }
 
@@ -307,12 +304,16 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
 ///////////////////////////////////////
 // The lyxstring Invariant tester
 ///////////////////////////////////////
-#ifdef DEVEL_VERSION
+
+// 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 
+ * 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
@@ -351,7 +352,6 @@ private:
 // 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
 //
-inline
 lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
 {
        // printf("lyxstringInvariant constructor\n");
@@ -359,7 +359,6 @@ lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
 }
 
 
-inline
 lyxstringInvariant::~lyxstringInvariant()
 {
        helper();
@@ -367,7 +366,6 @@ lyxstringInvariant::~lyxstringInvariant()
 }
 
 
-inline
 void lyxstringInvariant::helper() const
 {
        // Some of these tests might look pointless but they are
@@ -376,6 +374,7 @@ void lyxstringInvariant::helper() const
        // 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
@@ -389,7 +388,7 @@ void lyxstringInvariant::helper() const
 #define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
 #else
 #define TestlyxstringInvariant(s)
-#endif //DEVEL_VERSION
+#endif /* ENABLE_ASSERTIONS */
 
 
 ///////////////////////////////////////
@@ -400,7 +399,6 @@ lyxstring::size_type const lyxstring::npos =
 static_cast<lyxstring::size_type>(-1);
 
 
-inline
 lyxstring::lyxstring()
 {
        static Srep empty_rep(0, "");
@@ -409,10 +407,9 @@ lyxstring::lyxstring()
 }
 
 
-inline
 lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
 {
-       Assert(pos <= x.rep->sz); // STD!
+       lyx::Assert(pos <= x.rep->sz); // STD!
        if (pos == 0 && n >= x.length()) { // this is the default
                x.rep->ref++;
                rep = x.rep;
@@ -422,13 +419,12 @@ lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
 }
 
 
-inline
 lyxstring::lyxstring(value_type const * s, size_type n)
 {
-       Assert(s && n < npos); // STD!
+       lyx::Assert(s && n < npos); // STD!
        static Srep empty_rep(0, "");
-       if (*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;
@@ -436,10 +432,9 @@ lyxstring::lyxstring(value_type const * s, size_type n)
 }
 
 
-inline
 lyxstring::lyxstring(value_type const * s)
 {
-       Assert(s); // STD!
+       lyx::Assert(s); // STD!
        static Srep empty_rep(0, "");
        if (*s) { // s is not empty string
                rep = new Srep(strlen(s), s);
@@ -450,22 +445,19 @@ lyxstring::lyxstring(value_type const * s)
 }
 
 
-inline
 lyxstring::lyxstring(size_type n, value_type c)
 {
-       Assert(n < npos); // STD!
+       lyx::Assert(n < npos); // STD!
        rep = new Srep(n, c);
 }
 
 
-inline
 lyxstring::lyxstring(const_iterator first, const_iterator last)
 {
        rep = new Srep(last - first, first);
 }
 
 
-inline
 lyxstring::~lyxstring()
 {
        if (--rep->ref == 0) delete rep;
@@ -475,56 +467,50 @@ lyxstring::~lyxstring()
 // Iterators
 ///////////////////////
 
-inline
 lyxstring::iterator lyxstring::begin()
 {
+       rep = rep->get_own_copy();
        return rep->s;
 }
 
 
-inline
 lyxstring::const_iterator lyxstring::begin() const
 {
        return rep->s;
 }
 
 
-inline
 lyxstring::iterator lyxstring::end()
 {
+       rep = rep->get_own_copy();
        return rep->s + rep->sz;
 }
 
 
-inline
 lyxstring::const_iterator lyxstring::end() const
 {
        return rep->s + rep->sz;
 }
 
 #if 0
-inline
 reverse_iterator lyxstring::rbegin()
 {
        return reverse_iterator( end() );
 }
 
 
-inline
 const_reverse_iterator lyxstring::rbegin() const
 {
        return const_reverse_iterator( end() );
 }
 
 
-inline
 reverse_iterator lyxstring::rend()
 {
        return reverse_iterator( begin() );
 }
 
 
-inline
 const_reverse_iterator lyxstring::rend() const
 {
        return const_reverse_iterator( begin() );
@@ -536,17 +522,15 @@ const_reverse_iterator lyxstring::rend() const
 // Size and Capacity
 ///////////////////////
 
-inline
 lyxstring::size_type lyxstring::size() const
-{ 
+{
        return rep->sz;
 }
 
 
-inline
 void lyxstring::resize(size_type n, value_type c)
 {
-       Assert(n <= npos); // STD!
+       lyx::Assert(n <= npos); // STD!
        TestlyxstringInvariant(this);
 
        // This resets sz to res_arg
@@ -555,14 +539,12 @@ void lyxstring::resize(size_type n, value_type c)
 }
 
 
-inline
 lyxstring::size_type lyxstring::capacity() const
 {
        return rep->res;
 }
 
 
-inline
 void lyxstring::reserve(size_type res_arg)
 {
        TestlyxstringInvariant(this);
@@ -576,8 +558,7 @@ void lyxstring::reserve(size_type res_arg)
 // Assignment
 ////////////////
 
-inline
-lyxstring & lyxstring::operator= (lyxstring const & x)
+lyxstring & lyxstring::operator=(lyxstring const & x)
 {
        TestlyxstringInvariant(this);
 
@@ -585,10 +566,9 @@ lyxstring & lyxstring::operator= (lyxstring const & x)
 }
 
 
-inline
-lyxstring & lyxstring::operator= (value_type const * s)
+lyxstring & lyxstring::operator=(value_type const * s)
 {
-       Assert(s); // OURS!
+       lyx::Assert(s); // OURS!
        TestlyxstringInvariant(this);
 //     printf("lyxstring::operator= (value_type const *)\n");
 
@@ -596,7 +576,6 @@ lyxstring & lyxstring::operator= (value_type const * s)
 }
 
 
-inline
 lyxstring & lyxstring::operator=(value_type c)
 {
        TestlyxstringInvariant(this);
@@ -613,7 +592,6 @@ lyxstring & lyxstring::operator=(value_type c)
 }
 
 
-inline
 lyxstring & lyxstring::assign(lyxstring const & x)
 {
        TestlyxstringInvariant(this);
@@ -623,46 +601,41 @@ lyxstring & lyxstring::assign(lyxstring const & x)
        rep = x.rep; // share representation
        return *this;
 }
-       
 
-inline
+
 lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
 {
-       Assert(pos <= x.rep->sz); // STD!
+       lyx::Assert(pos <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        return assign(x.substr(pos, n));
 }
-       
 
-inline
+
 lyxstring & lyxstring::assign(value_type const * s, size_type n)
 {
-       Assert(s); // OURS!
+       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;
 }
-       
 
-inline
+
 lyxstring & lyxstring::assign(value_type const * s)
 {
-       Assert(s); // OURS!
+       lyx::Assert(s); // OURS!
        TestlyxstringInvariant(this);
 
        return assign(s, strlen(s));
 }
 
 
-inline
 lyxstring & lyxstring::assign(size_type n, value_type ch)
 {
        TestlyxstringInvariant(this);
@@ -673,7 +646,6 @@ lyxstring & lyxstring::assign(size_type n, value_type ch)
 }
 
 
-inline
 lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
 {
        TestlyxstringInvariant(this);
@@ -688,19 +660,22 @@ lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
 // Element Access
 ////////////////////
 
-inline
 lyxstring::const_reference lyxstring::operator[](size_type pos) const
 {
-       Assert(pos <= rep->sz); // OURS!
+#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
 }
 
 
-inline
 lyxstring::reference lyxstring::operator[](size_type pos)
 {
-       Assert(pos < rep->sz); // OURS!
+       lyx::Assert(pos < rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -708,18 +683,16 @@ lyxstring::reference lyxstring::operator[](size_type pos)
 }
 
 
-inline
 lyxstring::const_reference lyxstring::at(size_type n) const
 {
-       Assert(n < rep->sz); // STD!
+       lyx::Assert(n < rep->sz); // STD!
        return rep->s[n];
 }
 
 
-inline
 lyxstring::reference lyxstring::at(size_type n)
 {
-       Assert(n < rep->sz); // STD!
+       lyx::Assert(n < rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -731,7 +704,6 @@ lyxstring::reference lyxstring::at(size_type n)
 // Insert
 /////////////
 
-inline
 lyxstring & lyxstring::operator+=(lyxstring const & x)
 {
        TestlyxstringInvariant(this);
@@ -740,17 +712,15 @@ lyxstring & lyxstring::operator+=(lyxstring const & x)
 }
 
 
-inline
 lyxstring & lyxstring::operator+=(value_type const * x)
 {
-       Assert(x); // OURS!
+       lyx::Assert(x); // OURS!
        TestlyxstringInvariant(this);
 
        return append(x);
 }
 
 
-inline
 lyxstring & lyxstring::operator+=(value_type c)
 {
        TestlyxstringInvariant(this);
@@ -760,7 +730,6 @@ lyxstring & lyxstring::operator+=(value_type c)
 }
 
 
-inline
 void lyxstring::push_back(value_type c)
 {
        TestlyxstringInvariant(this);
@@ -770,7 +739,6 @@ void lyxstring::push_back(value_type c)
 }
 
 
-inline
 lyxstring & lyxstring::append(lyxstring const & x)
 {
        TestlyxstringInvariant(this);
@@ -782,43 +750,34 @@ lyxstring & lyxstring::append(lyxstring const & x)
 }
 
 
-inline
 lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
 {
-       Assert(pos <= x.rep->sz); // STD!
+       lyx::Assert(pos <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        return append(x.substr(pos, n));
 }
 
 
-inline
 lyxstring & lyxstring::append(value_type const * p, size_type n)
 {
-       Assert(p); // OURS!
+       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;
 }
 
 
-inline
 lyxstring & lyxstring::append(value_type const * p)
 {
-       Assert(p); // OURS!
-       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));
 }
 
 
-inline
 lyxstring & lyxstring::append(size_type n, value_type c)
 {
        TestlyxstringInvariant(this);
@@ -832,7 +791,6 @@ lyxstring & lyxstring::append(size_type n, value_type c)
 }
 
 
-inline
 lyxstring & lyxstring::append(iterator first, iterator last)
 {
        TestlyxstringInvariant(this);
@@ -842,9 +800,8 @@ lyxstring & lyxstring::append(iterator first, iterator last)
        return *this;
 }
 
-// insert value_typeacters before (*this)[pos]
+// insert characters before (*this)[pos]
 
-inline
 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
 {
        TestlyxstringInvariant(this);
@@ -853,11 +810,10 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
 }
 
 
-inline
 lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
-                         size_type pos2, size_type n)
+                             size_type pos2, size_type n)
 {
-       Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
+       lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -866,37 +822,27 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
 }
 
 
-inline
 lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
 {
-       Assert(p); // OURS!
+       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;
 }
 
 
-inline
 lyxstring & lyxstring::insert(size_type pos, value_type const * p)
 {
-       Assert(p); // OURS!
-       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));
 }
 
 
-inline
 lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
 {
        TestlyxstringInvariant(this);
@@ -910,7 +856,6 @@ lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
 }
 
 
-inline
 lyxstring::iterator lyxstring::insert(iterator p, value_type c)
 {
        TestlyxstringInvariant(this);
@@ -922,7 +867,6 @@ lyxstring::iterator lyxstring::insert(iterator p, value_type c)
 }
 
 
-inline
 void lyxstring::insert(iterator p, size_type n , value_type c)
 {
        TestlyxstringInvariant(this);
@@ -931,37 +875,37 @@ void lyxstring::insert(iterator p, size_type n , value_type c)
 }
 
 
-inline
 void lyxstring::insert(iterator p, iterator first, iterator last)
 {
        TestlyxstringInvariant(this);
 
        insert(p - begin(), first, last - first);
 }
-       
+
 
 ////////////////
 // Find
 ////////////////
-         // All the below find functions should be verified,
-         // it is very likely that I have mixed up or interpreted
-         // some of the parameters wrong, also some of the funcs can surely
-         // be written more effectively.
 
-inline
+        // All the below find functions should be verified,
+        // it is very likely that I have mixed up or interpreted
+        // some of the parameters wrong, also some of the funcs can surely
+        // be written more effectively.
+
 lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
 {
        if (!rep->sz || i >= rep->sz) return npos;
-       
+
        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;
@@ -974,13 +918,12 @@ lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
 }
 
 
-inline
 lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
-                                size_type n) const
+                                    size_type n) const
 {
-       Assert(ptr); // OURS!
+       lyx::Assert(ptr); // OURS!
        if (!rep->sz || !*ptr || i >= rep->sz) return npos;
-       
+
        TestlyxstringInvariant(this);
 
        // What is "n" here? is it the number of value_types to use in ptr
@@ -988,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));
+       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;
@@ -1006,12 +950,11 @@ lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
 {
-       Assert(s); // OURS!
+       lyx::Assert(s); // OURS!
        if (!rep->sz || i >= rep->sz) return npos;
-       
+
        TestlyxstringInvariant(this);
 
        if (!s || !*s) return npos;
@@ -1019,104 +962,101 @@ lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
 }
 
 
-inline
 lyxstring::size_type lyxstring::find(value_type c, size_type i) const
 {
        if (!rep->sz || i >= rep->sz) return npos;
 
        TestlyxstringInvariant(this);
 
-        for (size_type t = 0; t + i < rep->sz; ++t) {
-               if (rep->s[t + i] == c) return t + i;
+       for (size_type t = 0; t + i < rep->sz; ++t) {
+               if (rep->s[t + i] == c) return t + i;
        }
-        return npos;
+       return npos;
 }
 
 
-inline
 lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(rep->sz - 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 = rep->sz - 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;
 }
 
 
-inline
 lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
-                                 size_type n) const
+                                     size_type n) const
 {
-       Assert(ptr); // OURS!
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
-       if (!*ptr) return npos;
 
-       size_type ii = min(rep->sz - 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;
 }
 
 
-inline
 lyxstring::size_type lyxstring::rfind(value_type const * ptr,
                                      size_type i) const
 {
-       Assert(ptr); // OURS!
-       TestlyxstringInvariant(this);
-       if (!*ptr) return npos;
+       lyx::Assert(ptr); // OURS!
 
-       size_type ii = min(rep->sz - 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));
 }
 
 
-inline
 lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
 {
        TestlyxstringInvariant(this);
 
-       size_type ii = min(rep->sz - 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;
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
                                              size_type i) const
 {
-       Assert(i < rep->sz); // OURS!
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1126,27 +1066,25 @@ lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
                                              size_type i,
                                              size_type n) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
        if (!n) return npos;
 
        for (size_type t = i; t < rep->sz; ++t) {
-               if(memchr(ptr, rep->s[t], n) != 0) return t;
+               if (memchr(ptr, rep->s[t], n) != 0) return t;
        }
        return npos;
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
                                              size_type i) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1156,10 +1094,9 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
 {
-       Assert(i < rep->sz); // OURS!
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1169,7 +1106,6 @@ lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
                                             size_type i) const
 {
@@ -1183,28 +1119,26 @@ lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
                                             size_type i,
                                             size_type n) const
 {
-       Assert(ptr); // OURS!
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
        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;
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
                                             size_type i) const
 {
-       Assert(ptr); // OURS!
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
 
        size_type ii = min(rep->sz - 1, i);
@@ -1215,7 +1149,6 @@ lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
 {
        TestlyxstringInvariant(this);
@@ -1229,14 +1162,13 @@ lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
                                                  size_type i) const
 {
        TestlyxstringInvariant(this);
 
        if (!rep->sz) return npos;
-       Assert(i < rep->sz);
+       lyx::Assert(i <= rep->sz);
        for (size_type t = i; t < rep->sz; ++t) {
                if (a.find(rep->s[t]) == npos) return t;
        }
@@ -1244,27 +1176,25 @@ lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
                                                  size_type i,
                                                  size_type n) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        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;
+               if (memchr(ptr, rep->s[t], n) == 0) return t;
        }
        return npos;
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
                                                  size_type i) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       lyx::Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1274,12 +1204,11 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_first_not_of(value_type c,
                                                  size_type i) const
 {
        if (!rep->sz) return npos;
-       Assert(i < rep->sz); // OURS!
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1289,7 +1218,6 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type c,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
                                                 size_type i) const
 {
@@ -1303,29 +1231,27 @@ lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
                                                 size_type i,
                                                 size_type n) const
 {
-       Assert(ptr); // OURS!
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
 
        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;
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
                                                 size_type i) const
 {
-       Assert(ptr); // OURS!
+       lyx::Assert(ptr); // OURS!
        TestlyxstringInvariant(this);
 
        size_type ii = min(rep->sz - 1, i);
@@ -1336,7 +1262,6 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
 }
 
 
-inline
 lyxstring::size_type lyxstring::find_last_not_of(value_type c,
                                                 size_type i) const
 {
@@ -1354,21 +1279,19 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type c,
 // Replace
 /////////////////
 
-inline
 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
 {
-       Assert(i <= rep->sz); // OURS!
+       lyx::Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i, n, x, 0, x.rep->sz);
 }
 
 
-inline
 lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
                               size_type i2, size_type n2)
 {
-       Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
+       lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1377,11 +1300,10 @@ lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
 }
 
 
-inline
 lyxstring & lyxstring::replace(size_type i, size_type n,
                               value_type const * p, size_type n2)
 {
-       Assert(p && i < rep->sz); // OURS!
+       lyx::Assert(p && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1390,21 +1312,19 @@ lyxstring & lyxstring::replace(size_type i, size_type n,
 }
 
 
-inline
 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
 {
-       Assert(p && i < rep->sz); // OURS!
+       lyx::Assert(p && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
 }
 
 
-inline
 lyxstring & lyxstring::replace(size_type i, size_type n,
                               size_type n2, value_type c)
 {
-       Assert(i < rep->sz);  // OURS!
+       lyx::Assert(i <= rep->sz);  // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1416,37 +1336,40 @@ lyxstring & lyxstring::replace(size_type i, size_type n,
 }
 
 
-inline
+/// 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);
 
-       return replace(i - begin(), i2 - i, str); 
+       return replace(i - begin(), i2 - i, str);
 }
 
 
-inline
 lyxstring & lyxstring::replace(iterator i, iterator i2,
                               value_type const * p, size_type n)
 {
-       Assert(p); // OURS!
+       lyx::Assert(p); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i - begin(), i2 - i, p, n);
 }
 
 
-inline
 lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
 {
-       Assert(p); // OURS!
+       lyx::Assert(p); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i - begin(), i2 - i, p);
 }
 
 
-inline
 lyxstring & lyxstring::replace(iterator i, iterator i2,
                               size_type n , value_type c)
 {
@@ -1454,9 +1377,8 @@ lyxstring & lyxstring::replace(iterator i, iterator i2,
 
        return replace(i - begin(), i2 - i, n, c);
 }
-       
 
-inline
+
 lyxstring & lyxstring::replace(iterator i, iterator i2,
                               iterator j, iterator j2)
 {
@@ -1466,7 +1388,6 @@ lyxstring & lyxstring::replace(iterator i, iterator i2,
 }
 
 
-inline
 void lyxstring::swap(lyxstring & str)
 {
        if (rep == str.rep) return;
@@ -1476,10 +1397,9 @@ void lyxstring::swap(lyxstring & str)
 }
 
 
-inline
 lyxstring & lyxstring::erase(size_type i, size_type n)
 {
-       Assert(i <= rep->sz); // STD!
+       lyx::Assert(i <= rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1494,7 +1414,6 @@ lyxstring & lyxstring::erase(size_type i, size_type n)
 }
 
 
-inline
 lyxstring::iterator lyxstring::erase(iterator i)
 {
        TestlyxstringInvariant(this);
@@ -1506,7 +1425,6 @@ lyxstring::iterator lyxstring::erase(iterator i)
 }
 
 
-inline
 lyxstring::iterator lyxstring::erase(iterator first, iterator last)
 {
        TestlyxstringInvariant(this);
@@ -1520,7 +1438,6 @@ lyxstring::iterator lyxstring::erase(iterator first, iterator last)
 // Conversion to C-style Strings
 /////////////////////////////////////
 
-inline
 lyxstring::value_type const * lyxstring::c_str() const
 {
        rep->s[length()] = '\0';
@@ -1528,19 +1445,17 @@ lyxstring::value_type const * lyxstring::c_str() const
 }
 
 
-inline
 lyxstring::value_type const * lyxstring::data() const
 {
        return rep->s;
 }
 
 
-inline
 lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
                                     size_type pos) const
 {
-       Assert(buf); // OURS!
-       Assert(pos <= rep->sz); // STD!
+       lyx::Assert(buf); // OURS!
+       lyx::Assert(pos <= rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        register int nn = min(len, length() - pos);
@@ -1555,7 +1470,6 @@ lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
 
 // Compare funcs should be verified.
 
-inline
 int lyxstring::internal_compare(size_type pos, size_type n,
                                value_type const * s,
                                size_type slen, size_type n2) const
@@ -1564,11 +1478,11 @@ int lyxstring::internal_compare(size_type pos, size_type n,
        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);
+       // 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)
+       if (n == n2)
                return memcmp(&(rep->s[pos]), s, n);
        int c = memcmp(&(rep->s[pos]), s, min(n, n2));
        if (c)
@@ -1579,7 +1493,6 @@ int lyxstring::internal_compare(size_type pos, size_type n,
 }
 
 
-inline
 int lyxstring::compare(lyxstring const & str) const
 {
        TestlyxstringInvariant(this);
@@ -1588,32 +1501,29 @@ int lyxstring::compare(lyxstring const & str) const
 }
 
 
-inline
 int lyxstring::compare(value_type const * s) const
 {
-       Assert(s); //OURS!
+       lyx::Assert(s); //OURS!
        TestlyxstringInvariant(this);
        int n = (!s) ? 0 : strlen(s);
        return internal_compare(0, rep->sz, s, n, n);
 }
 
 
-inline
 int lyxstring::compare(size_type pos, size_type n,
                       lyxstring const & str) const
 {
-       Assert(pos <= rep->sz); // OURS!
+       lyx::Assert(pos <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
        return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
 }
 
 
-inline
 int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
                       size_type pos2, size_type n2) const
 {
-       Assert(pos <= rep->sz); // OURS!
-       Assert(pos2 <= str.rep->sz); // OURS!
+       lyx::Assert(pos <= rep->sz); // OURS!
+       lyx::Assert(pos2 <= str.rep->sz); // OURS!
        TestlyxstringInvariant(this);
        return internal_compare(pos, n,
                                str.rep->s + pos2,
@@ -1621,11 +1531,10 @@ int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
 }
 
 
-inline
 int lyxstring::compare(size_type pos, size_type n, value_type const * s,
                       size_type n2) const
 {
-       Assert(s && pos <= rep->sz); // OURS!
+       lyx::Assert(s && pos <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
        return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
 }
@@ -1636,10 +1545,9 @@ int lyxstring::compare(size_type pos, size_type n, value_type const * s,
 /////////////////
 
 // i = index, n = length
-inline
 lyxstring lyxstring::substr(size_type i, size_type n) const
 {
-       Assert(i <= rep->sz); // STD!
+       lyx::Assert(i <= rep->sz); // STD!
        TestlyxstringInvariant(this);
 
        return lyxstring(*this, i, n);
@@ -1658,14 +1566,14 @@ bool operator==(lyxstring const & a, lyxstring const & b)
 
 bool operator==(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       lyx::Assert(a); // OURS!
        return b.compare(a) == 0;
 }
 
 
 bool operator==(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b); // OURS!
+       lyx::Assert(b); // OURS!
        return a.compare(b) == 0;
 }
 
@@ -1678,14 +1586,14 @@ bool operator!=(lyxstring const & a, lyxstring const & b)
 
 bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       lyx::Assert(a); // OURS!
        return b.compare(a) != 0;
 }
 
 
 bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b); // OURS!
+       lyx::Assert(b); // OURS!
        return a.compare(b) != 0;
 }
 
@@ -1698,14 +1606,14 @@ bool operator>(lyxstring const & a, lyxstring const & b)
 
 bool operator>(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       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); // OURS!
+       lyx::Assert(b); // OURS!
        return a.compare(b) > 0;
 }
 
@@ -1718,14 +1626,14 @@ bool operator<(lyxstring const & a, lyxstring const & b)
 
 bool operator<(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       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); // OURS!
+       lyx::Assert(b); // OURS!
        return a.compare(b) < 0;
 }
 
@@ -1738,14 +1646,14 @@ bool operator>=(lyxstring const & a, lyxstring const & b)
 
 bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       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); // OURS!
+       lyx::Assert(b); // OURS!
        return a.compare(b) >= 0;
 }
 
@@ -1758,14 +1666,14 @@ bool operator<=(lyxstring const & a, lyxstring const & b)
 
 bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       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); // OURS!
+       lyx::Assert(b); // OURS!
        return a.compare(b) <= 0;
 }
 
@@ -1780,7 +1688,7 @@ lyxstring operator+(lyxstring const & a, lyxstring const & b)
 
 lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
 {
-       Assert(a); // OURS!
+       lyx::Assert(a); // OURS!
        lyxstring tmp(a);
        tmp += b;
        return tmp;
@@ -1798,7 +1706,7 @@ lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
 
 lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
 {
-       Assert(b); // OURS!
+       lyx::Assert(b); // OURS!
        lyxstring tmp(a);
        tmp += b;
        return tmp;
@@ -1858,7 +1766,7 @@ istream & getline(istream & is, lyxstring & s,
        // very bad solution
        char tmp = 0;
        s.erase();
-       while(is) {
+       while (is) {
                is.get(tmp);
                if (tmp != delim) {
                        s += tmp;
@@ -1868,3 +1776,12 @@ istream & getline(istream & is, lyxstring & s,
        }
        return is;
 }
+
+#ifdef TEST_MAIN
+int main() {
+       lyxstring a = "abcac";
+       cout << a.rfind("ab") << endl;
+       cout << a.rfind("c") << endl;
+       cout << a.rfind("d") << endl;
+}
+#endif