JS `String.prototype.slice(0, n)` in UTF-16 units, without splitting a surrogate pair (when the cut would split one, we stop one code unit short — a lone surrogate isn't representable in Rust and never round-trips through SQLite anyway). Returns (sliced, was_truncated_at_or_beyond_n).
(s: &str, n: usize)
| 161 | /// a lone surrogate isn't representable in Rust and never round-trips through |
| 162 | /// SQLite anyway). Returns (sliced, was_truncated_at_or_beyond_n). |
| 163 | pub fn slice_utf16(s: &str, n: usize) -> (String, bool) { |
| 164 | let mut used = 0usize; |
| 165 | let mut out = String::new(); |
| 166 | for c in s.chars() { |
| 167 | let w = c.len_utf16(); |
| 168 | if used + w > n { |
| 169 | return (out, true); |
| 170 | } |
| 171 | used += w; |
| 172 | out.push(c); |
| 173 | if used == n { |
| 174 | // Exactly at the limit: truncated iff any source remains. |
| 175 | let truncated = out.len() < s.len(); |
| 176 | return (out, truncated); |
| 177 | } |
| 178 | } |
| 179 | (out, false) |
| 180 | } |
| 181 | |
| 182 | /// objectKeyName (tree-sitter.ts): strip ONE leading and ONE trailing quote |
| 183 | /// character (`'`, `"`, or backtick). |
no test coverage detected