/** * Constructs automaton that accept strings representing the given integer. Surrounding whitespace * is permitted. * * @param value string representation of integer */ public static DefaultAutomaton makeIntegerValue(String value) { boolean minus = false; int i = 0; while (i < value.length()) { char c = value.charAt(i); if (c == '-') minus = true; if (c >= '1' && c <= '9') break; i++; } StringBuilder b = new StringBuilder(); b.append(value.substring(i)); if (b.length() == 0) b.append("0"); DefaultAutomaton s; if (minus) s = makeChar('-'); else s = makeChar('+').optional(); DefaultAutomaton ws = Datatypes.getWhitespaceAutomaton(); return (ws.concatenate( s.concatenate(makeChar('0').repeat()).concatenate(makeString(b.toString()))) .concatenate(ws)) .minimize(); }
/** * Constructs automaton that accept strings representing the given decimal number. Surrounding * whitespace is permitted. * * @param value string representation of decimal number */ public static DefaultAutomaton makeDecimalValue(String value) { boolean minus = false; int i = 0; while (i < value.length()) { char c = value.charAt(i); if (c == '-') minus = true; if ((c >= '1' && c <= '9') || c == '.') break; i++; } StringBuilder b1 = new StringBuilder(); StringBuilder b2 = new StringBuilder(); int p = value.indexOf('.', i); if (p == -1) b1.append(value.substring(i)); else { b1.append(value.substring(i, p)); i = value.length() - 1; while (i > p) { char c = value.charAt(i); if (c >= '1' && c <= '9') break; i--; } b2.append(value.substring(p + 1, i + 1)); } if (b1.length() == 0) b1.append("0"); DefaultAutomaton s; if (minus) s = makeChar('-'); else s = makeChar('+').optional(); DefaultAutomaton d; if (b2.length() == 0) d = makeChar('.').concatenate(repeat(makeChar('0'), 1)).optional(); else d = makeChar('.').concatenate(makeString(b2.toString())).concatenate(makeChar('0').repeat()); DefaultAutomaton ws = Datatypes.getWhitespaceAutomaton(); return (ws.concatenate( s.concatenate(makeChar('0').repeat()) .concatenate(makeString(b1.toString())) .concatenate(d)) .concatenate(ws)) .minimize(); }