Add willplus support

This commit is contained in:
2025-07-21 10:56:59 +08:00
parent 2c065a499c
commit 4ef2402fba
8 changed files with 373 additions and 1 deletions

View File

@@ -13,4 +13,6 @@ pub mod files;
pub mod img;
pub mod macros;
pub mod name_replacement;
#[cfg(feature = "utils-str")]
pub mod str;
pub mod struct_pack;

19
src/utils/str.rs Normal file
View File

@@ -0,0 +1,19 @@
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);
}