01: package uk.org.ponder.stringutil;
02:
03: /** A useful class which quickly parses numbers from character arrays rather than
04: * from strings as the slovenly Java libraries do.
05: */
06:
07: public class CharParser {
08: private static final int MAXO10 = Integer.MAX_VALUE / 10; // 214748364
09: private static final int MAXO16 = Integer.MAX_VALUE / 16; // 134217727
10:
11: /** Parses a positive integer from the contents of a character array. The
12: * specified section of the array must contain no non-numeric characters.
13: * @param buffer The array containing the data to be parsed.
14: * @param start The start position of the numeric data.
15: * @param length The length of the numeric data.
16: * @exception NumberFormatException If the specified array section includes
17: * non-numeric characters or represents an integer out of range for a positive
18: * signed 32-bit integer.
19: */
20:
21: public static int parsePositiveInt(char[] buffer, int start,
22: int length) throws NumberFormatException {
23: int accumulate = 0;
24: for (int i = 0; i < length; ++i) {
25: char c = buffer[i + start];
26: if (c < '0' || c > '9')
27: throw new NumberFormatException("Invalid character "
28: + c + " in positive integer");
29: if (accumulate > MAXO10)
30: throw new NumberFormatException(
31: "Number found greater than maximum integer: "
32: + new String(buffer, start, length));
33: accumulate *= 10;
34: int uncess = Integer.MAX_VALUE - accumulate;
35: int digit = (c - '0');
36: if (digit > uncess)
37: throw new NumberFormatException(
38: "Number found greater than maximum integer: "
39: + new String(buffer, start, length));
40: accumulate += (c - '0');
41: }
42: return accumulate;
43: }
44:
45: /** Parses a single hex digit as its integer equivalent.
46: * @return The integer corresponding to the supplied hex digit, or -1 if the
47: * character is not a valid hex digit.
48: */
49:
50: public static int fromHex(char digit) {
51: digit = Character.toUpperCase(digit);
52: if (digit <= '9' && digit >= '0')
53: return digit - '0';
54: else if (digit <= 'F' && digit >= 'A')
55: return 10 + digit - 'A';
56: else
57: return -1;
58: }
59:
60: /** Parses a positive integer stored as hexadecimal from the contents of a
61: * character array. The specified section of the array must contain no non-hexadecimal
62: * characters.
63: * @param buffer The array containing the data to be parsed.
64: * @param start The start position of the numeric data.
65: * @param length The length of the numeric data.
66: * @exception NumberFormatException If the specified array section includes
67: * non-hexadecimal characters or represents an integer out of range for a positive
68: * signed 32-bit integer.
69: */
70:
71: public static int parseHexInt(char[] buffer, int start, int length)
72: throws NumberFormatException {
73: int accumulate = 0;
74: for (int i = 0; i < length; ++i) {
75: char c = buffer[i + start];
76: int digit = fromHex(c);
77: if (digit == -1)
78: throw new NumberFormatException("Invalid character "
79: + c + " in hex string");
80: if (accumulate > MAXO16)
81: throw new NumberFormatException(
82: "Number found greater than maximum integer: "
83: + new String(buffer, start, length));
84: accumulate *= 16;
85: int uncess = Integer.MAX_VALUE - accumulate;
86: if (digit > uncess)
87: throw new NumberFormatException(
88: "Number found greater than maximum integer: "
89: + new String(buffer, start, length));
90: accumulate += digit;
91: }
92: return accumulate;
93: }
94: }
|