Skip to content

Instantly share code, notes, and snippets.

@deplinenoise
Created February 23, 2013 06:11
Show Gist options
  • Save deplinenoise/5018663 to your computer and use it in GitHub Desktop.
Save deplinenoise/5018663 to your computer and use it in GitHub Desktop.
uint32_t Djb2HashNoCase(const char *str_)
{
const uint8_t *str = (const uint8_t *) str_;
uint32_t hash = 5381;
int c;
while (0 != (c = *str++))
{
#if 1
// Branch free case folding for ASCII
const uint32_t cc = uint32_t(c - 'A');
const uint32_t is_upper = -(cc <= ('Z' - 'A'));
const uint32_t lower_case_c = c | 0x20;
const uint32_t nocase_c = (lower_case_c & is_upper) | (c & ~is_upper);
#else
int nocase_c = (c >= 'A') && (c <= 'Z') ? c | 0x20 : c;
#endif
hash = (hash * 33) + nocase_c;
}
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment