use std::{collections::HashSet, str};
use indicatif::{ProgressBar, ProgressStyle};
pub(crate) fn is_alphanumeric(text: &str) -> bool {
if text.is_empty() {
return false;
}
text.chars().all(|c| c.is_alphanumeric())
}
pub(crate) fn contains_punctuation(text: &str) -> bool {
if text.is_empty() {
return false;
}
text.chars().any(|c| c.is_ascii_punctuation())
}
pub(crate) fn contains_numbers(text: &str) -> bool {
if text.is_empty() {
return false;
}
text.chars().any(|c| c.is_ascii_digit())
}
pub(crate) fn contains_special_characters(text: &str, special_characters: HashSet<char>) -> bool {
if text.is_empty() {
return false;
}
text.chars().any(|c| special_characters.contains(&c))
}
pub(crate) fn get_progress_bar(total: u64) -> ProgressBar {
let progress_bar = ProgressBar::new(total);
progress_bar.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.green/blue}] {human_pos}/{human_len} ({eta})")
.unwrap()
.progress_chars("##-"));
progress_bar
}
pub fn hash_string(text: &str) -> String {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
text.hash(&mut hasher);
format!("{:x}", hasher.finish())
}
pub(crate) fn is_valid_utf8(text: &str) -> bool {
let b = text.as_bytes();
if str::from_utf8(b).is_err() {
return false;
} else {
return true;
}
}
pub(crate) fn char_to_byte(text: String, start: usize, end: usize) -> (usize, usize) {
let start = text.char_indices().nth(start);
let end = text.char_indices().nth(end);
let start = match start {
Some(start) => start.0,
None => 0,
};
let end = match end {
Some(end) => end.0,
None => text.len(),
};
(start, end)
}