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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package Torello.Java;

import java.util.regex.Pattern;

class IsOfPrimitiveType 
{
    // ********************************************************************************************
    // ********************************************************************************************
    // Char-Arrays for 'chec'
    // ********************************************************************************************
    // ********************************************************************************************


    // The minimum value for the byte primitive type, without the minus sign.
    static final char[] BYTE_MIN_VALUE_DIGITS_AS_CHARS = { '2', '5', '6' };

    // The minimum value for the short primitive type, without the minus sign.
    static final char[] SHORT_MIN_VALUE_DIGITS_AS_CHARS = { '6', '5', '5', '3', '6' };

    // The minimum value for the int primitive type, without the minus sign.
    static final char[] INT_MIN_VALUE_DIGITS_AS_CHARS =
    { '2', '1', '4', '7', '4', '8', '3', '6', '4', '8' };

    // The minimum value for the long primitive type, without the minus sign.
    static final char[] LONG_MIN_VALUE_DIGITS_AS_CHARS =
    {
        '2', '1', '4', '9', '2', '2', '3', '3', '7', '2', '0', '3', '6', '8', '5', '4', 
        '7', '7', '5', '8', '0', '8'
    };


    // ********************************************************************************************
    // ********************************************************************************************
    // boolean check(String, char[])
    // ********************************************************************************************
    // ********************************************************************************************


    static boolean check(String s, char[] minArr)
    {
        int length = s.length();

        // Zero length string's are not valid integers.
        if (length == 0)                                    return false;

        // A negative integer may begin with a minus-sign.
        boolean negative = s.charAt(0) == '-';


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // If the string is too short or too long, this method doesn't need to do any work.
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // 
        // We either know the answer immediately (too long), or we can call the simpler method
        // (in the case that it is too short)
        //
        // If a string is shorter than (for type 'int', for example): 2147483647 (10 chars)
        // then we ought use the simplified method which just checks if the string is an integer.

        if (length < minArr.length) return isInteger(s);


        // If the string is longer than (for type 'int', for example): -2147483648 (11 chars)
        // then it cannot be an integer that fits into primitive 'int', so return false.

        if (length > (minArr.length + 1)) return false;


        // If the String is *EXACTLY* 11 characters long (for primitive-type 'int', for example),
        // but doesn't begin with a negative sign, we also know the answer immediately.

        if ((!negative) && (length == (minArr.length + 1))) return false;


        // If the String *EXACTLY* the length of MAX_NUUMBER, but it begins with a negative sign,
        // we can call the simplified method, instead as well.

        if (negative && (length == minArr.length)) return isInteger(s);


        // The **REST** of the code is only executed if the numeric part of the String
        // (Specifically: leaving out the '-' negative sign, which may or may not be present)
        // ... if the numeric part of the String is precisely the length of MAX_VALUE / MAX_NUMBER
        // as determined by the length of the array 'minArr'...  If the input string is
        // **PRECISELY** that length, then the string must be checked in the loop below. 

        int     i                       = negative ? 1 : 0;
        int     j                       = 0;
        boolean guaranteedFitIfInteger  = false;
        char    c                       = 0;

        while (i < length)
        {
            c = s.charAt(i);

            if (! guaranteedFitIfInteger)
            {
                if (c > minArr[j]) return false;
                if (c < minArr[j]) guaranteedFitIfInteger = true;
            }

            if (c < '0') return false;
            if (c > '9') return false;

            i++; j++;
        }


        // THE COMMENT BELOW DELINEATES WHAT HAPPENS FOR THE INPUT-CASE OF PRIMITIVE-TYPE 'INT'
        // (2147483648)... But it generalizes for byte, short, and long as well.
        // 
        // This might seem very strange.  Since the MIN_VALUE ends with an '8', but the
        // MAX_VALUE ends with a '7', and since we are checking each character to see that
        // it falls within the array above, **RATHER THAN** just returning TRUE right here,
        // we have to catch the **LONE** border/edge case where some joker actually passed the
        // String 2147483648 - which must return FALSE, since the last positive integer is
        // 2147483647 (see that it has an ending of '7', rather than an '8').

        return guaranteedFitIfInteger || negative || (c != minArr[minArr.length-1]);
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // boolean isInteger(String s)
    // ********************************************************************************************
    // ********************************************************************************************


    static boolean isInteger(String s)
    {
        if (s == null) return false;

        int length = s.length();

        if (length == 0) return false;

        int i = 0;

        if (s.charAt(0) == '-')
        {
            if (length == 1) return false;
            i = 1;
        }

        while (i < length)
        {
            char c = s.charAt(i++);
            if (c < '0' || c > '9') return false;
        }

        return true;
    }


    // ********************************************************************************************
    // ********************************************************************************************
    // Pattern FLOATING_POINT_REGEX
    // ********************************************************************************************
    // ********************************************************************************************


    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;

    static final Pattern FLOATING_POINT_REGEX = Pattern.compile(
        // NOTE: Digits, HexDigits & Exp defined ABOVE

        "[\\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";
    );

}