MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / truncate_to_char_boundary

Function truncate_to_char_boundary

atomic-semantic/src/lib.rs:27–36  ·  view source on GitHub ↗

Find the largest byte index ≤ `max_bytes` that falls on a UTF-8 char boundary. Prevents panics when truncating strings that contain multi-byte characters (e.g., emoji, CJK, accented chars).

(s: &str, max_bytes: usize)

Source from the content-addressed store, hash-verified

25/// Prevents panics when truncating strings that contain multi-byte characters
26/// (e.g., emoji, CJK, accented chars).
27pub(crate) fn truncate_to_char_boundary(s: &str, max_bytes: usize) -> usize {
28 if max_bytes >= s.len() {
29 return s.len();
30 }
31 let mut end = max_bytes;
32 while !s.is_char_boundary(end) {
33 end -= 1;
34 }
35 end
36}
37
38pub use entity::{
39 ChangeType, Confidence, Entity, EntityChange, EntityKind, FileSummary, Reference,

Callers 3

build_const_signatureMethod · 0.85
extract_type_specMethod · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected