Bug 534136 Part 4: Inline accessors on nsIAtom. r=peterv sr=jst

This commit is contained in:
Jonas Sicking 2010-03-08 07:45:00 -08:00
parent 0e6d5d8188
commit e1f58838d3
6 changed files with 78 additions and 98 deletions

View File

@ -152,7 +152,8 @@ nsDOMDocumentType::GetText()
NS_IMETHODIMP
nsDOMDocumentType::GetName(nsAString& aName)
{
return mName->ToString(aName);
mName->ToString(aName);
return NS_OK;
}
NS_IMETHODIMP
@ -205,7 +206,8 @@ nsDOMDocumentType::GetInternalSubset(nsAString& aInternalSubset)
NS_IMETHODIMP
nsDOMDocumentType::GetNodeName(nsAString& aNodeName)
{
return mName->ToString(aNodeName);
mName->ToString(aNodeName);
return NS_OK;
}
NS_IMETHODIMP

View File

@ -38,12 +38,24 @@
#include "nsHtml5Atom.h"
nsHtml5Atom::nsHtml5Atom(const nsAString& aString)
: mData(aString)
{
mLength = aString.Length();
nsStringBuffer* buf = nsStringBuffer::FromString(aString);
if (buf) {
buf->AddRef();
mString = static_cast<PRUnichar*>(buf->Data());
}
else {
buf = nsStringBuffer::Alloc((mLength + 1) * sizeof(PRUnichar));
mString = static_cast<PRUnichar*>(buf->Data());
CopyUnicodeTo(aString, 0, mString, mLength);
mString[mLength] = PRUnichar(0);
}
}
nsHtml5Atom::~nsHtml5Atom()
{
nsStringBuffer::FromData(mString)->Release();
}
NS_IMETHODIMP_(nsrefcnt)
@ -67,11 +79,11 @@ nsHtml5Atom::QueryInterface(REFNSIID aIID, void** aInstancePtr)
return NS_ERROR_UNEXPECTED;
}
NS_IMETHODIMP
nsHtml5Atom::ToString(nsAString& aReturn)
NS_IMETHODIMP
nsHtml5Atom::ScriptableToString(nsAString& aBuf)
{
aReturn.Assign(mData);
return NS_OK;
NS_NOTREACHED("Should not call ScriptableToString.");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
@ -81,20 +93,6 @@ nsHtml5Atom::ToUTF8String(nsACString& aReturn)
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsHtml5Atom::GetUTF16String(const PRUnichar **aReturn)
{
NS_NOTREACHED("Should not attempt to get a UTF-16 string from nsHtml5Atom");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP_(PRUint32)
nsHtml5Atom::GetLength()
{
NS_NOTREACHED("Should not attempt to get a length from nsHtml5Atom");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP_(PRBool)
nsHtml5Atom::IsStaticAtom()
{
@ -102,15 +100,15 @@ nsHtml5Atom::IsStaticAtom()
}
NS_IMETHODIMP
nsHtml5Atom::Equals(const nsAString& aString, PRBool *aReturn)
nsHtml5Atom::ScriptableEquals(const nsAString& aString, PRBool* aResult)
{
*aReturn = mData.Equals(aString);
return NS_OK;
}
NS_IMETHODIMP
nsHtml5Atom::EqualsUTF8(const nsACString& aString, PRBool *aReturn)
{
NS_NOTREACHED("Should not attempt to compare with an UTF-8 string.");
NS_NOTREACHED("Should not call ScriptableEquals.");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP_(PRBool)
nsHtml5Atom::EqualsUTF8(const nsACString& aString)
{
NS_NOTREACHED("Should not attempt to compare with an UTF-8 string.");
return PR_FALSE;
}

View File

@ -55,9 +55,6 @@ class nsHtml5Atom : public nsIAtom
nsHtml5Atom(const nsAString& aString);
~nsHtml5Atom();
private:
nsString mData;
};
#endif // nsHtml5Atom_h_

View File

@ -130,7 +130,7 @@ struct AtomTableEntry : public PLDHashEntryHdr {
"SetAtomImpl() called on non-atom AtomTableEntry!");
NS_ASSERTION(aAtom, "Setting null atom");
mBits = PtrBits(aAtom);
mLength = aAtom->mLength;
mLength = aAtom->GetLength();
}
inline void ClearAtom() {
@ -157,7 +157,7 @@ struct AtomTableEntry : public PLDHashEntryHdr {
NS_ASSERTION(keyHash > 1,
"getAtomString() called on non-atom AtomTableEntry!");
return GetAtomImpl()->mString;
return GetAtomImpl()->GetUTF16String();
}
// get the string buffer
@ -344,8 +344,8 @@ NS_PurgeAtomTable()
}
AtomImpl::AtomImpl(const nsAString& aString)
: mLength(aString.Length())
{
mLength = aString.Length();
nsStringBuffer* buf = nsStringBuffer::FromString(aString);
if (buf) {
buf->AddRef();
@ -357,15 +357,23 @@ AtomImpl::AtomImpl(const nsAString& aString)
CopyUnicodeTo(aString, 0, mString, mLength);
mString[mLength] = PRUnichar(0);
}
NS_ASSERTION(mString[mLength] == PRUnichar(0), "null terminated");
NS_ASSERTION(buf && buf->StorageSize() >= (mLength+1) * sizeof(PRUnichar),
"enough storage");
NS_ASSERTION(Equals(aString), "correct data");
}
AtomImpl::AtomImpl(nsStringBuffer* aStringBuffer, PRUint32 aLength)
: mLength(aLength),
mString(static_cast<PRUnichar*>(aStringBuffer->Data()))
{
mLength = aLength;
mString = static_cast<PRUnichar*>(aStringBuffer->Data());
// Technically we could currently avoid doing this addref by instead making
// the static atom buffers have an initial refcount of 2.
aStringBuffer->AddRef();
NS_ASSERTION(mString[mLength] == PRUnichar(0), "null terminated");
NS_ASSERTION(aStringBuffer && aStringBuffer->StorageSize() == (mLength+1) * 2, "correct storage");
}
AtomImpl::~AtomImpl()
@ -428,7 +436,7 @@ void* PermanentAtomImpl::operator new ( size_t size, AtomImpl* aAtom ) CPP_THROW
}
NS_IMETHODIMP
AtomImpl::ToString(nsAString& aBuf)
AtomImpl::ScriptableToString(nsAString& aBuf)
{
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
return NS_OK;
@ -441,30 +449,15 @@ AtomImpl::ToUTF8String(nsACString& aBuf)
return NS_OK;
}
NS_IMETHODIMP
AtomImpl::GetUTF16String(const PRUnichar **aResult)
NS_IMETHODIMP_(PRBool)
AtomImpl::EqualsUTF8(const nsACString& aString)
{
NS_PRECONDITION(aResult, "null out param");
*aResult = mString;
return NS_OK;
}
NS_IMETHODIMP_(PRUint32)
AtomImpl::GetLength()
{
return mLength;
return CompareUTF8toUTF16(aString,
nsDependentString(mString, mLength)) == 0;
}
NS_IMETHODIMP
AtomImpl::EqualsUTF8(const nsACString& aString, PRBool* aResult)
{
*aResult = CompareUTF8toUTF16(aString,
nsDependentString(mString, mLength)) == 0;
return NS_OK;
}
NS_IMETHODIMP
AtomImpl::Equals(const nsAString& aString, PRBool* aResult)
AtomImpl::ScriptableEquals(const nsAString& aString, PRBool* aResult)
{
*aResult = aString.Equals(nsDependentString(mString, mLength));
return NS_OK;

View File

@ -85,12 +85,6 @@ public:
// for |#ifdef NS_BUILD_REFCNT_LOGGING| access to reference count
nsrefcnt GetRefCount() { return mRefCnt; }
// The length of the string in the atom.
PRUint32 mLength;
// This always points to the data owned by a nsStringBuffer
PRUnichar* mString;
};
/**

View File

@ -40,6 +40,7 @@
%{C++
#include "nsStringGlue.h"
#include "nsCOMPtr.h"
#include "nsStringBuffer.h"
%}
/*
@ -48,55 +49,50 @@
* pointer identity.
*/
[scriptable, uuid(96c82146-56f3-4b43-817f-25d6db1ad8e8)]
[scriptable, uuid(1f341018-521a-49de-b806-1bef5c9a00b0)]
interface nsIAtom : nsISupports
{
/**
* Get the Unicode or UTF8 value for the string
*/
AString toString();
AUTF8String toUTF8String();
[binaryname(ScriptableToString)] AString toString();
[noscript] AUTF8String toUTF8String();
/**
* Return a pointer to a zero terminated UTF16 string.
*/
[noscript] void getUTF16String([shared, retval] out wstring aResult);
[notxpcom] unsigned long getLength();
/**
* Compare the atom to a specific string value
* Note that this will NEVER return/throw an error condition.
*/
boolean equals(in AString aString);
[binaryname(ScriptableEquals)] boolean equals(in AString aString);
boolean equalsUTF8(in AUTF8String aString);
[noscript, notxpcom] boolean equalsUTF8(in AUTF8String aString);
%{C++
// note this is NOT virtual so this won't muck with the vtable!
inline PRBool Equals(const nsAString& s) {
PRBool result;
Equals(s, &result);
return result;
}
inline PRBool EqualsUTF8(const nsACString& s) {
PRBool result;
EqualsUTF8(s, &result);
return result;
}
inline const PRUnichar* GetUTF16String() {
const PRUnichar* result;
GetUTF16String(&result);
return result;
}
%}
/**
* Returns true if the atom is static and false otherwise.
*/
[noscript, notxpcom] boolean isStaticAtom();
%{C++
// note this is NOT virtual so this won't muck with the vtable!
inline PRBool Equals(const nsAString& aString) {
return aString.Equals(nsDependentString(mString, mLength));
}
inline const PRUnichar* GetUTF16String() {
return mString;
}
inline const PRUint32 GetLength() {
return mLength;
}
inline void ToString(nsAString& aBuf) {
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
}
protected:
PRUint32 mLength;
PRUnichar* mString;
%}
};