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