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