* Snap an arbitrary code-unit offset to the start of the containing grapheme. * If offset is already on a boundary, returns it unchanged.
(offset: number)
| 1514 | * If offset is already on a boundary, returns it unchanged. |
| 1515 | */ |
| 1516 | snapToGraphemeBoundary(offset: number): number { |
| 1517 | if (offset <= 0) return 0 |
| 1518 | if (offset >= this.text.length) return this.text.length |
| 1519 | const boundaries = this.getGraphemeBoundaries() |
| 1520 | // Binary search for largest boundary <= offset |
| 1521 | let lo = 0 |
| 1522 | let hi = boundaries.length - 1 |
| 1523 | while (lo < hi) { |
| 1524 | const mid = (lo + hi + 1) >> 1 |
| 1525 | if (boundaries[mid]! <= offset) lo = mid |
| 1526 | else hi = mid - 1 |
| 1527 | } |
| 1528 | return boundaries[lo]! |
| 1529 | } |
| 1530 | } |
| 1531 |
no test coverage detected