/
LineMap.java
53 lines (43 loc) · 1.06 KB
/
LineMap.java
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
package com.mobilesorcery.sdk.html5.debug;
import java.util.Map.Entry;
import java.util.TreeMap;
public class LineMap {
private final TreeMap<Integer, Integer> lines = new TreeMap<Integer, Integer>();
public LineMap(String text) {
char[] chars = text.toCharArray();
int startOfLine = 0;
int line = 1;
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
char peek = i < chars.length - 1 ? chars[i + 1] : '\0';
if (ch == '\r' || ch == '\n') {
line = addLine(startOfLine, line);
if (peek == '\n' && ch == '\r') {
i++;
}
startOfLine = i + 1;
}
}
}
private int addLine(int startIx, int line) {
lines.put(startIx, line);
return line + 1;
}
public int getLine(int pos) {
Integer line = lines.floorKey(pos);
if (line == null) {
return 1;
}
return lines.get(line);
}
public int getColumn(int pos) {
Entry<Integer, Integer> entry = lines.floorEntry(pos);
if (entry == null) {
return pos;
}
return lines.get(entry.getKey()) - pos;
}
public int getLineCount() {
return lines.size() + 1;
}
}