-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add option for whole-word search #32
Add option for whole-word search #32
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a few things to clean up.
@elliottslaughter friendly reminder I can't push the button |
src/app.rs
Outdated
if self.whole_word { | ||
match &self.last_word_regex { | ||
Some(regex) => regex.is_match(s), | ||
_ => false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a matter of taste, but I would write this:
let Some(regex) = &self.last_word_regex else {
unreachable!();
};
regex.is_match(s)
There are a couple reasons to do this. First, we don't want to just ignore an error here. So the unreachable!()
signals this is a branch we shouldn't ever hit (and is an error if we do). Because we know the else branch is unreachable, we can optimize the happy path with let ... else
, meaning that the variable regex
is always defined subsequent to this statement, and reducing the indentation of the continuation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For posterity, the other way to write this is:
let regex = self.last_word_regex.unwrap();
regex.is_match(s)
That's fine too if you want to write it that way. It's shorter, less explicit, and it's easier to miss the .unwrap()
(which panics if the option is not Some(...)
). I don't have a strong preference, but I feel the profiler backend suffers a bit from using unwrap()
all over the place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely better, still acclimating to let Some(...)
and matching in general, honestly reminds me (from the mists of memory) of Prolog.
src/app.rs
Outdated
self.last_whole_word = self.whole_word; | ||
if self.whole_word { | ||
let regex_string = format!("\\b{}\\b", escape(&self.query)); | ||
self.last_word_regex = Some(Regex::new(regex_string.as_str()).unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, forgot to mention: I'm pretty sure regex_string.as_str()
can be written ®ex_string
, because there's an impl somewhere for &String
to behave like &str
(again with those implied impls).
This PR adds checkbox option to the search that supports only returning results for whole-word matches.
cc @elliottslaughter @lightsighter
Searching for "64" with the whole-word option unchecked yields many results, including ones with 64 in the middle of words:
Checking the whole-word option updates to fewer results that only have "64" as an entire word: