зеркало из https://github.com/mozilla/pjs.git
Bug 226439. Use templates for ultra-efficient EqualsLiteral string method. r=darin,sr=dbaron. THIS MAY CAUSE BUSTAGE. DO NOT BE ALARMED.
This commit is contained in:
Родитель
5ac7273e52
Коммит
f6c16c16eb
|
@ -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 <string.h>
|
||||
|
||||
// declare nsAString
|
||||
#include "string-template-def-unichar.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<PRUnichar>
|
|||
#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<char>
|
|||
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 )
|
||||
|
|
|
@ -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 <string.h>
|
||||
|
||||
// declare nsSubstring
|
||||
#include "string-template-def-unichar.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<int N>
|
||||
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
|
||||
|
|
|
@ -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<int N>
|
||||
inline PRBool EqualsLiteral( const char (&str)[N] ) const
|
||||
{
|
||||
return EqualsASCII(str, N-1);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* assignment
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче