Skip to content

Commit e683204

Browse files
committed
UnboundList: Use simpler alphabet for encoding
1 parent 1509568 commit e683204

1 file changed

Lines changed: 13 additions & 22 deletions

File tree

‎shared/util/codeql/util/UnboundList.qll‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,33 @@ module Make<LocationSig Location, InputSig<Location> Input> {
5353
/** Gets the rank of element `e`, which is used internally in the string encoding. */
5454
int getRank(Element e) { e = DenseRank<DenseRankInput>::denseRank(result) }
5555

56-
/** Gets the ASCII printable excluding `.` with zero-based index `code`. */
56+
/** Gets the character that `code` represents when encoding elements. */
5757
pragma[nomagic]
58-
private string interpretAsciiCode(int code) {
59-
exists(int dot, int c |
60-
c = code + 1 and
61-
// `.` is used as element separator, so cannot be used to encode elements
62-
dot = asciiPrintable(".") and
63-
if c < dot then c = asciiPrintable(result) else c + 1 = asciiPrintable(result)
64-
)
58+
private string interpretCode(int code) {
59+
result = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".charAt(code)
6560
}
6661

67-
private int asciiCodes() { result = strictcount(interpretAsciiCode(_)) }
62+
private int codes() { result = strictcount(interpretCode(_)) }
6863

6964
/**
70-
* Gets the `i`th digit (modulo `asciiCodes()`) in a base-`asciiCodes()` integer
65+
* Gets the `i`th code (modulo `codes()`) in a base-`codes()` integer
7166
* representation of `getRank(e)`.
7267
*/
73-
private int getAsciiCodePart(Element e, int i) {
68+
private int getCodePart(Element e, int i) {
7469
result = getRank(e) and
7570
i = 0
7671
or
7772
exists(int mid |
78-
mid = getAsciiCodePart(e, i - 1) and
79-
result = mid / asciiCodes() and
73+
mid = getCodePart(e, i - 1) and
74+
result = mid / codes() and
8075
result > 0
8176
)
8277
}
8378

8479
cached
8580
private string encode(Element e) {
8681
result =
87-
strictconcat(string s, int i |
88-
s = interpretAsciiCode(getAsciiCodePart(e, i) % asciiCodes())
89-
|
90-
s order by i
91-
)
82+
strictconcat(string s, int i | s = interpretCode(getCodePart(e, i) % codes()) | s order by i)
9283
}
9384

9485
bindingset[s]
@@ -125,7 +116,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
125116
// Same as
126117
// `result = count(this.indexOf("."))`
127118
// but performs better because it doesn't use an aggregate
128-
result = this.regexpReplaceAll("[^\\.]+", "").length()
119+
result = this.regexpReplaceAll("[a-zA-Z0-9]+", "").length()
129120
}
130121

131122
/** Gets the list obtained by appending `suffix` onto this list. */
@@ -160,7 +151,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
160151
// `regexpCapture` will then always join in both groups, only to afterwards filter
161152
// based on the requested group (the group number is not part of the binding set
162153
// of `regexpCapture`)
163-
elem = this.regexpCapture("^([^\\.]+)\\..*$", 1) and
154+
elem = this.regexpCapture("^([a-zA-Z0-9]+)\\..*$", 1) and
164155
e = decode(elem) and
165156
suffix = this.suffix(elem.length() + 1)
166157
)
@@ -170,7 +161,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
170161
bindingset[this]
171162
predicate isSnoc(UnboundList prefix, Element e) {
172163
// same remark as above about not using multiple capture groups
173-
prefix = this.regexpCapture("^(|.+\\.)[^\\.]+\\.$", 1) and
164+
prefix = this.regexpCapture("^(|.+\\.)[a-zA-Z0-9]+\\.$", 1) and
174165
e = decode(this.substring(prefix.stringLength(), this.stringLength() - 1))
175166
}
176167

@@ -185,7 +176,7 @@ module Make<LocationSig Location, InputSig<Location> Input> {
185176
*/
186177
bindingset[this]
187178
UnboundList getProperPrefix(int i) {
188-
exists(string regexp, int occurrenceOffset | regexp = "[^\\.]+\\." |
179+
exists(string regexp, int occurrenceOffset | regexp = "[a-zA-Z0-9]+\\." |
189180
exists(this.regexpFind(regexp, i, occurrenceOffset)) and
190181
result = this.prefix(occurrenceOffset)
191182
)

0 commit comments

Comments
 (0)