diff options
author | Denys Séguret <cano.petrole@gmail.com> | 2022-11-04 21:14:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-04 21:14:17 +0100 |
commit | f3bc7d56cf6b3be4b09c0acc0f68541de679b5da (patch) | |
tree | d353806a343b37b91f7ae948be29c67afbf6af37 /src/pattern | |
parent | 6549df4bc93aef3961cd8350fe8b7d775d6506b5 (diff) |
make content search max file size configurable (#628)
Fix #626
Diffstat (limited to 'src/pattern')
-rw-r--r-- | src/pattern/content_pattern.rs | 8 | ||||
-rw-r--r-- | src/pattern/content_regex_pattern.rs | 6 | ||||
-rw-r--r-- | src/pattern/input_pattern.rs | 2 | ||||
-rw-r--r-- | src/pattern/pattern.rs | 9 |
4 files changed, 14 insertions, 11 deletions
diff --git a/src/pattern/content_pattern.rs b/src/pattern/content_pattern.rs index 7d25f97..d3d5fe5 100644 --- a/src/pattern/content_pattern.rs +++ b/src/pattern/content_pattern.rs @@ -24,10 +24,8 @@ impl fmt::Display for ContentExactPattern { impl ContentExactPattern { - pub fn from(pat: &str) -> Self { - Self { - needle: Needle::new(pat), - } + pub fn new(pat: &str, max_file_size: usize) -> Self { + Self { needle: Needle::new(pat, max_file_size) } } pub fn as_str(&self) -> &str { @@ -50,11 +48,9 @@ impl ContentExactPattern { Ok(ContentSearchResult::Found { .. }) => Some(1), Ok(ContentSearchResult::NotFound) => None, Ok(ContentSearchResult::NotSuitable) => { - // debug!("{:?} isn't suitable for search", &candidate.path); None } Err(e) => { - // today it mostly happens on empty files debug!("error while scanning {:?} : {:?}", &candidate.path, e); None } diff --git a/src/pattern/content_regex_pattern.rs b/src/pattern/content_regex_pattern.rs index dad7e77..10dcd18 100644 --- a/src/pattern/content_regex_pattern.rs +++ b/src/pattern/content_regex_pattern.rs @@ -18,6 +18,7 @@ use { pub struct ContentRegexPattern { rex: regex::Regex, flags: String, + max_file_size: usize } impl fmt::Display for ContentRegexPattern { @@ -28,10 +29,11 @@ impl fmt::Display for ContentRegexPattern { impl ContentRegexPattern { - pub fn from(pat: &str, flags: &str) -> Result<Self, PatternError> { + pub fn new(pat: &str, flags: &str, max_file_size: usize) -> Result<Self, PatternError> { Ok(Self { rex: super::build_regex(pat, flags)?, flags: flags.to_string(), + max_file_size, }) } @@ -54,7 +56,7 @@ impl ContentRegexPattern { } pub fn score_of(&self, candidate: Candidate) -> Option<i32> { - if !candidate.regular_file || is_path_binary(candidate.path) { + if !candidate.regular_file || !is_path_suitable(candidate.path, self.max_file_size) { return None; } match self.has_match(candidate.path) { diff --git a/src/pattern/input_pattern.rs b/src/pattern/input_pattern.rs index d8729e4..a8a9ff4 100644 --- a/src/pattern/input_pattern.rs +++ b/src/pattern/input_pattern.rs @@ -36,7 +36,7 @@ impl InputPattern { parts_expr: &BeTree<PatternOperator, PatternParts>, con: &AppContext, ) -> Result<Self, PatternError> { - let pattern = Pattern::new(parts_expr, &con.search_modes)?; + let pattern = Pattern::new(parts_expr, &con.search_modes, con.content_search_max_file_size)?; Ok(Self { raw, pattern }) } pub fn is_none(&self) -> bool { diff --git a/src/pattern/pattern.rs b/src/pattern/pattern.rs index 20edc70..66b41ac 100644 --- a/src/pattern/pattern.rs +++ b/src/pattern/pattern.rs @@ -32,6 +32,7 @@ impl Pattern { pub fn new( raw_expr: &BeTree<PatternOperator, PatternParts>, search_modes: &SearchModeMap, + content_search_max_file_size: usize, ) -> Result<Self, PatternError> { let expr: BeTree<PatternOperator, Pattern> = raw_expr .try_map_atoms::<_, PatternError, _>(|pattern_parts| { @@ -69,10 +70,14 @@ impl Pattern { TokPattern::new(core) ), SearchMode::ContentExact => Self::ContentExact( - ContentExactPattern::from(core) + ContentExactPattern::new(core, content_search_max_file_size) ), SearchMode::ContentRegex => Self::ContentRegex( - ContentRegexPattern::from(core, flags.unwrap_or(""))? + ContentRegexPattern::new( + core, + flags.unwrap_or(""), + content_search_max_file_size, + )? ), } } |