-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathrange.js
35 lines (31 loc) · 953 Bytes
/
range.js
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
/**
* ...something resembling a binary search, to find the lowest line within the range.
* And then you could break as soon as the line is longer than the range...
*/
module.exports.sliceRange = (lines, startCol, endCol, inclusive = false) => {
let start = 0
let end = lines.length
if (inclusive) {
// I consider this a temporary solution until I find an alternaive way to fix the "off by one issue"
--startCol
}
while (start < end) {
let mid = (start + end) >> 1
if (startCol >= lines[mid].endCol) {
start = mid + 1
} else if (endCol < lines[mid].startCol) {
end = mid - 1
} else {
end = mid
while (mid >= 0 && startCol < lines[mid].endCol && endCol >= lines[mid].startCol) {
--mid
}
start = mid + 1
break
}
}
while (end < lines.length && startCol < lines[end].endCol && endCol >= lines[end].startCol) {
++end
}
return lines.slice(start, end)
}