2016-09-01 13:10:54 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_FakeString_h__
|
|
|
|
#define mozilla_dom_FakeString_h__
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringBuffer.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
2018-08-16 13:15:07 +03:00
|
|
|
#include "mozilla/Span.h"
|
2016-09-01 13:10:54 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace binding_detail {
|
2019-10-29 22:02:42 +03:00
|
|
|
// A struct that has a layout compatible with nsAString, so that
|
|
|
|
// reinterpret-casting a FakeString as a const nsAString is safe, but much
|
|
|
|
// faster constructor and destructor behavior. FakeString uses inline storage
|
|
|
|
// for small strings and an nsStringBuffer for longer strings. It can also
|
|
|
|
// point to a literal (static-lifetime) string that's compiled into the binary,
|
|
|
|
// or point at the buffer of an nsAString whose lifetime is longer than that of
|
|
|
|
// the FakeString.
|
2020-01-08 00:37:12 +03:00
|
|
|
template <typename CharT>
|
2016-09-01 13:10:54 +03:00
|
|
|
struct FakeString {
|
2020-01-08 00:37:12 +03:00
|
|
|
using char_type = CharT;
|
|
|
|
using string_type = nsTString<CharT>;
|
|
|
|
using size_type = typename string_type::size_type;
|
|
|
|
using DataFlags = typename string_type::DataFlags;
|
|
|
|
using ClassFlags = typename string_type::ClassFlags;
|
|
|
|
using AString = nsTSubstring<CharT>;
|
|
|
|
|
|
|
|
static const size_t kInlineCapacity = 64;
|
|
|
|
using AutoString = nsTAutoStringN<CharT, kInlineCapacity>;
|
2020-01-04 13:36:49 +03:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
FakeString()
|
2020-01-08 00:37:12 +03:00
|
|
|
: mDataFlags(DataFlags::TERMINATED),
|
|
|
|
mClassFlags(ClassFlags::INLINE),
|
|
|
|
mInlineCapacity(kInlineCapacity - 1) {}
|
2016-09-01 13:10:54 +03:00
|
|
|
|
|
|
|
~FakeString() {
|
2020-01-08 00:37:12 +03:00
|
|
|
if (mDataFlags & DataFlags::REFCOUNTED) {
|
2019-10-29 22:02:42 +03:00
|
|
|
MOZ_ASSERT(mDataInitialized);
|
2016-09-01 13:10:54 +03:00
|
|
|
nsStringBuffer::FromData(mData)->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Share aString's string buffer, if it has one; otherwise, make this string
|
|
|
|
// depend upon aString's data. aString should outlive this instance of
|
|
|
|
// FakeString.
|
2020-01-08 00:37:12 +03:00
|
|
|
void ShareOrDependUpon(const AString& aString) {
|
2016-09-01 13:10:54 +03:00
|
|
|
RefPtr<nsStringBuffer> sharedBuffer = nsStringBuffer::FromString(aString);
|
|
|
|
if (!sharedBuffer) {
|
2019-10-29 22:02:42 +03:00
|
|
|
InitData(aString.BeginReading(), aString.Length());
|
|
|
|
if (!aString.IsTerminated()) {
|
2020-01-08 00:37:12 +03:00
|
|
|
mDataFlags &= ~DataFlags::TERMINATED;
|
2019-10-29 22:02:42 +03:00
|
|
|
}
|
2016-09-01 13:10:54 +03:00
|
|
|
} else {
|
2019-10-30 18:52:22 +03:00
|
|
|
AssignFromStringBuffer(sharedBuffer.forget(), aString.Length());
|
2016-09-01 13:10:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
void Truncate() { InitData(string_type::char_traits::sEmptyBuffer, 0); }
|
2016-09-01 13:10:54 +03:00
|
|
|
|
|
|
|
void SetIsVoid(bool aValue) {
|
|
|
|
MOZ_ASSERT(aValue, "We don't support SetIsVoid(false) on FakeString!");
|
|
|
|
Truncate();
|
2020-01-08 00:37:12 +03:00
|
|
|
mDataFlags |= DataFlags::VOIDED;
|
2016-09-01 13:10:54 +03:00
|
|
|
}
|
|
|
|
|
2020-01-04 13:36:49 +03:00
|
|
|
char_type* BeginWriting() {
|
2019-10-29 03:24:10 +03:00
|
|
|
MOZ_ASSERT(IsMutable());
|
2019-10-29 22:02:42 +03:00
|
|
|
MOZ_ASSERT(mDataInitialized);
|
2016-09-01 13:10:54 +03:00
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
size_type Length() const { return mLength; }
|
2016-09-01 13:10:54 +03:00
|
|
|
|
2020-01-04 13:36:49 +03:00
|
|
|
operator mozilla::Span<const char_type>() const {
|
2019-10-29 22:02:42 +03:00
|
|
|
MOZ_ASSERT(mDataInitialized);
|
2020-08-07 10:42:50 +03:00
|
|
|
// Explicitly specify template argument here to avoid instantiating
|
|
|
|
// Span<char_type> first and then implicitly converting to Span<const
|
|
|
|
// char_type>
|
|
|
|
return mozilla::Span<const char_type>{mData, Length()};
|
2018-08-16 13:15:07 +03:00
|
|
|
}
|
|
|
|
|
2020-01-04 13:36:49 +03:00
|
|
|
operator mozilla::Span<char_type>() {
|
2020-08-07 10:42:50 +03:00
|
|
|
return mozilla::Span{BeginWriting(), Length()};
|
2018-08-16 13:15:07 +03:00
|
|
|
}
|
|
|
|
|
2020-08-20 02:58:41 +03:00
|
|
|
mozilla::BulkWriteHandle<CharT> BulkWrite(size_type aCapacity,
|
|
|
|
size_type aPrefixToPreserve,
|
|
|
|
bool aAllowShrinking,
|
|
|
|
nsresult& aRv) {
|
2020-01-08 00:37:12 +03:00
|
|
|
MOZ_ASSERT(!mDataInitialized);
|
|
|
|
InitData(mStorage, 0);
|
|
|
|
mDataFlags |= DataFlags::INLINE;
|
|
|
|
return ToAStringPtr()->BulkWrite(aCapacity, aPrefixToPreserve,
|
2020-08-20 02:58:41 +03:00
|
|
|
aAllowShrinking, aRv);
|
2020-01-08 00:37:12 +03:00
|
|
|
}
|
|
|
|
|
2016-09-01 13:10:54 +03:00
|
|
|
// Reserve space to write aLength chars, not including null-terminator.
|
2020-01-08 00:37:12 +03:00
|
|
|
bool SetLength(size_type aLength, mozilla::fallible_t const&) {
|
|
|
|
// Use mStorage for small strings.
|
|
|
|
if (aLength < kInlineCapacity) {
|
|
|
|
InitData(mStorage, aLength);
|
|
|
|
mDataFlags |= DataFlags::INLINE;
|
2016-09-01 13:10:54 +03:00
|
|
|
} else {
|
|
|
|
RefPtr<nsStringBuffer> buf =
|
2020-01-04 13:36:49 +03:00
|
|
|
nsStringBuffer::Alloc((aLength + 1) * sizeof(char_type));
|
2016-09-01 13:10:54 +03:00
|
|
|
if (MOZ_UNLIKELY(!buf)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:02:42 +03:00
|
|
|
AssignFromStringBuffer(buf.forget(), aLength);
|
2016-09-01 13:10:54 +03:00
|
|
|
}
|
2019-10-29 22:02:42 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mDataInitialized);
|
2020-01-08 00:37:12 +03:00
|
|
|
mData[mLength] = char_type(0);
|
2016-09-01 13:10:54 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-29 03:24:10 +03:00
|
|
|
// Returns false on allocation failure.
|
|
|
|
bool EnsureMutable() {
|
2019-10-29 22:02:42 +03:00
|
|
|
MOZ_ASSERT(mDataInitialized);
|
|
|
|
|
2019-10-29 03:24:10 +03:00
|
|
|
if (IsMutable()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsStringBuffer> buffer;
|
2020-01-08 00:37:12 +03:00
|
|
|
if (mDataFlags & DataFlags::REFCOUNTED) {
|
2019-10-29 03:24:10 +03:00
|
|
|
// Make sure we'll drop it when we're done.
|
|
|
|
buffer = dont_AddRef(nsStringBuffer::FromData(mData));
|
|
|
|
// And make sure we don't release it twice by accident.
|
|
|
|
}
|
2020-01-04 13:36:49 +03:00
|
|
|
const char_type* oldChars = mData;
|
2019-10-29 03:24:10 +03:00
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
mDataFlags = DataFlags::TERMINATED;
|
2019-10-29 22:02:42 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Reset mDataInitialized because we're explicitly reinitializing
|
|
|
|
// it via the SetLength call.
|
|
|
|
mDataInitialized = false;
|
|
|
|
#endif // DEBUG
|
2019-10-29 03:24:10 +03:00
|
|
|
// SetLength will make sure we have our own buffer to work with. Note that
|
|
|
|
// we may be transitioning from having a (short) readonly stringbuffer to
|
|
|
|
// our inline storage or whatnot. That's all fine; SetLength is responsible
|
|
|
|
// for setting up our flags correctly.
|
|
|
|
if (!SetLength(Length(), fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(oldChars != mData, "Should have new chars now!");
|
|
|
|
MOZ_ASSERT(IsMutable(), "Why are we still not mutable?");
|
2020-01-04 13:36:49 +03:00
|
|
|
memcpy(mData, oldChars, Length() * sizeof(char_type));
|
2019-10-29 03:24:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:02:42 +03:00
|
|
|
void AssignFromStringBuffer(already_AddRefed<nsStringBuffer> aBuffer,
|
2019-10-30 18:52:22 +03:00
|
|
|
size_t aLength) {
|
2020-01-04 13:36:49 +03:00
|
|
|
InitData(static_cast<char_type*>(aBuffer.take()->Data()), aLength);
|
2020-01-08 00:37:12 +03:00
|
|
|
mDataFlags |= DataFlags::REFCOUNTED;
|
2019-10-30 18:52:22 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 03:26:28 +03:00
|
|
|
// The preferred way to assign literals to a FakeString. This should only be
|
|
|
|
// called with actual C++ literal strings (i.e. u"stuff") or character arrays
|
|
|
|
// that originally come from passing such literal strings.
|
|
|
|
template <int N>
|
2020-01-04 13:36:49 +03:00
|
|
|
void AssignLiteral(const char_type (&aData)[N]) {
|
2019-10-29 03:26:28 +03:00
|
|
|
AssignLiteral(aData, N - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign a literal to a FakeString when it's not an actual literal
|
|
|
|
// in the code, but is known to be a literal somehow (e.g. it came
|
|
|
|
// from an nsAString that tested true for IsLiteral()).
|
2020-01-04 13:36:49 +03:00
|
|
|
void AssignLiteral(const char_type* aData, size_t aLength) {
|
2019-10-29 22:02:42 +03:00
|
|
|
InitData(aData, aLength);
|
2020-01-08 00:37:12 +03:00
|
|
|
mDataFlags |= DataFlags::LITERAL;
|
2019-10-29 03:26:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-01 13:10:54 +03:00
|
|
|
// If this ever changes, change the corresponding code in the
|
2020-01-08 00:37:12 +03:00
|
|
|
// Optional<nsA[C]String> specialization as well.
|
|
|
|
const AString* ToAStringPtr() const {
|
|
|
|
return reinterpret_cast<const string_type*>(this);
|
2016-09-01 13:10:54 +03:00
|
|
|
}
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
operator const AString&() const { return *ToAStringPtr(); }
|
2016-09-01 13:10:54 +03:00
|
|
|
|
|
|
|
private:
|
2020-01-08 00:37:12 +03:00
|
|
|
AString* ToAStringPtr() { return reinterpret_cast<string_type*>(this); }
|
2016-09-01 13:10:54 +03:00
|
|
|
|
2018-01-05 23:37:42 +03:00
|
|
|
// mData is left uninitialized for optimization purposes.
|
2020-01-04 13:36:49 +03:00
|
|
|
MOZ_INIT_OUTSIDE_CTOR char_type* mData;
|
2018-01-05 23:37:42 +03:00
|
|
|
// mLength is left uninitialized for optimization purposes.
|
2020-01-08 00:37:12 +03:00
|
|
|
MOZ_INIT_OUTSIDE_CTOR size_type mLength;
|
|
|
|
DataFlags mDataFlags;
|
|
|
|
const ClassFlags mClassFlags;
|
2016-09-01 13:10:54 +03:00
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
const size_type mInlineCapacity;
|
|
|
|
char_type mStorage[kInlineCapacity];
|
2019-10-29 22:02:42 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mDataInitialized = false;
|
|
|
|
#endif // DEBUG
|
2016-09-01 13:10:54 +03:00
|
|
|
|
|
|
|
FakeString(const FakeString& other) = delete;
|
|
|
|
void operator=(const FakeString& other) = delete;
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
void InitData(const char_type* aData, size_type aLength) {
|
|
|
|
MOZ_ASSERT(mDataFlags == DataFlags::TERMINATED);
|
2019-10-29 22:02:42 +03:00
|
|
|
MOZ_ASSERT(!mDataInitialized);
|
2020-01-04 13:36:49 +03:00
|
|
|
mData = const_cast<char_type*>(aData);
|
2019-10-29 22:02:42 +03:00
|
|
|
mLength = aLength;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mDataInitialized = true;
|
|
|
|
#endif // DEBUG
|
2016-09-01 13:10:54 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 03:24:10 +03:00
|
|
|
bool IsMutable() {
|
2020-01-08 00:37:12 +03:00
|
|
|
return (mDataFlags & DataFlags::INLINE) ||
|
|
|
|
((mDataFlags & DataFlags::REFCOUNTED) &&
|
2019-10-29 03:24:10 +03:00
|
|
|
!nsStringBuffer::FromData(mData)->IsReadonly());
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
friend class NonNull<AString>;
|
2016-09-01 13:10:54 +03:00
|
|
|
|
|
|
|
// A class to use for our static asserts to ensure our object layout
|
|
|
|
// matches that of nsString.
|
|
|
|
class StringAsserter;
|
|
|
|
friend class StringAsserter;
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
class StringAsserter : public AutoString {
|
2016-09-01 13:10:54 +03:00
|
|
|
public:
|
|
|
|
static void StaticAsserts() {
|
2020-01-08 00:37:12 +03:00
|
|
|
static_assert(sizeof(AutoString) == sizeof(FakeString),
|
|
|
|
"Should be binary compatible with nsTAutoString");
|
|
|
|
static_assert(
|
|
|
|
offsetof(FakeString, mInlineCapacity) == sizeof(string_type),
|
|
|
|
"FakeString should include all nsString members");
|
2016-09-01 13:10:54 +03:00
|
|
|
static_assert(
|
|
|
|
offsetof(FakeString, mData) == offsetof(StringAsserter, mData),
|
|
|
|
"Offset of mData should match");
|
|
|
|
static_assert(
|
|
|
|
offsetof(FakeString, mLength) == offsetof(StringAsserter, mLength),
|
|
|
|
"Offset of mLength should match");
|
2017-07-10 22:23:10 +03:00
|
|
|
static_assert(offsetof(FakeString, mDataFlags) ==
|
|
|
|
offsetof(StringAsserter, mDataFlags),
|
|
|
|
"Offset of mDataFlags should match");
|
|
|
|
static_assert(offsetof(FakeString, mClassFlags) ==
|
|
|
|
offsetof(StringAsserter, mClassFlags),
|
|
|
|
"Offset of mClassFlags should match");
|
2020-01-08 00:37:12 +03:00
|
|
|
static_assert(offsetof(FakeString, mInlineCapacity) ==
|
|
|
|
offsetof(StringAsserter, mInlineCapacity),
|
|
|
|
"Offset of mInlineCapacity should match");
|
|
|
|
static_assert(
|
|
|
|
offsetof(FakeString, mStorage) == offsetof(StringAsserter, mStorage),
|
|
|
|
"Offset of mStorage should match");
|
2016-09-01 13:10:54 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} // namespace binding_detail
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2020-01-08 00:37:12 +03:00
|
|
|
template <typename CharT>
|
2019-10-30 18:52:22 +03:00
|
|
|
inline void AssignFromStringBuffer(
|
|
|
|
nsStringBuffer* aBuffer, size_t aLength,
|
2020-01-08 00:37:12 +03:00
|
|
|
mozilla::dom::binding_detail::FakeString<CharT>& aDest) {
|
2019-10-30 18:52:22 +03:00
|
|
|
aDest.AssignFromStringBuffer(do_AddRef(aBuffer), aLength);
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:23:10 +03:00
|
|
|
#endif /* mozilla_dom_FakeString_h__ */
|