#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
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;
// 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;
{
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;
}
{
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;
}
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));
}
{
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) {
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;
}
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) {
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) {
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;
}
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;
}
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;
}
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) {
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) {
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 & 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();
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));
}
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