aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/text/Cursor.java
blob: 14abebfa51cbb00046dbb45b780983ea77bca1ca (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.text;

import java.util.Objects;

/**
 * Cursor is a mutable offset into a fixed String, and useful for String parsing.
 *
 * @author hakonhall
 */
// @Mutable
public class Cursor {
    private final String text;
    private int offset;
    private TextLocation locationCache;

    /** Creates a pointer to the first char of {@code text}, which is EOT if {@code text} is empty. */
    public Cursor(String text) { this(text, 0, new TextLocation()); }

    public Cursor(Cursor that) { this(that.text, that.offset, that.locationCache); }

    private Cursor(String text, int offset, TextLocation location) {
        this.text = Objects.requireNonNull(text);
        this.offset = offset;
        this.locationCache = Objects.requireNonNull(location);
    }

    /** Returns the substring of {@code text} starting at {@link #offset()} (to EOT). */
    @Override
    public String toString() { return text.substring(offset); }

    public int offset() { return offset; }
    public boolean bot() { return offset == 0; }
    public boolean eot() { return offset == text.length(); }
    public boolean startsWith(char c) { return offset < text.length() && text.charAt(offset) == c; }
    public boolean startsWith(String prefix) { return text.startsWith(prefix, offset); }
    public char getChar() { return text.charAt(offset); }

    /** The number of chars between pointer and EOT. */
    public int length() { return text.length() - offset; }

    public String fullText() { return text; }

    /** Calculate the current text location in O(length(text)). */
    public TextLocation calculateLocation() {
        if (offset < locationCache.offset()) {
            locationCache = new TextLocation();
        } else if (offset == locationCache.offset()) {
            return locationCache;
        }

        int lineIndex = locationCache.lineIndex();
        int columnIndex = locationCache.columnIndex();
        for (int i = locationCache.offset(); i < offset; ++i) {
            if (text.charAt(i) == '\n') {
                ++lineIndex;
                columnIndex = 0;
            } else {
                ++columnIndex;
            }
        }

        locationCache = new TextLocation(offset, lineIndex, columnIndex);
        return locationCache;
    }

    public void set(Cursor that) {
        if (that.text != text) {
            throw new IllegalArgumentException("'that' doesn't refer to the same text");
        }

        this.offset = that.offset;
    }

    /** If the next substring.length() chars of matches substring pointing at Advance pointer past substring if pointer  Returns true if pointing at string. */
    public boolean skip(String substring) {
        if (startsWith(substring)) {
            offset += substring.length();
            return true;
        } else {
            return false;
        }
    }

    public boolean skip(char c) {
        if (startsWith(c)) {
            ++offset;
            return true;
        } else {
            return false;
        }
    }

    public boolean skipBackwards(String substring) {
        int newOffset = offset - substring.length();
        if (newOffset < 0) return false;
        if (text.startsWith(substring, newOffset)) {
            offset = newOffset;
            return true;
        } else {
            return false;
        }
    }

    /** If the current char is a whitespace, skip it and return true. */
    public boolean skipWhitespace() {
        if (!eot() && Character.isWhitespace(getChar())) {
            ++offset;
            return true;
        } else {
            return false;
        }
    }

    /** Returns true if at least one whitespace was skipped. */
    public boolean skipWhitespaces() {
        if (skipWhitespace()) {
            while (skipWhitespace())
                ++offset;
            return true;
        } else {
            return false;
        }
    }

    /** Return false if eot(), otherwise advance to the next char and return true. */
    public boolean increment() {
        if (eot()) return false;
        ++offset;
        return true;
    }

    /** Return false if bot(), otherwise retreat to the previous char and return true. */
    public boolean decrement() {
        if (bot()) return false;
        --offset;
        return true;
    }

    /**
     * Advance {@code distance} chars until bot() or eot() is reached (distance may be negative),
     * and return true if this cursor moved the full distance.
     */
    public boolean advance(int distance) {
        int newOffset = offset + distance;
        if (newOffset < 0) {
            this.offset = 0;
            return false;
        } else if (newOffset > text.length()) {
            this.offset = text.length();
            return false;
        } else {
            this.offset = newOffset;
            return true;
        }
    }

    /** Advance pointer until start of needle is found (and return true), or EOT is reached (and return false). */
    public boolean advanceTo(String needle) {
        int index = text.indexOf(needle, offset);
        if (index == -1) {
            offset = text.length();
            return false; // and eot() is true
        } else {
            offset = index;
            return true; // and eot() is false
        }
    }

    /** Advance pointer until start of needle is found (and return true), or EOT is reached (and return false). */
    public boolean advanceTo(char needle) {
        int index = text.indexOf(needle, offset);
        if (index == -1) {
            offset = text.length();
            return false; // and eot() is true
        } else {
            offset = index;
            return true; // and eot() is false
        }
    }

    /** Advance pointer past needle (and return true), or to EOT (and return false). */
    public boolean advancePast(String needle) {
        if (advanceTo(needle)) {
            offset += needle.length();
            return true; // and eot() may or may not be true
        } else {
            return false; // and eot() is true
        }
    }

    /** Advance pointer past needle (and return true), or to EOT (and return false). */
    public boolean advancePast(char needle) {
        if (advanceTo(needle)) {
            ++offset;
            return true; // and eot() may or may not be true
        } else {
            return false; // and eot() is true
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cursor cursor = (Cursor) o;
        return offset == cursor.offset && text.equals(cursor.text);
    }

    @Override
    public int hashCode() {
        return Objects.hash(text, offset);
    }

    private void throwIfEot() { if (eot()) throw new StringIndexOutOfBoundsException(offset); }
}