1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2009-2010, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.impl.locale; 10 11 import com.ibm.icu.impl.Utility; 12 13 public final class AsciiUtil { 14 public static boolean caseIgnoreMatch(String s1, String s2) { 15 if (Utility.sameObjects(s1, s2)) { 16 return true; 17 } 18 int len = s1.length(); 19 if (len != s2.length()) { 20 return false; 21 } 22 int i = 0; 23 while (i < len) { 24 char c1 = s1.charAt(i); 25 char c2 = s2.charAt(i); 26 if (c1 != c2 && toLower(c1) != toLower(c2)) { 27 break; 28 } 29 i++; 30 } 31 return (i == len); 32 } 33 34 public static int caseIgnoreCompare(String s1, String s2) { 35 if (Utility.sameObjects(s1, s2)) { 36 return 0; 37 } 38 return AsciiUtil.toLowerString(s1).compareTo(AsciiUtil.toLowerString(s2)); 39 } 40 41 42 public static char toUpper(char c) { 43 if (c >= 'a' && c <= 'z') { 44 c -= 0x20; 45 } 46 return c; 47 } 48 49 public static char toLower(char c) { 50 if (c >= 'A' && c <= 'Z') { 51 c += 0x20; 52 } 53 return c; 54 } 55 56 public static String toLowerString(String s) { 57 int idx = 0; 58 for (; idx < s.length(); idx++) { 59 char c = s.charAt(idx); 60 if (c >= 'A' && c <= 'Z') { 61 break; 62 } 63 } 64 if (idx == s.length()) { 65 return s; 66 } 67 StringBuilder buf = new StringBuilder(s.substring(0, idx)); 68 for (; idx < s.length(); idx++) { 69 buf.append(toLower(s.charAt(idx))); 70 } 71 return buf.toString(); 72 } 73 74 public static String toUpperString(String s) { 75 int idx = 0; 76 for (; idx < s.length(); idx++) { 77 char c = s.charAt(idx); 78 if (c >= 'a' && c <= 'z') { 79 break; 80 } 81 } 82 if (idx == s.length()) { 83 return s; 84 } 85 StringBuilder buf = new StringBuilder(s.substring(0, idx)); 86 for (; idx < s.length(); idx++) { 87 buf.append(toUpper(s.charAt(idx))); 88 } 89 return buf.toString(); 90 } 91 92 public static String toTitleString(String s) { 93 if (s.length() == 0) { 94 return s; 95 } 96 int idx = 0; 97 char c = s.charAt(idx); 98 if (!(c >= 'a' && c <= 'z')) { 99 for (idx = 1; idx < s.length(); idx++) { 100 if (c >= 'A' && c <= 'Z') { 101 break; 102 } 103 } 104 } 105 if (idx == s.length()) { 106 return s; 107 } 108 StringBuilder buf = new StringBuilder(s.substring(0, idx)); 109 if (idx == 0) { 110 buf.append(toUpper(s.charAt(idx))); 111 idx++; 112 } 113 for (; idx < s.length(); idx++) { 114 buf.append(toLower(s.charAt(idx))); 115 } 116 return buf.toString(); 117 } 118 119 public static boolean isAlpha(char c) { 120 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); 121 } 122 123 public static boolean isAlphaString(String s) { 124 boolean b = true; 125 for (int i = 0; i < s.length(); i++) { 126 if (!isAlpha(s.charAt(i))) { 127 b = false; 128 break; 129 } 130 } 131 return b; 132 } 133 134 public static boolean isNumeric(char c) { 135 return (c >= '0' && c <= '9'); 136 } 137 138 public static boolean isNumericString(String s) { 139 boolean b = true; 140 for (int i = 0; i < s.length(); i++) { 141 if (!isNumeric(s.charAt(i))) { 142 b = false; 143 break; 144 } 145 } 146 return b; 147 } 148 149 public static boolean isAlphaNumeric(char c) { 150 return isAlpha(c) || isNumeric(c); 151 } 152 153 public static boolean isAlphaNumericString(String s) { 154 boolean b = true; 155 for (int i = 0; i < s.length(); i++) { 156 if (!isAlphaNumeric(s.charAt(i))) { 157 b = false; 158 break; 159 } 160 } 161 return b; 162 } 163 164 public static class CaseInsensitiveKey { 165 private String _key; 166 private int _hash; 167 168 public CaseInsensitiveKey(String key) { 169 _key = key; 170 _hash = AsciiUtil.toLowerString(key).hashCode(); 171 } 172 173 @Override 174 public boolean equals(Object o) { 175 if (this == o) { 176 return true; 177 } 178 if (o instanceof CaseInsensitiveKey) { 179 return AsciiUtil.caseIgnoreMatch(_key, ((CaseInsensitiveKey)o)._key); 180 } 181 return false; 182 } 183 184 @Override 185 public int hashCode() { 186 return _hash; 187 } 188 } 189 } 190