Skip to content

Instantly share code, notes, and snippets.

@franz1981
Created July 14, 2023 16:39
Show Gist options
  • Save franz1981/5e9e71fa34a82a381b04123a44ea5c1a to your computer and use it in GitHub Desktop.
Save franz1981/5e9e71fa34a82a381b04123a44ea5c1a to your computer and use it in GitHub Desktop.
private static final int NOT_ASCII = 128;
private static final boolean[] RESERVED = new boolean[256];
static {
final int[] reservedChars = new int[] {'+', '#', '/', ';', '?', '&', ' ', '!', '=', '$', '|', '*', ':', '~', '-'};
for (int c : reservedChars) {
RESERVED[c] = true;
}
assert !RESERVED[NOT_ASCII];
}
public static int transformToASCIIOr128(char c) {
int notAsciiMask = ((127 - c) >> 31);
return (notAsciiMask & NOT_ASCII) | (~notAsciiMask & c);
}
private static void validateLiteral(char c) {
final int asciiOr128 = transformToASCIIOr128(c);
// 0xFF is used to help the JIT to skip the bound checks on RESERVED
// given that it can proce statically that any value produced will be < 256
if (RESERVED[asciiOr128 & 0xFF]) {
throw new IllegalArgumentException("Illegal character identified in the token at");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment