PageRenderTime 74ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/kyotocabinet-1.2.70/kcutil.h

#
C Header | 2830 lines | 1703 code | 376 blank | 751 comment | 729 complexity | 425375799fd136a460e21845f3c9f4b9 MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /*************************************************************************************************
  2. * Utility functions
  3. * Copyright (C) 2009-2011 FAL Labs
  4. * This file is part of Kyoto Cabinet.
  5. * This program is free software: you can redistribute it and/or modify it under the terms of
  6. * the GNU General Public License as published by the Free Software Foundation, either version
  7. * 3 of the License, or any later version.
  8. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  9. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. * You should have received a copy of the GNU General Public License along with this program.
  12. * If not, see <http://www.gnu.org/licenses/>.
  13. *************************************************************************************************/
  14. #ifndef _KCUTIL_H // duplication check
  15. #define _KCUTIL_H
  16. #include <kccommon.h>
  17. namespace kyotocabinet { // common namespace
  18. /** The maximum value of int8_t. */
  19. const int8_t INT8MAX = (std::numeric_limits<int8_t>::max)();
  20. /** The maximum value of int16_t. */
  21. const int16_t INT16MAX = (std::numeric_limits<int16_t>::max)();
  22. /** The maximum value of int32_t. */
  23. const int32_t INT32MAX = (std::numeric_limits<int32_t>::max)();
  24. /** The maximum value of int64_t. */
  25. const int64_t INT64MAX = (std::numeric_limits<int64_t>::max)();
  26. /** The minimum value of int8_t. */
  27. const int8_t INT8MIN = (std::numeric_limits<int8_t>::min)();
  28. /** The minimum value of int16_t. */
  29. const int16_t INT16MIN = (std::numeric_limits<int16_t>::min)();
  30. /** The minimum value of int32_t. */
  31. const int32_t INT32MIN = (std::numeric_limits<int32_t>::min)();
  32. /** The minimum value of int64_t. */
  33. const int64_t INT64MIN = (std::numeric_limits<int64_t>::min)();
  34. /** The maximum value of uint8_t. */
  35. const uint8_t UINT8MAX = (std::numeric_limits<uint8_t>::max)();
  36. /** The maximum value of uint16_t. */
  37. const uint16_t UINT16MAX = (std::numeric_limits<uint16_t>::max)();
  38. /** The maximum value of uint32_t. */
  39. const uint32_t UINT32MAX = (std::numeric_limits<uint32_t>::max)();
  40. /** The maximum value of uint64_t. */
  41. const uint64_t UINT64MAX = (std::numeric_limits<uint64_t>::max)();
  42. /** The maximum value of size_t. */
  43. const size_t SIZEMAX = (std::numeric_limits<size_t>::max)();
  44. /** The maximum value of float. */
  45. const float FLTMAX = (std::numeric_limits<float>::max)();
  46. /** The maximum value of double. */
  47. const double DBLMAX = (std::numeric_limits<double>::max)();
  48. /** An alias of hash map of strings. */
  49. typedef std::unordered_map<std::string, std::string> StringHashMap;
  50. /** An alias of tree map of strings. */
  51. typedef std::map<std::string, std::string> StringTreeMap;
  52. /** The package version. */
  53. extern const char* const VERSION;
  54. /** The library version. */
  55. extern const int32_t LIBVER;
  56. /** The library revision. */
  57. extern const int32_t LIBREV;
  58. /** The database format version. */
  59. extern const int32_t FMTVER;
  60. /** The system name. */
  61. extern const char* const OSNAME;
  62. /** The flag for big endian environments. */
  63. extern const bool BIGEND;
  64. /** The clock tick of interruption. */
  65. extern const int32_t CLOCKTICK;
  66. /** The size of a page. */
  67. extern const int32_t PAGESIZ;
  68. /** The extra feature list. */
  69. extern const char* const FEATURES;
  70. /** The buffer size for numeric data. */
  71. const size_t NUMBUFSIZ = 32;
  72. /** The maximum memory size for debugging. */
  73. const size_t MEMMAXSIZ = INT32MAX / 2;
  74. /**
  75. * Convert a decimal string to an integer.
  76. * @param str the decimal string.
  77. * @return the integer. If the string does not contain numeric expression, 0 is returned.
  78. */
  79. int64_t atoi(const char* str);
  80. /**
  81. * Convert a decimal string with a metric prefix to an integer.
  82. * @param str the decimal string, which can be trailed by a binary metric prefix. "K", "M", "G",
  83. * "T", "P", and "E" are supported. They are case-insensitive.
  84. * @return the integer. If the string does not contain numeric expression, 0 is returned. If
  85. * the integer overflows the domain, kyotocabinet::INT64MAX or kyotocabinet::INT64_MIN is
  86. * returned according to the sign.
  87. */
  88. int64_t atoix(const char* str);
  89. /**
  90. * Convert a hexadecimal string to an integer.
  91. * @param str the hexadecimal string.
  92. * @return the integer. If the string does not contain numeric expression, 0 is returned.
  93. */
  94. int64_t atoih(const char* str);
  95. /**
  96. * Convert a decimal byte array to an integer.
  97. * @param ptr the decimal byte array.
  98. * @param size the size of the decimal byte array.
  99. * @return the integer. If the string does not contain numeric expression, 0 is returned.
  100. */
  101. int64_t atoin(const char* ptr, size_t size);
  102. /**
  103. * Convert a decimal string to a real number.
  104. * @param str the decimal string.
  105. * @return the real number. If the string does not contain numeric expression, 0.0 is returned.
  106. */
  107. double atof(const char* str);
  108. /**
  109. * Convert a decimal byte array to a real number.
  110. * @param ptr the decimal byte array.
  111. * @param size the size of the decimal byte array.
  112. * @return the real number. If the string does not contain numeric expression, 0.0 is returned.
  113. */
  114. double atofn(const char* ptr, size_t size);
  115. /**
  116. * Normalize a 16-bit number in the native order into the network byte order.
  117. * @param num the 16-bit number in the native order.
  118. * @return the number in the network byte order.
  119. */
  120. uint16_t hton16(uint16_t num);
  121. /**
  122. * Normalize a 32-bit number in the native order into the network byte order.
  123. * @param num the 32-bit number in the native order.
  124. * @return the number in the network byte order.
  125. */
  126. uint32_t hton32(uint32_t num);
  127. /**
  128. * Normalize a 64-bit number in the native order into the network byte order.
  129. * @param num the 64-bit number in the native order.
  130. * @return the number in the network byte order.
  131. */
  132. uint64_t hton64(uint64_t num);
  133. /**
  134. * Denormalize a 16-bit number in the network byte order into the native order.
  135. * @param num the 16-bit number in the network byte order.
  136. * @return the converted number in the native order.
  137. */
  138. uint16_t ntoh16(uint16_t num);
  139. /**
  140. * Denormalize a 32-bit number in the network byte order into the native order.
  141. * @param num the 32-bit number in the network byte order.
  142. * @return the converted number in the native order.
  143. */
  144. uint32_t ntoh32(uint32_t num);
  145. /**
  146. * Denormalize a 64-bit number in the network byte order into the native order.
  147. * @param num the 64-bit number in the network byte order.
  148. * @return the converted number in the native order.
  149. */
  150. uint64_t ntoh64(uint64_t num);
  151. /**
  152. * Write a number in fixed length format into a buffer.
  153. * @param buf the desitination buffer.
  154. * @param num the number.
  155. * @param width the width.
  156. */
  157. void writefixnum(void* buf, uint64_t num, size_t width);
  158. /**
  159. * Read a number in fixed length format from a buffer.
  160. * @param buf the source buffer.
  161. * @param width the width.
  162. * @return the read number.
  163. */
  164. uint64_t readfixnum(const void* buf, size_t width);
  165. /**
  166. * Write a number in variable length format into a buffer.
  167. * @param buf the desitination buffer.
  168. * @param num the number.
  169. * @return the length of the written region.
  170. */
  171. size_t writevarnum(void* buf, uint64_t num);
  172. /**
  173. * Read a number in variable length format from a buffer.
  174. * @param buf the source buffer.
  175. * @param size the size of the source buffer.
  176. * @param np the pointer to the variable into which the read number is assigned.
  177. * @return the length of the read region, or 0 on failure.
  178. */
  179. size_t readvarnum(const void* buf, size_t size, uint64_t* np);
  180. /**
  181. * Check the size of variable length format of a number.
  182. * @return the size of variable length format.
  183. */
  184. size_t sizevarnum(uint64_t num);
  185. /**
  186. * Get the hash value by MurMur hashing.
  187. * @param buf the source buffer.
  188. * @param size the size of the source buffer.
  189. * @return the hash value.
  190. */
  191. uint64_t hashmurmur(const void* buf, size_t size);
  192. /**
  193. * Get the hash value by FNV hashing.
  194. * @param buf the source buffer.
  195. * @param size the size of the source buffer.
  196. * @return the hash value.
  197. */
  198. uint64_t hashfnv(const void* buf, size_t size);
  199. /**
  200. * Get the hash value suitable for a file name.
  201. * @param buf the source buffer.
  202. * @param size the size of the source buffer.
  203. * @param obuf the buffer into which the result hash string is written. It must be more than
  204. * NUMBUFSIZ.
  205. * @return the auxiliary hash value.
  206. */
  207. uint32_t hashpath(const void* buf, size_t size, char* obuf);
  208. /**
  209. * Get a prime number nearby a number.
  210. * @param num a natural number.
  211. * @return the result number.
  212. */
  213. uint64_t nearbyprime(uint64_t num);
  214. /**
  215. * Get the quiet Not-a-Number value.
  216. * @return the quiet Not-a-Number value.
  217. */
  218. double nan();
  219. /**
  220. * Get the positive infinity value.
  221. * @return the positive infinity value.
  222. */
  223. double inf();
  224. /**
  225. * Check a number is a Not-a-Number value.
  226. * @return true for the number is a Not-a-Number value, or false if not.
  227. */
  228. bool chknan(double num);
  229. /**
  230. * Check a number is an infinity value.
  231. * @return true for the number is an infinity value, or false if not.
  232. */
  233. bool chkinf(double num);
  234. /**
  235. * Append a formatted string at the end of a string.
  236. * @param dest the destination string.
  237. * @param format the printf-like format string. The conversion character `%' can be used with
  238. * such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', and `%'.
  239. * @param ap used according to the format string.
  240. */
  241. void vstrprintf(std::string* dest, const char* format, va_list ap);
  242. /**
  243. * Append a formatted string at the end of a string.
  244. * @param dest the destination string.
  245. * @param format the printf-like format string. The conversion character `%' can be used with
  246. * such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', and `%'.
  247. * @param ... used according to the format string.
  248. */
  249. void strprintf(std::string* dest, const char* format, ...);
  250. /**
  251. * Generate a formatted string.
  252. * @param format the printf-like format string. The conversion character `%' can be used with
  253. * such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', and `%'.
  254. * @param ... used according to the format string.
  255. * @return the result string.
  256. */
  257. std::string strprintf(const char* format, ...);
  258. /**
  259. * Split a string with a delimiter.
  260. * @param str the string.
  261. * @param delim the delimiter.
  262. * @param elems a vector object into which the result elements are pushed.
  263. * @return the number of result elements.
  264. */
  265. size_t strsplit(const std::string& str, char delim, std::vector<std::string>* elems);
  266. /**
  267. * Split a string with delimiters.
  268. * @param str the string.
  269. * @param delims the delimiters.
  270. * @param elems a vector object into which the result elements are pushed.
  271. * @return the number of result elements.
  272. */
  273. size_t strsplit(const std::string& str, const std::string& delims,
  274. std::vector<std::string>* elems);
  275. /**
  276. * Convert the letters of a string into upper case.
  277. * @param str the string to convert.
  278. * @return the string itself.
  279. */
  280. std::string* strtoupper(std::string* str);
  281. /**
  282. * Convert the letters of a string into lower case.
  283. * @param str the string to convert.
  284. * @return the string itself.
  285. */
  286. std::string* strtolower(std::string* str);
  287. /**
  288. * Check whether a string begins with a key.
  289. * @param str the string.
  290. * @param key the forward matching key string.
  291. * @return true if the target string begins with the key, else, it is false.
  292. */
  293. bool strfwm(const std::string& str, const std::string& key);
  294. /**
  295. * Check whether a string ends with a key.
  296. * @param str the string.
  297. * @param key the backward matching key string.
  298. * @return true if the target string ends with the key, else, it is false.
  299. */
  300. bool strbwm(const std::string& str, const std::string& key);
  301. /**
  302. * Cut space characters at head or tail of a string.
  303. * @param str the string to convert.
  304. * @return the string itself.
  305. */
  306. std::string* strtrim(std::string* str);
  307. /**
  308. * Convert a UTF-8 string into a UCS-4 array.
  309. * @param src the source object.
  310. * @param dest the destination object.
  311. */
  312. void strutftoucs(const std::string& src, std::vector<uint32_t>* dest);
  313. /**
  314. * Convert a UCS-4 array into a UTF-8 string.
  315. * @param src the source object.
  316. * @param dest the destination object.
  317. */
  318. void strucstoutf(const std::vector<uint32_t>& src, std::string* dest);
  319. /**
  320. * Serialize a string vector object into a string object.
  321. * @param src the source object.
  322. * @param dest the destination object.
  323. */
  324. void strvecdump(const std::vector<std::string>& src, std::string* dest);
  325. /**
  326. * Deserialize a string object into a string vector object.
  327. * @param src the source object.
  328. * @param dest the destination object.
  329. */
  330. void strvecload(const std::string& src, std::vector<std::string>* dest);
  331. /**
  332. * Serialize a string vector object into a string object.
  333. * @param src the source object.
  334. * @param dest the destination object.
  335. */
  336. void strmapdump(const std::map<std::string, std::string>& src, std::string* dest);
  337. /**
  338. * Deserialize a string object into a string map object.
  339. * @param src the source object.
  340. * @param dest the destination object.
  341. */
  342. void strmapload(const std::string& src, std::map<std::string, std::string>* dest);
  343. /**
  344. * Encode a serial object by hexadecimal encoding.
  345. * @param buf the pointer to the region.
  346. * @param size the size of the region.
  347. * @return the result string.
  348. * @note Because the region of the return value is allocated with the the new[] operator, it
  349. * should be released with the delete[] operator when it is no longer in use.
  350. */
  351. char* hexencode(const void* buf, size_t size);
  352. /**
  353. * Decode a string encoded by hexadecimal encoding.
  354. * @param str specifies the encoded string.
  355. * @param sp the pointer to the variable into which the size of the region of the return value
  356. * is assigned.
  357. * @return the pointer to the region of the result.
  358. * @note Because an additional zero code is appended at the end of the region of the return
  359. * value, the return value can be treated as a character string. Because the region of the
  360. * return value is allocated with the the new[] operator, it should be released with the delete[]
  361. * operator when it is no longer in use.
  362. */
  363. char* hexdecode(const char* str, size_t* sp);
  364. /**
  365. * Encode a serial object by URL encoding.
  366. * @param buf the pointer to the region.
  367. * @param size the size of the region.
  368. * @return the result string.
  369. * @note Because the region of the return value is allocated with the the new[] operator, it
  370. * should be released with the delete[] operator when it is no longer in use.
  371. */
  372. char* urlencode(const void* buf, size_t size);
  373. /**
  374. * Decode a string encoded by URL encoding.
  375. * @param str specifies the encoded string.
  376. * @param sp the pointer to the variable into which the size of the region of the return value
  377. * is assigned.
  378. * @return the pointer to the region of the result.
  379. * @note Because an additional zero code is appended at the end of the region of the return
  380. * value, the return value can be treated as a character string. Because the region of the
  381. * return value is allocated with the the new[] operator, it should be released with the delete[]
  382. * operator when it is no longer in use.
  383. */
  384. char* urldecode(const char* str, size_t* sp);
  385. /**
  386. * Encode a serial object by Quoted-printable encoding.
  387. * @param buf the pointer to the region.
  388. * @param size the size of the region.
  389. * @return the result string.
  390. * @note Because the region of the return value is allocated with the the new[] operator, it
  391. * should be released with the delete[] operator when it is no longer in use.
  392. */
  393. char* quoteencode(const void* buf, size_t size);
  394. /**
  395. * Decode a string encoded by Quoted-printable encoding.
  396. * @param str specifies the encoded string.
  397. * @param sp the pointer to the variable into which the size of the region of the return value
  398. * is assigned.
  399. * @return the pointer to the region of the result.
  400. * @note Because an additional zero code is appended at the end of the region of the return
  401. * value, the return value can be treated as a character string. Because the region of the
  402. * return value is allocated with the the new[] operator, it should be released with the delete[]
  403. * operator when it is no longer in use.
  404. */
  405. char* quotedecode(const char* str, size_t* sp);
  406. /**
  407. * Encode a serial object by Base64 encoding.
  408. * @param buf the pointer to the region.
  409. * @param size the size of the region.
  410. * @return the result string.
  411. * @note Because the region of the return value is allocated with the the new[] operator, it
  412. * should be released with the delete[] operator when it is no longer in use.
  413. */
  414. char* baseencode(const void* buf, size_t size);
  415. /**
  416. * Decode a string encoded by Base64 encoding.
  417. * @param str specifies the encoded string.
  418. * @param sp the pointer to the variable into which the size of the region of the return value
  419. * is assigned.
  420. * @return the pointer to the region of the result.
  421. * @note Because an additional zero code is appended at the end of the region of the return
  422. * value, the return value can be treated as a character string. Because the region of the
  423. * return value is allocated with the the new[] operator, it should be released with the delete[]
  424. * operator when it is no longer in use.
  425. */
  426. char* basedecode(const char* str, size_t* sp);
  427. /**
  428. * Cipher or decipher a serial object with the Arcfour stream cipher.
  429. * @param ptr the pointer to the region.
  430. * @param size the size of the region.
  431. * @param kbuf the pointer to the region of the cipher key.
  432. * @param ksiz the size of the region of the cipher key.
  433. * @param obuf the pointer to the region into which the result data is written. The size of the
  434. * buffer should be equal to or more than the input region. The region can be the same as the
  435. * source region.
  436. */
  437. void arccipher(const void* ptr, size_t size, const void* kbuf, size_t ksiz, void* obuf);
  438. /**
  439. * Duplicate a region on memory.
  440. * @param ptr the source buffer.
  441. * @param size the size of the source buffer.
  442. * @note Because the region of the return value is allocated with the the new[] operator, it
  443. * should be released with the delete[] operator when it is no longer in use.
  444. */
  445. char* memdup(const char* ptr, size_t size);
  446. /**
  447. * Compare two regions by case insensitive evaluation.
  448. * @param abuf a buffer.
  449. * @param bbuf the other buffer.
  450. * @param size the size of each buffer.
  451. * @return positive if the former is big, negative if the latter is big, 0 if both are
  452. * equivalent.
  453. */
  454. int32_t memicmp(const void* abuf, const void* bbuf, size_t size);
  455. /**
  456. * Find the first occurrence of a sub pattern.
  457. * @param hbuf the target pattern buffer.
  458. * @param hsiz the size of the target pattern buffer.
  459. * @param nbuf the sub pattern buffer.
  460. * @param nsiz the size of the sub pattern buffer.
  461. * @return the pointer to the beginning of the sub pattern in the target pattern buffer, or NULL
  462. * if the sub pattern is not found.
  463. */
  464. void* memmem(const void* hbuf, size_t hsiz, const void* nbuf, size_t nsiz);
  465. /**
  466. * Find the first occurrence of a sub pattern by case insensitive evaluation.
  467. * @param hbuf the target pattern buffer.
  468. * @param hsiz the size of the target pattern buffer.
  469. * @param nbuf the sub pattern buffer.
  470. * @param nsiz the size of the sub pattern buffer.
  471. * @return the pointer to the beginning of the sub pattern in the target pattern buffer, or NULL
  472. * if the sub pattern is not found.
  473. */
  474. void* memimem(const void* hbuf, size_t hsiz, const void* nbuf, size_t nsiz);
  475. /**
  476. * Duplicate a string on memory.
  477. * @param str the source string.
  478. * @note Because the region of the return value is allocated with the the new[] operator, it
  479. * should be released with the delete[] operator when it is no longer in use.
  480. */
  481. char* strdup(const char* str);
  482. /**
  483. * Convert the letters of a string into upper case.
  484. * @param str the string to convert.
  485. * @return the string itself.
  486. */
  487. char* strtoupper(char* str);
  488. /**
  489. * Convert the letters of a string into lower case.
  490. * @param str the string to convert.
  491. * @return the string itself.
  492. */
  493. char* strtolower(char* str);
  494. /**
  495. * Cut space characters at head or tail of a string.
  496. * @param str the string to convert.
  497. * @return the string itself.
  498. */
  499. char* strtrim(char* str);
  500. /**
  501. * Squeeze space characters in a string and trim it.
  502. * @param str the string to convert.
  503. * @return the string itself.
  504. */
  505. char* strsqzspc(char* str);
  506. /**
  507. * Normalize space characters in a string and trim it.
  508. * @param str the string to convert.
  509. * @return the string itself.
  510. */
  511. char* strnrmspc(char* str);
  512. /**
  513. * Compare two strings by case insensitive evaluation.
  514. * @param astr a string.
  515. * @param bstr the other string.
  516. * @return positive if the former is big, negative if the latter is big, 0 if both are
  517. * equivalent.
  518. */
  519. int32_t stricmp(const char* astr, const char* bstr);
  520. /**
  521. * Find the first occurrence of a substring by case insensitive evaluation.
  522. * @param hstr the target string.
  523. * @param nstr the substring.
  524. * @return the pointer to the beginning of the substring in the target string, or NULL if the
  525. * substring is not found.
  526. */
  527. char* stristr(const char* hstr, const char* nstr);
  528. /**
  529. * Check whether a string begins with a key.
  530. * @param str the string.
  531. * @param key the forward matching key string.
  532. * @return true if the target string begins with the key, else, it is false.
  533. */
  534. bool strfwm(const char* str, const char* key);
  535. /**
  536. * Check whether a string begins with a key by case insensitive evaluation.
  537. * @param str the string.
  538. * @param key the forward matching key string.
  539. * @return true if the target string begins with the key, else, it is false.
  540. */
  541. bool strifwm(const char* str, const char* key);
  542. /**
  543. * Check whether a string ends with a key.
  544. * @param str the string.
  545. * @param key the backward matching key string.
  546. * @return true if the target string ends with the key, else, it is false.
  547. */
  548. bool strbwm(const char* str, const char* key);
  549. /**
  550. * Check whether a string ends with a key by case insensitive evaluation.
  551. * @param str the string.
  552. * @param key the backward matching key string.
  553. * @return true if the target string ends with the key, else, it is false.
  554. */
  555. bool stribwm(const char* str, const char* key);
  556. /**
  557. * Get the number of characters in a UTF-8 string.
  558. * @param str the UTF-8 string.
  559. * @return the number of characters in the string.
  560. */
  561. size_t strutflen(const char* str);
  562. /**
  563. * Convert a UTF-8 string into a UCS-4 array.
  564. * @param src the source object.
  565. * @param dest the destination object. It must have enough size.
  566. * @param np the pointer to the variable into which the number of elements in the destination
  567. * object is assgined.
  568. */
  569. void strutftoucs(const char* src, uint32_t* dest, size_t* np);
  570. /**
  571. * Convert a UCS-4 array into a UTF-8 string.
  572. * @param src the source object.
  573. * @param snum the number of elements in the source object.
  574. * @param dest the destination object. It must have enough size.
  575. * @return the size of the result string.
  576. */
  577. size_t strucstoutf(const uint32_t* src, size_t snum, char* dest);
  578. /**
  579. * Allocate a region on memory.
  580. * @param size the size of the region.
  581. * @return the pointer to the allocated region.
  582. */
  583. void* xmalloc(size_t size);
  584. /**
  585. * Allocate a nullified region on memory.
  586. * @param nmemb the number of elements.
  587. * @param size the size of each element.
  588. * @return the pointer to the allocated region.
  589. */
  590. void* xcalloc(size_t nmemb, size_t size);
  591. /**
  592. * Re-allocate a region on memory.
  593. * @param ptr the pointer to the region.
  594. * @param size the size of the region.
  595. * @return the pointer to the re-allocated region.
  596. */
  597. void* xrealloc(void* ptr, size_t size);
  598. /**
  599. * Free a region on memory.
  600. * @param ptr the pointer to the region.
  601. */
  602. void xfree(void* ptr);
  603. /**
  604. * Allocate a nullified region on mapped memory.
  605. * @param size the size of the region.
  606. * @return the pointer to the allocated region. It should be released with the memfree call.
  607. */
  608. void* mapalloc(size_t size);
  609. /**
  610. * Free a region on mapped memory.
  611. * @param ptr the pointer to the allocated region.
  612. */
  613. void mapfree(void* ptr);
  614. /**
  615. * Get the time of day in seconds.
  616. * @return the time of day in seconds. The accuracy is in microseconds.
  617. */
  618. double time();
  619. /**
  620. * Get the process ID.
  621. * @return the process ID.
  622. */
  623. int64_t getpid();
  624. /**
  625. * Get the value of an environment variable.
  626. * @return the value of the environment variable, or NULL on failure.
  627. */
  628. const char* getenv(const char* name);
  629. /**
  630. * Get system information of the environment.
  631. * @param strmap a string map to contain the result.
  632. */
  633. void getsysinfo(std::map<std::string, std::string>* strmap);
  634. /**
  635. * Set the standard streams into the binary mode.
  636. */
  637. void setstdiobin();
  638. /**
  639. * Dummy test driver.
  640. * @return always true.
  641. */
  642. bool _dummytest();
  643. /**
  644. * Convert a decimal string to an integer.
  645. */
  646. inline int64_t atoi(const char* str) {
  647. _assert_(str);
  648. while (*str > '\0' && *str <= ' ') {
  649. str++;
  650. }
  651. int32_t sign = 1;
  652. int64_t num = 0;
  653. if (*str == '-') {
  654. str++;
  655. sign = -1;
  656. } else if (*str == '+') {
  657. str++;
  658. }
  659. while (*str != '\0') {
  660. if (*str < '0' || *str > '9') break;
  661. num = num * 10 + *str - '0';
  662. str++;
  663. }
  664. return num * sign;
  665. }
  666. /**
  667. * Convert a decimal string with a metric prefix to an integer.
  668. */
  669. inline int64_t atoix(const char* str) {
  670. _assert_(str);
  671. while (*str > '\0' && *str <= ' ') {
  672. str++;
  673. }
  674. int32_t sign = 1;
  675. if (*str == '-') {
  676. str++;
  677. sign = -1;
  678. } else if (*str == '+') {
  679. str++;
  680. }
  681. long double num = 0;
  682. while (*str != '\0') {
  683. if (*str < '0' || *str > '9') break;
  684. num = num * 10 + *str - '0';
  685. str++;
  686. }
  687. if (*str == '.') {
  688. str++;
  689. long double base = 10;
  690. while (*str != '\0') {
  691. if (*str < '0' || *str > '9') break;
  692. num += (*str - '0') / base;
  693. str++;
  694. base *= 10;
  695. }
  696. }
  697. num *= sign;
  698. while (*str > '\0' && *str <= ' ') {
  699. str++;
  700. }
  701. if (*str == 'k' || *str == 'K') {
  702. num *= 1LL << 10;
  703. } else if (*str == 'm' || *str == 'M') {
  704. num *= 1LL << 20;
  705. } else if (*str == 'g' || *str == 'G') {
  706. num *= 1LL << 30;
  707. } else if (*str == 't' || *str == 'T') {
  708. num *= 1LL << 40;
  709. } else if (*str == 'p' || *str == 'P') {
  710. num *= 1LL << 50;
  711. } else if (*str == 'e' || *str == 'E') {
  712. num *= 1LL << 60;
  713. }
  714. if (num > INT64MAX) return INT64MAX;
  715. if (num < INT64MIN) return INT64MIN;
  716. return (int64_t)num;
  717. }
  718. /**
  719. * Convert a hexadecimal string to an integer.
  720. */
  721. inline int64_t atoih(const char* str) {
  722. _assert_(str);
  723. while (*str > '\0' && *str <= ' ') {
  724. str++;
  725. }
  726. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
  727. str += 2;
  728. }
  729. int64_t num = 0;
  730. while (true) {
  731. if (*str >= '0' && *str <= '9') {
  732. num = num * 0x10 + *str - '0';
  733. } else if (*str >= 'a' && *str <= 'f') {
  734. num = num * 0x10 + *str - 'a' + 10;
  735. } else if (*str >= 'A' && *str <= 'F') {
  736. num = num * 0x10 + *str - 'A' + 10;
  737. } else {
  738. break;
  739. }
  740. str++;
  741. }
  742. return num;
  743. }
  744. /**
  745. * Convert a decimal byte array to an integer.
  746. */
  747. inline int64_t atoin(const char* ptr, size_t size) {
  748. _assert_(ptr && size <= MEMMAXSIZ);
  749. while (size > 0 && *ptr >= '\0' && *ptr <= ' ') {
  750. ptr++;
  751. size--;
  752. }
  753. int32_t sign = 1;
  754. int64_t num = 0;
  755. if (size > 0) {
  756. if (*ptr == '-') {
  757. ptr++;
  758. size--;
  759. sign = -1;
  760. } else if (*ptr == '+') {
  761. ptr++;
  762. size--;
  763. }
  764. }
  765. while (size > 0) {
  766. if (*ptr < '0' || *ptr > '9') break;
  767. num = num * 10 + *ptr - '0';
  768. ptr++;
  769. size--;
  770. }
  771. return num * sign;
  772. }
  773. /**
  774. * Convert a decimal string to a real number.
  775. */
  776. inline double atof(const char* str) {
  777. _assert_(str);
  778. while (*str > '\0' && *str <= ' ') {
  779. str++;
  780. }
  781. int32_t sign = 1;
  782. if (*str == '-') {
  783. str++;
  784. sign = -1;
  785. } else if (*str == '+') {
  786. str++;
  787. }
  788. if ((str[0] == 'i' || str[0] == 'I') && (str[1] == 'n' || str[1] == 'N') &&
  789. (str[2] == 'f' || str[2] == 'F')) return HUGE_VAL * sign;
  790. if ((str[0] == 'n' || str[0] == 'N') && (str[1] == 'a' || str[1] == 'A') &&
  791. (str[2] == 'n' || str[2] == 'N')) return nan();
  792. long double num = 0;
  793. int32_t col = 0;
  794. while (*str != '\0') {
  795. if (*str < '0' || *str > '9') break;
  796. num = num * 10 + *str - '0';
  797. str++;
  798. if (num > 0) col++;
  799. }
  800. if (*str == '.') {
  801. str++;
  802. long double fract = 0.0;
  803. long double base = 10;
  804. while (col < 16 && *str != '\0') {
  805. if (*str < '0' || *str > '9') break;
  806. fract += (*str - '0') / base;
  807. str++;
  808. col++;
  809. base *= 10;
  810. }
  811. num += fract;
  812. }
  813. if (*str == 'e' || *str == 'E') {
  814. str++;
  815. num *= std::pow((long double)10, (long double)atoi(str));
  816. }
  817. return num * sign;
  818. }
  819. /**
  820. * Convert a decimal byte array to a real number.
  821. */
  822. inline double atofn(const char* ptr, size_t size) {
  823. _assert_(ptr && size <= MEMMAXSIZ);
  824. while (size > 0 && *ptr >= '\0' && *ptr <= ' ') {
  825. ptr++;
  826. size--;
  827. }
  828. int32_t sign = 1;
  829. if (size > 0) {
  830. if (*ptr == '-') {
  831. ptr++;
  832. size--;
  833. sign = -1;
  834. } else if (*ptr == '+') {
  835. ptr++;
  836. size--;
  837. }
  838. }
  839. if (size > 2) {
  840. if ((ptr[0] == 'i' || ptr[0] == 'I') && (ptr[1] == 'n' || ptr[1] == 'N') &&
  841. (ptr[2] == 'f' || ptr[2] == 'F')) return HUGE_VAL * sign;
  842. if ((ptr[0] == 'n' || ptr[0] == 'N') && (ptr[1] == 'a' || ptr[1] == 'A') &&
  843. (ptr[2] == 'n' || ptr[2] == 'N')) return nan();
  844. }
  845. long double num = 0;
  846. int32_t col = 0;
  847. while (size > 0) {
  848. if (*ptr < '0' || *ptr > '9') break;
  849. num = num * 10 + *ptr - '0';
  850. ptr++;
  851. size--;
  852. if (num > 0) col++;
  853. }
  854. if (size > 0 && *ptr == '.') {
  855. ptr++;
  856. size--;
  857. long double fract = 0.0;
  858. long double base = 10;
  859. while (col < 16 && size > 0) {
  860. if (*ptr < '0' || *ptr > '9') break;
  861. fract += (*ptr - '0') / base;
  862. ptr++;
  863. size--;
  864. col++;
  865. base *= 10;
  866. }
  867. num += fract;
  868. }
  869. if (size > 0 && (*ptr == 'e' || *ptr == 'E')) {
  870. ptr++;
  871. size--;
  872. num *= std::pow((long double)10, (long double)atoin(ptr, size));
  873. }
  874. return num * sign;
  875. }
  876. /**
  877. * Normalize a 16-bit number in the native order into the network byte order.
  878. */
  879. inline uint16_t hton16(uint16_t num) {
  880. _assert_(true);
  881. if (BIGEND) return num;
  882. return ((num & 0x00ffU) << 8) | ((num & 0xff00U) >> 8);
  883. }
  884. /**
  885. * Normalize a 32-bit number in the native order into the network byte order.
  886. */
  887. inline uint32_t hton32(uint32_t num) {
  888. _assert_(true);
  889. if (BIGEND) return num;
  890. return ((num & 0x000000ffUL) << 24) | ((num & 0x0000ff00UL) << 8) | \
  891. ((num & 0x00ff0000UL) >> 8) | ((num & 0xff000000UL) >> 24);
  892. }
  893. /**
  894. * Normalize a 64-bit number in the native order into the network byte order.
  895. */
  896. inline uint64_t hton64(uint64_t num) {
  897. _assert_(true);
  898. if (BIGEND) return num;
  899. return ((num & 0x00000000000000ffULL) << 56) | ((num & 0x000000000000ff00ULL) << 40) |
  900. ((num & 0x0000000000ff0000ULL) << 24) | ((num & 0x00000000ff000000ULL) << 8) |
  901. ((num & 0x000000ff00000000ULL) >> 8) | ((num & 0x0000ff0000000000ULL) >> 24) |
  902. ((num & 0x00ff000000000000ULL) >> 40) | ((num & 0xff00000000000000ULL) >> 56);
  903. }
  904. /**
  905. * Denormalize a 16-bit number in the network byte order into the native order.
  906. */
  907. inline uint16_t ntoh16(uint16_t num) {
  908. _assert_(true);
  909. return hton16(num);
  910. }
  911. /**
  912. * Denormalize a 32-bit number in the network byte order into the native order.
  913. */
  914. inline uint32_t ntoh32(uint32_t num) {
  915. _assert_(true);
  916. return hton32(num);
  917. }
  918. /**
  919. * Denormalize a 64-bit number in the network byte order into the native order.
  920. */
  921. inline uint64_t ntoh64(uint64_t num) {
  922. _assert_(true);
  923. return hton64(num);
  924. }
  925. /**
  926. * Write a number in fixed length format into a buffer.
  927. */
  928. inline void writefixnum(void* buf, uint64_t num, size_t width) {
  929. _assert_(buf && width <= sizeof(int64_t));
  930. num = hton64(num);
  931. std::memcpy(buf, (const char*)&num + sizeof(num) - width, width);
  932. }
  933. /**
  934. * Read a number in fixed length format from a buffer.
  935. */
  936. inline uint64_t readfixnum(const void* buf, size_t width) {
  937. _assert_(buf && width <= sizeof(int64_t));
  938. uint64_t num = 0;
  939. std::memcpy(&num, buf, width);
  940. return ntoh64(num) >> ((sizeof(num) - width) * 8);
  941. }
  942. /**
  943. * Write a number in variable length format into a buffer.
  944. */
  945. inline size_t writevarnum(void* buf, uint64_t num) {
  946. _assert_(buf);
  947. unsigned char* wp = (unsigned char*)buf;
  948. if (num < (1ULL << 7)) {
  949. *(wp++) = num;
  950. } else if (num < (1ULL << 14)) {
  951. *(wp++) = (num >> 7) | 0x80;
  952. *(wp++) = num & 0x7f;
  953. } else if (num < (1ULL << 21)) {
  954. *(wp++) = (num >> 14) | 0x80;
  955. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  956. *(wp++) = num & 0x7f;
  957. } else if (num < (1ULL << 28)) {
  958. *(wp++) = (num >> 21) | 0x80;
  959. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  960. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  961. *(wp++) = num & 0x7f;
  962. } else if (num < (1ULL << 35)) {
  963. *(wp++) = (num >> 28) | 0x80;
  964. *(wp++) = ((num >> 21) & 0x7f) | 0x80;
  965. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  966. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  967. *(wp++) = num & 0x7f;
  968. } else if (num < (1ULL << 42)) {
  969. *(wp++) = (num >> 35) | 0x80;
  970. *(wp++) = ((num >> 28) & 0x7f) | 0x80;
  971. *(wp++) = ((num >> 21) & 0x7f) | 0x80;
  972. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  973. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  974. *(wp++) = num & 0x7f;
  975. } else if (num < (1ULL << 49)) {
  976. *(wp++) = (num >> 42) | 0x80;
  977. *(wp++) = ((num >> 35) & 0x7f) | 0x80;
  978. *(wp++) = ((num >> 28) & 0x7f) | 0x80;
  979. *(wp++) = ((num >> 21) & 0x7f) | 0x80;
  980. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  981. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  982. *(wp++) = num & 0x7f;
  983. } else if (num < (1ULL << 56)) {
  984. *(wp++) = (num >> 49) | 0x80;
  985. *(wp++) = ((num >> 42) & 0x7f) | 0x80;
  986. *(wp++) = ((num >> 35) & 0x7f) | 0x80;
  987. *(wp++) = ((num >> 28) & 0x7f) | 0x80;
  988. *(wp++) = ((num >> 21) & 0x7f) | 0x80;
  989. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  990. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  991. *(wp++) = num & 0x7f;
  992. } else if (num < (1ULL << 63)) {
  993. *(wp++) = (num >> 56) | 0x80;
  994. *(wp++) = ((num >> 49) & 0x7f) | 0x80;
  995. *(wp++) = ((num >> 42) & 0x7f) | 0x80;
  996. *(wp++) = ((num >> 35) & 0x7f) | 0x80;
  997. *(wp++) = ((num >> 28) & 0x7f) | 0x80;
  998. *(wp++) = ((num >> 21) & 0x7f) | 0x80;
  999. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  1000. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  1001. *(wp++) = num & 0x7f;
  1002. } else {
  1003. *(wp++) = (num >> 63) | 0x80;
  1004. *(wp++) = ((num >> 56) & 0x7f) | 0x80;
  1005. *(wp++) = ((num >> 49) & 0x7f) | 0x80;
  1006. *(wp++) = ((num >> 42) & 0x7f) | 0x80;
  1007. *(wp++) = ((num >> 35) & 0x7f) | 0x80;
  1008. *(wp++) = ((num >> 28) & 0x7f) | 0x80;
  1009. *(wp++) = ((num >> 21) & 0x7f) | 0x80;
  1010. *(wp++) = ((num >> 14) & 0x7f) | 0x80;
  1011. *(wp++) = ((num >> 7) & 0x7f) | 0x80;
  1012. *(wp++) = num & 0x7f;
  1013. }
  1014. return wp - (unsigned char*)buf;
  1015. }
  1016. /**
  1017. * Read a number in variable length format from a buffer.
  1018. */
  1019. inline size_t readvarnum(const void* buf, size_t size, uint64_t* np) {
  1020. _assert_(buf && size <= MEMMAXSIZ && np);
  1021. const unsigned char* rp = (const unsigned char*)buf;
  1022. const unsigned char* ep = rp + size;
  1023. uint64_t num = 0;
  1024. uint32_t c;
  1025. do {
  1026. if (rp >= ep) {
  1027. *np = 0;
  1028. return 0;
  1029. }
  1030. c = *rp;
  1031. num = (num << 7) + (c & 0x7f);
  1032. rp++;
  1033. } while (c >= 0x80);
  1034. *np = num;
  1035. return rp - (const unsigned char*)buf;
  1036. }
  1037. /**
  1038. * Check the size of variable length format of a number.
  1039. */
  1040. inline size_t sizevarnum(uint64_t num) {
  1041. _assert_(true);
  1042. if (num < (1ULL << 7)) return 1;
  1043. if (num < (1ULL << 14)) return 2;
  1044. if (num < (1ULL << 21)) return 3;
  1045. if (num < (1ULL << 28)) return 4;
  1046. if (num < (1ULL << 35)) return 5;
  1047. if (num < (1ULL << 42)) return 6;
  1048. if (num < (1ULL << 49)) return 7;
  1049. if (num < (1ULL << 56)) return 8;
  1050. if (num < (1ULL << 63)) return 9;
  1051. return 10;
  1052. }
  1053. /**
  1054. * Get the hash value by MurMur hashing.
  1055. */
  1056. inline uint64_t hashmurmur(const void* buf, size_t size) {
  1057. _assert_(buf && size <= MEMMAXSIZ);
  1058. const uint64_t mul = 0xc6a4a7935bd1e995ULL;
  1059. const int32_t rtt = 47;
  1060. uint64_t hash = 19780211ULL ^ (size * mul);
  1061. const unsigned char* rp = (const unsigned char*)buf;
  1062. while (size >= sizeof(uint64_t)) {
  1063. uint64_t num = ((uint64_t)rp[0] << 0) | ((uint64_t)rp[1] << 8) |
  1064. ((uint64_t)rp[2] << 16) | ((uint64_t)rp[3] << 24) |
  1065. ((uint64_t)rp[4] << 32) | ((uint64_t)rp[5] << 40) |
  1066. ((uint64_t)rp[6] << 48) | ((uint64_t)rp[7] << 56);
  1067. num *= mul;
  1068. num ^= num >> rtt;
  1069. num *= mul;
  1070. hash *= mul;
  1071. hash ^= num;
  1072. rp += sizeof(uint64_t);
  1073. size -= sizeof(uint64_t);
  1074. }
  1075. switch (size) {
  1076. case 7: hash ^= (uint64_t)rp[6] << 48;
  1077. case 6: hash ^= (uint64_t)rp[5] << 40;
  1078. case 5: hash ^= (uint64_t)rp[4] << 32;
  1079. case 4: hash ^= (uint64_t)rp[3] << 24;
  1080. case 3: hash ^= (uint64_t)rp[2] << 16;
  1081. case 2: hash ^= (uint64_t)rp[1] << 8;
  1082. case 1: hash ^= (uint64_t)rp[0];
  1083. hash *= mul;
  1084. };
  1085. hash ^= hash >> rtt;
  1086. hash *= mul;
  1087. hash ^= hash >> rtt;
  1088. return hash;
  1089. }
  1090. /**
  1091. * Get the hash value by FNV hashing.
  1092. */
  1093. inline uint64_t hashfnv(const void* buf, size_t size) {
  1094. _assert_(buf && size <= MEMMAXSIZ);
  1095. uint64_t hash = 14695981039346656037ULL;
  1096. const unsigned char* rp = (unsigned char*)buf;
  1097. const unsigned char* ep = rp + size;
  1098. while (rp < ep) {
  1099. hash = (hash ^ *(rp++)) * 109951162811ULL;
  1100. }
  1101. return hash;
  1102. }
  1103. /**
  1104. * Get the hash value suitable for a file name.
  1105. */
  1106. inline uint32_t hashpath(const void* buf, size_t size, char* obuf) {
  1107. _assert_(buf && size <= MEMMAXSIZ && obuf);
  1108. const unsigned char* rp = (const unsigned char*)buf;
  1109. uint32_t rv;
  1110. char* wp = obuf;
  1111. if (size <= 10) {
  1112. if (size > 0) {
  1113. const unsigned char* ep = rp + size;
  1114. while (rp < ep) {
  1115. int32_t num = *rp >> 4;
  1116. if (num < 10) {
  1117. *(wp++) = '0' + num;
  1118. } else {
  1119. *(wp++) = 'a' + num - 10;
  1120. }
  1121. num = *rp & 0x0f;
  1122. if (num < 10) {
  1123. *(wp++) = '0' + num;
  1124. } else {
  1125. *(wp++) = 'a' + num - 10;
  1126. }
  1127. rp++;
  1128. }
  1129. } else {
  1130. *(wp++) = '0';
  1131. }
  1132. uint64_t hash = hashmurmur(buf, size);
  1133. rv = (((hash & 0xffff000000000000ULL) >> 48) | ((hash & 0x0000ffff00000000ULL) >> 16)) ^
  1134. (((hash & 0x000000000000ffffULL) << 16) | ((hash & 0x00000000ffff0000ULL) >> 16));
  1135. } else {
  1136. *(wp++) = 'f' + 1 + (size & 0x0f);
  1137. for (int32_t i = 0; i <= 6; i += 3) {
  1138. uint32_t num = (rp[i] ^ rp[i+1] ^ rp[i+2] ^
  1139. rp[size-i-1] ^ rp[size-i-2] ^ rp[size-i-3]) % 36;
  1140. if (num < 10) {
  1141. *(wp++) = '0' + num;
  1142. } else {
  1143. *(wp++) = 'a' + num - 10;
  1144. }
  1145. }
  1146. uint64_t hash = hashmurmur(buf, size);
  1147. rv = (((hash & 0xffff000000000000ULL) >> 48) | ((hash & 0x0000ffff00000000ULL) >> 16)) ^
  1148. (((hash & 0x000000000000ffffULL) << 16) | ((hash & 0x00000000ffff0000ULL) >> 16));
  1149. uint64_t inc = hashfnv(buf, size);
  1150. inc = (((inc & 0xffff000000000000ULL) >> 48) | ((inc & 0x0000ffff00000000ULL) >> 16)) ^
  1151. (((inc & 0x000000000000ffffULL) << 16) | ((inc & 0x00000000ffff0000ULL) >> 16));
  1152. for (size_t i = 0; i < sizeof(hash); i++) {
  1153. uint32_t least = hash >> ((sizeof(hash) - 1) * 8);
  1154. uint64_t num = least >> 4;
  1155. if (inc & 0x01) num += 0x10;
  1156. inc = inc >> 1;
  1157. if (num < 10) {
  1158. *(wp++) = '0' + num;
  1159. } else {
  1160. *(wp++) = 'a' + num - 10;
  1161. }
  1162. num = least & 0x0f;
  1163. if (inc & 0x01) num += 0x10;
  1164. inc = inc >> 1;
  1165. if (num < 10) {
  1166. *(wp++) = '0' + num;
  1167. } else {
  1168. *(wp++) = 'a' + num - 10;
  1169. }
  1170. hash = hash << 8;
  1171. }
  1172. }
  1173. *wp = '\0';
  1174. return rv;
  1175. }
  1176. /**
  1177. * Get a prime number nearby a number.
  1178. */
  1179. inline uint64_t nearbyprime(uint64_t num) {
  1180. _assert_(true);
  1181. static uint64_t table[] = {
  1182. 2ULL, 3ULL, 5ULL, 7ULL, 11ULL, 13ULL, 17ULL, 19ULL, 23ULL, 29ULL, 31ULL, 37ULL, 41ULL,
  1183. 43ULL, 47ULL, 53ULL, 59ULL, 61ULL, 67ULL, 71ULL, 79ULL, 97ULL, 107ULL, 131ULL, 157ULL,
  1184. 181ULL, 223ULL, 257ULL, 307ULL, 367ULL, 431ULL, 521ULL, 613ULL, 727ULL, 863ULL, 1031ULL,
  1185. 1217ULL, 1451ULL, 1723ULL, 2053ULL, 2437ULL, 2897ULL, 3449ULL, 4099ULL, 4871ULL, 5801ULL,
  1186. 6899ULL, 8209ULL, 9743ULL, 11587ULL, 13781ULL, 16411ULL, 19483ULL, 23173ULL, 27581ULL,
  1187. 32771ULL, 38971ULL, 46349ULL, 55109ULL, 65537ULL, 77951ULL, 92681ULL, 110221ULL, 131101ULL,
  1188. 155887ULL, 185363ULL, 220447ULL, 262147ULL, 311743ULL, 370759ULL, 440893ULL, 524309ULL,
  1189. 623521ULL, 741457ULL, 881743ULL, 1048583ULL, 1246997ULL, 1482919ULL, 1763491ULL,
  1190. 2097169ULL, 2493949ULL, 2965847ULL, 3526987ULL, 4194319ULL, 4987901ULL, 5931641ULL,
  1191. 7053971ULL, 8388617ULL, 9975803ULL, 11863289ULL, 14107921ULL, 16777259ULL, 19951597ULL,
  1192. 23726569ULL, 28215809ULL, 33554467ULL, 39903197ULL, 47453149ULL, 56431657ULL,
  1193. 67108879ULL, 79806341ULL, 94906297ULL, 112863217ULL, 134217757ULL, 159612679ULL,
  1194. 189812533ULL, 225726419ULL, 268435459ULL, 319225391ULL, 379625083ULL, 451452839ULL,
  1195. 536870923ULL, 638450719ULL, 759250133ULL, 902905657ULL, 1073741827ULL, 1276901429ULL,
  1196. 1518500279ULL, 1805811341ULL, 2147483659ULL, 2553802871ULL, 3037000507ULL, 3611622607ULL,
  1197. 4294967311ULL, 5107605691ULL, 6074001001ULL, 7223245229ULL, 8589934609ULL, 10215211387ULL,
  1198. 12148002047ULL, 14446490449ULL, 17179869209ULL, 20430422699ULL, 24296004011ULL,
  1199. 28892980877ULL, 34359738421ULL, 40860845437ULL, 48592008053ULL, 57785961671ULL,
  1200. 68719476767ULL, 81721690807ULL, 97184016049ULL, 115571923303ULL, 137438953481ULL,
  1201. 163443381347ULL, 194368032011ULL, 231143846587ULL, 274877906951ULL, 326886762733ULL,
  1202. 388736063999ULL, 462287693167ULL, 549755813911ULL, 653773525393ULL, 777472128049ULL,
  1203. 924575386373ULL, 1099511627791ULL, 1307547050819ULL, 1554944255989ULL, 1849150772699ULL,
  1204. 2199023255579ULL, 2615094101561ULL, 3109888512037ULL, 3698301545321ULL,
  1205. 4398046511119ULL, 5230188203153ULL, 6219777023959ULL, 7396603090651ULL,
  1206. 8796093022237ULL, 10460376406273ULL, 12439554047911ULL, 14793206181251ULL,
  1207. 17592186044423ULL, 20920752812471ULL, 24879108095833ULL, 29586412362491ULL,
  1208. 35184372088891ULL, 41841505624973ULL, 49758216191633ULL, 59172824724919ULL,
  1209. 70368744177679ULL, 83683011249917ULL, 99516432383281ULL, 118345649449813ULL,
  1210. 140737488355333ULL, 167366022499847ULL, 199032864766447ULL, 236691298899683ULL,
  1211. 281474976710677ULL, 334732044999557ULL, 398065729532981ULL, 473382597799229ULL,
  1212. 562949953421381ULL, 669464089999087ULL, 796131459065743ULL, 946765195598473ULL,
  1213. 1125899906842679ULL, 1338928179998197ULL, 1592262918131449ULL, 1893530391196921ULL,
  1214. 2251799813685269ULL, 2677856359996339ULL, 3184525836262943ULL, 3787060782393821ULL,
  1215. 4503599627370517ULL, 5355712719992603ULL, 6369051672525833ULL, 7574121564787633ULL
  1216. };
  1217. static const size_t tnum = sizeof(table) / sizeof(table[0]);
  1218. uint64_t* ub = std::lower_bound(table, table + tnum, num);
  1219. return ub == (uint64_t*)table + tnum ? num : *ub;
  1220. }
  1221. /**
  1222. * Get the quiet Not-a-Number value.
  1223. */
  1224. inline double nan() {
  1225. _assert_(true);
  1226. return std::numeric_limits<double>::quiet_NaN();
  1227. }
  1228. /**
  1229. * Get the positive infinity value.
  1230. */
  1231. inline double inf() {
  1232. _assert_(true);
  1233. return std::numeric_limits<double>::infinity();
  1234. }
  1235. /**
  1236. * Check a number is a Not-a-Number value.
  1237. */
  1238. inline bool chknan(double num) {
  1239. _assert_(true);
  1240. return num != num;
  1241. }
  1242. /**
  1243. * Check a number is an infinity value.
  1244. */
  1245. inline bool chkinf(double num) {
  1246. _assert_(true);
  1247. return num == inf() || num == -inf();
  1248. }
  1249. /**
  1250. * Append a formatted string at the end of a string.
  1251. */
  1252. inline void vstrprintf(std::string* dest, const char* format, va_list ap) {
  1253. _assert_(dest && format);
  1254. while (*format != '\0') {
  1255. if (*format == '%') {
  1256. char cbuf[NUMBUFSIZ];
  1257. cbuf[0] = '%';
  1258. size_t cbsiz = 1;
  1259. int32_t lnum = 0;
  1260. format++;
  1261. while (std::strchr("0123456789 .+-hlLz", *format) && *format != '\0' &&
  1262. cbsiz < NUMBUFSIZ - 1) {
  1263. if (*format == 'l' || *format == 'L') lnum++;
  1264. cbuf[cbsiz++] = *(format++);
  1265. }
  1266. cbuf[cbsiz++] = *format;
  1267. cbuf[cbsiz] = '\0';
  1268. switch (*format) {
  1269. case 's': {
  1270. const char* tmp = va_arg(ap, const char*);
  1271. if (tmp) {
  1272. dest->append(tmp);
  1273. } else {
  1274. dest->append("(null)");
  1275. }
  1276. break;
  1277. }
  1278. case 'd': {
  1279. char tbuf[NUMBUFSIZ*4];
  1280. size_t tsiz;
  1281. if (lnum >= 2) {
  1282. tsiz = std::sprintf(tbuf, cbuf, va_arg(ap, long long));
  1283. } else if (lnum >= 1) {
  1284. tsiz = std::sprintf(tbuf, cbuf, va_arg(ap, long));
  1285. } else {
  1286. tsiz = std::sprintf(tbuf, cbuf, va_arg(ap, int));
  1287. }
  1288. dest->append(tbuf, tsiz);
  1289. break;
  1290. }
  1291. case 'o': case 'u': case 'x': case 'X': case 'c': {
  1292. char tbuf[NUMBUFSIZ*4];
  1293. size_t tsiz;
  1294. if (lnum >= 2) {
  1295. tsiz = std::sprintf(tbuf, cbuf, va_arg(ap, unsigned long long));
  1296. } else if (lnum >= 1) {
  1297. tsiz = std::sprintf(tbuf, cbuf, va_arg(ap, unsigned long));
  1298. } else {
  1299. tsiz = std::sprintf(tbuf, cbuf, va_arg(ap, unsigned int));
  1300. }
  1301. dest->append(tbuf, tsiz);
  1302. break;
  1303. }
  1304. case 'e': case 'E': case 'f': case 'g': case 'G': {
  1305. char tbuf[NUMBUFSIZ*4];
  1306. size_t tsiz;
  1307. if (lnum >= 1) {
  1308. tsiz = std::snprintf(tbuf, sizeof(tbuf), cbuf, va_arg(ap, long double));
  1309. } else {
  1310. tsiz = std::snprintf(tbuf, sizeof(tbuf), cbuf, va_arg(ap, double));
  1311. }
  1312. if (tsiz > sizeof(tbuf)) {
  1313. tbuf[sizeof(tbuf)-1] = '*';
  1314. tsiz = sizeof(tbuf);
  1315. }
  1316. dest->append(tbuf, tsiz);
  1317. break;
  1318. }
  1319. case 'p': {
  1320. char tbuf[NUMBUFSIZ*4];
  1321. size_t tsiz = std::sprintf(tbuf, "%p", va_arg(ap, void*));
  1322. dest->append(tbuf, tsiz);
  1323. break;
  1324. }
  1325. case '%': {
  1326. dest->append("%", 1);
  1327. break;
  1328. }
  1329. }
  1330. } else {
  1331. dest->append(format, 1);
  1332. }
  1333. format++;
  1334. }
  1335. }
  1336. /**
  1337. * Append a formatted string at the end of a string.
  1338. */
  1339. inline void strprintf(std::string* dest, const char* format, ...) {
  1340. _assert_(dest && format);
  1341. va_list ap;
  1342. va_start(ap, format);
  1343. vstrprintf(dest, format, ap);
  1344. va_end(ap);
  1345. }
  1346. /**
  1347. * Generate a formatted string on memory.
  1348. */
  1349. inline std::string strprintf(const char* format, ...) {
  1350. _assert_(format);
  1351. std::string str;
  1352. va_list ap;
  1353. va_start(ap, format);
  1354. vstrprintf(&str, format, ap);
  1355. va_end(ap);
  1356. return str;
  1357. }
  1358. /**
  1359. * Split a string with a delimiter
  1360. */
  1361. inline size_t strsplit(const std::string& str, char delim, std::vector<std::string>* elems) {
  1362. _assert_(elems);
  1363. elems->clear();
  1364. std::string::const_iterator it = str.begin();
  1365. std::string::const_iterator pv = it;
  1366. while (it != str.end()) {
  1367. if (*it == delim) {
  1368. std::string col(pv, it);
  1369. elems->push_back(col);
  1370. pv = it + 1;
  1371. }
  1372. ++it;
  1373. }
  1374. std::string col(pv, it);
  1375. elems->push_back(col);
  1376. return elems->size();
  1377. }
  1378. /**
  1379. * Split a string with delimiters.
  1380. */
  1381. inline size_t strsplit(const std::string& str, const std::string& delims,
  1382. std::vector<std::string>* elems) {
  1383. _assert_(elems);
  1384. elems->clear();
  1385. std::string::const_iterator it = str.begin();
  1386. std::string::const_iterator pv = it;
  1387. while (it != str.end()) {
  1388. while (delims.find(*it, 0) != std::string::npos) {
  1389. std::string col(pv, it);
  1390. elems->push_back(col);
  1391. pv = it + 1;
  1392. break;
  1393. }
  1394. ++it;
  1395. }
  1396. std::string col(pv, it);
  1397. elems->push_back(col);
  1398. return elems->size();
  1399. }
  1400. /**
  1401. * Convert the letters of a string into upper case.
  1402. */
  1403. inline std::string* strtoupper(std::string* str) {
  1404. _assert_(str);
  1405. size_t size = str->size();
  1406. for (size_t i = 0; i < size; i++) {
  1407. int32_t c = (unsigned char)(*str)[i];
  1408. if (c >= 'a' && c <= 'z') (*str)[i] = c - ('a' - 'A');
  1409. }
  1410. return str;
  1411. }
  1412. /**
  1413. * Convert the letters of a string into lower case.
  1414. */
  1415. inline std::string* strtolower(std::string* str) {
  1416. _assert_(str);
  1417. size_t size = str->size();
  1418. for (size_t i = 0; i < size; i++) {
  1419. int32_t c = (unsigned char)(*str)[i];
  1420. if (c >= 'A' && c <= 'Z') (*str)[i] = c + ('a' - 'A');
  1421. }
  1422. return str;
  1423. }
  1424. /**
  1425. * Check whether a string begins with a key.
  1426. */
  1427. inline bool strfwm(const std::string& str, const std::string& key) {
  1428. _assert_(true);
  1429. size_t ksiz = key.size();
  1430. if (ksiz > str.size()) return false;
  1431. return !std::memcmp(str.data(), key.data(), ksiz);
  1432. }
  1433. /**
  1434. * Check whether a string ends with a key.
  1435. */
  1436. inline bool strbwm(const std::string& str, const std::string& key) {
  1437. _assert_(true);
  1438. size_t ksiz = key.size();
  1439. if (ksiz > str.size()) return false;
  1440. return !std::memcmp(str.data() + str.size() - ksiz, key.data(), ksiz);
  1441. }
  1442. /**
  1443. * Cut space characters at head or tail of a string.
  1444. */
  1445. inline std::string* strtrim(std::string* str) {
  1446. _assert_(str);
  1447. size_t size = str->size();
  1448. size_t wi = 0;
  1449. size_t li = 0;
  1450. for (size_t i = 0; i < size; i++) {
  1451. int32_t c = (unsigned char)(*str)[i];
  1452. if (c >= '\0' && c <= ' ') {
  1453. if (wi > 0) (*str)[wi++] = c;
  1454. } else {
  1455. (*str)[wi++] = c;
  1456. li = wi;
  1457. }
  1458. }
  1459. str->resize(li);
  1460. return str;
  1461. }
  1462. /**
  1463. * Convert a UTF-8 string into a UCS-4 array.
  1464. */
  1465. inline void strutftoucs(const std::string& src, std::vector<uint32_t>* dest) {
  1466. _assert_(dest);
  1467. dest->reserve(dest->size() + src.size());
  1468. size_t size = src.size();
  1469. size_t ri = 0;
  1470. while (ri < size) {
  1471. uint32_t c = (unsigned char)src[ri];
  1472. if (c < 0x80) {
  1473. dest->push_back(c);
  1474. } else if (c < 0xe0) {
  1475. if (c >= 0xc0 && ri + 1 < size) {
  1476. c = ((c & 0x1f) << 6) | (src[ri+1] & 0x3f);
  1477. if (c >= 0x80) dest->push_back(c);
  1478. ri++;
  1479. }
  1480. } else if (c < 0xf0) {

Large files files are truncated, but you can click here to view the full file