Line data Source code
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 "nsString.h"
9 :
10 : class nsHtml5TreeBuilder;
11 :
12 : /**
13 : * A pass-by-value type that combines an unsafe `nsStringBuffer*` with its
14 : * logical length (`uint32_t`). (`nsStringBuffer` knows its capacity but not
15 : * its logical length, i.e. how much of the capacity is in use.)
16 : *
17 : * Holding or passing this type is as unsafe as holding or passing
18 : * `nsStringBuffer*`.
19 : *
20 : * Empty strings and null strings are distinct. Since an empty nsString does
21 : * not have a an `nsStringBuffer`, both empty and null `nsHtml5String` have
22 : * `nullptr` as `mBuffer`. If `mBuffer` is `nullptr`, the empty case is marked
23 : * with `mLength` being zero and the null case with `mLength` being non-zero.
24 : */
25 : class nsHtml5String final
26 : {
27 : public:
28 : /**
29 : * Default constructor.
30 : */
31 8 : inline nsHtml5String()
32 8 : : nsHtml5String(nullptr)
33 : {
34 8 : }
35 :
36 : /**
37 : * Constructor from nullptr.
38 : */
39 47 : inline MOZ_IMPLICIT nsHtml5String(decltype(nullptr))
40 47 : : mBuffer(nullptr)
41 47 : , mLength(UINT32_MAX)
42 : {
43 47 : }
44 :
45 59 : inline uint32_t Length() const { return mBuffer ? mLength : 0; }
46 :
47 : /**
48 : * False iff the string is logically null
49 : */
50 37 : inline MOZ_IMPLICIT operator bool() const { return !(!mBuffer && mLength); }
51 :
52 : void ToString(nsAString& aString);
53 :
54 : void CopyToBuffer(char16_t* aBuffer);
55 :
56 : bool LowerCaseEqualsASCII(const char* aLowerCaseLiteral);
57 :
58 : bool EqualsASCII(const char* aLiteral);
59 :
60 : bool LowerCaseStartsWithASCII(const char* aLowerCaseLiteral);
61 :
62 : bool Equals(nsHtml5String aOther);
63 :
64 : nsHtml5String Clone();
65 :
66 : void Release();
67 :
68 : static nsHtml5String FromBuffer(char16_t* aBuffer,
69 : int32_t aLength,
70 : nsHtml5TreeBuilder* aTreeBuilder);
71 :
72 : static nsHtml5String FromLiteral(const char* aLiteral);
73 :
74 : static nsHtml5String FromString(const nsAString& aString);
75 :
76 : static nsHtml5String EmptyString();
77 :
78 : private:
79 : /**
80 : * Constructor from raw parts.
81 : */
82 : nsHtml5String(already_AddRefed<nsStringBuffer> aBuffer, uint32_t aLength);
83 :
84 : /**
85 : * nullptr if the string is logically null or logically empty
86 : */
87 : nsStringBuffer* mBuffer;
88 :
89 : /**
90 : * The length of the string. non-zero if the string is logically null.
91 : */
92 : uint32_t mLength;
93 : };
94 :
95 : #endif // nsHtml5String_h
|