MCPcopy Create free account
hub / github.com/bytesize-rs/bytesize / to_string_truncate

Function to_string_truncate

src/parse.rs:257–277  ·  view source on GitHub ↗

Safely truncates

(unit: &str)

Source from the content-addressed store, hash-verified

255
256/// Safely truncates
257fn to_string_truncate(unit: &str) -> String {
258 const MAX_UNIT_LEN: usize = 3;
259
260 if unit.len() > MAX_UNIT_LEN {
261 // TODO(MSRV 1.91): use ceil_char_boundary
262
263 if unit.is_char_boundary(3) {
264 format!("{}...", &unit[..3])
265 } else if unit.is_char_boundary(4) {
266 format!("{}...", &unit[..4])
267 } else if unit.is_char_boundary(5) {
268 format!("{}...", &unit[..5])
269 } else if unit.is_char_boundary(6) {
270 format!("{}...", &unit[..6])
271 } else {
272 unreachable!("char boundary will be within 4 bytes")
273 }
274 } else {
275 unit.to_owned()
276 }
277}
278
279/// Error returned when parsing a [`Unit`] fails.
280#[derive(Debug)]

Callers 1

from_strMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected