@Test public void testParseString() { testParseString(getidt(3, false), "", 7, true); testParseString(getidt(3, false), "abc", 7, true); testParseString(getidt(3, false), "0b+111", 7, true); testParseString(getidt(3, false), "0b-111", 7, true); testParseString(getidt(3, false), "0b111", 7, false); testParseString(getidt(3, false), "+0B111", 7, false); testParseString(getidt(4, true), "-0b111", -7, false); testParseString(getidt(3, true), "0b111", 0, true); testParseString(getidt(3, false), "-0b111", 7, true); testParseString(getidt(64, false), "0b"+Long.toBinaryString(0xFFFFFFFA_FFFFFFFFl), 0xFFFFFFFA_FFFFFFFFl, false); testParseString(getidt(32, true), "10", 10, false); testParseString(getidt(32, true), "-10", -10, false); testParseString(getidt(32, false), "-10", 0, true); testParseString(getidt(32, false), "0xFFFFFFFF", 0xffffffffL, false); testParseString(getidt(64, false), "0XFFFFFFFF_FFFFFFFF", 0xffffffff_ffffffffL, false); testParseString(getidt(64, true), "0xFFFFFFFF_FFFFFFFF", 0xffffffff_ffffffffL, true); testParseString(getidt(10, true), "0o8", 0, true); testParseString(getidt(10, true), "0o7", 7, false); testParseString(getidt(10, true), "0O77", 7*8+7, false); testParseString(getidt(32, false), "3735928559", 3735928559L, false); }
@Override public Long parseString(String stringValue) { String sv = stringValue.replace("_", ""); if (sv.length() == 0) throw new NumberFormatException("Zero length string"); int off = 0; char sv0 = sv.charAt(0); boolean negative = false; int radix = 10; if (sv0 == '-') { if (!signed) { throw new NumberFormatException("negative number specified for unsigned integer"); } negative = true; off++; } else if (sv0 == '+') { off++; } if (sv.startsWith("0b", off) || sv.startsWith("0B", off)) { off += 2; radix = 2; } else if (sv.startsWith("0o", off) || sv.startsWith("0O", off)) { off += 2; radix = 8; } else if (sv.startsWith("0x", off) || sv.startsWith("0X", off)) { off += 2; radix = 16; } if (sv.startsWith("-", off) || sv.startsWith("+", off)) throw new NumberFormatException("Sign character in the middle of the number"); BigInteger bn = new BigInteger(sv.substring(off), radix); int bs = sizeInBits; if (signed) { bs--; } if (bn.bitLength() > bs) { throw new NumberFormatException("Number " + stringValue + " does not fit the bit size (" + sizeInBits + (signed ? "/signed" : "unsigned") + ")"); } long x = bn.longValue(); if (negative) { x = -x; } return x; }
IntegerDataType extends NumericDataType { @Override public Long parseString(String stringValue) { String sv = stringValue.replace("_", ""); if (sv.length() == 0) throw new NumberFormatException("Zero length string"); int off = 0; char sv0 = sv.charAt(0); boolean negative = false; int radix = 10; if (sv0 == '-') { if (!signed) { throw new NumberFormatException("negative number specified for unsigned integer"); } negative = true; off++; } else if (sv0 == '+') { off++; } if (sv.startsWith("0b", off) || sv.startsWith("0B", off)) { off += 2; radix = 2; } else if (sv.startsWith("0o", off) || sv.startsWith("0O", off)) { off += 2; radix = 8; } else if (sv.startsWith("0x", off) || sv.startsWith("0X", off)) { off += 2; radix = 16; } if (sv.startsWith("-", off) || sv.startsWith("+", off)) throw new NumberFormatException("Sign character in the middle of the number"); BigInteger bn = new BigInteger(sv.substring(off), radix); int bs = sizeInBits; if (signed) { bs--; } if (bn.bitLength() > bs) { throw new NumberFormatException("Number " + stringValue + " does not fit the bit size (" + sizeInBits + (signed ? "/signed" : "unsigned") + ")"); } long x = bn.longValue(); if (negative) { x = -x; } return x; } }
IntegerDataType extends NumericDataType { @Override public Long parseString(String stringValue) { String sv = stringValue.replace("_", ""); if (sv.length() == 0) throw new NumberFormatException("Zero length string"); int off = 0; char sv0 = sv.charAt(0); boolean negative = false; int radix = 10; if (sv0 == '-') { if (!signed) { throw new NumberFormatException("negative number specified for unsigned integer"); } negative = true; off++; } else if (sv0 == '+') { off++; } if (sv.startsWith("0b", off) || sv.startsWith("0B", off)) { off += 2; radix = 2; } else if (sv.startsWith("0o", off) || sv.startsWith("0O", off)) { off += 2; radix = 8; } else if (sv.startsWith("0x", off) || sv.startsWith("0X", off)) { off += 2; radix = 16; } if (sv.startsWith("-", off) || sv.startsWith("+", off)) throw new NumberFormatException("Sign character in the middle of the number"); BigInteger bn = new BigInteger(sv.substring(off), radix); int bs = sizeInBits; if (signed) { bs--; } if (bn.bitLength() > bs) { throw new NumberFormatException("Number " + stringValue + " does not fit the bit size (" + sizeInBits + (signed ? "/signed" : "unsigned") + ")"); } long x = bn.longValue(); if (negative) { x = -x; } return x; } protected  IntegerDataType(Builder<?> builder); protected  IntegerDataType(IntegerDataType t); }
IntegerDataType extends NumericDataType { @Override public Long parseString(String stringValue) { String sv = stringValue.replace("_", ""); if (sv.length() == 0) throw new NumberFormatException("Zero length string"); int off = 0; char sv0 = sv.charAt(0); boolean negative = false; int radix = 10; if (sv0 == '-') { if (!signed) { throw new NumberFormatException("negative number specified for unsigned integer"); } negative = true; off++; } else if (sv0 == '+') { off++; } if (sv.startsWith("0b", off) || sv.startsWith("0B", off)) { off += 2; radix = 2; } else if (sv.startsWith("0o", off) || sv.startsWith("0O", off)) { off += 2; radix = 8; } else if (sv.startsWith("0x", off) || sv.startsWith("0X", off)) { off += 2; radix = 16; } if (sv.startsWith("-", off) || sv.startsWith("+", off)) throw new NumberFormatException("Sign character in the middle of the number"); BigInteger bn = new BigInteger(sv.substring(off), radix); int bs = sizeInBits; if (signed) { bs--; } if (bn.bitLength() > bs) { throw new NumberFormatException("Number " + stringValue + " does not fit the bit size (" + sizeInBits + (signed ? "/signed" : "unsigned") + ")"); } long x = bn.longValue(); if (negative) { x = -x; } return x; } protected  IntegerDataType(Builder<?> builder); protected  IntegerDataType(IntegerDataType t); boolean isSigned(); int getSizeInBits(); IntegerValidRange getValidRange(); void setInitialValue(Long initialValue); Long getInitialValue(); @Override Long parseString(String stringValue); @Override Type getValueType(); @Override String getTypeAsString(); }
IntegerDataType extends NumericDataType { @Override public Long parseString(String stringValue) { String sv = stringValue.replace("_", ""); if (sv.length() == 0) throw new NumberFormatException("Zero length string"); int off = 0; char sv0 = sv.charAt(0); boolean negative = false; int radix = 10; if (sv0 == '-') { if (!signed) { throw new NumberFormatException("negative number specified for unsigned integer"); } negative = true; off++; } else if (sv0 == '+') { off++; } if (sv.startsWith("0b", off) || sv.startsWith("0B", off)) { off += 2; radix = 2; } else if (sv.startsWith("0o", off) || sv.startsWith("0O", off)) { off += 2; radix = 8; } else if (sv.startsWith("0x", off) || sv.startsWith("0X", off)) { off += 2; radix = 16; } if (sv.startsWith("-", off) || sv.startsWith("+", off)) throw new NumberFormatException("Sign character in the middle of the number"); BigInteger bn = new BigInteger(sv.substring(off), radix); int bs = sizeInBits; if (signed) { bs--; } if (bn.bitLength() > bs) { throw new NumberFormatException("Number " + stringValue + " does not fit the bit size (" + sizeInBits + (signed ? "/signed" : "unsigned") + ")"); } long x = bn.longValue(); if (negative) { x = -x; } return x; } protected  IntegerDataType(Builder<?> builder); protected  IntegerDataType(IntegerDataType t); boolean isSigned(); int getSizeInBits(); IntegerValidRange getValidRange(); void setInitialValue(Long initialValue); Long getInitialValue(); @Override Long parseString(String stringValue); @Override Type getValueType(); @Override String getTypeAsString();  }