/* -*- 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/. */
namespace mozilla::dom::binding_detail { // 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 StringBuffer 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. template <typename CharT> struct FakeString { 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>; using LengthStorage = mozilla::detail::nsTStringLengthStorage<CharT>;
staticconst size_t kInlineCapacity = 64; using AutoString = nsTAutoStringN<CharT, kInlineCapacity>;
// 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. void ShareOrDependUpon(const AString& aString) {
RefPtr<StringBuffer> sharedBuffer = aString.GetStringBuffer(); if (!sharedBuffer) {
InitData(aString.BeginReading(), aString.Length()); if (!aString.IsTerminated()) {
mDataFlags &= ~DataFlags::TERMINATED;
}
} else {
AssignFromStringBuffer(sharedBuffer.forget(), aString.Length());
}
}
// Returns false on allocation failure. bool EnsureMutable() {
MOZ_ASSERT(mDataInitialized);
if (IsMutable()) { returntrue;
}
RefPtr<StringBuffer> buffer; if (mDataFlags & DataFlags::REFCOUNTED) { // Make sure we'll drop it when we're done.
buffer = dont_AddRef(StringBuffer::FromData(mData)); // And make sure we don't release it twice by accident.
} const char_type* oldChars = mData;
mDataFlags = DataFlags::TERMINATED; #ifdef DEBUG // Reset mDataInitialized because we're explicitly reinitializing // it via the SetLength call.
mDataInitialized = false; #endif// DEBUG // 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)) { returnfalse;
}
MOZ_ASSERT(oldChars != mData, "Should have new chars now!");
MOZ_ASSERT(IsMutable(), "Why are we still not mutable?");
memcpy(mData, oldChars, Length() * sizeof(char_type)); returntrue;
}
// 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> void AssignLiteral(const char_type (&aData)[N]) {
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()). void AssignLiteral(const char_type* aData, size_t aLength) {
InitData(aData, aLength);
mDataFlags |= DataFlags::LITERAL;
}
// If this ever changes, change the corresponding code in the // Optional<nsA[C]String> specialization as well. const AString* ToAStringPtr() const { returnreinterpret_cast<const string_type*>(this);
}
// mData is left uninitialized for optimization purposes.
MOZ_INIT_OUTSIDE_CTOR char_type* mData; // mLength is left uninitialized for optimization purposes.
MOZ_INIT_OUTSIDE_CTOR uint32_t mLength;
DataFlags mDataFlags; const ClassFlags mClassFlags;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.