Created
November 1, 2016 22:44
-
-
Save elucidator/2103ac47df8d9b3259e0d0bcf547efa7 to your computer and use it in GitHub Desktop.
Simple Reader for KeyValues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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