|
| 1 | +import codeql.util.Numbers |
| 2 | + |
| 3 | +/** |
| 4 | + * Provides properties of a Unicode code point, where the property is of 'enumeration', 'catalog', |
| 5 | + * or 'string-valued' type, however, the only supported property is `NFC_QC`. |
| 6 | + * |
| 7 | + * For example, `Block` is an enumeration property, `Line_Break` is a catalog property, and |
| 8 | + * `Uppercase_Mapping` is a string-valued property. |
| 9 | + * |
| 10 | + * For boolean properties, see `unicodeHasBooleanProperty`, and for numeric properties, see |
| 11 | + * `unicodeHasNumericProperty`. |
| 12 | + */ |
| 13 | +extensible predicate unicodeHasProperty(int codePoint, string propertyName, string propertyValue); |
| 14 | + |
| 15 | +/** |
| 16 | + * Holds when the Unicode code point's boolean property of the given name is true. |
| 17 | + * |
| 18 | + * For example, `Alphabetic` is a boolean property that can be true or false for a code point. |
| 19 | + * |
| 20 | + * For other types of properties, see `unicodeHasProperty`. |
| 21 | + */ |
| 22 | +extensible predicate unicodeHasBooleanProperty(int codePoint, string propertyName); |
| 23 | + |
| 24 | +bindingset[input] |
| 25 | +int unescapedCodePointAt(string input, int index) { |
| 26 | + exists(string match | |
| 27 | + match = input.regexpFind("(\\\\u[0-9a-fA-F]{4}|.)", index, _) and |
| 28 | + if match.matches("\\u%") |
| 29 | + then result = parseHexInt(match.substring(2, match.length())) |
| 30 | + else result = match.codePointAt(0) |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +bindingset[input] |
| 35 | +string unescapeUnicode(string input) { |
| 36 | + result = |
| 37 | + concat(int code, int idx | |
| 38 | + code = unescapedCodePointAt(input, idx) |
| 39 | + | |
| 40 | + code.toUnicode() order by idx |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +bindingset[id] |
| 45 | +predicate nonUax44IdentifierCodepoint(string id, int index) { |
| 46 | + exists(int codePoint | |
| 47 | + codePoint = id.codePointAt(index) and |
| 48 | + ( |
| 49 | + not unicodeHasBooleanProperty(codePoint, "XID_Start") and |
| 50 | + not unicodeHasBooleanProperty(codePoint, "XID_Continue") |
| 51 | + or |
| 52 | + index = 0 and |
| 53 | + not unicodeHasBooleanProperty(codePoint, "XID_Start") |
| 54 | + ) |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +bindingset[id] |
| 59 | +predicate nonNfcNormalizedCodepoint(string id, int index, string noOrMaybe) { |
| 60 | + exists(int codePoint | |
| 61 | + codePoint = id.codePointAt(index) and |
| 62 | + unicodeHasProperty(codePoint, "NFC_QC", noOrMaybe) and |
| 63 | + noOrMaybe = ["N", "M"] |
| 64 | + ) |
| 65 | +} |
0 commit comments