Add stochastic string truncation utils.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-12-23 23:18:48 +00:00
parent 4229a1d630
commit 568a28220c
2 changed files with 49 additions and 1 deletions

View File

@@ -31,6 +31,34 @@ pub fn string_array<const LENGTH: usize>() -> ArrayString<LENGTH> {
ret
}
#[must_use]
pub fn truncate_string(mut str: String, range: Range<u64>) -> String {
let len = thread_rng()
.gen_range(range)
.try_into()
.unwrap_or(usize::MAX);
if let Some((i, _)) = str.char_indices().nth(len) {
str.truncate(i);
}
str
}
#[inline]
#[must_use]
pub fn truncate_str(str: &str, range: Range<u64>) -> &str {
let len = thread_rng()
.gen_range(range)
.try_into()
.unwrap_or(usize::MAX);
str.char_indices()
.nth(len)
.map(|(i, _)| str.split_at(i).0)
.unwrap_or(str)
}
#[inline]
#[must_use]
pub fn time_from_now_secs(range: Range<u64>) -> SystemTime {

View File

@@ -5,7 +5,7 @@ mod tests;
mod unquote;
mod unquoted;
use std::mem::replace;
use std::{mem::replace, ops::Range};
pub use self::{between::Between, split::SplitInfallible, unquote::Unquote, unquoted::Unquoted};
use crate::{Result, smallstr::SmallString};
@@ -112,6 +112,26 @@ pub fn common_prefix<T: AsRef<str>>(choice: &[T]) -> &str {
})
}
#[inline]
#[must_use]
#[allow(clippy::arithmetic_side_effects)]
pub fn truncate_deterministic(str: &str, range: Option<Range<usize>>) -> &str {
let range = range.unwrap_or(0..str.len());
let len = str
.as_bytes()
.iter()
.copied()
.map(Into::into)
.fold(0_usize, usize::wrapping_add)
.wrapping_rem(str.len().max(1))
.clamp(range.start, range.end);
str.char_indices()
.nth(len)
.map(|(i, _)| str.split_at(i).0)
.unwrap_or(str)
}
pub fn to_small_string<const CAP: usize, T>(t: T) -> SmallString<[u8; CAP]>
where
T: std::fmt::Display,