NumberParser assignNatural()

in src/main/java/org/apache/commons/jexl3/parser/NumberParser.java [59:112]


    NumberParser assignNatural(final boolean negative, final String natural) {
        String s = natural;
        Number result;
        Class<? extends Number> rclass;
        // determine the base
        final int base;
        if (s.charAt(0) == '0') {
            if (s.length() > 1 && (s.charAt(1) == 'x' || s.charAt(1) == 'X')) {
                base = 16;
                s = s.substring(2); // Trim the 0x off the front
            } else {
                base = 8;
            }
        } else {
            base = 10;
        }
        // switch on suffix if any
        final int last = s.length() - 1;
        switch (s.charAt(last)) {
            case 'l':
            case 'L': {
                rclass = Long.class;
                final long l = Long.parseLong(s.substring(0, last), base);
                result = negative? -l : l;
                break;
            }
            case 'h':
            case 'H': {
                rclass = BigInteger.class;
                final BigInteger bi = new BigInteger(s.substring(0, last), base);
                result = negative? bi.negate() : bi;
                break;
            }
            default: {
                // preferred literal class is integer
                rclass = Integer.class;
                try {
                    final int i = Integer.parseInt(s, base);
                    result = negative? -i : i;
                } catch (final NumberFormatException take2) {
                    try {
                        final long l = Long.parseLong(s, base);
                        result = negative? -l : l;
                    } catch (final NumberFormatException take3) {
                        final BigInteger bi = new BigInteger(s, base);
                        result = negative? bi.negate() : bi;
                    }
                }
            }
        }
        literal = result;
        clazz = rclass;
        return this;
    }