(s: &str, max_bytes: usize)
| 19 | use crate::util::env_var::get_term; |
| 20 | |
| 21 | pub 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`. |
no test coverage detected