diff --git a/mozilla/xpcom/string/public/nsAString.h b/mozilla/xpcom/string/public/nsAString.h index 4ad7eac140b..ccfb342477c 100644 --- a/mozilla/xpcom/string/public/nsAString.h +++ b/mozilla/xpcom/string/public/nsAString.h @@ -51,6 +51,11 @@ #include "nsObsoleteAString.h" #endif +// If some platform(s) can't handle our template that matches literal strings, +// then we'll disable it on those platforms. +// #define NS_DISABLE_LITERAL_TEMPLATE + +#include // declare nsAString #include "string-template-def-unichar.h" diff --git a/mozilla/xpcom/string/public/nsCharTraits.h b/mozilla/xpcom/string/public/nsCharTraits.h index 872d38a67d2..00f05d19df8 100644 --- a/mozilla/xpcom/string/public/nsCharTraits.h +++ b/mozilla/xpcom/string/public/nsCharTraits.h @@ -62,6 +62,11 @@ // for |PRUnichar| #endif +#ifndef nsDebug_h__ +#include "nsDebug.h" + // for NS_ASSERTION +#endif + #ifdef HAVE_CPP_BOOL typedef bool nsCharTraits_bool; #else @@ -181,6 +186,20 @@ struct nsCharTraits #endif } + static + int + compareASCII( const char_type* s1, const char* s2, size_t n ) + { + for ( ; n--; ++s1, ++s2 ) + { + NS_ASSERTION(!(*s2 & ~0x7F), "Unexpected non-ASCII character"); + if ( !eq_int_type(to_int_type(*s1), to_int_type(*s2)) ) + return to_int_type(*s1) - to_int_type(*s2); + } + + return 0; + } + static size_t length( const char_type* s ) @@ -332,6 +351,19 @@ struct nsCharTraits return memcmp(s1, s2, n); } + static + int + compareASCII( const char_type* s1, const char* s2, size_t n ) + { +#ifdef DEBUG + for (size_t i = 0; i < n; ++i) + { + NS_ASSERTION(!(s2[i] & ~0x7F), "Unexpected non-ASCII character"); + } +#endif + return compare(s1, s2, n); + } + static size_t length( const char_type* s ) diff --git a/mozilla/xpcom/string/public/nsSubstring.h b/mozilla/xpcom/string/public/nsSubstring.h index fbb761569a7..0802f77aef5 100644 --- a/mozilla/xpcom/string/public/nsSubstring.h +++ b/mozilla/xpcom/string/public/nsSubstring.h @@ -45,6 +45,11 @@ #define kNotFound -1 +// If some platform(s) can't handle our template that matches literal strings, +// then we'll disable it on those platforms. +// #define NS_DISABLE_LITERAL_TEMPLATE + +#include // declare nsSubstring #include "string-template-def-unichar.h" diff --git a/mozilla/xpcom/string/public/nsTAString.h b/mozilla/xpcom/string/public/nsTAString.h index be565f183dc..53bc374cb15 100644 --- a/mozilla/xpcom/string/public/nsTAString.h +++ b/mozilla/xpcom/string/public/nsTAString.h @@ -197,6 +197,23 @@ class nsTAString_CharT NS_COM PRBool Equals( const char_type* ) const; NS_COM PRBool Equals( const char_type*, const comparator_type& ) const; + /** + * An efficient comparison with ASCII that can be used even for + * wide strings. + */ + NS_COM PRBool EqualsASCII( const char* data, size_type len ) const; +#ifdef NS_DISABLE_LITERAL_TEMPLATE + inline PRBool EqualsLiteral( const char* str ) const + { + return EqualsASCII(str, strlen(str)); + } +#else + template + inline PRBool EqualsLiteral( const char (&str)[N] ) const + { + return EqualsASCII(str, N-1); + } +#endif /** * A string always references a non-null data pointer. In some diff --git a/mozilla/xpcom/string/public/nsTSubstring.h b/mozilla/xpcom/string/public/nsTSubstring.h index 218d69f6276..7aca6c6a4ea 100644 --- a/mozilla/xpcom/string/public/nsTSubstring.h +++ b/mozilla/xpcom/string/public/nsTSubstring.h @@ -215,6 +215,19 @@ class nsTSubstring_CharT : public nsTAString_CharT NS_COM PRBool Equals( const char_type* data ) const; NS_COM PRBool Equals( const char_type* data, const comparator_type& comp ) const; + NS_COM PRBool EqualsASCII( const char* data, size_type len ) const; +#ifdef NS_DISABLE_LITERAL_TEMPLATE + inline PRBool EqualsLiteral( const char* str ) const + { + return EqualsASCII(str, strlen(str)); + } +#else + template + inline PRBool EqualsLiteral( const char (&str)[N] ) const + { + return EqualsASCII(str, N-1); + } +#endif /** * assignment diff --git a/mozilla/xpcom/string/src/nsTAString.cpp b/mozilla/xpcom/string/src/nsTAString.cpp index b01a0666d98..bd0aa3c0bf4 100644 --- a/mozilla/xpcom/string/src/nsTAString.cpp +++ b/mozilla/xpcom/string/src/nsTAString.cpp @@ -105,6 +105,15 @@ nsTAString_CharT::Equals( const char_type* data, const comparator_type& comparat return ToSubstring().Equals(data, comparator); } +PRBool +nsTAString_CharT::EqualsASCII( const char* data, size_type len ) const + { + if (mVTable == obsolete_string_type::sCanonicalVTable) + return AsSubstring()->EqualsASCII(data, len); + + return ToSubstring().EqualsASCII(data, len); + } + PRBool nsTAString_CharT::IsVoid() const { diff --git a/mozilla/xpcom/string/src/nsTSubstring.cpp b/mozilla/xpcom/string/src/nsTSubstring.cpp index be331f2e835..d457959290f 100644 --- a/mozilla/xpcom/string/src/nsTSubstring.cpp +++ b/mozilla/xpcom/string/src/nsTSubstring.cpp @@ -555,6 +555,12 @@ nsTSubstring_CharT::Equals( const char_type* data, const comparator_type& comp ) return mLength == length && comp(mData, data, mLength) == 0; } +PRBool +nsTSubstring_CharT::EqualsASCII( const char* data, size_type len ) const + { + return mLength == len && char_traits::compareASCII(mData, data, len) == 0; + } + nsTSubstring_CharT::size_type nsTSubstring_CharT::CountChar( char_type c ) const {