2017-03-20 15:45:15 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsHtml5String.h"
|
|
|
|
#include "nsCharTraits.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
|
|
|
#include "nsHtml5TreeBuilder.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5String::ToString(nsAString& aString)
|
|
|
|
{
|
2017-08-11 09:22:57 +03:00
|
|
|
switch (GetKind()) {
|
|
|
|
case eStringBuffer:
|
|
|
|
return AsStringBuffer()->ToString(Length(), aString);
|
|
|
|
case eAtom:
|
|
|
|
return AsAtom()->ToString(aString);
|
|
|
|
case eEmpty:
|
|
|
|
aString.Truncate();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
aString.Truncate();
|
2017-03-20 15:45:15 +03:00
|
|
|
aString.SetIsVoid(true);
|
2017-08-11 09:22:57 +03:00
|
|
|
return;
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-11 09:22:57 +03:00
|
|
|
nsHtml5String::CopyToBuffer(char16_t* aBuffer) const
|
2017-03-20 15:45:15 +03:00
|
|
|
{
|
2017-08-11 09:22:57 +03:00
|
|
|
memcpy(aBuffer, AsPtr(), Length() * sizeof(char16_t));
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-08-11 09:22:57 +03:00
|
|
|
nsHtml5String::LowerCaseEqualsASCII(const char* aLowerCaseLiteral) const
|
2017-03-20 15:45:15 +03:00
|
|
|
{
|
|
|
|
return !nsCharTraits<char16_t>::compareLowerCaseToASCIINullTerminated(
|
2017-08-11 09:22:57 +03:00
|
|
|
AsPtr(), Length(), aLowerCaseLiteral);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-08-11 09:22:57 +03:00
|
|
|
nsHtml5String::EqualsASCII(const char* aLiteral) const
|
2017-03-20 15:45:15 +03:00
|
|
|
{
|
|
|
|
return !nsCharTraits<char16_t>::compareASCIINullTerminated(
|
2017-08-11 09:22:57 +03:00
|
|
|
AsPtr(), Length(), aLiteral);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-08-11 09:22:57 +03:00
|
|
|
nsHtml5String::LowerCaseStartsWithASCII(const char* aLowerCaseLiteral) const
|
2017-03-20 15:45:15 +03:00
|
|
|
{
|
|
|
|
const char* litPtr = aLowerCaseLiteral;
|
2017-08-11 09:22:57 +03:00
|
|
|
const char16_t* strPtr = AsPtr();
|
2017-03-20 15:45:15 +03:00
|
|
|
const char16_t* end = strPtr + Length();
|
|
|
|
char16_t litChar;
|
2017-10-09 10:43:48 +03:00
|
|
|
while ((litChar = *litPtr)) {
|
2017-03-20 15:45:15 +03:00
|
|
|
MOZ_ASSERT(!(litChar >= 'A' && litChar <= 'Z'),
|
|
|
|
"Literal isn't in lower case.");
|
2017-10-09 10:43:48 +03:00
|
|
|
if (strPtr == end) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-20 15:45:15 +03:00
|
|
|
char16_t strChar = *strPtr;
|
|
|
|
if (strChar >= 'A' && strChar <= 'Z') {
|
|
|
|
strChar += 0x20;
|
|
|
|
}
|
|
|
|
if (litChar != strChar) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++litPtr;
|
|
|
|
++strPtr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-08-11 09:22:57 +03:00
|
|
|
nsHtml5String::Equals(nsHtml5String aOther) const
|
2017-03-20 15:45:15 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(operator bool());
|
|
|
|
MOZ_ASSERT(aOther);
|
2017-08-11 09:22:57 +03:00
|
|
|
if (Length() != aOther.Length()) {
|
2017-03-20 15:45:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !memcmp(
|
2017-08-11 09:22:57 +03:00
|
|
|
AsPtr(), aOther.AsPtr(), Length() * sizeof(char16_t));
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5String
|
|
|
|
nsHtml5String::Clone()
|
|
|
|
{
|
2017-08-11 09:22:57 +03:00
|
|
|
switch (GetKind()) {
|
|
|
|
case eStringBuffer:
|
|
|
|
AsStringBuffer()->AddRef();
|
|
|
|
break;
|
|
|
|
case eAtom:
|
|
|
|
AsAtom()->AddRef();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return nsHtml5String(mBits);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5String::Release()
|
|
|
|
{
|
2017-08-11 09:22:57 +03:00
|
|
|
switch (GetKind()) {
|
|
|
|
case eStringBuffer:
|
|
|
|
AsStringBuffer()->Release();
|
|
|
|
break;
|
|
|
|
case eAtom:
|
|
|
|
AsAtom()->Release();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mBits = eNull;
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsHtml5String
|
|
|
|
nsHtml5String::FromBuffer(char16_t* aBuffer,
|
|
|
|
int32_t aLength,
|
|
|
|
nsHtml5TreeBuilder* aTreeBuilder)
|
|
|
|
{
|
|
|
|
if (!aLength) {
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(eEmpty);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
// Work with nsStringBuffer directly to make sure that storage is actually
|
|
|
|
// nsStringBuffer and to make sure the allocation strategy matches
|
|
|
|
// nsAttrValue::GetStringBuffer, so that it doesn't need to reallocate and
|
|
|
|
// copy.
|
|
|
|
RefPtr<nsStringBuffer> buffer(
|
|
|
|
nsStringBuffer::Alloc((aLength + 1) * sizeof(char16_t)));
|
|
|
|
if (!buffer) {
|
|
|
|
if (!aTreeBuilder) {
|
|
|
|
MOZ_CRASH("Out of memory.");
|
|
|
|
}
|
|
|
|
aTreeBuilder->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
buffer = nsStringBuffer::Alloc(2 * sizeof(char16_t));
|
|
|
|
if (!buffer) {
|
|
|
|
MOZ_CRASH(
|
|
|
|
"Out of memory so badly that couldn't even allocate placeholder.");
|
|
|
|
}
|
|
|
|
char16_t* data = reinterpret_cast<char16_t*>(buffer->Data());
|
|
|
|
data[0] = 0xFFFD;
|
|
|
|
data[1] = 0;
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(reinterpret_cast<uintptr_t>(buffer.forget().take()) | eStringBuffer);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
char16_t* data = reinterpret_cast<char16_t*>(buffer->Data());
|
|
|
|
memcpy(data, aBuffer, aLength * sizeof(char16_t));
|
|
|
|
data[aLength] = 0;
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(reinterpret_cast<uintptr_t>(buffer.forget().take()) | eStringBuffer);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsHtml5String
|
|
|
|
nsHtml5String::FromLiteral(const char* aLiteral)
|
|
|
|
{
|
|
|
|
size_t length = std::strlen(aLiteral);
|
|
|
|
if (!length) {
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(eEmpty);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
// Work with nsStringBuffer directly to make sure that storage is actually
|
|
|
|
// nsStringBuffer and to make sure the allocation strategy matches
|
|
|
|
// nsAttrValue::GetStringBuffer, so that it doesn't need to reallocate and
|
|
|
|
// copy.
|
|
|
|
RefPtr<nsStringBuffer> buffer(
|
|
|
|
nsStringBuffer::Alloc((length + 1) * sizeof(char16_t)));
|
|
|
|
if (!buffer) {
|
|
|
|
MOZ_CRASH("Out of memory.");
|
|
|
|
}
|
|
|
|
char16_t* data = reinterpret_cast<char16_t*>(buffer->Data());
|
|
|
|
LossyConvertEncoding8to16 converter(data);
|
|
|
|
converter.write(aLiteral, length);
|
|
|
|
data[length] = 0;
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(reinterpret_cast<uintptr_t>(buffer.forget().take()) | eStringBuffer);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsHtml5String
|
|
|
|
nsHtml5String::FromString(const nsAString& aString)
|
|
|
|
{
|
|
|
|
auto length = aString.Length();
|
|
|
|
if (!length) {
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(eEmpty);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
RefPtr<nsStringBuffer> buffer = nsStringBuffer::FromString(aString);
|
2017-08-11 09:22:57 +03:00
|
|
|
if (buffer && (length == buffer->StorageSize()/sizeof(char16_t) - 1)) {
|
|
|
|
return nsHtml5String(reinterpret_cast<uintptr_t>(buffer.forget().take()) | eStringBuffer);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
buffer = nsStringBuffer::Alloc((length + 1) * sizeof(char16_t));
|
|
|
|
if (!buffer) {
|
|
|
|
MOZ_CRASH("Out of memory.");
|
|
|
|
}
|
|
|
|
char16_t* data = reinterpret_cast<char16_t*>(buffer->Data());
|
|
|
|
memcpy(data, aString.BeginReading(), length * sizeof(char16_t));
|
|
|
|
data[length] = 0;
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(reinterpret_cast<uintptr_t>(buffer.forget().take()) | eStringBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsHtml5String
|
2017-10-03 01:05:19 +03:00
|
|
|
nsHtml5String::FromAtom(already_AddRefed<nsAtom> aAtom)
|
2017-08-11 09:22:57 +03:00
|
|
|
{
|
|
|
|
return nsHtml5String(reinterpret_cast<uintptr_t>(aAtom.take()) | eAtom);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsHtml5String
|
|
|
|
nsHtml5String::EmptyString()
|
|
|
|
{
|
2017-08-11 09:22:57 +03:00
|
|
|
return nsHtml5String(eEmpty);
|
2017-03-20 15:45:15 +03:00
|
|
|
}
|