Backed out changeset bcbab342eed8 (bug 1889658) for causing wpt reftest failures...
[gecko.git] / parser / html / nsHtml5String.h
blobf725630f9e47487395bd9ee0ce443245b7665743
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
8 #include "nsAtom.h"
9 #include "nsString.h"
10 #include "nsStringBuffer.h"
12 class nsHtml5TreeBuilder;
14 /**
15 * A pass-by-value type that can represent
16 * * nullptr
17 * * empty string
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 {
25 private:
26 static const uintptr_t kKindMask = uintptr_t(3);
28 static const uintptr_t kPtrMask = ~kKindMask;
30 enum Kind : uintptr_t {
31 eNull = 0,
32 eEmpty = 1,
33 eStringBuffer = 2,
34 eAtom = 3,
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 {
50 switch (GetKind()) {
51 case eStringBuffer:
52 return reinterpret_cast<char16_t*>(AsStringBuffer()->Data());
53 case eAtom:
54 return AsAtom()->GetUTF16String();
55 default:
56 return nsCharTraits<char16_t>::sEmptyBuffer;
60 public:
61 /**
62 * Default constructor.
64 inline nsHtml5String() : nsHtml5String(nullptr) {}
66 /**
67 * Constructor from nullptr.
69 inline MOZ_IMPLICIT nsHtml5String(decltype(nullptr)) : mBits(eNull) {}
71 inline uint32_t Length() const {
72 switch (GetKind()) {
73 case eStringBuffer:
74 return (AsStringBuffer()->StorageSize() / sizeof(char16_t) - 1);
75 case eAtom:
76 return AsAtom()->GetLength();
77 default:
78 return 0;
82 /**
83 * False iff the string is logically null
85 inline MOZ_IMPLICIT operator bool() const { return mBits; }
87 /**
88 * Get the underlying nsAtom* or nullptr if this nsHtml5String
89 * does not hold an atom.
91 inline nsAtom* MaybeAsAtom() {
92 if (GetKind() == eAtom) {
93 return AsAtom();
95 return nullptr;
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();
112 void Release();
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();
125 private:
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
134 * bits are tag bits.
136 uintptr_t mBits;
139 #endif // nsHtml5String_h