Skip to content

Python: Improve computation of regex fragments inside string parts #14317

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

Merged
merged 10 commits into from
Sep 28, 2023

Conversation

yoff
Copy link
Contributor

@yoff yoff commented Sep 26, 2023

No description provided.

@yoff yoff requested a review from a team as a code owner September 26, 2023 10:10
@yoff yoff added the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Sep 26, 2023
Copy link
Contributor

@max-schaefer max-schaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@@ -154,6 +154,24 @@ class StringPart extends StringPart_, AstNode {
override string toString() { result = StringPart_.super.toString() }

override Location getLocation() { result = StringPart_.super.getLocation() }

/** Holds if the content of string `StringPart` is surrounded by `prefix` and `quote`. */
predicate context(string prefix, string quote) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like callers of this predicate are really only interested in the length of the prefix and the quote; perhaps it makes sense to just expose that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider that; it might also be easier on the string pool. It just did not feel as generally useful, but perhaps that is a silly concern. (And given that the prefix and the quote are part of the string, it is actually the same information.)

* `localOffset` will be the offset of this `RegExpTerm` inside `result`.
*/
StringPart getPart(int localOffset) {
exists(int index, int prefixLength | index = max(int i | this.getPartOffset(i) < start) |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this < be a <=?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yes, I think so...good catch!

exists(int re_start, int prefix_len | prefix_len = re.getPrefix().length() |
re.getLocation().hasLocationInfo(filepath, startline, re_start, endline, _) and
startcolumn = re_start + start + prefix_len and
endcolumn = re_start + end + prefix_len - 1
/* inclusive vs exclusive */
)
or
exists(StringPart part, int localOffset | part = this.getPart(localOffset) |
filepath = part.getLocation().getFile().getAbsolutePath() and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using part.getLocation().hasLocationInfo (or perhaps even part.hasLocationInfo if it exists) to bind all of these variables in one go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I went this way because it look clear, but actually going via hasLocationInfo is preferable also because it abstracts away that we choose the absolute path for the file.

@yoff yoff requested a review from max-schaefer September 26, 2023 19:03
Copy link
Contributor

@tausbn tausbn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Had to double-check that everything still made sense for StringParts arising from f-strings. (Luckily, it did.)

I think this looks good. 👍

:shipit:

@yoff
Copy link
Contributor Author

yoff commented Sep 28, 2023

Sorry for the delay. Had to double-check that everything still made sense for StringParts arising from f-strings. (Luckily, it did.)

I think this looks good. 👍

:shipit:

No worries, that was exactly the kind of check, I was looking for 👍

@yoff yoff merged commit bc17bf6 into github:main Sep 28, 2023
@sidshank sidshank removed the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants