summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java
blob: 8d0715dd7f3280ff295b23277c99415fd6eff3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bjc.utils.funcutils;

import java.util.regex.Pattern;

/*
 * Checks if a string would pass Double.parseDouble.
 *
 * Uses a regex from the javadoc for Double.valueOf()
 */
class DoubleMatcher {
	private static final String	Digits		= "(\\p{Digit}+)";
	private static final String	HexDigits	= "(\\p{XDigit}+)";

	/*
	 * an exponent is 'e' or 'E' followed by an optionally signed decimal
	 * integer.
	 */
	private static final String Exp = "[eE][+-]?" + Digits;

	private static final String fpRegex = "[\\x00-\\x20]*" + // Optional
									// leading
									// "whitespace"
			"[+-]?(" + // Optional sign character
			"NaN|" + // "NaN" string
			"Infinity|" + // "Infinity" string

			/*
			 * A decimal floating-point string representing a finite
			 * positive number without a leading sign has at most
			 * five basic pieces: Digits . Digits ExponentPart
			 * FloatTypeSuffix
			 *
			 * Since this method allows integer-only strings as
			 * input in addition to strings of floating-point
			 * literals, the two sub-patterns below are
			 * simplifications of the grammar productions from
			 * section 3.10.2 of The Java™ Language Specification.
			 */

			/*
			 * Digits ._opt Digits_opt ExponentPart_opt
			 * FloatTypeSuffix_opt
			 */
			"(((" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" +

			/*
			 * . Digits ExponentPart_opt FloatTypeSuffix_opt
			 */
			"(\\.(" + Digits + ")(" + Exp + ")?)|" +

			/*
			 * Hexadecimal strings
			 */
			"((" +
			/*
			 * 0[xX] HexDigits ._opt BinaryExponent
			 * FloatTypeSuffix_opt
			 */
			"(0[xX]" + HexDigits + "(\\.)?)|" +

			/*
			 * 0[xX] HexDigits_opt . HexDigits BinaryExponent
			 * FloatTypeSuffix_opt
			 */
			"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +

			")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*"; // Optional
											// trailing
											// "whitespace"

	public static final Pattern floatingLiteral = Pattern.compile("\\A" + fpRegex + "\\Z");
}