Bug 1845715 - Check for failure when getting RegExp match result template r=iain
[gecko.git] / parser / html / nsHtml5Portability.cpp
blobe947c9e2572c3c21edea079a19de65ccaafe6aeb
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 #include "nsHtml5Portability.h"
6 #include "jArray.h"
7 #include "nsAtom.h"
8 #include "nsHtml5TreeBuilder.h"
9 #include "nsString.h"
10 #include "mozilla/CheckedInt.h"
12 int32_t nsHtml5Portability::checkedAdd(int32_t a, int32_t b) {
13 mozilla::CheckedInt<int32_t> sum(a);
14 sum += b;
15 MOZ_RELEASE_ASSERT(sum.isValid(),
16 "HTML input too large for signed 32-bit integer.");
17 return sum.value();
20 nsAtom* nsHtml5Portability::newLocalNameFromBuffer(char16_t* buf,
21 int32_t length,
22 nsHtml5AtomTable* interner) {
23 NS_ASSERTION(interner, "Didn't get an atom service.");
24 return interner->GetAtom(nsDependentSubstring(buf, buf + length));
27 static bool ContainsWhiteSpace(mozilla::Span<char16_t> aSpan) {
28 for (char16_t c : aSpan) {
29 if (nsContentUtils::IsHTMLWhitespace(c)) {
30 return true;
33 return false;
36 nsHtml5String nsHtml5Portability::newStringFromBuffer(
37 char16_t* buf, int32_t offset, int32_t length,
38 nsHtml5TreeBuilder* treeBuilder, bool maybeAtomize) {
39 if (!length) {
40 return nsHtml5String::EmptyString();
42 if (maybeAtomize &&
43 !ContainsWhiteSpace(mozilla::Span(buf + offset, length))) {
44 return nsHtml5String::FromAtom(
45 NS_AtomizeMainThread(nsDependentSubstring(buf + offset, length)));
47 return nsHtml5String::FromBuffer(buf + offset, length, treeBuilder);
50 nsHtml5String nsHtml5Portability::newEmptyString() {
51 return nsHtml5String::EmptyString();
54 nsHtml5String nsHtml5Portability::newStringFromLiteral(const char* literal) {
55 return nsHtml5String::FromLiteral(literal);
58 nsHtml5String nsHtml5Portability::newStringFromString(nsHtml5String string) {
59 return string.Clone();
62 jArray<char16_t, int32_t> nsHtml5Portability::newCharArrayFromLocal(
63 nsAtom* local) {
64 nsAutoString temp;
65 local->ToString(temp);
66 int32_t len = temp.Length();
67 jArray<char16_t, int32_t> arr = jArray<char16_t, int32_t>::newJArray(len);
68 memcpy(arr, temp.BeginReading(), len * sizeof(char16_t));
69 return arr;
72 jArray<char16_t, int32_t> nsHtml5Portability::newCharArrayFromString(
73 nsHtml5String string) {
74 MOZ_RELEASE_ASSERT(string);
75 uint32_t len = string.Length();
76 MOZ_RELEASE_ASSERT(len < INT32_MAX);
77 jArray<char16_t, int32_t> arr = jArray<char16_t, int32_t>::newJArray(len);
78 string.CopyToBuffer(arr);
79 return arr;
82 bool nsHtml5Portability::localEqualsBuffer(nsAtom* local, char16_t* buf,
83 int32_t length) {
84 return local->Equals(buf, length);
87 bool nsHtml5Portability::lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(
88 const char* lowerCaseLiteral, nsHtml5String string) {
89 return string.LowerCaseStartsWithASCII(lowerCaseLiteral);
92 bool nsHtml5Portability::lowerCaseLiteralEqualsIgnoreAsciiCaseString(
93 const char* lowerCaseLiteral, nsHtml5String string) {
94 return string.LowerCaseEqualsASCII(lowerCaseLiteral);
97 bool nsHtml5Portability::literalEqualsString(const char* literal,
98 nsHtml5String string) {
99 return string.EqualsASCII(literal);
102 bool nsHtml5Portability::stringEqualsString(nsHtml5String one,
103 nsHtml5String other) {
104 return one.Equals(other);
107 void nsHtml5Portability::initializeStatics() {}
109 void nsHtml5Portability::releaseStatics() {}