Bug 1843499 - Part 2: Rethrow with exception stack in for-of loop. r=iain
[gecko.git] / parser / html / javasrc / Portability.java
blob1d65894a86f8a5a02e75ba4a12bc329652505d0b
1 /*
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.
36 assert a >= 0;
37 assert b >= 0;
38 int sum = a + b;
39 if (sum < a || sum < b) {
40 throw new SAXException("Integer overflow");
42 return sum;
45 // Allocating methods
47 /**
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
57 ) {
58 return new String(buf, offset, length);
61 public static String newEmptyString() {
62 return "";
65 public static String newStringFromLiteral(@Literal String literal) {
66 return literal;
69 public static String newStringFromString(String string) {
70 return 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) {
85 // No-op in Java
88 // Comparison methods
90 public static boolean localEqualsBuffer(@Local String local, @NoLength char[] buf, int length) {
91 if (local.length() != length) {
92 return false;
94 for (int i = 0; i < length; i++) {
95 if (local.charAt(i) != buf[i]) {
96 return false;
99 return true;
102 public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral,
103 String string) {
104 if (string == null) {
105 return false;
107 if (lowerCaseLiteral.length() > string.length()) {
108 return false;
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') {
114 c1 += 0x20;
116 if (c0 != c1) {
117 return false;
120 return true;
123 public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral,
124 String string) {
125 if (string == null) {
126 return false;
128 if (lowerCaseLiteral.length() != string.length()) {
129 return false;
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') {
135 c1 += 0x20;
137 if (c0 != c1) {
138 return false;
141 return true;
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) {