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