MCPcopy Create free account
hub / github.com/aws/amazon-q-developer-cli / truncate_safe

Function truncate_safe

crates/chat-cli/src/cli/chat/util/mod.rs:21–37  ·  view source on GitHub ↗
(s: &str, max_bytes: usize)

Source from the content-addressed store, hash-verified

19use crate::util::env_var::get_term;
20
21pub fn truncate_safe(s: &str, max_bytes: usize) -> &str {
22 if s.len() <= max_bytes {
23 return s;
24 }
25
26 let mut byte_count = 0;
27 let mut char_indices = s.char_indices();
28
29 for (byte_idx, _) in &mut char_indices {
30 if byte_count + (byte_idx - byte_count) > max_bytes {
31 break;
32 }
33 byte_count = byte_idx;
34 }
35
36 &s[..byte_count]
37}
38
39/// Truncates `s` to a maximum length of `max_bytes`, appending `suffix` if `s` was truncated. The
40/// result is always guaranteed to be at least less than `max_bytes`.

Callers 5

truncate_safe_in_placeFunction · 0.70
truncate_messageFunction · 0.50
build_env_stateFunction · 0.50
sanitize_user_promptFunction · 0.50

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected