Python: only expose lengths of quote and prefix

This commit is contained in:
Rasmus Lerchedahl Petersen
2023-09-26 20:45:24 +02:00
parent d25b93d944
commit 73aa302bd2
2 changed files with 12 additions and 8 deletions

View File

@@ -155,11 +155,15 @@ class StringPart extends StringPart_, AstNode {
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) {
/**
* Holds if the content of string `StringPart` is surrounded by
* a prefix (including a quote) of length `prefixLength` and
* a quote of length `quoteLength`.
*/
predicate contextSize(int prefixLength, int quoteLength) {
exists(int occurrenceOffset |
quote = this.getText().regexpFind("\"{3}|\"{1}|'{3}|'{1}", 0, occurrenceOffset) and
prefix = this.getText().prefix(occurrenceOffset + quote.length())
quoteLength = this.getText().regexpFind("\"{3}|\"{1}|'{3}|'{1}", 0, occurrenceOffset).length() and
prefixLength = occurrenceOffset + quoteLength
)
}
@@ -168,8 +172,8 @@ class StringPart extends StringPart_, AstNode {
* See `context` for obtaining the prefix and the quote.
*/
int getContentLength() {
exists(string prefix, string quote | this.context(prefix, quote) |
result = this.getText().length() - prefix.length() - quote.length()
exists(int prefixLength, int quoteLength | this.contextSize(prefixLength, quoteLength) |
result = this.getText().length() - prefixLength - quoteLength
)
}
}

View File

@@ -230,7 +230,7 @@ module Impl implements RegexTreeViewSig {
index > 0 and
exists(int previousOffset | previousOffset = this.getPartOffset(index - 1) |
result =
previousOffset + re.(StrConst).getImplicitlyConcatenatedPart(index - 1).getContentlength()
previousOffset + re.(StrConst).getImplicitlyConcatenatedPart(index - 1).getContentLength()
)
}
@@ -241,7 +241,7 @@ module Impl implements RegexTreeViewSig {
StringPart getPart(int localOffset) {
exists(int index, int prefixLength | index = max(int i | this.getPartOffset(i) < start) |
result = re.(StrConst).getImplicitlyConcatenatedPart(index) and
exists(string prefix | result.context(prefix, _) | prefixLength = prefix.length()) and
result.contextSize(prefixLength, _) and
// Example:
// re.compile('...' r"""...this..""")
// - `start` is the offset from `(` to `this` as counted after concatenating all parts.