mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-07 05:18:44 +08:00
Add willplus support
This commit is contained in:
@@ -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
19
src/utils/str.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user