Skip to content

Commit 80f00bc

Browse files
committed
FileSystem: Improve regex by only matching once instead of 3x.
1 parent cfd08f2 commit 80f00bc

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

shared/util/codeql/util/FileSystem.qll

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ module Make<InputSig Input> {
8484
*/
8585
string getAbsolutePath() { result = super.getAbsolutePath() }
8686

87+
/**
88+
* Holds if either,
89+
* - `part` is the base name of this container and `i = 1`, or
90+
* - `part` is the stem of this container and `i = 2`, or
91+
* - `part` is the extension of this container and `i = 3`.
92+
*/
93+
cached
94+
private predicate splitAbsolutePath(string part, int i) {
95+
part = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", i)
96+
}
97+
8798
/**
8899
* Gets the base name of this container including extension, that is, the last
89100
* segment of its absolute path, or the empty string if it has no segments.
@@ -101,9 +112,7 @@ module Make<InputSig Input> {
101112
* <tr><td>"//FileServer/"</td><td>""</td></tr>
102113
* </table>
103114
*/
104-
string getBaseName() {
105-
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", 1)
106-
}
115+
string getBaseName() { this.splitAbsolutePath(result, 1) }
107116

108117
/**
109118
* Gets the extension of this container, that is, the suffix of its base name
@@ -128,9 +137,7 @@ module Make<InputSig Input> {
128137
* <tr><td>"/tmp/x.tar.gz"</td><td>"gz"</td></tr>
129138
* </table>
130139
*/
131-
string getExtension() {
132-
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(\\.([^.]*))?", 3)
133-
}
140+
string getExtension() { this.splitAbsolutePath(result, 3) }
134141

135142
/** Gets the file in this container that has the given `baseName`, if any. */
136143
File getFile(string baseName) {
@@ -183,9 +190,7 @@ module Make<InputSig Input> {
183190
* <tr><td>"/tmp/x.tar.gz"</td><td>"x.tar"</td></tr>
184191
* </table>
185192
*/
186-
string getStem() {
187-
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(?:\\.([^.]*))?", 1)
188-
}
193+
string getStem() { this.splitAbsolutePath(result, 2) }
189194

190195
/**
191196
* Gets a URL representing the location of this container.

0 commit comments

Comments
 (0)