Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify and add some hash #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/data/regex.json
Original file line number Diff line number Diff line change
Expand Up @@ -2822,5 +2822,56 @@
],
"Invalid": []
}
},
{
"Name": "sha1",
"Regex": "^[0-9a-fA-F]{40}$",
"plural_name": false,
"Description": "sha1 hash",
"Rarity": 1,
"URL": null,
"Tags": [
"hash"
],
"Examples": {
"Valid": [
"5511f894571b58ad5fe231297ed88be4c410c34c"
],
"Invalid": []
}
},
{
"Name": "sha256",
"Regex": "^[0-9a-fA-F]{64}$",
"plural_name": false,
"Description": "sha256 hash",
"Rarity": 1,
"URL": null,
"Tags": [
"hash"
],
"Examples": {
"Valid": [
"7c13517e2bd3f4d4dc4c44be73f56c4a7f15caf3776cd68c33f7d6054f2ee919"
],
"Invalid": []
}
},
{
"Name": "md5",
"Regex": "^[0-9a-fA-F]{32}$",
"plural_name": false,
"Description": "md5 hash",
"Rarity": 1,
"URL": null,
"Tags": [
"hash"
],
"Examples": {
"Valid": [
"c528dcb56c26016a869f89090e1c80eb"
],
"Invalid": []
}
}
]
111 changes: 54 additions & 57 deletions src/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use {
pub mod bytes;

use once_cell::sync::Lazy;
use rayon::iter::IndexedParallelIterator;
use regex::Regex;
use serde::Serialize;

Expand Down Expand Up @@ -125,38 +126,39 @@ impl Identifier {
&REGEX
};

if self.file_support && is_file(text) {
let strings = read_file_to_strings(text);

strings
.par_iter()
.map(|text| {
DATA.iter()
.enumerate()
.filter_map(|(i, e)| {
if is_valid_filter(self, e) && regexes[i].is_match(text) {
Some(Match::new(text.to_owned(), e.clone()))
} else {
None
}
})
.collect::<Vec<Match>>()
})
.flatten()
.collect()
} else {
// iter has almost same or sometimes better performance than par_iter for single text!
DATA.iter()
.enumerate()
let check_fn = |data: rayon::slice::Iter<Data>, text: &String| {
data.enumerate()
.filter_map(|(i, e)| {
if is_valid_filter(self, e) && regexes[i].is_match(text) {
let validity = is_valid_filter(self, e);
if !validity.is_valid() {
// eprintln!("{} is not valid: {:?}", e.name, validity);
None
} else if regexes[i].is_match(text) {
// eprintln!("{:?} matched", regexes[i]);
Some(Match::new(text.to_owned(), e.clone()))
} else {
None
}
})
.collect::<Vec<Match>>()
};
#[cfg(not(target_arch = "wasm32"))]
match (self.file_support, is_file(text)) {
(true, true) => {
let strings = read_file_to_strings(text);

strings
.par_iter()
.map(|text| check_fn(DATA.par_iter(), text))
.flatten()
.collect()
}

_ => check_fn(DATA.par_iter(), &text.to_string()),
}

#[cfg(target_arch = "wasm32")]
check_fn(DATA.to_vec(), &text.to_string())
}

/// This returns the first identification.
Expand Down Expand Up @@ -188,7 +190,7 @@ impl Identifier {
for (i, x) in DATA
.iter()
.enumerate()
.filter(|(_, x)| is_valid_filter(self, x))
.filter(|(_, x)| is_valid_filter(self, x).is_valid())
{
// only consider the regex which compiles!
if regexes[i].is_match(text) {
Expand All @@ -200,32 +202,6 @@ impl Identifier {
}
}

// Identifier implementation for wasm
#[cfg(target_arch = "wasm32")]
impl Identifier {
// There is no file system on the web, so we are not reading strings from file.
// let the user perform the I/O and read the file, then pass the content of it.
pub fn identify(&self, text: &[String]) -> Vec<Match> {
Its-Just-Nans marked this conversation as resolved.
Show resolved Hide resolved
let regexes = if self.boundaryless {
&BOUNDARYLESS_REGEX
} else {
&REGEX
};

text.iter()
.flat_map(|text| {
DATA.iter().enumerate().filter_map(|(i, e)| {
if is_valid_filter(self, e) && regexes[i].is_match(text) {
Some(Match::new(text.to_owned(), e.clone()))
} else {
None
}
})
})
.collect()
}
}

// Output Implementation
impl Identifier {
/// Convert [`Vec<Match>`] to JSON
Expand Down Expand Up @@ -266,30 +242,51 @@ fn is_file(name: &str) -> bool {
}
}

fn is_valid_filter(configs: &Identifier, regex_data: &Data) -> bool {
/// Validation filter
#[derive(Debug)]
pub enum ValidationFilter {
DataLowerRarity,
DataHigherRarity,
DataInTags,
DataInExclude,
Good,
}
Its-Just-Nans marked this conversation as resolved.
Show resolved Hide resolved

impl ValidationFilter {
#[inline]
pub fn is_valid(&self) -> bool {
match self {
ValidationFilter::Good => true,
_ => false,
}
}
}

#[inline]
pub fn is_valid_filter(configs: &Identifier, regex_data: &Data) -> ValidationFilter {
if regex_data.rarity < configs.min_rarity {
return false;
return ValidationFilter::DataLowerRarity;
}
if regex_data.rarity > configs.max_rarity {
return false;
return ValidationFilter::DataHigherRarity;
}

if configs
.tags
.iter()
.any(|y| !regex_data.tags.iter().any(|x| x == y))
{
return false;
return ValidationFilter::DataInTags;
}
if configs
.exclude_tags
.iter()
.any(|y| regex_data.tags.iter().any(|x| x == y))
{
return false;
return ValidationFilter::DataInExclude;
}

true
ValidationFilter::Good
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down