Byte offsets of each line start, for UTF-16 column conversion.
(src: &str)
| 160 | |
| 161 | /// Byte offsets of each line start, for UTF-16 column conversion. |
| 162 | pub fn line_starts(src: &str) -> LineStarts { |
| 163 | let mut starts = vec![0usize]; |
| 164 | for (i, b) in src.bytes().enumerate() { |
| 165 | if b == b'\n' { |
| 166 | starts.push(i + 1); |
| 167 | } |
| 168 | } |
| 169 | let mut wide = Vec::new(); |
| 170 | if !src.is_ascii() { |
| 171 | let mut spare = 0usize; |
| 172 | for (at, c) in src.char_indices() { |
| 173 | if c.len_utf8() > 1 { |
| 174 | spare += c.len_utf8() - c.len_utf16(); |
| 175 | wide.push((at, spare)); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | LineStarts { starts, wide } |
| 180 | } |
| 181 | |
| 182 | /// UTF-16 code units in `s` — what web-tree-sitter (and JS string ops) |
| 183 | /// count, so kernel-emitted columns are byte-identical to the wasm path's. |