Advertisement
JoshDreamland

C++ String Switch

Dec 14th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1.     while (sscanf(remainder, "%s", buf) == 1) {
  2.       string flag = buf;
  3.       switch (cleanHash(flag.c_str(), flag.length())) {
  4.         case str_case(flag, "OnCeil") {
  5.           ...
  6.           break;
  7.         }
  8.         case str_case(flag, "OnFaces") {
  9.           ...
  10.           break;
  11.         }
  12.         case str_case(flag, "OnFloor") {
  13.           ...
  14.           break;
  15.         }
  16.         case str_case(flag, "OnSurf") {
  17.           ...
  18.           break;
  19.         }
  20.         case str_case(flag, "OnTorch") {
  21.           ...
  22.           break;
  23.         }
  24.         case str_case(flag, "OnWall") {
  25.           ...
  26.           break;
  27.         }
  28.       }
  29.  
  30.  
  31.  
  32. // The magic
  33. constexpr int filthyHash(const char *x, int y, int z, int res) {
  34.   return z < y
  35.       ? filthyHash(x, y, z + 1, (int)((long)res * 31 + x[z]))
  36.       : res;
  37. }
  38. constexpr int cleanHash(const char* x, int y) {
  39.   return filthyHash(x, y, 0, 0) << 4 | (y & ((1 << 4) - 1));
  40. }
  41. template<size_t N> constexpr int dirtyHash(const char (&x)[N]) {
  42.   return cleanHash(x, N);
  43. }
  44. #define str_case(key, x) dirtyHash(x): if ((key) == (x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement