Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/ASCIICaseInsensitiveComparator.java
38829 views
/*1* Copyright (c) 2002, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.misc;2627import java.util.Comparator;2829/** Implements a locale and case insensitive comparator suitable for30strings that are known to only contain ASCII characters. Some31tables internal to the JDK contain only ASCII data and are using32the "generalized" java.lang.String case-insensitive comparator33which converts each character to both upper and lower case. */3435public class ASCIICaseInsensitiveComparator implements Comparator<String> {36public static final Comparator<String> CASE_INSENSITIVE_ORDER =37new ASCIICaseInsensitiveComparator();3839public int compare(String s1, String s2) {40int n1=s1.length(), n2=s2.length();41int minLen = n1 < n2 ? n1 : n2;42for (int i=0; i < minLen; i++) {43char c1 = s1.charAt(i);44char c2 = s2.charAt(i);45assert c1 <= '\u007F' && c2 <= '\u007F';46if (c1 != c2) {47c1 = (char)toLower(c1);48c2 = (char)toLower(c2);49if (c1 != c2) {50return c1 - c2;51}52}53}54return n1 - n2;55}5657/**58* A case insensitive hash code method to go with the case insensitive59* compare() method.60*61* Returns a hash code for this ASCII string as if it were lower case.62*63* returns same answer as:<p>64* <code>s.toLowerCase(Locale.US).hashCode();</code><p>65* but does not allocate memory (it does NOT have the special66* case Turkish rules).67*68* @param s a String to compute the hashcode on.69* @return a hash code value for this object.70*/71public static int lowerCaseHashCode(String s) {72int h = 0;73int len = s.length();7475for (int i = 0; i < len; i++) {76h = 31*h + toLower(s.charAt(i));77}7879return h;80}8182/* If java.util.regex.ASCII ever becomes public or sun.*, use its code instead:*/83static boolean isLower(int ch) {84return ((ch-'a')|('z'-ch)) >= 0;85}8687static boolean isUpper(int ch) {88return ((ch-'A')|('Z'-ch)) >= 0;89}9091static int toLower(int ch) {92return isUpper(ch) ? (ch + 0x20) : ch;93}9495static int toUpper(int ch) {96return isLower(ch) ? (ch - 0x20) : ch;97}98}99100101