neo_solidity/neo/
source.rs

1fn clamp_utf8<'a>(value: &'a str, max_len: usize) -> Cow<'a, str> {
2    if value.len() <= max_len {
3        return Cow::Borrowed(value);
4    }
5
6    let mut end = max_len.min(value.len());
7    while end > 0 && !value.is_char_boundary(end) {
8        end -= 1;
9    }
10    Cow::Owned(value[..end].to_string())
11}
12
13/// Clamp a NEF source string to the maximum allowed byte length, preserving
14/// UTF-8 boundaries.
15pub fn clamp_nef_source<'a>(value: &'a str) -> Cow<'a, str> {
16    clamp_utf8(value, MAX_SOURCE_LENGTH)
17}
18
19/// Clamp a NEF source string and report whether truncation occurred.
20pub fn clamp_nef_source_with_flag<'a>(value: &'a str) -> (Cow<'a, str>, bool) {
21    let clamped = clamp_nef_source(value);
22    let truncated = clamped.len() < value.len();
23    (clamped, truncated)
24}