string_utils.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include <cstdio>
  7. #include <cstring>
  8. #include "assert.h"
  9. #include "types.h"
  10. #include "config.h"
  11. #include "macros.h"
  12. namespace crown
  13. {
  14. inline size_t strlen(const char* str)
  15. {
  16. return ::strlen(str);
  17. }
  18. inline const char* strstr(const char* str1, const char* str2)
  19. {
  20. return ::strstr(str1, str2);
  21. }
  22. inline int32_t strcmp(const char* str1, const char* str2)
  23. {
  24. return ::strcmp(str1, str2);
  25. }
  26. inline int32_t strncmp(const char* s1, const char* s2, size_t len)
  27. {
  28. return ::strncmp(s1, s2, len);
  29. }
  30. inline char* strncpy(char* dest, const char* src, size_t len)
  31. {
  32. char* ret = ::strncpy(dest, src, len);
  33. dest[len - 1] = '\0';
  34. return ret;
  35. }
  36. inline char* strcat(char* dest, const char* src)
  37. {
  38. return ::strcat(dest, src);
  39. }
  40. inline char* strncat(char* dest, const char* src, size_t len)
  41. {
  42. return ::strncat(dest, src, len);
  43. }
  44. inline const char* begin(const char* str)
  45. {
  46. CE_ASSERT(str != NULL, "Str must be != NULL");
  47. return str;
  48. }
  49. inline const char* end(const char* str)
  50. {
  51. CE_ASSERT(str != NULL, "Str must be != NULL");
  52. return str + strlen(str) + 1;
  53. }
  54. inline const char* find_first(const char* str, char c)
  55. {
  56. CE_ASSERT(str != NULL, "Str must be != NULL");
  57. const char* str_begin = begin(str);
  58. while (str_begin != end(str))
  59. {
  60. if ((*str_begin) == c)
  61. {
  62. return str_begin;
  63. }
  64. str_begin++;
  65. }
  66. return end(str);
  67. }
  68. inline const char* find_last(const char* str, char c)
  69. {
  70. CE_ASSERT(str != NULL, "Str must be != NULL");
  71. const char* str_end = end(str) - 1;
  72. while (str_end != begin(str) - 1)
  73. {
  74. if ((*str_end) == c)
  75. {
  76. return str_end;
  77. }
  78. str_end--;
  79. }
  80. return end(str);
  81. }
  82. inline void substring(const char* begin, const char* end, char* out, size_t len)
  83. {
  84. CE_ASSERT(begin != NULL, "Begin must be != NULL");
  85. CE_ASSERT(end != NULL, "End must be != NULL");
  86. CE_ASSERT(out != NULL, "Out must be != NULL");
  87. size_t i = 0;
  88. char* out_iterator = out;
  89. while (begin != end && i < len)
  90. {
  91. (*out_iterator) = (*begin);
  92. begin++;
  93. out_iterator++;
  94. i++;
  95. }
  96. out[i] = '\0';
  97. }
  98. inline int32_t parse_int(const char* string)
  99. {
  100. int val;
  101. int ok = sscanf(string, "%d", &val);
  102. CE_ASSERT(ok == 1, "Failed to parse int: %s", string);
  103. CE_UNUSED(ok);
  104. return val;
  105. }
  106. inline uint32_t parse_uint(const char* string)
  107. {
  108. unsigned int val;
  109. int ok = sscanf(string, "%u", &val);
  110. CE_ASSERT(ok == 1, "Failed to parse uint: %s", string);
  111. CE_UNUSED(ok);
  112. return val;
  113. }
  114. inline float parse_float(const char* string)
  115. {
  116. float val;
  117. int ok = sscanf(string, "%f", &val);
  118. CE_ASSERT(ok == 1, "Failed to parse float: %s", string);
  119. CE_UNUSED(ok);
  120. return val;
  121. }
  122. inline double parse_double(const char* string)
  123. {
  124. double val;
  125. int ok = sscanf(string, "%lf", &val);
  126. CE_ASSERT(ok == 1, "Failed to parse float: %s", string);
  127. CE_UNUSED(ok);
  128. return val;
  129. }
  130. /// MurmurHash2, by Austin Appleby
  131. ///
  132. /// @note
  133. /// This code makes a few assumptions about how your machine behaves
  134. ///
  135. /// 1. We can read a 4-byte value from any address without crashing
  136. /// 2. sizeof(int) == 4
  137. ///
  138. /// And it has a few limitations -
  139. ///
  140. /// 1. It will not work incrementally.
  141. /// 2. It will not produce the same results on little-endian and big-endian
  142. /// machines.
  143. inline uint32_t murmur2_32(const void* key, size_t len, uint32_t seed = 0)
  144. {
  145. CE_ASSERT_NOT_NULL(key);
  146. // 'm' and 'r' are mixing constants generated offline.
  147. // They're not really 'magic', they just happen to work well.
  148. const unsigned int m = 0x5bd1e995;
  149. const int r = 24;
  150. // Initialize the hash to a 'random' value
  151. unsigned int h = seed ^ len;
  152. // Mix 4 bytes at a time into the hash
  153. const unsigned char * data = (const unsigned char *)key;
  154. while(len >= 4)
  155. {
  156. unsigned int k = *(unsigned int *)data;
  157. k *= m;
  158. k ^= k >> r;
  159. k *= m;
  160. h *= m;
  161. h ^= k;
  162. data += 4;
  163. len -= 4;
  164. }
  165. // Handle the last few bytes of the input array
  166. switch(len)
  167. {
  168. case 3: h ^= data[2] << 16;
  169. case 2: h ^= data[1] << 8;
  170. case 1: h ^= data[0];
  171. h *= m;
  172. };
  173. // Do a few final mixes of the hash to ensure the last few
  174. // bytes are well-incorporated.
  175. h ^= h >> 13;
  176. h *= m;
  177. h ^= h >> 15;
  178. return h;
  179. }
  180. inline uint64_t murmur2_64(const void* key, int len, uint64_t seed = 0)
  181. {
  182. const uint64_t m = 0xc6a4a7935bd1e995ull;
  183. const int r = 47;
  184. uint64_t h = seed ^ (len * m);
  185. const uint64_t * data = (const uint64_t *)key;
  186. const uint64_t * end = data + (len/8);
  187. while(data != end)
  188. {
  189. uint64_t k = *data++;
  190. k *= m;
  191. k ^= k >> r;
  192. k *= m;
  193. h ^= k;
  194. h *= m;
  195. }
  196. const unsigned char * data2 = (const unsigned char*)data;
  197. switch(len & 7)
  198. {
  199. case 7: h ^= uint64_t(data2[6]) << 48;
  200. case 6: h ^= uint64_t(data2[5]) << 40;
  201. case 5: h ^= uint64_t(data2[4]) << 32;
  202. case 4: h ^= uint64_t(data2[3]) << 24;
  203. case 3: h ^= uint64_t(data2[2]) << 16;
  204. case 2: h ^= uint64_t(data2[1]) << 8;
  205. case 1: h ^= uint64_t(data2[0]);
  206. h *= m;
  207. };
  208. h ^= h >> r;
  209. h *= m;
  210. h ^= h >> r;
  211. return h;
  212. }
  213. #ifdef CROWN_DEBUG
  214. inline uint32_t HASH32(const char *s, uint32_t value)
  215. {
  216. CE_ASSERT(murmur2_32(s, strlen(s), 0) == value, "Hash mismatch");
  217. return value;
  218. }
  219. inline uint64_t HASH64(const char* s, uint64_t value)
  220. {
  221. CE_ASSERT(murmur2_64(s, strlen(s), 0) == value, "Hash mismatch");
  222. return value;
  223. }
  224. #else
  225. #define HASH32(s, v) (v)
  226. #define HASH64(s, v) (v)
  227. #endif
  228. } // namespace crown