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)
| 109 | /// a lone surrogate isn't representable in Rust and never round-trips through |
| 110 | /// SQLite anyway). Returns (sliced, was_truncated_at_or_beyond_n). |
| 111 | pub fn slice_utf16(s: &str, n: usize) -> (String, bool) { |
| 112 | let mut used = 0usize; |
| 113 | let mut out = String::new(); |
| 114 | for c in s.chars() { |
| 115 | let w = c.len_utf16(); |
| 116 | if used + w > n { |
| 117 | return (out, true); |
| 118 | } |
| 119 | used += w; |
| 120 | out.push(c); |
| 121 | if used == n { |
| 122 | // Exactly at the limit: truncated iff any source remains. |
| 123 | let truncated = out.len() < s.len(); |
| 124 | return (out, truncated); |
| 125 | } |
| 126 | } |
| 127 | (out, false) |
| 128 | } |
| 129 | |
| 130 | /// objectKeyName (tree-sitter.ts): strip ONE leading and ONE trailing quote |
| 131 | /// character (`'`, `"`, or backtick). |
no test coverage detected