Skip to content

Commit 2fa5853

Browse files
Fixed fixed string bug
1 parent a8c9a8c commit 2fa5853

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

text/grep.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,11 @@ impl Patterns {
309309
}
310310

311311
/// Checks if input string matches the present patterns.
312-
///
313-
/// # Arguments
314-
///
315-
/// * `input` - object that implements [AsRef](AsRef) for [str](str) and describes line.
316-
///
317-
/// # Returns
318-
///
319-
/// Returns [bool](bool) - `true` if input matches present patterns, else `false`.
320312
fn matches(
321313
&self,
322-
input: impl AsRef<[u8]>,
314+
input: &[u8],
323315
collect_matching_substrings: bool,
324316
) -> Result<MatchesData, Box<dyn Error>> {
325-
let input = input.as_ref();
326-
327317
let mut matching_substrings = Vec::<Vec<u8>>::new();
328318

329319
let mut any_pattern_matched = false;
@@ -332,7 +322,7 @@ impl Patterns {
332322
Patterns::Fixed(patterns, ignore_case, line_regexp) => {
333323
let input_ascii_lowercase: Vec<u8>;
334324

335-
let input = if *ignore_case {
325+
let input_after_ignore_case = if *ignore_case {
336326
input_ascii_lowercase = input.to_ascii_lowercase();
337327

338328
input_ascii_lowercase.as_slice()
@@ -341,8 +331,10 @@ impl Patterns {
341331
};
342332

343333
for pattern in patterns {
334+
let pattern_as_bytes = pattern.as_bytes();
335+
344336
if *line_regexp {
345-
if input != pattern.as_bytes() {
337+
if input_after_ignore_case != pattern_as_bytes {
346338
continue;
347339
}
348340

@@ -352,20 +344,24 @@ impl Patterns {
352344

353345
any_pattern_matched = true;
354346

355-
matching_substrings.push(pattern.as_bytes().to_vec());
347+
matching_substrings.push(input.to_vec());
356348
} else {
357-
let pattern_len = pattern.len();
358-
359-
for index in memmem::find_iter(input, pattern) {
360-
if !collect_matching_substrings {
361-
return Ok(MatchesData::fast_path_match());
362-
}
349+
if collect_matching_substrings {
350+
let pattern_as_bytes_len = pattern_as_bytes.len();
363351

364-
any_pattern_matched = true;
352+
for index in
353+
memmem::find_iter(input_after_ignore_case, pattern_as_bytes)
354+
{
355+
any_pattern_matched = true;
365356

366-
let match_slice = &input[index..(index + pattern_len)];
357+
let match_slice = &input[index..(index + pattern_as_bytes_len)];
367358

368-
matching_substrings.push(match_slice.to_vec());
359+
matching_substrings.push(match_slice.to_vec());
360+
}
361+
} else {
362+
if memmem::find(input_after_ignore_case, pattern_as_bytes).is_some() {
363+
return Ok(MatchesData::fast_path_match());
364+
}
369365
}
370366
}
371367
}
@@ -503,6 +499,7 @@ struct MatchesData {
503499
}
504500

505501
impl MatchesData {
502+
#[inline]
506503
pub fn fast_path_match() -> MatchesData {
507504
MatchesData {
508505
any_pattern_matched: true,

0 commit comments

Comments
 (0)