Last active
April 9, 2016 17:22
-
-
Save jkotas/6a6337a606565517fd396bdfeab2462d to your computer and use it in GitHub Desktop.
IsUrlSafeChar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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