-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Use AhoCorasick
to speed up quote match
#9773
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
use aho_corasick::{AhoCorasick, AhoCorasickKind, Anchored, Input, MatchKind, StartKind}; | ||
use once_cell::sync::Lazy; | ||
|
||
use ruff_text_size::{TextLen, TextRange}; | ||
|
||
/// Includes all permutations of `r`, `u`, `f`, and `fr` (`ur` is invalid, as is `uf`). This | ||
|
@@ -124,18 +127,6 @@ pub const SINGLE_QUOTE_BYTE_PREFIXES: &[&str] = &[ | |
"b'", | ||
]; | ||
|
||
#[rustfmt::skip] | ||
const TRIPLE_QUOTE_SUFFIXES: &[&str] = &[ | ||
"\"\"\"", | ||
"'''", | ||
]; | ||
|
||
#[rustfmt::skip] | ||
const SINGLE_QUOTE_SUFFIXES: &[&str] = &[ | ||
"\"", | ||
"'", | ||
]; | ||
|
||
/// Strip the leading and trailing quotes from a string. | ||
/// Assumes that the string is a valid string literal, but does not verify that the string | ||
/// is a "simple" string literal (i.e., that it does not contain any implicit concatenations). | ||
|
@@ -155,28 +146,41 @@ pub fn raw_contents_range(contents: &str) -> Option<TextRange> { | |
)) | ||
} | ||
|
||
/// An [`AhoCorasick`] matcher for string and byte literal prefixes. | ||
static PREFIX_MATCHER: Lazy<AhoCorasick> = Lazy::new(|| { | ||
AhoCorasick::builder() | ||
.start_kind(StartKind::Anchored) | ||
.match_kind(MatchKind::LeftmostLongest) | ||
.kind(Some(AhoCorasickKind::DFA)) | ||
.build( | ||
TRIPLE_QUOTE_STR_PREFIXES | ||
.iter() | ||
.chain(TRIPLE_QUOTE_BYTE_PREFIXES) | ||
.chain(SINGLE_QUOTE_STR_PREFIXES) | ||
.chain(SINGLE_QUOTE_BYTE_PREFIXES), | ||
) | ||
.unwrap() | ||
}); | ||
|
||
/// Return the leading quote for a string or byte literal (e.g., `"""`). | ||
pub fn leading_quote(content: &str) -> Option<&str> { | ||
TRIPLE_QUOTE_STR_PREFIXES | ||
.iter() | ||
.chain(TRIPLE_QUOTE_BYTE_PREFIXES) | ||
.chain(SINGLE_QUOTE_STR_PREFIXES) | ||
.chain(SINGLE_QUOTE_BYTE_PREFIXES) | ||
.find_map(|pattern| { | ||
if content.starts_with(pattern) { | ||
Some(*pattern) | ||
} else { | ||
None | ||
} | ||
}) | ||
let mat = PREFIX_MATCHER.find(Input::new(content).anchored(Anchored::Yes))?; | ||
Some(&content[mat.start()..mat.end()]) | ||
} | ||
|
||
/// Return the trailing quote string for a string or byte literal (e.g., `"""`). | ||
pub fn trailing_quote(content: &str) -> Option<&&str> { | ||
TRIPLE_QUOTE_SUFFIXES | ||
.iter() | ||
.chain(SINGLE_QUOTE_SUFFIXES) | ||
.find(|&pattern| content.ends_with(pattern)) | ||
pub fn trailing_quote(content: &str) -> Option<&str> { | ||
if content.ends_with("'''") { | ||
Some("'''") | ||
} else if content.ends_with("\"\"\"") { | ||
Some("\"\"\"") | ||
} else if content.ends_with('\'') { | ||
Some("'") | ||
} else if content.ends_with('\"') { | ||
Some("\"") | ||
} else { | ||
None | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also a bit faster than the iterator approach though both are quite fast (1ns vs. 800ps). |
||
} | ||
|
||
/// Return `true` if the string is a triple-quote string or byte prefix. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Perfect. I'm glad you found
StartKind::Anchored
.