Bug 928351 - Char16.h and xpcom/strings/public parts r=ehsan,Waldo

This commit is contained in:
Jacek Caban 2013-11-27 14:40:54 +01:00
parent 3e096d9c3d
commit 12b4d7fd02
8 changed files with 265 additions and 2 deletions

View File

@ -47,6 +47,9 @@
* typedef from wchar_t.
*/
# define MOZ_CHAR16_IS_NOT_WCHAR
# ifdef WIN32
# define MOZ_USE_CHAR16_WRAPPER
# endif
#elif !defined(__cplusplus)
# if defined(WIN32)
# include <yvals.h>
@ -63,6 +66,108 @@
# error "Char16.h requires C++11 (or something like it) for UTF-16 support."
#endif
#ifdef MOZ_USE_CHAR16_WRAPPER
# include <string>
/**
* Win32 API extensively uses wchar_t, which is represented by a separated
* builtin type than char16_t per spec. It's not the case for MSVC, but GCC
* follows the spec. We want to mix wchar_t and char16_t on Windows builds.
* This class is supposed to make it easier. It stores char16_t const pointer,
* but provides implicit casts for wchar_t as well. On other platforms, we
* simply use |typedef const char16_t* char16ptr_t|. Here, we want to make
* the class as similar to this typedef, including providing some casts that
* are allowed by the typedef.
*/
class char16ptr_t
{
private:
const char16_t* ptr;
static_assert(sizeof(char16_t) == sizeof(wchar_t), "char16_t and wchar_t sizes differ");
public:
char16ptr_t(const char16_t* ptr) : ptr(ptr) {}
char16ptr_t(const wchar_t* ptr) : ptr(reinterpret_cast<const char16_t*>(ptr)) {}
/* Without this, nullptr assignment would be ambiguous. */
constexpr char16ptr_t(decltype(nullptr)) : ptr(nullptr) {}
operator const char16_t*() const {
return ptr;
}
operator const wchar_t*() const {
return reinterpret_cast<const wchar_t*>(ptr);
}
operator const void*() const {
return ptr;
}
operator bool() const {
return ptr != nullptr;
}
operator std::wstring() const {
return std::wstring(static_cast<const wchar_t*>(*this));
}
/* Explicit cast operators to allow things like (char16_t*)str. */
explicit operator char16_t*() const {
return const_cast<char16_t*>(ptr);
}
explicit operator wchar_t*() const {
return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
}
/**
* Some Windows API calls accept BYTE* but require that data actually be WCHAR*.
* Supporting this requires explicit operators to support the requisite explicit
* casts.
*/
explicit operator const char*() const {
return reinterpret_cast<const char*>(ptr);
}
explicit operator const unsigned char*() const {
return reinterpret_cast<const unsigned char*>(ptr);
}
explicit operator unsigned char*() const {
return const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(ptr));
}
explicit operator void*() const {
return const_cast<char16_t*>(ptr);
}
/* Some operators used on pointers. */
char16_t operator[](size_t i) const {
return ptr[i];
}
bool operator==(const char16ptr_t &x) const {
return ptr == x.ptr;
}
bool operator==(decltype(nullptr)) const {
return ptr == nullptr;
}
bool operator!=(const char16ptr_t &x) const {
return ptr != x.ptr;
}
bool operator!=(decltype(nullptr)) const {
return ptr != nullptr;
}
char16ptr_t operator+(size_t add) const {
return char16ptr_t(ptr + add);
}
ptrdiff_t operator-(const char16ptr_t &other) const {
return ptr - other.ptr;
}
};
inline decltype((char*)0-(char*)0)
operator-(const char16_t* x, const char16ptr_t y) {
return x - static_cast<const char16_t*>(y);
}
#else
typedef const char16_t* char16ptr_t;
#endif
/* This is a temporary hack until bug 927728 is fixed. */
#define __PRUNICHAR__
typedef char16_t PRUnichar;

View File

