2 * Copyright (c) 2008-2015 Mozilla Foundation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 package nu
.validator
.htmlparser
.impl
;
25 import org
.xml
.sax
.SAXException
;
27 import nu
.validator
.htmlparser
.annotation
.Literal
;
28 import nu
.validator
.htmlparser
.annotation
.Local
;
29 import nu
.validator
.htmlparser
.annotation
.NoLength
;
30 import nu
.validator
.htmlparser
.common
.Interner
;
32 public final class Portability
{
34 public static int checkedAdd(int a
, int b
) throws SAXException
{
35 // This can't be translated code, because in C++ signed integer overflow is UB, so the below code would be wrong.
39 if (sum
< a
|| sum
< b
) {
40 throw new SAXException("Integer overflow");
48 * Allocates a new local name object. In C++, the refcount must be set up in such a way that
49 * calling <code>releaseLocal</code> on the return value balances the refcount set by this method.
51 public static @Local String
newLocalNameFromBuffer(@NoLength char[] buf
, int length
, Interner interner
) {
52 return new String(buf
, 0, length
).intern();
55 public static String
newStringFromBuffer(@NoLength char[] buf
, int offset
, int length
56 // CPPONLY: , TreeBuilder treeBuilder, boolean maybeAtomize
58 return new String(buf
, offset
, length
);
61 public static String
newEmptyString() {
65 public static String
newStringFromLiteral(@Literal String literal
) {
69 public static String
newStringFromString(String string
) {
73 // XXX get rid of this
74 public static char[] newCharArrayFromLocal(@Local String local
) {
75 return local
.toCharArray();
78 public static char[] newCharArrayFromString(String string
) {
79 return string
.toCharArray();
82 // Deallocation methods
84 public static void releaseString(String str
) {
90 public static boolean localEqualsBuffer(@Local String local
, @NoLength char[] buf
, int length
) {
91 if (local
.length() != length
) {
94 for (int i
= 0; i
< length
; i
++) {
95 if (local
.charAt(i
) != buf
[i
]) {
102 public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral
,
104 if (string
== null) {
107 if (lowerCaseLiteral
.length() > string
.length()) {
110 for (int i
= 0; i
< lowerCaseLiteral
.length(); i
++) {
111 char c0
= lowerCaseLiteral
.charAt(i
);
112 char c1
= string
.charAt(i
);
113 if (c1
>= 'A' && c1
<= 'Z') {
123 public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral
,
125 if (string
== null) {
128 if (lowerCaseLiteral
.length() != string
.length()) {
131 for (int i
= 0; i
< lowerCaseLiteral
.length(); i
++) {
132 char c0
= lowerCaseLiteral
.charAt(i
);
133 char c1
= string
.charAt(i
);
134 if (c1
>= 'A' && c1
<= 'Z') {
144 public static boolean literalEqualsString(@Literal String literal
, String string
) {
145 return literal
.equals(string
);
148 public static boolean stringEqualsString(String one
, String other
) {
149 return one
.equals(other
);
152 public static void delete(Object o
) {
156 public static void deleteArray(Object o
) {