1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/parser/html/javasrc/Portability.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 +/* 1.5 + * Copyright (c) 2008-2009 Mozilla Foundation 1.6 + * 1.7 + * Permission is hereby granted, free of charge, to any person obtaining a 1.8 + * copy of this software and associated documentation files (the "Software"), 1.9 + * to deal in the Software without restriction, including without limitation 1.10 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, 1.11 + * and/or sell copies of the Software, and to permit persons to whom the 1.12 + * Software is furnished to do so, subject to the following conditions: 1.13 + * 1.14 + * The above copyright notice and this permission notice shall be included in 1.15 + * all copies or substantial portions of the Software. 1.16 + * 1.17 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1.18 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1.19 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1.20 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1.21 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 1.22 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 1.23 + * DEALINGS IN THE SOFTWARE. 1.24 + */ 1.25 + 1.26 +package nu.validator.htmlparser.impl; 1.27 + 1.28 +import nu.validator.htmlparser.annotation.Literal; 1.29 +import nu.validator.htmlparser.annotation.Local; 1.30 +import nu.validator.htmlparser.annotation.NoLength; 1.31 +import nu.validator.htmlparser.common.Interner; 1.32 + 1.33 +public final class Portability { 1.34 + 1.35 + // Allocating methods 1.36 + 1.37 + /** 1.38 + * Allocates a new local name object. In C++, the refcount must be set up in such a way that 1.39 + * calling <code>releaseLocal</code> on the return value balances the refcount set by this method. 1.40 + */ 1.41 + public static @Local String newLocalNameFromBuffer(@NoLength char[] buf, int offset, int length, Interner interner) { 1.42 + return new String(buf, offset, length).intern(); 1.43 + } 1.44 + 1.45 + public static String newStringFromBuffer(@NoLength char[] buf, int offset, int length) { 1.46 + return new String(buf, offset, length); 1.47 + } 1.48 + 1.49 + public static String newEmptyString() { 1.50 + return ""; 1.51 + } 1.52 + 1.53 + public static String newStringFromLiteral(@Literal String literal) { 1.54 + return literal; 1.55 + } 1.56 + 1.57 + public static String newStringFromString(String string) { 1.58 + return string; 1.59 + } 1.60 + 1.61 + // XXX get rid of this 1.62 + public static char[] newCharArrayFromLocal(@Local String local) { 1.63 + return local.toCharArray(); 1.64 + } 1.65 + 1.66 + public static char[] newCharArrayFromString(String string) { 1.67 + return string.toCharArray(); 1.68 + } 1.69 + 1.70 + public static @Local String newLocalFromLocal(@Local String local, Interner interner) { 1.71 + return local; 1.72 + } 1.73 + 1.74 + // Deallocation methods 1.75 + 1.76 + public static void releaseString(String str) { 1.77 + // No-op in Java 1.78 + } 1.79 + 1.80 + // Comparison methods 1.81 + 1.82 + public static boolean localEqualsBuffer(@Local String local, @NoLength char[] buf, int offset, int length) { 1.83 + if (local.length() != length) { 1.84 + return false; 1.85 + } 1.86 + for (int i = 0; i < length; i++) { 1.87 + if (local.charAt(i) != buf[offset + i]) { 1.88 + return false; 1.89 + } 1.90 + } 1.91 + return true; 1.92 + } 1.93 + 1.94 + public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, 1.95 + String string) { 1.96 + if (string == null) { 1.97 + return false; 1.98 + } 1.99 + if (lowerCaseLiteral.length() > string.length()) { 1.100 + return false; 1.101 + } 1.102 + for (int i = 0; i < lowerCaseLiteral.length(); i++) { 1.103 + char c0 = lowerCaseLiteral.charAt(i); 1.104 + char c1 = string.charAt(i); 1.105 + if (c1 >= 'A' && c1 <= 'Z') { 1.106 + c1 += 0x20; 1.107 + } 1.108 + if (c0 != c1) { 1.109 + return false; 1.110 + } 1.111 + } 1.112 + return true; 1.113 + } 1.114 + 1.115 + public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, 1.116 + String string) { 1.117 + if (string == null) { 1.118 + return false; 1.119 + } 1.120 + if (lowerCaseLiteral.length() != string.length()) { 1.121 + return false; 1.122 + } 1.123 + for (int i = 0; i < lowerCaseLiteral.length(); i++) { 1.124 + char c0 = lowerCaseLiteral.charAt(i); 1.125 + char c1 = string.charAt(i); 1.126 + if (c1 >= 'A' && c1 <= 'Z') { 1.127 + c1 += 0x20; 1.128 + } 1.129 + if (c0 != c1) { 1.130 + return false; 1.131 + } 1.132 + } 1.133 + return true; 1.134 + } 1.135 + 1.136 + public static boolean literalEqualsString(@Literal String literal, String string) { 1.137 + return literal.equals(string); 1.138 + } 1.139 + 1.140 + public static boolean stringEqualsString(String one, String other) { 1.141 + return one.equals(other); 1.142 + } 1.143 + 1.144 + public static void delete(Object o) { 1.145 + 1.146 + } 1.147 + 1.148 + public static void deleteArray(Object o) { 1.149 + 1.150 + } 1.151 +}