@ -143,6 +143,9 @@ public:
NS_HIDDEN_(void) Append( char_type c ) { Replace(size_type(-1), 0, c); }
NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) ) { Replace(size_type(-1), 0, data, length); }
#ifdef MOZ_USE_CHAR16_WRAPPER
NS_HIDDEN_(void) Append( char16ptr_t data, size_type length = size_type(-1) ) { Append(static_cast<const char16_t*>(data), length); }
#endif
NS_HIDDEN_(void) Append( const self_type& readable ) { Replace(size_type(-1), 0, readable); }
NS_HIDDEN_(void) AppendLiteral( const char *aASCIIStr );
NS_HIDDEN_(void) AppendASCII( const char *aASCIIStr ) { AppendLiteral(aASCIIStr); }
@ -808,15 +811,21 @@ public:
{
NS_StringContainerInit2(*this, aData, aLength, 0);
}
#ifdef MOZ_USE_CHAR16_WRAPPER
explicit
nsString(char16ptr_t aData, size_type aLength = UINT32_MAX)
: nsString(static_cast<const char16_t*>(aData), aLength) {}
#endif
~nsString()
{
NS_StringContainerFinish(*this);
}
const char_type* get() const
char16ptr_t get() const
{
return BeginReading();
return char16ptr_t(BeginReading());
}
self_type& operator=(const self_type& aString) { Assign(aString); return *this; }
@ -917,6 +926,13 @@ public:
: nsString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
{}
#ifdef MOZ_USE_CHAR16_WRAPPER
explicit
nsDependentString(char16ptr_t aData, size_type aLength = UINT32_MAX)
: nsDependentString(static_cast<const char16_t*>(aData), aLength)
{}
#endif
void Rebind(const char_type* aData, size_type aLength = UINT32_MAX)
{
NS_StringContainerFinish(*this);

View File

@ -53,6 +53,13 @@ bool AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest,
void AppendUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest );
void AppendUTF8toUTF16( const char* aSource, nsAString& aDest );
#ifdef MOZ_USE_CHAR16_WRAPPER
inline void AppendUTF16toUTF8( char16ptr_t aSource, nsACString& aDest )
{
return AppendUTF16toUTF8(static_cast<const char16_t*>(aSource), aDest);
}
#endif
/**
* Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
*

View File

@ -74,6 +74,15 @@ class NS_LossyConvertUTF16toASCII : public nsAutoCString
LossyAppendUTF16toASCII(Substring(aString, aLength), *this);
}
#ifdef MOZ_USE_CHAR16_WRAPPER
explicit
NS_LossyConvertUTF16toASCII( char16ptr_t aString )
: NS_LossyConvertUTF16toASCII(static_cast<const char16_t*>(aString)) {}
NS_LossyConvertUTF16toASCII( char16ptr_t aString, uint32_t aLength )
: NS_LossyConvertUTF16toASCII(static_cast<const char16_t*>(aString), aLength) {}
#endif
explicit
NS_LossyConvertUTF16toASCII( const nsAString& aString )
{
@ -129,6 +138,13 @@ class NS_ConvertUTF16toUTF8 : public nsAutoCString
AppendUTF16toUTF8(Substring(aString, aLength), *this);
}
#ifdef MOZ_USE_CHAR16_WRAPPER
NS_ConvertUTF16toUTF8( char16ptr_t aString ) : NS_ConvertUTF16toUTF8(static_cast<const PRUnichar*>(aString)) {}
NS_ConvertUTF16toUTF8( char16ptr_t aString, uint32_t aLength )
: NS_ConvertUTF16toUTF8(static_cast<const PRUnichar*>(aString), aLength) {}
#endif
explicit
NS_ConvertUTF16toUTF8( const nsAString& aString )
{

View File

@ -40,6 +40,11 @@ class nsTDependentString_CharT : public nsTString_CharT
AssertValidDepedentString();
}
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
nsTDependentString_CharT( char16ptr_t data, uint32_t length )
: nsTDependentString_CharT(static_cast<const char16_t*>(data), length) {}
#endif
explicit
nsTDependentString_CharT( const char_type* data )
: string_type(const_cast<char_type*>(data), uint32_t(char_traits::length(data)), F_TERMINATED)
@ -47,6 +52,12 @@ class nsTDependentString_CharT : public nsTString_CharT
AssertValidDepedentString();
}
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
explicit
nsTDependentString_CharT( char16ptr_t data )
: nsTDependentString_CharT( static_cast<const char16_t*>(data)) {}
#endif
nsTDependentString_CharT( const string_type& str, uint32_t startPos )
: string_type()
{

View File

@ -45,6 +45,14 @@ class nsTDependentSubstring_CharT : public nsTSubstring_CharT
nsTDependentSubstring_CharT( const char_type* start, const char_type* end )
: substring_type(const_cast<char_type*>(start), uint32_t(end - start), F_NONE) {}
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
nsTDependentSubstring_CharT( char16ptr_t data, size_type length )
: nsTDependentSubstring_CharT(static_cast<const char16_t*>(data), length) {}
nsTDependentSubstring_CharT( char16ptr_t start, char16ptr_t end )
: nsTDependentSubstring_CharT(static_cast<const char16_t*>(start), static_cast<const char16_t*>(end)) {}
#endif
nsTDependentSubstring_CharT( const const_iterator& start, const const_iterator& end )
: substring_type(const_cast<char_type*>(start.get()), uint32_t(end.get() - start.get()), F_NONE) {}

View File

@ -39,6 +39,15 @@ class nsTString_CharT : public nsTSubstring_CharT
Assign(data, length);
}
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
explicit
nsTString_CharT( char16ptr_t data, size_type length = size_type(-1) )
: substring_type()
{
Assign(static_cast<const char16_t*>(data), length);
}
#endif
nsTString_CharT( const self_type& str )
: substring_type()
{
@ -63,6 +72,9 @@ class nsTString_CharT : public nsTSubstring_CharT
self_type& operator=( char_type c ) { Assign(c); return *this; }
self_type& operator=( const char_type* data ) { Assign(data); return *this; }
self_type& operator=( const self_type& str ) { Assign(str); return *this; }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
self_type& operator=( const char16ptr_t data ) { Assign(static_cast<const char16_t*>(data)); return *this; }
#endif
self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
@ -70,7 +82,11 @@ class nsTString_CharT : public nsTSubstring_CharT
* returns the null-terminated string
*/
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
char16ptr_t get() const
#else
const char_type* get() const
#endif
{
return mData;
}
@ -115,6 +131,12 @@ class nsTString_CharT : public nsTSubstring_CharT
#ifdef CharT_is_PRUnichar
int32_t Find( const nsAFlatString& aString, int32_t aOffset=0, int32_t aCount=-1 ) const;
int32_t Find( const PRUnichar* aString, int32_t aOffset=0, int32_t aCount=-1 ) const;
#ifdef MOZ_USE_CHAR16_WRAPPER
int32_t Find( char16ptr_t aString, int32_t aOffset=0, int32_t aCount=-1 ) const
{
return Find(static_cast<const char16_t*>(aString), aOffset, aCount);
}
#endif
#endif
@ -474,6 +496,13 @@ class nsTAutoString_CharT : public nsTFixedString_CharT
Assign(data, length);
}
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
explicit
nsTAutoString_CharT( char16ptr_t data, size_type length = size_type(-1) )
: nsTAutoString_CharT(static_cast<const char16_t*>(data), length)
{}
#endif
nsTAutoString_CharT( const self_type& str )
: fixed_string_type(mStorage, kDefaultStorageSize, 0)
{
@ -496,6 +525,9 @@ class nsTAutoString_CharT : public nsTFixedString_CharT
// |operator=| does not inherit, so we must define our own
self_type& operator=( char_type c ) { Assign(c); return *this; }
self_type& operator=( const char_type* data ) { Assign(data); return *this; }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
self_type& operator=( char16ptr_t data ) { Assign(data); return *this; }
#endif
self_type& operator=( const self_type& str ) { Assign(str); return *this; }
self_type& operator=( const substring_type& str ) { Assign(str); return *this; }
self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
@ -567,7 +599,11 @@ class nsTXPIDLString_CharT : public nsTString_CharT
}
// return nullptr if we are voided
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
char16ptr_t get() const
#else
const char_type* get() const
#endif
{
return (mFlags & F_VOIDED) ? nullptr : mData;
}

