|
1 /* |
|
2 * Copyright (c) 2008-2009 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 nu.validator.htmlparser.annotation.Literal; |
|
26 import nu.validator.htmlparser.annotation.Local; |
|
27 import nu.validator.htmlparser.annotation.NoLength; |
|
28 import nu.validator.htmlparser.common.Interner; |
|
29 |
|
30 public final class Portability { |
|
31 |
|
32 // Allocating methods |
|
33 |
|
34 /** |
|
35 * Allocates a new local name object. In C++, the refcount must be set up in such a way that |
|
36 * calling <code>releaseLocal</code> on the return value balances the refcount set by this method. |
|
37 */ |
|
38 public static @Local String newLocalNameFromBuffer(@NoLength char[] buf, int offset, int length, Interner interner) { |
|
39 return new String(buf, offset, length).intern(); |
|
40 } |
|
41 |
|
42 public static String newStringFromBuffer(@NoLength char[] buf, int offset, int length) { |
|
43 return new String(buf, offset, length); |
|
44 } |
|
45 |
|
46 public static String newEmptyString() { |
|
47 return ""; |
|
48 } |
|
49 |
|
50 public static String newStringFromLiteral(@Literal String literal) { |
|
51 return literal; |
|
52 } |
|
53 |
|
54 public static String newStringFromString(String string) { |
|
55 return string; |
|
56 } |
|
57 |
|
58 // XXX get rid of this |
|
59 public static char[] newCharArrayFromLocal(@Local String local) { |
|
60 return local.toCharArray(); |
|
61 } |
|
62 |
|
63 public static char[] newCharArrayFromString(String string) { |
|
64 return string.toCharArray(); |
|
65 } |
|
66 |
|
67 public static @Local String newLocalFromLocal(@Local String local, Interner interner) { |
|
68 return local; |
|
69 } |
|
70 |
|
71 // Deallocation methods |
|
72 |
|
73 public static void releaseString(String str) { |
|
74 // No-op in Java |
|
75 } |
|
76 |
|
77 // Comparison methods |
|
78 |
|
79 public static boolean localEqualsBuffer(@Local String local, @NoLength char[] buf, int offset, int length) { |
|
80 if (local.length() != length) { |
|
81 return false; |
|
82 } |
|
83 for (int i = 0; i < length; i++) { |
|
84 if (local.charAt(i) != buf[offset + i]) { |
|
85 return false; |
|
86 } |
|
87 } |
|
88 return true; |
|
89 } |
|
90 |
|
91 public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, |
|
92 String string) { |
|
93 if (string == null) { |
|
94 return false; |
|
95 } |
|
96 if (lowerCaseLiteral.length() > string.length()) { |
|
97 return false; |
|
98 } |
|
99 for (int i = 0; i < lowerCaseLiteral.length(); i++) { |
|
100 char c0 = lowerCaseLiteral.charAt(i); |
|
101 char c1 = string.charAt(i); |
|
102 if (c1 >= 'A' && c1 <= 'Z') { |
|
103 c1 += 0x20; |
|
104 } |
|
105 if (c0 != c1) { |
|
106 return false; |
|
107 } |
|
108 } |
|
109 return true; |
|
110 } |
|
111 |
|
112 public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral, |
|
113 String string) { |
|
114 if (string == null) { |
|
115 return false; |
|
116 } |
|
117 if (lowerCaseLiteral.length() != string.length()) { |
|
118 return false; |
|
119 } |
|
120 for (int i = 0; i < lowerCaseLiteral.length(); i++) { |
|
121 char c0 = lowerCaseLiteral.charAt(i); |
|
122 char c1 = string.charAt(i); |
|
123 if (c1 >= 'A' && c1 <= 'Z') { |
|
124 c1 += 0x20; |
|
125 } |
|
126 if (c0 != c1) { |
|
127 return false; |
|
128 } |
|
129 } |
|
130 return true; |
|
131 } |
|
132 |
|
133 public static boolean literalEqualsString(@Literal String literal, String string) { |
|
134 return literal.equals(string); |
|
135 } |
|
136 |
|
137 public static boolean stringEqualsString(String one, String other) { |
|
138 return one.equals(other); |
|
139 } |
|
140 |
|
141 public static void delete(Object o) { |
|
142 |
|
143 } |
|
144 |
|
145 public static void deleteArray(Object o) { |
|
146 |
|
147 } |
|
148 } |