You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What do you think about adding predicates to the CodeQL type string for determining whether a string has a given prefix or suffix or contains a substring? For example startsWith(string), endsWith(string) and contains(string).
Currently the workarounds are using indexOf(...) = 0 or matches(...%) (which seems to be faster than indexOf, see github/codeql#6479 (comment)). However, these predicates do not convey the intention as clearly, might not be that performant and for matches one must take care not to accidentally use %or _ where the intention was to match them literally.
To give you some insight about what happens under the hood in the CodeQL engine, matches("...%") gets translated into an operation like startsWith("..."), and similarly matches("%...") becomes endsWith("..."). So the engine is already doing that optimisation for you. Using matches in this way is indeed preferable over indexOf, because indexOf has to produce all (character, index) pairs.
This isn't high on our list of priorities, but I agree that either exposing startsWith/endsWith primitives, or updating the docs for matches to clearly describe this case, is a good way to convey the intention.
Marcono1234
changed the title
Add string predicates startsWith and endsWith
Add string predicates startsWith, endsWith and containsSep 7, 2021
What do you think about adding predicates to the CodeQL type
string
for determining whether a string has a given prefix or suffix or contains a substring? For examplestartsWith(string)
,endsWith(string)
andcontains(string)
.Currently the workarounds are using
indexOf(...) = 0
ormatches(...%)
(which seems to be faster thanindexOf
, see github/codeql#6479 (comment)). However, these predicates do not convey the intention as clearly, might not be that performant and formatches
one must take care not to accidentally use%
or_
where the intention was to match them literally.In the github/codeql repository (at github/codeql@3953331) there are at least:
startsWith
could be used(I searched for the regex
matches\("[^%_]*%"\)
in CodeQL source files)endsWith
could be used(I searched for the regex
matches\("%[^%_]*"\)
in CodeQL source files)The text was updated successfully, but these errors were encountered: