MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / slice_utf16

Function slice_utf16

codegraph-kernel/src/textutil.rs:207–224  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

205/// a lone surrogate isn't representable in Rust and never round-trips through
206/// SQLite anyway). Returns (sliced, was_truncated_at_or_beyond_n).
207pub fn slice_utf16(s: &str, n: usize) -> (String, bool) {
208 let mut used = 0usize;
209 let mut out = String::new();
210 for c in s.chars() {
211 let w = c.len_utf16();
212 if used + w > n {
213 return (out, true);
214 }
215 used += w;
216 out.push(c);
217 if used == n {
218 // Exactly at the limit: truncated iff any source remains.
219 let truncated = out.len() < s.len();
220 return (out, truncated);
221 }
222 }
223 (out, false)
224}
225
226/// objectKeyName (tree-sitter.ts): strip ONE leading and ONE trailing quote
227/// character (`'`, `"`, or backtick).

Callers 6

emit_requireMethod · 0.85
init_signatureFunction · 0.85
visitMethod · 0.85
hook_callMethod · 0.85
extract_rtk_endpointsMethod · 0.85

Calls 2

pushMethod · 0.80
lenMethod · 0.80

Tested by

no test coverage detected