Conversation
MrCurtis
commented
Aug 16, 2023
vcf/src/parse.rs
Outdated
| // Repeatedly match either non-comma/non-quote characters or blocks of text enclosed in | ||
| // quotes, until we can't, in which case we're either at a non-quote-enclosed comma or the | ||
| // end of the string. | ||
| let re = Regex::new(r#"(?:[^,"]+|(?:"[^"]*"))+"#).unwrap(); |
Contributor
Author
There was a problem hiding this comment.
This regex gets compiled every time the function gets called, which doesn't seem very efficient. How do I place it outside the function call so that it is only compiled once?
There was a problem hiding this comment.
Use something like lazy_static!:
https://stackoverflow.com/a/35169402
Or rust-lang/regex#709 (comment) which uses std rust. Seems like there's something in the works to compile statically actually in the regex crate which would be nice.
Contributor
Author
There was a problem hiding this comment.
Using lazy_static! seems to have done the job. Cheers.
This avoids repeatedly compiling the regex each call to parse.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This ensures that the text from nested headers is not split on commas that are enclosed in quotes.
In particular, it ensures that this line
from the example in section 1.1 of the specs can be parsed.
Note that section 1.2 lists the comma as one of the special characters which should always be 'represented with the
capitalized percent encoding' when they are not used for their specific meaning. However, I assume this refers only to cases where they are not enclosed within quotes.