aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/zwirn/GoodStringReader.java
blob: a39612036678cd0514939e9a3656f54894c68a0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package moe.nea.zwirn;

import java.util.Stack;

public class GoodStringReader {
    final String source;
    Stack<Integer> stack = new Stack<>();
    int index = 0;

    public GoodStringReader(String source) {
        this.source = source;
    }

    public void push() {
        stack.push(index);
    }

    public void reset() {
        index = stack.pop();
    }

    public void discard() {
        stack.pop();
    }

    public char nextChar() {
        return source.charAt(index++);
    }

    public String readUntil(char... cs) {
        int minI = -1;
        for (char c : cs) {
            int i = source.indexOf(c, index);
            if (i < 0) continue;
            minI = minI < 0 ? i : Math.min(minI, i);
        }
        if (minI < 0) return null;
        int startIndex = index;
        index = minI + 1;
        return source.substring(startIndex, index - 1);
    }

    public char peekChar() {
        return source.charAt(index);
    }
}