Skip to content

Instantly share code, notes, and snippets.

@usagi
Created January 1, 2014 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/8205120 to your computer and use it in GitHub Desktop.
Save usagi/8205120 to your computer and use it in GitHub Desktop.
cmake_minimum_required(VERSION 2.8.10)
project("string-switch")
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
add_executable("string-switch" "string-switch.cxx")
#include <iostream>
#include <cstdint>
#include <stdexcept>
constexpr uint_fast64_t to_int(const char* const s, const size_t h = 0)
{ return !s[h] ? 5381ull : (to_int(s, h + 1) * 33) ^ s[h] ; }
int main(const int n, const char* const * const as)
try
{
if(n < 2)
throw std::runtime_error("require argument");
switch(to_int(as[1]))
{
#define CASE(x) \
case to_int(x): std::cout << x ": " << to_int(x) << "\n"; break;
CASE("abc")
CASE("def")
CASE("xyz")
CASE("this is a pen")
CASE("昆布茶")
CASE("ほんだし")
#undef CASE
default:
throw std::runtime_error("unkown case");
}
}
catch(const std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
return 1;
}
@usagi
Copy link
Author

usagi commented Jan 1, 2014

results:

./string-switch abc
abc: 193415941

./string-switch def
def: 193410914

./string-switch "this is a pen"
this is a pen: 1200543880250723683

./string-switch "昆布茶"
昆布茶: 18196897787883024465

./string-switch "ほんだし"
ほんだし: 12022550795961921433

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment