Created
January 1, 2014 04:36
-
-
Save usagi/8205120 to your computer and use it in GitHub Desktop.
This file contains 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
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") |
This file contains 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
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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