Portability.java (5129B)
1 /* 2 * Copyright (c) 2008-2015 Mozilla Foundation 3 * 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: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 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. 21 */ 22 23 package nu.validator.htmlparser.impl; 24 25 import org.xml.sax.SAXException; 26 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; 31 32 public final class Portability { 33 34 // [NOCPP[ 35 public static int checkedAdd(int a, int b) throws SAXException { 36 // This can't be translated code, because in C++ signed integer overflow is UB, so the below code would be wrong. 37 assert a >= 0; 38 assert b >= 0; 39 int sum = a + b; 40 if (sum < a || sum < b) { 41 throw new SAXException("Integer overflow"); 42 } 43 return sum; 44 } 45 // ]NOCPP] 46 47 // Allocating methods 48 49 /** 50 * Allocates a new local name object. In C++, the refcount must be set up in such a way that 51 * calling <code>releaseLocal</code> on the return value balances the refcount set by this method. 52 */ 53 public static @Local String newLocalNameFromBuffer(@NoLength char[] buf, int length, Interner interner) { 54 return new String(buf, 0, length).intern(); 55 } 56 57 public static String newStringFromBuffer(@NoLength char[] buf, int offset, int length 58 // CPPONLY: , TreeBuilder treeBuilder, boolean maybeAtomize 59 ) { 60 return new String(buf, offset, length); 61 } 62 63 public static String newEmptyString() { 64 return ""; 65 } 66 67 public static String newStringFromLiteral(@Literal String literal) { 68 return literal; 69 } 70 71 public static String newStringFromString(String string) { 72 return string; 73 } 74 75 // XXX get rid of this 76 public static char[] newCharArrayFromLocal(@Local String local) { 77 return local.toCharArray(); 78 } 79 80 public static char[] newCharArrayFromString(String string) { 81 return string.toCharArray(); 82 } 83 84 // Deallocation methods 85 86 public static void releaseString(String str) { 87 // No-op in Java 88 } 89 90 // Comparison methods 91 92 public static boolean localEqualsBuffer(@Local String local, @NoLength char[] buf, int length) { 93 if (local.length() != length) { 94 return false; 95 } 96 for (int i = 0; i < length; i++) { 97 if (local.charAt(i) != buf[i]) { 98 return false; 99 } 100 } 101 return true; 102 } 103 104 public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, 105 String string) { 106 if (string == null) { 107 return false; 108 } 109 if (lowerCaseLiteral.length() > string.length()) { 110 return false; 111 } 112 for (int i = 0; i < lowerCaseLiteral.length(); i++) { 113 char c0 = lowerCaseLiteral.charAt(i); 114 char c1 = string.charAt(i); 115 if (c1 >= 'A' && c1 <= 'Z') { 116 c1 += 0x20; 117 } 118 if (c0 != c1) { 119 return false; 120 } 121 } 122 return true; 123 } 124 125 public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, 126 String string) { 127 if (string == null) { 128 return false; 129 } 130 if (lowerCaseLiteral.length() != string.length()) { 131 return false; 132 } 133 for (int i = 0; i < lowerCaseLiteral.length(); i++) { 134 char c0 = lowerCaseLiteral.charAt(i); 135 char c1 = string.charAt(i); 136 if (c1 >= 'A' && c1 <= 'Z') { 137 c1 += 0x20; 138 } 139 if (c0 != c1) { 140 return false; 141 } 142 } 143 return true; 144 } 145 146 public static boolean literalEqualsString(@Literal String literal, String string) { 147 return literal.equals(string); 148 } 149 150 public static boolean stringEqualsString(String one, String other) { 151 return one.equals(other); 152 } 153 154 public static void delete(Object o) { 155 156 } 157 158 public static void deleteArray(Object o) { 159 160 } 161 }