* Generate Position object with line / column information using recorded * newline positions. We know the index is always going to be an already * processed index, so all the newlines up to this index should have been * recorded.
(index: number)
| 294 | * recorded. |
| 295 | */ |
| 296 | public getPos(index: number): Position { |
| 297 | let line = 1 |
| 298 | let column = index + 1 |
| 299 | const length = this.newlines.length |
| 300 | let j = -1 |
| 301 | if (length > 100) { |
| 302 | let l = -1 |
| 303 | let r = length |
| 304 | while (l + 1 < r) { |
| 305 | const m = (l + r) >>> 1 |
| 306 | this.newlines[m] < index ? (l = m) : (r = m) |
| 307 | } |
| 308 | j = l |
| 309 | } else { |
| 310 | for (let i = length - 1; i >= 0; i--) { |
| 311 | if (index > this.newlines[i]) { |
| 312 | j = i |
| 313 | break |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (j >= 0) { |
| 319 | line = j + 2 |
| 320 | column = index - this.newlines[j] |
| 321 | } |
| 322 | |
| 323 | return { |
| 324 | column, |
| 325 | line, |
| 326 | offset: index, |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | private peek() { |
| 331 | return this.buffer.charCodeAt(this.index + 1) |