stage
This commit is contained in:
51
common/src/lib.rs
Normal file
51
common/src/lib.rs
Normal 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())
|
||||
}
|
||||
Reference in New Issue
Block a user