52 lines
1.2 KiB
Rust
52 lines
1.2 KiB
Rust
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())
|
|
}
|