001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.tomcat.util.buf;
019:
020: /**
021: * This class implements some basic ASCII character handling functions.
022: *
023: * @author dac@eng.sun.com
024: * @author James Todd [gonzo@eng.sun.com]
025: */
026: public final class Ascii {
027: /*
028: * Character translation tables.
029: */
030:
031: private static final byte[] toUpper = new byte[256];
032: private static final byte[] toLower = new byte[256];
033:
034: /*
035: * Character type tables.
036: */
037:
038: private static final boolean[] isAlpha = new boolean[256];
039: private static final boolean[] isUpper = new boolean[256];
040: private static final boolean[] isLower = new boolean[256];
041: private static final boolean[] isWhite = new boolean[256];
042: private static final boolean[] isDigit = new boolean[256];
043:
044: /*
045: * Initialize character translation and type tables.
046: */
047:
048: static {
049: for (int i = 0; i < 256; i++) {
050: toUpper[i] = (byte) i;
051: toLower[i] = (byte) i;
052: }
053:
054: for (int lc = 'a'; lc <= 'z'; lc++) {
055: int uc = lc + 'A' - 'a';
056:
057: toUpper[lc] = (byte) uc;
058: toLower[uc] = (byte) lc;
059: isAlpha[lc] = true;
060: isAlpha[uc] = true;
061: isLower[lc] = true;
062: isUpper[uc] = true;
063: }
064:
065: isWhite[' '] = true;
066: isWhite['\t'] = true;
067: isWhite['\r'] = true;
068: isWhite['\n'] = true;
069: isWhite['\f'] = true;
070: isWhite['\b'] = true;
071:
072: for (int d = '0'; d <= '9'; d++) {
073: isDigit[d] = true;
074: }
075: }
076:
077: /**
078: * Returns the upper case equivalent of the specified ASCII character.
079: */
080:
081: public static int toUpper(int c) {
082: return toUpper[c & 0xff] & 0xff;
083: }
084:
085: /**
086: * Returns the lower case equivalent of the specified ASCII character.
087: */
088:
089: public static int toLower(int c) {
090: return toLower[c & 0xff] & 0xff;
091: }
092:
093: /**
094: * Returns true if the specified ASCII character is upper or lower case.
095: */
096:
097: public static boolean isAlpha(int c) {
098: return isAlpha[c & 0xff];
099: }
100:
101: /**
102: * Returns true if the specified ASCII character is upper case.
103: */
104:
105: public static boolean isUpper(int c) {
106: return isUpper[c & 0xff];
107: }
108:
109: /**
110: * Returns true if the specified ASCII character is lower case.
111: */
112:
113: public static boolean isLower(int c) {
114: return isLower[c & 0xff];
115: }
116:
117: /**
118: * Returns true if the specified ASCII character is white space.
119: */
120:
121: public static boolean isWhite(int c) {
122: return isWhite[c & 0xff];
123: }
124:
125: /**
126: * Returns true if the specified ASCII character is a digit.
127: */
128:
129: public static boolean isDigit(int c) {
130: return isDigit[c & 0xff];
131: }
132:
133: /**
134: * Parses an unsigned integer from the specified subarray of bytes.
135: * @param b the bytes to parse
136: * @param off the start offset of the bytes
137: * @param len the length of the bytes
138: * @exception NumberFormatException if the integer format was invalid
139: */
140: public static int parseInt(byte[] b, int off, int len)
141: throws NumberFormatException {
142: int c;
143:
144: if (b == null || len <= 0 || !isDigit(c = b[off++])) {
145: throw new NumberFormatException();
146: }
147:
148: int n = c - '0';
149:
150: while (--len > 0) {
151: if (!isDigit(c = b[off++])) {
152: throw new NumberFormatException();
153: }
154: n = n * 10 + c - '0';
155: }
156:
157: return n;
158: }
159:
160: public static int parseInt(char[] b, int off, int len)
161: throws NumberFormatException {
162: int c;
163:
164: if (b == null || len <= 0 || !isDigit(c = b[off++])) {
165: throw new NumberFormatException();
166: }
167:
168: int n = c - '0';
169:
170: while (--len > 0) {
171: if (!isDigit(c = b[off++])) {
172: throw new NumberFormatException();
173: }
174: n = n * 10 + c - '0';
175: }
176:
177: return n;
178: }
179:
180: /**
181: * Parses an unsigned long from the specified subarray of bytes.
182: * @param b the bytes to parse
183: * @param off the start offset of the bytes
184: * @param len the length of the bytes
185: * @exception NumberFormatException if the long format was invalid
186: */
187: public static long parseLong(byte[] b, int off, int len)
188: throws NumberFormatException {
189: int c;
190:
191: if (b == null || len <= 0 || !isDigit(c = b[off++])) {
192: throw new NumberFormatException();
193: }
194:
195: long n = c - '0';
196: long m;
197:
198: while (--len > 0) {
199: if (!isDigit(c = b[off++])) {
200: throw new NumberFormatException();
201: }
202: m = n * 10 + c - '0';
203:
204: if (m < n) {
205: // Overflow
206: throw new NumberFormatException();
207: } else {
208: n = m;
209: }
210: }
211:
212: return n;
213: }
214:
215: public static long parseLong(char[] b, int off, int len)
216: throws NumberFormatException {
217: int c;
218:
219: if (b == null || len <= 0 || !isDigit(c = b[off++])) {
220: throw new NumberFormatException();
221: }
222:
223: long n = c - '0';
224: long m;
225:
226: while (--len > 0) {
227: if (!isDigit(c = b[off++])) {
228: throw new NumberFormatException();
229: }
230: m = n * 10 + c - '0';
231:
232: if (m < n) {
233: // Overflow
234: throw new NumberFormatException();
235: } else {
236: n = m;
237: }
238: }
239:
240: return n;
241: }
242:
243: }
|