michael@0: /* michael@0: * Copyright (c) 2008-2009 Mozilla Foundation michael@0: * michael@0: * Permission is hereby granted, free of charge, to any person obtaining a michael@0: * copy of this software and associated documentation files (the "Software"), michael@0: * to deal in the Software without restriction, including without limitation michael@0: * the rights to use, copy, modify, merge, publish, distribute, sublicense, michael@0: * and/or sell copies of the Software, and to permit persons to whom the michael@0: * Software is furnished to do so, subject to the following conditions: michael@0: * michael@0: * The above copyright notice and this permission notice shall be included in michael@0: * all copies or substantial portions of the Software. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL michael@0: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER michael@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING michael@0: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER michael@0: * DEALINGS IN THE SOFTWARE. michael@0: */ michael@0: michael@0: package nu.validator.htmlparser.impl; michael@0: michael@0: import nu.validator.htmlparser.annotation.Literal; michael@0: import nu.validator.htmlparser.annotation.Local; michael@0: import nu.validator.htmlparser.annotation.NoLength; michael@0: import nu.validator.htmlparser.common.Interner; michael@0: michael@0: public final class Portability { michael@0: michael@0: // Allocating methods michael@0: michael@0: /** michael@0: * Allocates a new local name object. In C++, the refcount must be set up in such a way that michael@0: * calling releaseLocal on the return value balances the refcount set by this method. michael@0: */ michael@0: public static @Local String newLocalNameFromBuffer(@NoLength char[] buf, int offset, int length, Interner interner) { michael@0: return new String(buf, offset, length).intern(); michael@0: } michael@0: michael@0: public static String newStringFromBuffer(@NoLength char[] buf, int offset, int length) { michael@0: return new String(buf, offset, length); michael@0: } michael@0: michael@0: public static String newEmptyString() { michael@0: return ""; michael@0: } michael@0: michael@0: public static String newStringFromLiteral(@Literal String literal) { michael@0: return literal; michael@0: } michael@0: michael@0: public static String newStringFromString(String string) { michael@0: return string; michael@0: } michael@0: michael@0: // XXX get rid of this michael@0: public static char[] newCharArrayFromLocal(@Local String local) { michael@0: return local.toCharArray(); michael@0: } michael@0: michael@0: public static char[] newCharArrayFromString(String string) { michael@0: return string.toCharArray(); michael@0: } michael@0: michael@0: public static @Local String newLocalFromLocal(@Local String local, Interner interner) { michael@0: return local; michael@0: } michael@0: michael@0: // Deallocation methods michael@0: michael@0: public static void releaseString(String str) { michael@0: // No-op in Java michael@0: } michael@0: michael@0: // Comparison methods michael@0: michael@0: public static boolean localEqualsBuffer(@Local String local, @NoLength char[] buf, int offset, int length) { michael@0: if (local.length() != length) { michael@0: return false; michael@0: } michael@0: for (int i = 0; i < length; i++) { michael@0: if (local.charAt(i) != buf[offset + i]) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, michael@0: String string) { michael@0: if (string == null) { michael@0: return false; michael@0: } michael@0: if (lowerCaseLiteral.length() > string.length()) { michael@0: return false; michael@0: } michael@0: for (int i = 0; i < lowerCaseLiteral.length(); i++) { michael@0: char c0 = lowerCaseLiteral.charAt(i); michael@0: char c1 = string.charAt(i); michael@0: if (c1 >= 'A' && c1 <= 'Z') { michael@0: c1 += 0x20; michael@0: } michael@0: if (c0 != c1) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, michael@0: String string) { michael@0: if (string == null) { michael@0: return false; michael@0: } michael@0: if (lowerCaseLiteral.length() != string.length()) { michael@0: return false; michael@0: } michael@0: for (int i = 0; i < lowerCaseLiteral.length(); i++) { michael@0: char c0 = lowerCaseLiteral.charAt(i); michael@0: char c1 = string.charAt(i); michael@0: if (c1 >= 'A' && c1 <= 'Z') { michael@0: c1 += 0x20; michael@0: } michael@0: if (c0 != c1) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: public static boolean literalEqualsString(@Literal String literal, String string) { michael@0: return literal.equals(string); michael@0: } michael@0: michael@0: public static boolean stringEqualsString(String one, String other) { michael@0: return one.equals(other); michael@0: } michael@0: michael@0: public static void delete(Object o) { michael@0: michael@0: } michael@0: michael@0: public static void deleteArray(Object o) { michael@0: michael@0: } michael@0: }