2 *******************************************************************************
3 * Copyright (C) 2009-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl.locale;
9 public final class AsciiUtil {
10 public static boolean caseIgnoreMatch(String s1, String s2) {
14 int len = s1.length();
15 if (len != s2.length()) {
20 char c1 = s1.charAt(i);
21 char c2 = s2.charAt(i);
22 if (c1 != c2 && toLower(c1) != toLower(c2)) {
30 public static int caseIgnoreCompare(String s1, String s2) {
34 return AsciiUtil.toLowerString(s1).compareTo(AsciiUtil.toLowerString(s2));
38 public static char toUpper(char c) {
39 if (c >= 'a' && c <= 'z') {
45 public static char toLower(char c) {
46 if (c >= 'A' && c <= 'Z') {
52 public static String toLowerString(String s) {
54 for (; idx < s.length(); idx++) {
55 char c = s.charAt(idx);
56 if (c >= 'A' && c <= 'Z') {
60 if (idx == s.length()) {
63 StringBuilder buf = new StringBuilder(s.substring(0, idx));
64 for (; idx < s.length(); idx++) {
65 buf.append(toLower(s.charAt(idx)));
67 return buf.toString();
70 public static String toUpperString(String s) {
72 for (; idx < s.length(); idx++) {
73 char c = s.charAt(idx);
74 if (c >= 'a' && c <= 'z') {
78 if (idx == s.length()) {
81 StringBuilder buf = new StringBuilder(s.substring(0, idx));
82 for (; idx < s.length(); idx++) {
83 buf.append(toUpper(s.charAt(idx)));
85 return buf.toString();
88 public static String toTitleString(String s) {
89 if (s.length() == 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') {
101 if (idx == s.length()) {
104 StringBuilder buf = new StringBuilder(s.substring(0, idx));
106 buf.append(toUpper(s.charAt(idx)));
109 for (; idx < s.length(); idx++) {
110 buf.append(toLower(s.charAt(idx)));
112 return buf.toString();
115 public static boolean isAlpha(char c) {
116 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
119 public static boolean isAlphaString(String s) {
121 for (int i = 0; i < s.length(); i++) {
122 if (!isAlpha(s.charAt(i))) {
130 public static boolean isNumeric(char c) {
131 return (c >= '0' && c <= '9');
134 public static boolean isNumericString(String s) {
136 for (int i = 0; i < s.length(); i++) {
137 if (!isNumeric(s.charAt(i))) {
145 public static boolean isAlphaNumeric(char c) {
146 return isAlpha(c) || isNumeric(c);
149 public static boolean isAlphaNumericString(String s) {
151 for (int i = 0; i < s.length(); i++) {
152 if (!isAlphaNumeric(s.charAt(i))) {
160 public static class CaseInsensitiveKey {
164 public CaseInsensitiveKey(String key) {
166 _hash = AsciiUtil.toLowerString(key).hashCode();
169 public boolean equals(Object o) {
173 if (o instanceof CaseInsensitiveKey) {
174 return AsciiUtil.caseIgnoreMatch(_key, ((CaseInsensitiveKey)o)._key);
179 public int hashCode() {