Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing 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/. */
5 #include "nsIAtom.h"
6 #include "nsString.h"
7 #include "jArray.h"
8 #include "nsHtml5Portability.h"
10 nsIAtom*
11 nsHtml5Portability::newLocalNameFromBuffer(char16_t* buf, int32_t offset, int32_t length, nsHtml5AtomTable* interner)
12 {
13 NS_ASSERTION(!offset, "The offset should always be zero here.");
14 NS_ASSERTION(interner, "Didn't get an atom service.");
15 return interner->GetAtom(nsDependentSubstring(buf, buf + length));
16 }
18 nsString*
19 nsHtml5Portability::newStringFromBuffer(char16_t* buf, int32_t offset, int32_t length)
20 {
21 return new nsString(buf + offset, length);
22 }
24 nsString*
25 nsHtml5Portability::newEmptyString()
26 {
27 return new nsString();
28 }
30 nsString*
31 nsHtml5Portability::newStringFromLiteral(const char* literal)
32 {
33 nsString* str = new nsString();
34 str->AssignASCII(literal);
35 return str;
36 }
38 nsString*
39 nsHtml5Portability::newStringFromString(nsString* string) {
40 nsString* newStr = new nsString();
41 newStr->Assign(*string);
42 return newStr;
43 }
45 jArray<char16_t,int32_t>
46 nsHtml5Portability::newCharArrayFromLocal(nsIAtom* local)
47 {
48 nsAutoString temp;
49 local->ToString(temp);
50 int32_t len = temp.Length();
51 jArray<char16_t,int32_t> arr = jArray<char16_t,int32_t>::newJArray(len);
52 memcpy(arr, temp.BeginReading(), len * sizeof(char16_t));
53 return arr;
54 }
56 jArray<char16_t,int32_t>
57 nsHtml5Portability::newCharArrayFromString(nsString* string)
58 {
59 int32_t len = string->Length();
60 jArray<char16_t,int32_t> arr = jArray<char16_t,int32_t>::newJArray(len);
61 memcpy(arr, string->BeginReading(), len * sizeof(char16_t));
62 return arr;
63 }
65 nsIAtom*
66 nsHtml5Portability::newLocalFromLocal(nsIAtom* local, nsHtml5AtomTable* interner)
67 {
68 NS_PRECONDITION(local, "Atom was null.");
69 NS_PRECONDITION(interner, "Atom table was null");
70 if (!local->IsStaticAtom()) {
71 nsAutoString str;
72 local->ToString(str);
73 local = interner->GetAtom(str);
74 }
75 return local;
76 }
78 void
79 nsHtml5Portability::releaseString(nsString* str)
80 {
81 delete str;
82 }
84 bool
85 nsHtml5Portability::localEqualsBuffer(nsIAtom* local, char16_t* buf, int32_t offset, int32_t length)
86 {
87 return local->Equals(nsDependentSubstring(buf + offset, buf + offset + length));
88 }
90 bool
91 nsHtml5Portability::lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(const char* lowerCaseLiteral, nsString* string)
92 {
93 if (!string) {
94 return false;
95 }
96 const char* litPtr = lowerCaseLiteral;
97 const char16_t* strPtr = string->BeginReading();
98 const char16_t* end = string->EndReading();
99 char16_t litChar;
100 while ((litChar = *litPtr)) {
101 NS_ASSERTION(!(litChar >= 'A' && litChar <= 'Z'), "Literal isn't in lower case.");
102 if (strPtr == end) {
103 return false;
104 }
105 char16_t strChar = *strPtr;
106 if (strChar >= 'A' && strChar <= 'Z') {
107 strChar += 0x20;
108 }
109 if (litChar != strChar) {
110 return false;
111 }
112 ++litPtr;
113 ++strPtr;
114 }
115 return true;
116 }
118 bool
119 nsHtml5Portability::lowerCaseLiteralEqualsIgnoreAsciiCaseString(const char* lowerCaseLiteral, nsString* string)
120 {
121 if (!string) {
122 return false;
123 }
124 return string->LowerCaseEqualsASCII(lowerCaseLiteral);
125 }
127 bool
128 nsHtml5Portability::literalEqualsString(const char* literal, nsString* string)
129 {
130 if (!string) {
131 return false;
132 }
133 return string->EqualsASCII(literal);
134 }
136 bool
137 nsHtml5Portability::stringEqualsString(nsString* one, nsString* other)
138 {
139 return one->Equals(*other);
140 }
142 void
143 nsHtml5Portability::initializeStatics()
144 {
145 }
147 void
148 nsHtml5Portability::releaseStatics()
149 {
150 }