]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/completions/parsing/SubstringReader.java
New SCL completion implementation
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / completions / parsing / SubstringReader.java
1 package org.simantics.scl.compiler.completions.parsing;
2
3 import java.io.IOException;
4 import java.io.Reader;
5
6 /**
7  * Based on java.io.StringReader.
8  */
9
10 public class SubstringReader extends Reader {
11
12     private String str;
13     private int end;
14     private int pos;
15     private int mark;
16
17     public SubstringReader(String str, int begin, int end) {
18         this.str = str;
19         this.pos = begin;
20         this.mark = begin;
21         this.end = end;
22     }
23
24     public int read() throws IOException {
25         if (pos >= end)
26             return -1;
27         return str.charAt(pos++);
28     }
29
30     public int read(char cbuf[], int off, int len) throws IOException {
31         if ((off < 0) || (off > cbuf.length) || (len < 0) ||
32                 ((off + len) > cbuf.length) || ((off + len) < 0))
33             throw new IndexOutOfBoundsException();
34         else if (len == 0)
35             return 0;
36         if (pos >= end)
37             return -1;
38         int n = Math.min(end - pos, len);
39         str.getChars(pos, pos + n, cbuf, off);
40         pos += n;
41         return n;
42     }
43
44     public long skip(long ns) throws IOException {
45         if (pos >= end)
46             return 0;
47         // Bound skip by beginning and end of the source
48         long n = Math.min(end - pos, ns);
49         n = Math.max(-pos, n);
50         pos += n;
51         return n;
52     }
53
54     public boolean ready() throws IOException {
55         return true;
56     }
57
58     public boolean markSupported() {
59         return true;
60     }
61
62     public void mark(int readAheadLimit) throws IOException {
63         if (readAheadLimit < 0)
64             throw new IllegalArgumentException("Read-ahead limit < 0");
65         mark = pos;
66     }
67
68     public void reset() throws IOException {
69         pos = mark;
70     }
71
72     public void close() {
73     }
74 }