Skip to content

Instantly share code, notes, and snippets.

@elucidator
Created November 1, 2016 22:44
Show Gist options
  • Save elucidator/2103ac47df8d9b3259e0d0bcf547efa7 to your computer and use it in GitHub Desktop.
Save elucidator/2103ac47df8d9b3259e0d0bcf547efa7 to your computer and use it in GitHub Desktop.
Simple Reader for KeyValues
class KeyValueReader {
private final String line;
private int idx;
private int mark;
public KeyValueReader(String l) {
this.line = l;
}
public void mark() {
this.mark = this.idx;
}
public String getMarkedSegment() {
correctState(this.mark <= this.idx, "mark is greater than this.idx");
return this.line.substring(this.mark, this.idx);
}
public int getc() {
correctState(this.idx + 1 < this.line.length(), "Attempt to read past end");
return this.line.charAt(this.idx++);
}
public boolean available() {
return idx < (line.length() - 1);
}
public boolean is(char c) {
return idx < line.length() && this.line.charAt(this.idx) == c;
}
private boolean was(char c) {
correctState(this.idx >= 1, "Reading before beginning of data");
return this.line.charAt(this.idx - 1) == c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment