/src/mozilla-central/parser/html/nsHtml5String.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
4 | | |
5 | | #ifndef nsHtml5String_h |
6 | | #define nsHtml5String_h |
7 | | |
8 | | #include "nsAtom.h" |
9 | | #include "nsString.h" |
10 | | #include "nsStringBuffer.h" |
11 | | |
12 | | class nsHtml5TreeBuilder; |
13 | | |
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* |
20 | | * |
21 | | * Holding or passing this type is as unsafe as holding or passing |
22 | | * `nsStringBuffer*`/`nsAtom*`. |
23 | | */ |
24 | | class nsHtml5String final |
25 | | { |
26 | | private: |
27 | | static const uintptr_t kKindMask = uintptr_t(3); |
28 | | |
29 | | static const uintptr_t kPtrMask = ~kKindMask; |
30 | | |
31 | | enum Kind : uintptr_t |
32 | | { |
33 | | eNull = 0, |
34 | | eEmpty = 1, |
35 | | eStringBuffer = 2, |
36 | | eAtom = 3, |
37 | | }; |
38 | | |
39 | 0 | inline Kind GetKind() const { return (Kind)(mBits & kKindMask); } |
40 | | |
41 | | inline nsStringBuffer* AsStringBuffer() const |
42 | 0 | { |
43 | 0 | MOZ_ASSERT(GetKind() == eStringBuffer); |
44 | 0 | return reinterpret_cast<nsStringBuffer*>(mBits & kPtrMask); |
45 | 0 | } |
46 | | |
47 | | inline nsAtom* AsAtom() const |
48 | 0 | { |
49 | 0 | MOZ_ASSERT(GetKind() == eAtom); |
50 | 0 | return reinterpret_cast<nsAtom*>(mBits & kPtrMask); |
51 | 0 | } |
52 | | |
53 | | inline const char16_t* AsPtr() const |
54 | | { |
55 | | switch (GetKind()) { |
56 | | case eStringBuffer: |
57 | | return reinterpret_cast<char16_t*>(AsStringBuffer()->Data()); |
58 | | case eAtom: |
59 | | return AsAtom()->GetUTF16String(); |
60 | | default: |
61 | | return nullptr; |
62 | | } |
63 | | } |
64 | | |
65 | | public: |
66 | | /** |
67 | | * Default constructor. |
68 | | */ |
69 | | inline nsHtml5String() |
70 | | : nsHtml5String(nullptr) |
71 | 0 | { |
72 | 0 | } |
73 | | |
74 | | /** |
75 | | * Constructor from nullptr. |
76 | | */ |
77 | | inline MOZ_IMPLICIT nsHtml5String(decltype(nullptr)) |
78 | | : mBits(eNull) |
79 | 0 | { |
80 | 0 | } |
81 | | |
82 | | inline uint32_t Length() const |
83 | | { |
84 | | switch (GetKind()) { |
85 | | case eStringBuffer: |
86 | | return (AsStringBuffer()->StorageSize() / sizeof(char16_t) - 1); |
87 | | case eAtom: |
88 | | return AsAtom()->GetLength(); |
89 | | default: |
90 | | return 0; |
91 | | } |
92 | | } |
93 | | |
94 | | /** |
95 | | * False iff the string is logically null |
96 | | */ |
97 | 0 | inline MOZ_IMPLICIT operator bool() const { return mBits; } |
98 | | |
99 | | /** |
100 | | * Get the underlying nsAtom* or nullptr if this nsHtml5String |
101 | | * does not hold an atom. |
102 | | */ |
103 | | inline nsAtom* MaybeAsAtom() |
104 | 0 | { |
105 | 0 | if (GetKind() == eAtom) { |
106 | 0 | return AsAtom(); |
107 | 0 | } |
108 | 0 | return nullptr; |
109 | 0 | } |
110 | | |
111 | | void ToString(nsAString& aString); |
112 | | |
113 | | void CopyToBuffer(char16_t* aBuffer) const; |
114 | | |
115 | | bool LowerCaseEqualsASCII(const char* aLowerCaseLiteral) const; |
116 | | |
117 | | bool EqualsASCII(const char* aLiteral) const; |
118 | | |
119 | | bool LowerCaseStartsWithASCII(const char* aLowerCaseLiteral) const; |
120 | | |
121 | | bool Equals(nsHtml5String aOther) const; |
122 | | |
123 | | nsHtml5String Clone(); |
124 | | |
125 | | void Release(); |
126 | | |
127 | | static nsHtml5String FromBuffer(char16_t* aBuffer, |
128 | | int32_t aLength, |
129 | | nsHtml5TreeBuilder* aTreeBuilder); |
130 | | |
131 | | static nsHtml5String FromLiteral(const char* aLiteral); |
132 | | |
133 | | static nsHtml5String FromString(const nsAString& aString); |
134 | | |
135 | | static nsHtml5String FromAtom(already_AddRefed<nsAtom> aAtom); |
136 | | |
137 | | static nsHtml5String EmptyString(); |
138 | | |
139 | | private: |
140 | | /** |
141 | | * Constructor from raw bits. |
142 | | */ |
143 | | explicit nsHtml5String(uintptr_t aBits) |
144 | 0 | : mBits(aBits){}; |
145 | | |
146 | | /** |
147 | | * Zero if null, one if empty, otherwise tagged pointer |
148 | | * to either nsAtom or nsStringBuffer. The two least-significant |
149 | | * bits are tag bits. |
150 | | */ |
151 | | uintptr_t mBits; |
152 | | }; |
153 | | |
154 | | #endif // nsHtml5String_h |