Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/AsciiUtil.java
38918 views
1
/*
2
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
*******************************************************************************
28
* Copyright (C) 2009, International Business Machines Corporation and *
29
* others. All Rights Reserved. *
30
*******************************************************************************
31
*/
32
package sun.util.locale;
33
34
public final class AsciiUtil {
35
public static boolean caseIgnoreMatch(String s1, String s2) {
36
if (s1 == s2) {
37
return true;
38
}
39
int len = s1.length();
40
if (len != s2.length()) {
41
return false;
42
}
43
int i = 0;
44
while (i < len) {
45
char c1 = s1.charAt(i);
46
char c2 = s2.charAt(i);
47
if (c1 != c2 && toLower(c1) != toLower(c2)) {
48
break;
49
}
50
i++;
51
}
52
return (i == len);
53
}
54
55
public static int caseIgnoreCompare(String s1, String s2) {
56
if (s1 == s2) {
57
return 0;
58
}
59
return AsciiUtil.toLowerString(s1).compareTo(AsciiUtil.toLowerString(s2));
60
}
61
62
63
public static char toUpper(char c) {
64
if (c >= 'a' && c <= 'z') {
65
c -= 0x20;
66
}
67
return c;
68
}
69
70
public static char toLower(char c) {
71
if (c >= 'A' && c <= 'Z') {
72
c += 0x20;
73
}
74
return c;
75
}
76
77
public static String toLowerString(String s) {
78
int idx = 0;
79
for (; idx < s.length(); idx++) {
80
char c = s.charAt(idx);
81
if (c >= 'A' && c <= 'Z') {
82
break;
83
}
84
}
85
if (idx == s.length()) {
86
return s;
87
}
88
StringBuilder buf = new StringBuilder(s.substring(0, idx));
89
for (; idx < s.length(); idx++) {
90
buf.append(toLower(s.charAt(idx)));
91
}
92
return buf.toString();
93
}
94
95
public static String toUpperString(String s) {
96
int idx = 0;
97
for (; idx < s.length(); idx++) {
98
char c = s.charAt(idx);
99
if (c >= 'a' && c <= 'z') {
100
break;
101
}
102
}
103
if (idx == s.length()) {
104
return s;
105
}
106
StringBuilder buf = new StringBuilder(s.substring(0, idx));
107
for (; idx < s.length(); idx++) {
108
buf.append(toUpper(s.charAt(idx)));
109
}
110
return buf.toString();
111
}
112
113
public static String toTitleString(String s) {
114
if (s.length() == 0) {
115
return s;
116
}
117
int idx = 0;
118
char c = s.charAt(idx);
119
if (!(c >= 'a' && c <= 'z')) {
120
for (idx = 1; idx < s.length(); idx++) {
121
if (c >= 'A' && c <= 'Z') {
122
break;
123
}
124
}
125
}
126
if (idx == s.length()) {
127
return s;
128
}
129
StringBuilder buf = new StringBuilder(s.substring(0, idx));
130
if (idx == 0) {
131
buf.append(toUpper(s.charAt(idx)));
132
idx++;
133
}
134
for (; idx < s.length(); idx++) {
135
buf.append(toLower(s.charAt(idx)));
136
}
137
return buf.toString();
138
}
139
140
public static boolean isAlpha(char c) {
141
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
142
}
143
144
public static boolean isAlphaString(String s) {
145
boolean b = true;
146
for (int i = 0; i < s.length(); i++) {
147
if (!isAlpha(s.charAt(i))) {
148
b = false;
149
break;
150
}
151
}
152
return b;
153
}
154
155
public static boolean isNumeric(char c) {
156
return (c >= '0' && c <= '9');
157
}
158
159
public static boolean isNumericString(String s) {
160
boolean b = true;
161
for (int i = 0; i < s.length(); i++) {
162
if (!isNumeric(s.charAt(i))) {
163
b = false;
164
break;
165
}
166
}
167
return b;
168
}
169
170
public static boolean isAlphaNumeric(char c) {
171
return isAlpha(c) || isNumeric(c);
172
}
173
174
public static boolean isAlphaNumericString(String s) {
175
boolean b = true;
176
for (int i = 0; i < s.length(); i++) {
177
if (!isAlphaNumeric(s.charAt(i))) {
178
b = false;
179
break;
180
}
181
}
182
return b;
183
}
184
185
public static class CaseInsensitiveKey {
186
private String _key;
187
private int _hash;
188
189
public CaseInsensitiveKey(String key) {
190
_key = key;
191
_hash = AsciiUtil.toLowerString(key).hashCode();
192
}
193
194
public boolean equals(Object o) {
195
if (this == o) {
196
return true;
197
}
198
if (o instanceof CaseInsensitiveKey) {
199
return AsciiUtil.caseIgnoreMatch(_key, ((CaseInsensitiveKey)o)._key);
200
}
201
return false;
202
}
203
204
public int hashCode() {
205
return _hash;
206
}
207
}
208
}
209
210