]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxstring.C
small cleanup, doxygen, formatting changes
[lyx.git] / src / support / lyxstring.C
index 4e7438d69f5128183056ac42d59ea9d2311c9291..bc17eef026a3cc8252fabeecf664c69c4a86c7a7 100644 (file)
 
 #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
@@ -884,12 +888,14 @@ lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
        
        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;
@@ -915,12 +921,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;
@@ -962,17 +969,24 @@ 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;
 }
 
@@ -982,19 +996,25 @@ lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
 {
        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;
 }
 
@@ -1003,21 +1023,9 @@ lyxstring::size_type lyxstring::rfind(value_type const * ptr,
                                      size_type i) const
 {
        Assert(ptr); // OURS!
-       TestlyxstringInvariant(this);
-       if (!*ptr) return npos;
 
-       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));
 }
 
 
@@ -1025,18 +1033,20 @@ 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;
 }
 
 
 lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
                                              size_type i) const
 {
-       Assert(i < rep->sz); // OURS!
+       Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1050,12 +1060,12 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
                                              size_type i,
                                              size_type n) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       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;
 }
@@ -1064,7 +1074,7 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
 lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
                                              size_type i) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1076,7 +1086,7 @@ 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); // OURS!
+       Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1109,7 +1119,7 @@ lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
 
        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;
 }
@@ -1148,7 +1158,7 @@ lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
        TestlyxstringInvariant(this);
 
        if (!rep->sz) return npos;
-       Assert(i < rep->sz);
+       Assert(i <= rep->sz);
        for (size_type t = i; t < rep->sz; ++t) {
                if (a.find(rep->s[t]) == npos) return t;
        }
@@ -1160,12 +1170,12 @@ 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!
+       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;
 }
@@ -1174,7 +1184,7 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
 lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
                                                  size_type i) const
 {
-       Assert(ptr && i < rep->sz); // OURS!
+       Assert(ptr && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1188,7 +1198,7 @@ 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!
+       Assert(i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        for (size_type t = i; t < rep->sz; ++t) {
@@ -1222,7 +1232,7 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
        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;
 }
@@ -1283,7 +1293,7 @@ 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)
 {
-       Assert(p && i < rep->sz); // OURS!
+       Assert(p && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        rep = rep->get_own_copy();
@@ -1294,7 +1304,7 @@ lyxstring & lyxstring::replace(size_type i, size_type n,
 
 lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
 {
-       Assert(p && i < rep->sz); // OURS!
+       Assert(p && i <= rep->sz); // OURS!
        TestlyxstringInvariant(this);
 
        return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
@@ -1749,3 +1759,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