Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Created May 7, 2015 21:50
Show Gist options
  • Save witoldsz/0a40d8b19b79a68b9046 to your computer and use it in GitHub Desktop.
Save witoldsz/0a40d8b19b79a68b9046 to your computer and use it in GitHub Desktop.
class FastScanner {
private final int radix = 10;
private int pos;
private String line;
void init(String line) {
this.line = line;
pos = 0;
}
boolean hasNext() {
return pos < line.length();
}
char nextChar() {
char result = 0;
if (hasNext()) {
result = line.charAt(pos++);
}
if (hasNext()) {
++pos;
}
return result;
}
long nextLong() {
int begin = pos;
long result = 0;
boolean negative = false;
for (char c; hasNext() && (c = line.charAt(pos)) != ' '; ++pos) {
if (c == '-') negative = true;
if (c == '+' || c == '-' || c == '.') continue;
result *= radix;
int digit = Character.digit(c, radix);
if (digit == -1) {
throw new NumberFormatException("'" + line.substring(begin, pos + 1) + "' in '" + line + "'");
}
result += digit;
}
++pos;
return negative ? -result : result;
}
int nextInt() {
return (int) nextLong();
}
UUID nextUUID() {
return UUID.fromString(next());
}
String next() {
int begin = pos, end = pos;
while (hasNext() && line.charAt(pos) != ' ') {
++pos;
++end;
}
String result = line.substring(begin, end);
++pos;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment