9 package javolution.text;
11 import java.io.IOException;
67 int end = csq.length();
68 if ((end >= start + 5)
69 && (csq.charAt(start) ==
'f' || csq.charAt(start) ==
'F')) {
70 if ((csq.charAt(++start) ==
'a' || csq.charAt(start) ==
'A')
71 && (csq.charAt(++start) ==
'l' || csq.charAt(start) ==
'L')
72 && (csq.charAt(++start) ==
's' || csq.charAt(start) ==
'S')
73 && (csq.charAt(++start) ==
'e' || csq.charAt(start) ==
'E')) {
77 }
else if ((end >= start + 4)
78 && (csq.charAt(start) ==
't' || csq.charAt(start) ==
'T'))
79 if ((csq.charAt(++start) ==
'r' || csq.charAt(start) ==
'R')
80 && (csq.charAt(++start) ==
'u' || csq.charAt(start) ==
'U')
81 && (csq.charAt(++start) ==
'e' || csq.charAt(start) ==
'E')) {
85 throw new IllegalArgumentException(
"Invalid boolean representation");
99 if (!cursor.
atEnd(csq))
100 throw new IllegalArgumentException(
"Extraneous characters \""
101 + cursor.
tail(csq) +
"\"");
117 int i =
parseInt(csq, radix, cursor);
118 if ((i < Byte.MIN_VALUE) || (i > Byte.MAX_VALUE))
119 throw new NumberFormatException(
"Overflow");
134 public static byte parseByte(CharSequence csq,
int radix) {
136 byte result =
parseByte(csq, radix, cursor);
137 if (!cursor.
atEnd(csq))
138 throw new IllegalArgumentException(
"Extraneous characters \""
139 + cursor.
tail(csq) +
"\"");
183 int i =
parseInt(csq, radix, cursor);
184 if ((i < Short.MIN_VALUE) || (i > Short.MAX_VALUE))
185 throw new NumberFormatException(
"Overflow");
200 public static short parseShort(CharSequence csq,
int radix) {
202 short result =
parseShort(csq, radix, cursor);
203 if (!cursor.
atEnd(csq))
204 throw new IllegalArgumentException(
"Extraneous characters \""
205 + cursor.
tail(csq) +
"\"");
250 int end = csq.length();
251 boolean isNegative =
false;
254 for (; i < end; i++) {
255 char c = csq.charAt(i);
256 int digit = (c <=
'9') ? c -
'0'
257 : ((c <=
'Z') && (c >=
'A')) ? c -
'A' + 10
258 : ((c <=
'z') && (c >=
'a')) ? c -
'a' + 10 : -1;
259 if ((digit >= 0) && (digit < radix)) {
260 int newResult = result * radix - digit;
261 if (newResult > result)
262 throw new NumberFormatException(
"Overflow parsing "
263 + csq.subSequence(start, end));
265 }
else if ((c ==
'-') && (i == start))
267 else if ((c ==
'+') && (i == start)) {
273 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) !=
'0')))
274 throw new NumberFormatException(
275 "Invalid integer representation for "
276 + csq.subSequence(start, end));
277 if ((result == Integer.MIN_VALUE) && !isNegative)
278 throw new NumberFormatException(
"Overflow parsing "
279 + csq.subSequence(start, end));
281 return isNegative ? result : -result;
295 public static int parseInt(CharSequence csq,
int radix) {
297 int result =
parseInt(csq, radix, cursor);
298 if (!cursor.
atEnd(csq))
299 throw new IllegalArgumentException(
"Extraneous characters \""
300 + cursor.
tail(csq) +
"\"");
344 final int start = cursor.
getIndex();
345 final int end = csq.length();
346 boolean isNegative =
false;
349 for (; i < end; i++) {
350 char c = csq.charAt(i);
351 int digit = (c <=
'9') ? c -
'0'
352 : ((c <=
'Z') && (c >=
'A')) ? c -
'A' + 10
353 : ((c <=
'z') && (c >=
'a')) ? c -
'a' + 10 : -1;
354 if ((digit >= 0) && (digit < radix)) {
355 long newResult = result * radix - digit;
356 if (newResult > result)
357 throw new NumberFormatException(
"Overflow parsing "
358 + csq.subSequence(start, end));
360 }
else if ((c ==
'-') && (i == start))
362 else if ((c ==
'+') && (i == start)) {
368 if ((result == 0) && ((end == 0) || (csq.charAt(i - 1) !=
'0')))
369 throw new NumberFormatException(
370 "Invalid integer representation for "
371 + csq.subSequence(start, end));
372 if ((result == Long.MIN_VALUE) && !isNegative)
373 throw new NumberFormatException(
"Overflow parsing "
374 + csq.subSequence(start, end));
376 return isNegative ? result : -result;
390 public static long parseLong(CharSequence csq,
int radix) {
392 long result =
parseLong(csq, radix, cursor);
393 if (!cursor.
atEnd(csq))
394 throw new IllegalArgumentException(
"Extraneous characters \""
395 + cursor.
tail(csq) +
"\"");
465 throws NumberFormatException {
466 final int start = cursor.getIndex();
467 final int end = csq.length();
469 char c = csq.charAt(i);
472 if ((c ==
'N') &&
match(
"NaN", csq, i, end)) {
478 boolean isNegative = (c ==
'-');
479 if ((isNegative || (c ==
'+')) && (++i < end))
483 if ((c ==
'I') &&
match(
"Infinity", csq, i, end)) {
484 cursor.increment(i + 8 - start);
485 return isNegative ? Double.NEGATIVE_INFINITY
486 : Double.POSITIVE_INFINITY;
490 if (((c <
'0') || (c >
'9')) && (c !=
'.'))
491 throw new NumberFormatException(
"Digit or '.' required");
495 int decimalPoint = -1;
498 if ((digit >= 0) && (digit < 10)) {
499 long tmp = decimal * 10 + digit;
501 throw new NumberFormatException(
502 "Too many digits - Overflow");
504 }
else if ((c ==
'.') && (decimalPoint < 0))
514 int fractionLength = (decimalPoint >= 0) ? i - decimalPoint - 1 : 0;
518 if ((i < end) && ((c ==
'E') || (c ==
'e'))) {
520 boolean isNegativeExp = (c ==
'-');
521 if ((isNegativeExp || (c ==
'+')) && (++i < end))
523 if ((c <
'0') || (c >
'9'))
524 throw new NumberFormatException(
"Invalid exponent");
527 if ((digit >= 0) && (digit < 10)) {
528 int tmp = exp * 10 + digit;
530 throw new NumberFormatException(
"Exponent Overflow");
541 cursor.increment(i - start);
560 throws NumberFormatException {
563 if (!cursor.
atEnd(csq))
564 throw new IllegalArgumentException(
"Extraneous characters \""
565 + cursor.
tail(csq) +
"\"");
569 static boolean match(String str, CharSequence csq,
int start,
int length) {
570 for (
int i = 0; i < str.length(); i++) {
571 if ((start + i >= length) || csq.charAt(start + i) != str.charAt(i))
577 static boolean match(String str, String csq,
int start,
int length) {
578 for (
int i = 0; i < str.length(); i++) {
579 if ((start + i >= length) || csq.charAt(start + i) != str.charAt(i))
597 public static Appendable
format(
boolean b, Appendable a)
throws IOException {
598 return b ? a.append(
"true") : a.append(
"false");
611 public static Appendable
format(
int i, Appendable a)
throws IOException {
630 public static Appendable
format(
int i,
int radix, Appendable a)
649 public static Appendable
format(
long l, Appendable a)
throws IOException {
669 public static Appendable
format(
long l,
int radix, Appendable a)
686 public static Appendable
format(
float f, Appendable a)
throws IOException {
700 public static Appendable
format(
double d, Appendable a)
throws IOException {
723 public static Appendable
format(
double d,
int digits,
boolean scientific,
724 boolean showZero, Appendable a)
throws IOException {
726 return ((
TextBuilder) a).append(d, digits, scientific, showZero);
728 tb.
append(d, digits, scientific, showZero);