From 6f2167dddb6146ad776ca763459c8096f324fe47 Mon Sep 17 00:00:00 2001 From: Dekel Tsur Date: Sat, 20 Jan 2001 15:37:54 +0000 Subject: [PATCH] Fix lyxstring::rfind git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@1355 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ChangeLog | 5 ++- lib/languages | 2 +- src/support/ChangeLog | 4 ++ src/support/lyxstring.C | 81 ++++++++++++++++++++++++----------------- 4 files changed, 55 insertions(+), 37 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index ca346dbaf8..1a137b5fb7 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,6 @@ 2001-01-20 Dekel Tsur - * languages: Add extra_options field. - + * languages: Add extra_options field. It is used to fix the ~n + problem with Spanish. + diff --git a/lib/languages b/lib/languages index 15095b328e..6f5c099428 100644 --- a/lib/languages +++ b/lib/languages @@ -39,7 +39,7 @@ polish polish "Polish" false iso8859-2 pl "" portuges portuges "Portuges" false iso8859-1 pt "" romanian romanian "Romanian" false iso8859-2 ro "" russian russian "Russian" false koi8 ru "" -scottish scottish "Scottish" false iso8859-1 gd_GB " +scottish scottish "Scottish" false iso8859-1 gd_GB "" spanish spanish "Spanish" false iso8859-1 es "\addto\extrasspanish{\bbl@deactivate{~}}" slovak slovak "Slovak" false iso8859-2 sk_SL "" slovene slovene "Slovene" false iso8859-2 sl_SI "" diff --git a/src/support/ChangeLog b/src/support/ChangeLog index e69de29bb2..4bf2aec687 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -0,0 +1,4 @@ +2001-01-20 Dekel Tsur + + * lyxstring.C (rfind): Fix broken functions. + (find): Few optimizations. diff --git a/src/support/lyxstring.C b/src/support/lyxstring.C index 515e90533b..ff25d6fba9 100644 --- a/src/support/lyxstring.C +++ b/src/support/lyxstring.C @@ -888,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; @@ -919,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; @@ -966,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; } @@ -986,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; } @@ -1007,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)); } @@ -1755,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 -- 2.39.2