1
0
This commit is contained in:
2025-06-20 11:23:21 +08:00
commit b5601aef8c
47 changed files with 1829 additions and 0 deletions

51
common/src/lib.rs Normal file
View File

@@ -0,0 +1,51 @@
use anyhow::{Result, anyhow};
pub fn is_valid_english(input_str: &str, ratio: Option<f32>) -> bool {
let ratio = ratio.unwrap_or(0.6);
let mut total_chars = 0;
let mut alphabet_chars = 0;
let mut prev_char = None;
for c in input_str.chars() {
// 检查连续空格
if c == ' ' && prev_char == Some(' ') {
return false;
}
// 检查是否为可打印字符
if !(c.is_ascii_lowercase()
|| c.is_ascii_uppercase()
|| c.is_ascii_digit()
|| " .,!?;:'\"-()".contains(c))
{
return false;
}
// 检查是否为换行符
if c == '\n' {
return false;
}
total_chars += 1;
if c.is_alphabetic() {
alphabet_chars += 1;
}
prev_char = Some(c);
}
// 字符占文本的比例
let alphabet_ratio = alphabet_chars as f32 / total_chars as f32;
if alphabet_ratio < ratio {
return false;
}
true
}
pub fn xor_with_key(input: &[u8], key: &[u8]) -> Result<Vec<u8>> {
if key.is_empty() {
return Err(anyhow!("empty key"));
}
Ok(input
.iter()
.zip(key.iter().cycle())
.map(|(&a, &b)| a ^ b)
.collect())
}