Skip to main content
How are we doing? Please help us improve Stack Overflow. Take our short survey
Captain Obvlious's user avatar
Captain Obvlious's user avatar
Captain Obvlious's user avatar
Captain Obvlious
Senior Software Engineer
  • Member for 13 years, 10 months
  • Last seen this week
About

.

//////////////////////////
// strings as case labels
// this does not handle hash collisions
//////////////////////////

constexpr uint32_t djb2Hash(
    const char* str, int index = 0)
{
    return !str[index]
        ? 0x1505
        : (djb2Hash(str, index + 1) * 0x21) ^ str[index];
}

// Create a literal type for short-hand case strings
constexpr unsigned int operator"" _C(
    const char str[], size_t size)
{
    return djb2Hash(str);
}

void Bloblawblah(const std::string& cond)
{
    switch(djb2Hash(cond.c_str()))
    {
    case "Hello"_C: std::cout << "Goodbye"; break;
    case "World"_C: std::cout << "Planet"; break;
    default: std::cout << "BOGUS!";
    }
    std::cout << std::endl;
}
5
gold badges
48
silver badges
81
bronze badges
1,493
Score
579
Posts
98
Posts %
128
Score
35
Posts
6
Posts %
119
Score
37
Posts
6
Posts %
110
Score
24
Posts
4
Posts %
98
Score
25
Posts
4
Posts %
96
Score
25
Posts
4
Posts %