src/java.base/share/classes/java/lang/String.java

Print this page
rev 13827 : 8151384: Examine sun.misc.ASCIICaseInsensitiveComparator
Reviewed-by:


1205      * A Comparator that orders {@code String} objects as by
1206      * {@code compareToIgnoreCase}. This comparator is serializable.
1207      * <p>
1208      * Note that this Comparator does <em>not</em> take locale into account,
1209      * and will result in an unsatisfactory ordering for certain locales.
1210      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1211      *
1212      * @see     java.text.Collator
1213      * @since   1.2
1214      */
1215     public static final Comparator<String> CASE_INSENSITIVE_ORDER
1216                                          = new CaseInsensitiveComparator();
1217     private static class CaseInsensitiveComparator
1218             implements Comparator<String>, java.io.Serializable {
1219         // use serialVersionUID from JDK 1.2.2 for interoperability
1220         private static final long serialVersionUID = 8575799808933029326L;
1221 
1222         public int compare(String s1, String s2) {
1223             byte v1[] = s1.value;
1224             byte v2[] = s2.value;
1225             int n1 = s1.length();
1226             int n2 = s2.length();
1227             boolean s1IsLatin1 = s1.isLatin1();
1228             boolean s2IsLatin1 = s2.isLatin1();
1229             int min = Math.min(n1, n2);
1230             for (int i = 0; i < min; i++) {
1231                 char c1 = s1IsLatin1 ? StringLatin1.getChar(v1, i)
1232                                      : StringUTF16.getChar(v1, i);
1233                 char c2 = s2IsLatin1 ? StringLatin1.getChar(v2, i)
1234                                      : StringUTF16.getChar(v2, i);































1235                 if (c1 != c2) {
1236                     c1 = Character.toUpperCase(c1);
1237                     c2 = Character.toUpperCase(c2);
1238                     if (c1 != c2) {
1239                         c1 = Character.toLowerCase(c1);
1240                         c2 = Character.toLowerCase(c2);
1241                         if (c1 != c2) {
1242                             // No overflow because of numeric promotion
1243                             return c1 - c2;
1244                         }
1245                     }
1246                 }
1247             }
1248             return n1 - n2;























1249         }
1250 
1251         /** Replaces the de-serialized object. */
1252         private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1253     }
1254 
1255     /**
1256      * Compares two strings lexicographically, ignoring case
1257      * differences. This method returns an integer whose sign is that of
1258      * calling {@code compareTo} with normalized versions of the strings
1259      * where case differences have been eliminated by calling
1260      * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1261      * each character.
1262      * <p>
1263      * Note that this method does <em>not</em> take locale into account,
1264      * and will result in an unsatisfactory ordering for certain locales.
1265      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1266      *
1267      * @param   str   the {@code String} to be compared.
1268      * @return  a negative integer, zero, or a positive integer as the




1205      * A Comparator that orders {@code String} objects as by
1206      * {@code compareToIgnoreCase}. This comparator is serializable.
1207      * <p>
1208      * Note that this Comparator does <em>not</em> take locale into account,
1209      * and will result in an unsatisfactory ordering for certain locales.
1210      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1211      *
1212      * @see     java.text.Collator
1213      * @since   1.2
1214      */
1215     public static final Comparator<String> CASE_INSENSITIVE_ORDER
1216                                          = new CaseInsensitiveComparator();
1217     private static class CaseInsensitiveComparator
1218             implements Comparator<String>, java.io.Serializable {
1219         // use serialVersionUID from JDK 1.2.2 for interoperability
1220         private static final long serialVersionUID = 8575799808933029326L;
1221 
1222         public int compare(String s1, String s2) {
1223             byte v1[] = s1.value;
1224             byte v2[] = s2.value;


1225             boolean s1IsLatin1 = s1.isLatin1();
1226             if (s1.coder() == s2.coder()) {
1227                 return s1IsLatin1 ? compareLatin1(v1, v2)
1228                         : compareUTF16(v1, v2);
1229             }
1230             return s1IsLatin1 ? compareLatin1ToUTF16(v1, v2)
1231                     : compareLatin1ToUTF16(v2, v1) * -1;
1232         }
1233 
1234         private static final int compareLatin1(byte[] v1, byte[] v2) {
1235             int len1 = v1.length;
1236             int len2 = v2.length;
1237             int lim = Math.min(len1, len2);
1238             for (int i = 0; i < lim; i++) {
1239                 char c1 = StringLatin1.getChar(v1, i);
1240                 char c2 = StringLatin1.getChar(v2, i);
1241                 if (c1 != c2) {
1242                     c1 = (char) CharacterDataLatin1.instance.toUpperCase(c1);
1243                     c2 = (char) CharacterDataLatin1.instance.toUpperCase(c2);
1244                     if (c1 != c2) {
1245                         c1 = (char) CharacterDataLatin1.instance.toLowerCase(c1);
1246                         c2 = (char) CharacterDataLatin1.instance.toLowerCase(c2);
1247                         if (c1 != c2) {
1248                             // No overflow because of numeric promotion
1249                             return c1 - c2;
1250                         }
1251                     }
1252                 }
1253             }
1254             return len1 - len2;
1255         }
1256 
1257         private static final int compareUTF16(byte[] v1, byte[] v2) {
1258             int len1 = StringUTF16.length(v1);
1259             int len2 = StringUTF16.length(v2);
1260             int lim = Math.min(len1, len2);
1261             for (int i = 0; i < lim; i++) {
1262                 char c1 = StringUTF16.getChar(v1, i);
1263                 char c2 = StringUTF16.getChar(v2, i);
1264                 if (c1 != c2) {
1265                     c1 = Character.toUpperCase(c1);
1266                     c2 = Character.toUpperCase(c2);
1267                     if (c1 != c2) {
1268                         c1 = Character.toLowerCase(c1);
1269                         c2 = Character.toLowerCase(c2);
1270                         if (c1 != c2) {
1271                             // No overflow because of numeric promotion
1272                             return c1 - c2;
1273                         }
1274                     }
1275                 }
1276             }
1277             return len1 - len2;
1278         }
1279 
1280         private static final int compareLatin1ToUTF16(byte[] v1, byte[] v2) {
1281             int len1 = v1.length;
1282             int len2 = StringUTF16.length(v2);
1283             int lim = Math.min(len1, len2);
1284             for (int i = 0; i < lim; i++) {
1285                 char c1 = StringLatin1.getChar(v1, i);
1286                 char c2 = StringUTF16.getChar(v2, i);
1287                 if (c1 != c2) {
1288                     c1 = (char) CharacterDataLatin1.instance.toUpperCase(c1);
1289                     c2 = Character.toUpperCase(c2);
1290                     if (c1 != c2) {
1291                         c1 = (char) CharacterDataLatin1.instance.toLowerCase(c1);
1292                         c2 = Character.toLowerCase(c2);
1293                         if (c1 != c2) {
1294                             // No overflow because of numeric promotion
1295                             return c1 - c2;
1296                         }
1297                     }
1298                 }
1299             }
1300             return len1 - len2;
1301         }
1302 
1303         /** Replaces the de-serialized object. */
1304         private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1305     }
1306 
1307     /**
1308      * Compares two strings lexicographically, ignoring case
1309      * differences. This method returns an integer whose sign is that of
1310      * calling {@code compareTo} with normalized versions of the strings
1311      * where case differences have been eliminated by calling
1312      * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1313      * each character.
1314      * <p>
1315      * Note that this method does <em>not</em> take locale into account,
1316      * and will result in an unsatisfactory ordering for certain locales.
1317      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1318      *
1319      * @param   str   the {@code String} to be compared.
1320      * @return  a negative integer, zero, or a positive integer as the