aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/text/Cursor.java
blob: bc0fa642339bc4a1c1062a768a32fafed0c4cbfb (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
// 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 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;
        }
    }

    /** Returns true if at least one whitespace was skipped. */
    public boolean skipWhitespace() {
        if (!eot() && Character.isWhitespace(getChar())) {
            do {
                ++offset;
            } while (!eot() && Character.isWhitespace(getChar()));

            return true;
        }

        return false;
    }

    /** Advance to the next char (if any) and return eot(). */
    public boolean increment() {
        if (eot()) return true;
        ++offset;
        return eot();
    }

    /** Advance {@code distance} chars and return {@link #eot()}. */
    public boolean advance(int distance) {
        this.offset = Math.max(0, Math.min(this.offset + distance, text.length()));
        return eot();
    }

    /** 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 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
        }
    }

    @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); }
}