From 5f8f895b28a763d9c35c43065ecd87ba169f6afd Mon Sep 17 00:00:00 2001 From: "scc%mozilla.org" Date: Sun, 13 May 2001 05:16:10 +0000 Subject: [PATCH] bug #75551: r=jst, sr=sfraser. Added |CaseInsensitiveFindInReadable| git-svn-id: svn://10.0.0.236/trunk@94803 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/string/public/nsReadableUtils.h | 7 +- mozilla/string/src/nsReadableUtils.cpp | 117 ++++++++---------- mozilla/xpcom/string/public/nsReadableUtils.h | 7 +- mozilla/xpcom/string/src/nsReadableUtils.cpp | 117 ++++++++---------- 4 files changed, 114 insertions(+), 134 deletions(-) diff --git a/mozilla/string/public/nsReadableUtils.h b/mozilla/string/public/nsReadableUtils.h index 1057153ce72..8737bae5d58 100755 --- a/mozilla/string/public/nsReadableUtils.h +++ b/mozilla/string/public/nsReadableUtils.h @@ -178,11 +178,14 @@ NS_COM void ToLowerCase( nsACString& ); * point to the match. If no match was found, returns |PR_FALSE| and makes |aSearchStart == aSearchEnd|. * * Currently, this is equivalent to the O(m*n) implementation previously on |ns[C]String|. - * If we need something faster; we can implement that later. + * If we need something faster, then we can implement that later. */ NS_COM PRBool FindInReadable( const nsAString& aPattern, nsReadingIterator&, nsReadingIterator& ); NS_COM PRBool FindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); +NS_COM PRBool CaseInsensitiveFindInReadable( const nsAString& aPattern, nsReadingIterator&, nsReadingIterator& ); +NS_COM PRBool CaseInsensitiveFindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); + /** * Finds the rightmost occurance of |aPattern| @@ -190,7 +193,7 @@ NS_COM PRBool FindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); NS_COM PRBool RFindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); diff --git a/mozilla/string/src/nsReadableUtils.cpp b/mozilla/string/src/nsReadableUtils.cpp index 1dd545de6f7..92dbc4cf8c2 100755 --- a/mozilla/string/src/nsReadableUtils.cpp +++ b/mozilla/string/src/nsReadableUtils.cpp @@ -410,16 +410,16 @@ ToLowerCase( nsACString& aCString ) copy_string(aCString.BeginWriting(fromBegin), aCString.EndWriting(fromEnd), converter); } -NS_COM +template PRBool -FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) +FindInReadable_Impl( const StringT& aPattern, StringT::const_iterator& aSearchStart, StringT::const_iterator& aSearchEnd, const Comparator& compare ) { PRBool found_it = PR_FALSE; // only bother searching at all if we're given a non-empty range to search if ( aSearchStart != aSearchEnd ) { - nsReadingIterator aPatternStart, aPatternEnd; + typename StringT::const_iterator aPatternStart, aPatternEnd; aPattern.BeginReading(aPatternStart); aPattern.EndReading(aPatternEnd); @@ -427,7 +427,7 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch while ( !found_it ) { // fast inner loop (that's what it's called, not what it is) looks for a potential match - while ( aSearchStart != aSearchEnd && *aPatternStart != *aSearchStart ) + while ( aSearchStart != aSearchEnd && !compare(*aPatternStart, *aSearchStart) ) ++aSearchStart; // if we broke out of the `fast' loop because we're out of string ... we're done: no match @@ -435,8 +435,8 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch break; // otherwise, we're at a potential match, let's see if we really hit one - nsReadingIterator testPattern(aPatternStart); - nsReadingIterator testSearch(aSearchStart); + typename StringT::const_iterator testPattern(aPatternStart); + typename StringT::const_iterator testSearch(aSearchStart); // slow inner loop verifies the potential match (found by the `fast' loop) at the current position for(;;) @@ -464,7 +464,7 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch // else if we mismatched ... it's time to advance to the next search position // and get back into the `fast' loop - if ( *testPattern != *testSearch ) + if ( !compare(*testPattern, *testSearch) ) { ++aSearchStart; break; @@ -476,70 +476,57 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch return found_it; } + +class CaseSensitivePRUnicharComparator + { + public: + PRBool operator()( PRUnichar lhs, PRUnichar rhs ) const { return lhs == rhs; } + }; + +class CaseSensitiveCharComparator + { + public: + PRBool operator()( char lhs, char rhs ) const { return lhs == rhs; } + }; + +class CaseInsensitivePRUnicharComparator + { + public: + PRBool operator()( PRUnichar lhs, PRUnichar rhs ) const { return nsCRT::ToUpper(lhs) == nsCRT::ToUpper(rhs); } + }; + +class CaseInsensitiveCharComparator + { + public: + PRBool operator()( char lhs, char rhs ) const { return nsCRT::ToUpper(lhs) == nsCRT::ToUpper(rhs); } + }; + +NS_COM +PRBool +FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) + { + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseSensitivePRUnicharComparator()); + } + NS_COM PRBool FindInReadable( const nsACString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) { - PRBool found_it = PR_FALSE; + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseSensitiveCharComparator()); + } - // only bother searching at all if we're given a non-empty range to search - if ( aSearchStart != aSearchEnd ) - { - nsReadingIterator aPatternStart, aPatternEnd; - aPattern.BeginReading(aPatternStart); - aPattern.EndReading(aPatternEnd); +NS_COM +PRBool +CaseInsensitiveFindInReadable( const nsAString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) + { + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseInsensitivePRUnicharComparator()); + } - // outer loop keeps searching till we find it or run out of string to search - while ( !found_it ) - { - // fast inner loop (that's what it's called, not what it is) looks for a potential match - while ( aSearchStart != aSearchEnd && *aPatternStart != *aSearchStart ) - ++aSearchStart; - - // if we broke out of the `fast' loop because we're out of string ... we're done: no match - if ( aSearchStart == aSearchEnd ) - break; - - // otherwise, we're at a potential match, let's see if we really hit one - nsReadingIterator testPattern(aPatternStart); - nsReadingIterator testSearch(aSearchStart); - - // slow inner loop verifies the potential match (found by the `fast' loop) at the current position - for(;;) - { - // we already compared the first character in the outer loop, - // so we'll advance before the next comparison - ++testPattern; - ++testSearch; - - // if we verified all the way to the end of the pattern, then we found it! - if ( testPattern == aPatternEnd ) - { - found_it = PR_TRUE; - aSearchEnd = testSearch; // return the exact found range through the parameters - break; - } - - // if we got to end of the string we're searching before we hit the end of the - // pattern, we'll never find what we're looking for - if ( testSearch == aSearchEnd ) - { - aSearchStart = aSearchEnd; - break; - } - - // else if we mismatched ... it's time to advance to the next search position - // and get back into the `fast' loop - if ( *testPattern != *testSearch ) - { - ++aSearchStart; - break; - } - } - } - } - - return found_it; +NS_COM +PRBool +CaseInsensitiveFindInReadable( const nsACString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) + { + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseInsensitiveCharComparator()); } /** diff --git a/mozilla/xpcom/string/public/nsReadableUtils.h b/mozilla/xpcom/string/public/nsReadableUtils.h index 1057153ce72..8737bae5d58 100755 --- a/mozilla/xpcom/string/public/nsReadableUtils.h +++ b/mozilla/xpcom/string/public/nsReadableUtils.h @@ -178,11 +178,14 @@ NS_COM void ToLowerCase( nsACString& ); * point to the match. If no match was found, returns |PR_FALSE| and makes |aSearchStart == aSearchEnd|. * * Currently, this is equivalent to the O(m*n) implementation previously on |ns[C]String|. - * If we need something faster; we can implement that later. + * If we need something faster, then we can implement that later. */ NS_COM PRBool FindInReadable( const nsAString& aPattern, nsReadingIterator&, nsReadingIterator& ); NS_COM PRBool FindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); +NS_COM PRBool CaseInsensitiveFindInReadable( const nsAString& aPattern, nsReadingIterator&, nsReadingIterator& ); +NS_COM PRBool CaseInsensitiveFindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); + /** * Finds the rightmost occurance of |aPattern| @@ -190,7 +193,7 @@ NS_COM PRBool FindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); NS_COM PRBool RFindInReadable( const nsACString& aPattern, nsReadingIterator&, nsReadingIterator& ); diff --git a/mozilla/xpcom/string/src/nsReadableUtils.cpp b/mozilla/xpcom/string/src/nsReadableUtils.cpp index 1dd545de6f7..92dbc4cf8c2 100755 --- a/mozilla/xpcom/string/src/nsReadableUtils.cpp +++ b/mozilla/xpcom/string/src/nsReadableUtils.cpp @@ -410,16 +410,16 @@ ToLowerCase( nsACString& aCString ) copy_string(aCString.BeginWriting(fromBegin), aCString.EndWriting(fromEnd), converter); } -NS_COM +template PRBool -FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) +FindInReadable_Impl( const StringT& aPattern, StringT::const_iterator& aSearchStart, StringT::const_iterator& aSearchEnd, const Comparator& compare ) { PRBool found_it = PR_FALSE; // only bother searching at all if we're given a non-empty range to search if ( aSearchStart != aSearchEnd ) { - nsReadingIterator aPatternStart, aPatternEnd; + typename StringT::const_iterator aPatternStart, aPatternEnd; aPattern.BeginReading(aPatternStart); aPattern.EndReading(aPatternEnd); @@ -427,7 +427,7 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch while ( !found_it ) { // fast inner loop (that's what it's called, not what it is) looks for a potential match - while ( aSearchStart != aSearchEnd && *aPatternStart != *aSearchStart ) + while ( aSearchStart != aSearchEnd && !compare(*aPatternStart, *aSearchStart) ) ++aSearchStart; // if we broke out of the `fast' loop because we're out of string ... we're done: no match @@ -435,8 +435,8 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch break; // otherwise, we're at a potential match, let's see if we really hit one - nsReadingIterator testPattern(aPatternStart); - nsReadingIterator testSearch(aSearchStart); + typename StringT::const_iterator testPattern(aPatternStart); + typename StringT::const_iterator testSearch(aSearchStart); // slow inner loop verifies the potential match (found by the `fast' loop) at the current position for(;;) @@ -464,7 +464,7 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch // else if we mismatched ... it's time to advance to the next search position // and get back into the `fast' loop - if ( *testPattern != *testSearch ) + if ( !compare(*testPattern, *testSearch) ) { ++aSearchStart; break; @@ -476,70 +476,57 @@ FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearch return found_it; } + +class CaseSensitivePRUnicharComparator + { + public: + PRBool operator()( PRUnichar lhs, PRUnichar rhs ) const { return lhs == rhs; } + }; + +class CaseSensitiveCharComparator + { + public: + PRBool operator()( char lhs, char rhs ) const { return lhs == rhs; } + }; + +class CaseInsensitivePRUnicharComparator + { + public: + PRBool operator()( PRUnichar lhs, PRUnichar rhs ) const { return nsCRT::ToUpper(lhs) == nsCRT::ToUpper(rhs); } + }; + +class CaseInsensitiveCharComparator + { + public: + PRBool operator()( char lhs, char rhs ) const { return nsCRT::ToUpper(lhs) == nsCRT::ToUpper(rhs); } + }; + +NS_COM +PRBool +FindInReadable( const nsAString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) + { + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseSensitivePRUnicharComparator()); + } + NS_COM PRBool FindInReadable( const nsACString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) { - PRBool found_it = PR_FALSE; + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseSensitiveCharComparator()); + } - // only bother searching at all if we're given a non-empty range to search - if ( aSearchStart != aSearchEnd ) - { - nsReadingIterator aPatternStart, aPatternEnd; - aPattern.BeginReading(aPatternStart); - aPattern.EndReading(aPatternEnd); +NS_COM +PRBool +CaseInsensitiveFindInReadable( const nsAString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) + { + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseInsensitivePRUnicharComparator()); + } - // outer loop keeps searching till we find it or run out of string to search - while ( !found_it ) - { - // fast inner loop (that's what it's called, not what it is) looks for a potential match - while ( aSearchStart != aSearchEnd && *aPatternStart != *aSearchStart ) - ++aSearchStart; - - // if we broke out of the `fast' loop because we're out of string ... we're done: no match - if ( aSearchStart == aSearchEnd ) - break; - - // otherwise, we're at a potential match, let's see if we really hit one - nsReadingIterator testPattern(aPatternStart); - nsReadingIterator testSearch(aSearchStart); - - // slow inner loop verifies the potential match (found by the `fast' loop) at the current position - for(;;) - { - // we already compared the first character in the outer loop, - // so we'll advance before the next comparison - ++testPattern; - ++testSearch; - - // if we verified all the way to the end of the pattern, then we found it! - if ( testPattern == aPatternEnd ) - { - found_it = PR_TRUE; - aSearchEnd = testSearch; // return the exact found range through the parameters - break; - } - - // if we got to end of the string we're searching before we hit the end of the - // pattern, we'll never find what we're looking for - if ( testSearch == aSearchEnd ) - { - aSearchStart = aSearchEnd; - break; - } - - // else if we mismatched ... it's time to advance to the next search position - // and get back into the `fast' loop - if ( *testPattern != *testSearch ) - { - ++aSearchStart; - break; - } - } - } - } - - return found_it; +NS_COM +PRBool +CaseInsensitiveFindInReadable( const nsACString& aPattern, nsReadingIterator& aSearchStart, nsReadingIterator& aSearchEnd ) + { + return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, CaseInsensitiveCharComparator()); } /**