View File

@ -200,7 +200,11 @@ class nsTSubstring_CharT
*/
// returns pointer to string data (not necessarily null-terminated)
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
char16ptr_t Data() const
#else
const char_type *Data() const
#endif
{
return mData;
}
@ -263,6 +267,17 @@ class nsTSubstring_CharT
bool NS_FASTCALL Equals( const char_type* data ) const;
bool NS_FASTCALL Equals( const char_type* data, const comparator_type& comp ) const;
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
bool NS_FASTCALL Equals( char16ptr_t data ) const
{
return Equals(static_cast<const char16_t*>(data));
}
bool NS_FASTCALL Equals( char16ptr_t data, const comparator_type& comp ) const
{
return Equals(static_cast<const char16_t*>(data), comp);
}
#endif
/**
* An efficient comparison with ASCII that can be used even
* for wide strings. Call this version when you know the
@ -351,6 +366,28 @@ class nsTSubstring_CharT
void NS_FASTCALL Assign( const substring_tuple_type& );
bool NS_FASTCALL Assign( const substring_tuple_type&, const fallible_t& ) NS_WARN_UNUSED_RESULT;
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
void Assign (char16ptr_t data)
{
Assign(static_cast<const char16_t*>(data));
}
bool Assign(char16ptr_t data, const fallible_t&) NS_WARN_UNUSED_RESULT
{
return Assign(static_cast<const char16_t*>(data), fallible_t());
}
void Assign (char16ptr_t data, size_type length)
{
Assign(static_cast<const char16_t*>(data), length);
}
bool Assign(char16ptr_t data, size_type length, const fallible_t&) NS_WARN_UNUSED_RESULT
{
return Assign(static_cast<const char16_t*>(data), length, fallible_t());
}
#endif
void NS_FASTCALL AssignASCII( const char* data, size_type length );
bool NS_FASTCALL AssignASCII( const char* data, size_type length, const fallible_t& ) NS_WARN_UNUSED_RESULT;
@ -383,6 +420,9 @@ class nsTSubstring_CharT
self_type& operator=( char_type c ) { Assign(c); return *this; }
self_type& operator=( const char_type* data ) { Assign(data); return *this; }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
self_type& operator=( char16ptr_t data ) { Assign(data); return *this; }
#endif
self_type& operator=( const self_type& str ) { Assign(str); return *this; }
self_type& operator=( const substring_tuple_type& tuple ) { Assign(tuple); return *this; }
@ -402,6 +442,11 @@ class nsTSubstring_CharT
void Append( char_type c ) { Replace(mLength, 0, c); }
void Append( const char_type* data, size_type length = size_type(-1) ) { Replace(mLength, 0, data, length); }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
void Append( char16ptr_t data, size_type length = size_type(-1) ) { Append(static_cast<const char16_t*>(data), length); }
#endif
void Append( const self_type& str ) { Replace(mLength, 0, str); }
void Append( const substring_tuple_type& tuple ) { Replace(mLength, 0, tuple); }
@ -466,11 +511,18 @@ class nsTSubstring_CharT
self_type& operator+=( char_type c ) { Append(c); return *this; }
self_type& operator+=( const char_type* data ) { Append(data); return *this; }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
self_type& operator+=( char16ptr_t data ) { Append(data); return *this; }
#endif
self_type& operator+=( const self_type& str ) { Append(str); return *this; }
self_type& operator+=( const substring_tuple_type& tuple ) { Append(tuple); return *this; }
void Insert( char_type c, index_type pos ) { Replace(pos, 0, c); }
void Insert( const char_type* data, index_type pos, size_type length = size_type(-1) ) { Replace(pos, 0, data, length); }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
void Insert( char16ptr_t data, index_type pos, size_type length = size_type(-1) )
{ Insert(static_cast<const char16_t*>(data), pos, length); }
#endif
void Insert( const self_type& str, index_type pos ) { Replace(pos, 0, str); }
void Insert( const substring_tuple_type& tuple, index_type pos ) { Replace(pos, 0, tuple); }
@ -549,6 +601,18 @@ class nsTSubstring_CharT
return mLength;
}
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
size_type GetMutableData( wchar_t** data, size_type newLen = size_type(-1) )
{
return GetMutableData(reinterpret_cast<char16_t**>(data), newLen);
}
size_type GetMutableData( wchar_t** data, size_type newLen, const fallible_t& )
{
return GetMutableData(reinterpret_cast<char16_t**>(data), newLen, fallible_t());
}
#endif
/**
* string data is never null, but can be marked void. if true, the