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