mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-07 05:18:44 +08:00
21 lines
721 B
Rust
21 lines
721 B
Rust
//! String Utilities
|
|
use crate::types::*;
|
|
use crate::utils::encoding::*;
|
|
use anyhow::Result;
|
|
use unicode_segmentation::UnicodeSegmentation;
|
|
|
|
/// Truncate a string to a specified length, encoding it with the given encoding.
|
|
/// Output size may less than or equal to the specified length.
|
|
pub fn truncate_string(s: &str, length: usize, encoding: Encoding, check: bool) -> Result<Vec<u8>> {
|
|
let vec: Vec<_> = UnicodeSegmentation::graphemes(s, true).collect();
|
|
let mut result = Vec::new();
|
|
for graphemes in vec {
|
|
let data = encode_string(encoding, graphemes, check)?;
|
|
if result.len() + data.len() > length {
|
|
break;
|
|
}
|
|
result.extend(data);
|
|
}
|
|
return Ok(result);
|
|
}
|