Skip to content

Instantly share code, notes, and snippets.

@jkotas
Last active April 9, 2016 17:22
Show Gist options
  • Save jkotas/6a6337a606565517fd396bdfeab2462d to your computer and use it in GitHub Desktop.
Save jkotas/6a6337a606565517fd396bdfeab2462d to your computer and use it in GitHub Desktop.
IsUrlSafeChar
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsUrlSafeChar(char ch)
{
/*
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
return true;
switch (ch)
{
case '-':
case '_':
case '.':
case '!':
case '*':
case '(':
case ')':
return true;
}
return false;
*/
// Optimized version of the above:
int code = (int)ch;
const int safeSpecialCharMask = 0x03FF0000 | // 0..9
1 << ((int)'!' - 0x20) | // 0x21
1 << ((int)'(' - 0x20) | // 0x28
1 << ((int)')' - 0x20) | // 0x29
1 << ((int)'*' - 0x20) | // 0x2A
1 << ((int)'-' - 0x20) | // 0x2D
1 << ((int)'.' - 0x20); // 0x2E
return ((uint)(code - 'a') <= (uint)('z' - 'a')) ||
((uint)(code - 'A') <= (uint)('Z' - 'A')) ||
((uint)(code - 0x20) <= (uint)('9' - 0x20) && ((1 << (code - 0x20)) & safeSpecialCharMask) != 0) ||
(code == (int)'_');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment