1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsHtml5String_h
6 #define nsHtml5String_h
10 #include "nsStringBuffer.h"
12 class nsHtml5TreeBuilder
;
15 * A pass-by-value type that can represent
18 * * Non-empty string as exactly-sized (capacity is length) `nsStringBuffer*`
19 * * Non-empty string as an nsAtom*
21 * Holding or passing this type is as unsafe as holding or passing
22 * `nsStringBuffer*`/`nsAtom*`.
24 class nsHtml5String final
{
26 static const uintptr_t kKindMask
= uintptr_t(3);
28 static const uintptr_t kPtrMask
= ~kKindMask
;
30 enum Kind
: uintptr_t {
37 inline Kind
GetKind() const { return (Kind
)(mBits
& kKindMask
); }
39 inline nsStringBuffer
* AsStringBuffer() const {
40 MOZ_ASSERT(GetKind() == eStringBuffer
);
41 return reinterpret_cast<nsStringBuffer
*>(mBits
& kPtrMask
);
44 inline nsAtom
* AsAtom() const {
45 MOZ_ASSERT(GetKind() == eAtom
);
46 return reinterpret_cast<nsAtom
*>(mBits
& kPtrMask
);
49 inline const char16_t
* AsPtr() const {
52 return reinterpret_cast<char16_t
*>(AsStringBuffer()->Data());
54 return AsAtom()->GetUTF16String();
56 return nsCharTraits
<char16_t
>::sEmptyBuffer
;
62 * Default constructor.
64 inline nsHtml5String() : nsHtml5String(nullptr) {}
67 * Constructor from nullptr.
69 inline MOZ_IMPLICIT
nsHtml5String(decltype(nullptr)) : mBits(eNull
) {}
71 inline uint32_t Length() const {
74 return (AsStringBuffer()->StorageSize() / sizeof(char16_t
) - 1);
76 return AsAtom()->GetLength();
83 * False iff the string is logically null
85 inline MOZ_IMPLICIT
operator bool() const { return mBits
; }
88 * Get the underlying nsAtom* or nullptr if this nsHtml5String
89 * does not hold an atom.
91 inline nsAtom
* MaybeAsAtom() {
92 if (GetKind() == eAtom
) {
98 void ToString(nsAString
& aString
);
100 void CopyToBuffer(char16_t
* aBuffer
) const;
102 bool LowerCaseEqualsASCII(const char* aLowerCaseLiteral
) const;
104 bool EqualsASCII(const char* aLiteral
) const;
106 bool LowerCaseStartsWithASCII(const char* aLowerCaseLiteral
) const;
108 bool Equals(nsHtml5String aOther
) const;
110 nsHtml5String
Clone();
114 static nsHtml5String
FromBuffer(char16_t
* aBuffer
, int32_t aLength
,
115 nsHtml5TreeBuilder
* aTreeBuilder
);
117 static nsHtml5String
FromLiteral(const char* aLiteral
);
119 static nsHtml5String
FromString(const nsAString
& aString
);
121 static nsHtml5String
FromAtom(already_AddRefed
<nsAtom
> aAtom
);
123 static nsHtml5String
EmptyString();
127 * Constructor from raw bits.
129 explicit nsHtml5String(uintptr_t aBits
) : mBits(aBits
){};
132 * Zero if null, one if empty, otherwise tagged pointer
133 * to either nsAtom or nsStringBuffer. The two least-significant
139 #endif // nsHtml5String_h