2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.misc;
|
|
27 |
|
|
28 |
import java.util.Comparator;
|
|
29 |
|
|
30 |
/** Implements a locale and case insensitive comparator suitable for
|
|
31 |
strings that are known to only contain ASCII characters. Some
|
|
32 |
tables internal to the JDK contain only ASCII data and are using
|
|
33 |
the "generalized" java.lang.String case-insensitive comparator
|
|
34 |
which converts each character to both upper and lower case. */
|
|
35 |
|
|
36 |
public class ASCIICaseInsensitiveComparator implements Comparator<String> {
|
|
37 |
public static final Comparator<String> CASE_INSENSITIVE_ORDER =
|
|
38 |
new ASCIICaseInsensitiveComparator();
|
|
39 |
|
|
40 |
public int compare(String s1, String s2) {
|
|
41 |
int n1=s1.length(), n2=s2.length();
|
|
42 |
int minLen = n1 < n2 ? n1 : n2;
|
|
43 |
for (int i=0; i < minLen; i++) {
|
|
44 |
char c1 = s1.charAt(i);
|
|
45 |
char c2 = s2.charAt(i);
|
|
46 |
assert c1 <= '\u007F' && c2 <= '\u007F';
|
|
47 |
if (c1 != c2) {
|
|
48 |
c1 = (char)toLower(c1);
|
|
49 |
c2 = (char)toLower(c2);
|
|
50 |
if (c1 != c2) {
|
|
51 |
return c1 - c2;
|
|
52 |
}
|
|
53 |
}
|
|
54 |
}
|
|
55 |
return n1 - n2;
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* A case insensitive hash code method to go with the case insensitive
|
|
60 |
* compare() method.
|
|
61 |
*
|
|
62 |
* Returns a hash code for this ASCII string as if it were lower case.
|
|
63 |
*
|
|
64 |
* returns same answer as:<p>
|
|
65 |
* <code>s.toLowerCase(Locale.US).hashCode();</code><p>
|
|
66 |
* but does not allocate memory (it does NOT have the special
|
|
67 |
* case Turkish rules).
|
|
68 |
*
|
|
69 |
* @param s a String to compute the hashcode on.
|
|
70 |
* @return a hash code value for this object.
|
|
71 |
*/
|
|
72 |
public static int lowerCaseHashCode(String s) {
|
|
73 |
int h = 0;
|
|
74 |
int len = s.length();
|
|
75 |
|
|
76 |
for (int i = 0; i < len; i++) {
|
|
77 |
h = 31*h + toLower(s.charAt(i));
|
|
78 |
}
|
|
79 |
|
|
80 |
return h;
|
|
81 |
}
|
|
82 |
|
|
83 |
/* If java.util.regex.ASCII ever becomes public or sun.*, use its code instead:*/
|
|
84 |
static boolean isLower(int ch) {
|
|
85 |
return ((ch-'a')|('z'-ch)) >= 0;
|
|
86 |
}
|
|
87 |
|
|
88 |
static boolean isUpper(int ch) {
|
|
89 |
return ((ch-'A')|('Z'-ch)) >= 0;
|
|
90 |
}
|
|
91 |
|
|
92 |
static int toLower(int ch) {
|
|
93 |
return isUpper(ch) ? (ch + 0x20) : ch;
|
|
94 |
}
|
|
95 |
|
|
96 |
static int toUpper(int ch) {
|
|
97 |
return isLower(ch) ? (ch - 0x20) : ch;
|
|
98 |
}
|
|
99 |
}
|