xs.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  2. #ifndef _XS_H
  3. #define _XS_H
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <unistd.h>
  9. #include <stdarg.h>
  10. #include <signal.h>
  11. #include <errno.h>
  12. typedef enum {
  13. XSTYPE_STRING = 0x02, /* C string (\0 delimited) (NOT STORED) */
  14. XSTYPE_NUMBER = 0x17, /* double in spirit, stored as a C string (\0 delimited) */
  15. XSTYPE_NULL = 0x18, /* Special NULL value */
  16. XSTYPE_TRUE = 0x06, /* Boolean */
  17. XSTYPE_FALSE = 0x15, /* Boolean */
  18. XSTYPE_LIST = 0x1d, /* Sequence of LITEMs up to EOM (with size) */
  19. XSTYPE_LITEM = 0x1f, /* Element of a list (any type) */
  20. XSTYPE_DICT = 0x1c, /* Sequence of KEYVALs up to EOM (with size) */
  21. XSTYPE_KEYVAL = 0x1e, /* key + value (STRING key + any type) */
  22. XSTYPE_DATA = 0x10 /* A block of anonymous data */
  23. } xstype;
  24. /* types */
  25. typedef char xs_val;
  26. typedef char xs_str;
  27. typedef char xs_list;
  28. typedef char xs_keyval;
  29. typedef char xs_dict;
  30. typedef char xs_number;
  31. typedef char xs_data;
  32. /* size in bytes of the type size */
  33. #define _XS_TYPE_SIZE 4
  34. /* auto-destroyable strings */
  35. #define xs __attribute__ ((__cleanup__ (_xs_destroy))) xs_val
  36. /* not really all, just very much */
  37. #define XS_ALL 0xfffffff
  38. #ifndef xs_countof
  39. #define xs_countof(a) (sizeof((a)) / sizeof((*a)))
  40. #endif
  41. void *xs_free(void *ptr);
  42. void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func);
  43. #define xs_realloc(ptr, size) _xs_realloc(ptr, size, __FILE__, __LINE__, __func__)
  44. int _xs_blk_size(int sz);
  45. void _xs_destroy(char **var);
  46. #define xs_debug() raise(SIGTRAP)
  47. xstype xs_type(const xs_val *data);
  48. int xs_size(const xs_val *data);
  49. int xs_is_null(const xs_val *data);
  50. int xs_cmp(const xs_val *v1, const xs_val *v2);
  51. const xs_val *xs_or(const xs_val *v1, const xs_val *v2);
  52. xs_val *xs_dup(const xs_val *data);
  53. xs_val *xs_expand(xs_val *data, int offset, int size);
  54. xs_val *xs_collapse(xs_val *data, int offset, int size);
  55. xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size);
  56. #define xs_insert(data, offset, data2) xs_insert_m(data, offset, data2, xs_size(data2))
  57. #define xs_append_m(data, mem, size) xs_insert_m(data, xs_size(data) - 1, mem, size)
  58. xs_val *xs_stock(int type);
  59. xs_str *xs_str_new(const char *str);
  60. xs_str *xs_str_new_sz(const char *mem, int sz);
  61. xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix);
  62. #define xs_str_prepend_i(str, prefix) xs_str_wrap_i(prefix, str, NULL)
  63. xs_str *_xs_str_cat(xs_str *str, const char *strs[]);
  64. #define xs_str_cat(str, ...) _xs_str_cat(str, (const char *[]){ __VA_ARGS__, NULL })
  65. xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times);
  66. #define xs_replace_i(str, sfrom, sto) xs_replace_in(str, sfrom, sto, XS_ALL)
  67. #define xs_replace(str, sfrom, sto) xs_replace_in(xs_dup(str), sfrom, sto, XS_ALL)
  68. #define xs_replace_n(str, sfrom, sto, times) xs_replace_in(xs_dup(str), sfrom, sto, times)
  69. xs_str *xs_fmt(const char *fmt, ...);
  70. int xs_str_in(const char *haystack, const char *needle);
  71. int xs_between(const char *prefix, const char *str, const char *suffix);
  72. #define xs_startswith(str, prefix) xs_between(prefix, str, NULL)
  73. #define xs_endswith(str, suffix) xs_between(NULL, str, suffix)
  74. xs_str *xs_crop_i(xs_str *str, int start, int end);
  75. xs_str *xs_lstrip_chars_i(xs_str *str, const char *chars);
  76. xs_str *xs_rstrip_chars_i(xs_str *str, const char *chars);
  77. xs_str *xs_strip_chars_i(xs_str *str, const char *chars);
  78. #define xs_strip_i(str) xs_strip_chars_i(str, " \r\n\t\v\f")
  79. xs_str *xs_tolower_i(xs_str *str);
  80. xs_list *xs_list_new(void);
  81. xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz);
  82. xs_list *_xs_list_append(xs_list *list, const xs_val *vals[]);
  83. #define xs_list_append(list, ...) _xs_list_append(list, (const xs_val *[]){ __VA_ARGS__, NULL })
  84. int xs_list_iter(xs_list **list, const xs_val **value);
  85. int xs_list_next(const xs_list *list, const xs_val **value, int *ctxt);
  86. int xs_list_len(const xs_list *list);
  87. const xs_val *xs_list_get(const xs_list *list, int num);
  88. xs_list *xs_list_del(xs_list *list, int num);
  89. xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data);
  90. xs_list *xs_list_set(xs_list *list, int num, const xs_val *data);
  91. xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last);
  92. #define xs_list_pop(list, data) xs_list_dequeue(list, data, 1)
  93. #define xs_list_shift(list, data) xs_list_dequeue(list, data, 0)
  94. int xs_list_in(const xs_list *list, const xs_val *val);
  95. xs_str *xs_join(const xs_list *list, const char *sep);
  96. xs_list *xs_split_n(const char *str, const char *sep, int times);
  97. #define xs_split(str, sep) xs_split_n(str, sep, XS_ALL)
  98. xs_list *xs_list_cat(xs_list *l1, const xs_list *l2);
  99. int xs_keyval_size(const xs_str *key, const xs_val *value);
  100. xs_str *xs_keyval_key(const xs_keyval *keyval);
  101. xs_val *xs_keyval_value(const xs_keyval *keyval);
  102. xs_keyval *xs_keyval_make(xs_keyval *keyval, const xs_str *key, const xs_val *value);
  103. xs_dict *xs_dict_new(void);
  104. xs_dict *xs_dict_append(xs_dict *dict, const xs_str *key, const xs_val *value);
  105. xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value);
  106. int xs_dict_next(const xs_dict *dict, const xs_str **key, const xs_val **value, int *ctxt);
  107. const xs_val *xs_dict_get(const xs_dict *dict, const xs_str *key);
  108. #define xs_dict_get_def(dict, key, def) xs_or(xs_dict_get(dict, key), def)
  109. xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key);
  110. xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *data);
  111. xs_dict *xs_dict_gc(const xs_dict *dict);
  112. const xs_val *xs_dict_get_path_sep(const xs_dict *dict, const char *path, const char *sep);
  113. #define xs_dict_get_path(dict, path) xs_dict_get_path_sep(dict, path, ".")
  114. xs_dict *xs_dict_set_path_sep(xs_dict *dict, const char *path, const xs_val *value, const char *sep);
  115. #define xs_dict_set_path(dict, path, value) xs_dict_set_path_sep(dict, path, value, ".")
  116. xs_val *xs_val_new(xstype t);
  117. xs_number *xs_number_new(double f);
  118. double xs_number_get(const xs_number *v);
  119. const char *xs_number_str(const xs_number *v);
  120. xs_data *xs_data_new(const void *data, int size);
  121. int xs_data_size(const xs_data *value);
  122. void xs_data_get(void *data, const xs_data *value);
  123. void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size);
  124. unsigned int xs_hash_func(const char *data, int size);
  125. #ifdef XS_ASSERT
  126. #include <assert.h>
  127. #define XS_ASSERT_TYPE(v, t) assert(xs_type(v) == t)
  128. #define XS_ASSERT_TYPE_NULL(v, t) assert(v == NULL || xs_type(v) == t)
  129. #else
  130. #define XS_ASSERT_TYPE(v, t) (void)(0)
  131. #define XS_ASSERT_TYPE_NULL(v, t) (void)(0)
  132. #endif
  133. #define xs_return(v) xs_val *__r = v; v = NULL; return __r
  134. #define xs_is_true(v) (xs_type((v)) == XSTYPE_TRUE)
  135. #define xs_is_false(v) (xs_type((v)) == XSTYPE_FALSE)
  136. #define xs_not(v) xs_stock(xs_is_true((v)) ? XSTYPE_FALSE : XSTYPE_TRUE)
  137. #define xs_list_foreach(l, v) for (int ct_##__LINE__ = 0; xs_list_next(l, &v, &ct_##__LINE__); )
  138. #define xs_dict_foreach(l, k, v) for (int ct_##__LINE__ = 0; xs_dict_next(l, &k, &v, &ct_##__LINE__); )
  139. #ifdef XS_IMPLEMENTATION
  140. void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func)
  141. {
  142. xs_val *ndata = realloc(ptr, size);
  143. if (ndata == NULL) {
  144. fprintf(stderr, "ERROR: out of memory at %s:%d: %s()\n", file, line, func);
  145. abort();
  146. }
  147. #ifdef XS_DEBUG
  148. if (ndata != ptr) {
  149. int n;
  150. FILE *f = fopen("xs_memory.out", "a");
  151. if (ptr != NULL)
  152. fprintf(f, "%p r\n", ptr);
  153. fprintf(f, "%p a %ld %s:%d: %s", ndata, size, file, line, func);
  154. if (ptr != NULL) {
  155. fprintf(f, " [");
  156. for (n = 0; n < 32 && ndata[n]; n++) {
  157. if (ndata[n] >= 32 && ndata[n] <= 127)
  158. fprintf(f, "%c", ndata[n]);
  159. else
  160. fprintf(f, "\\%02x", (unsigned char)ndata[n]);
  161. }
  162. fprintf(f, "]");
  163. }
  164. fprintf(f, "\n");
  165. fclose(f);
  166. }
  167. #else
  168. (void)file;
  169. (void)line;
  170. (void)func;
  171. #endif
  172. return ndata;
  173. }
  174. void *xs_free(void *ptr)
  175. {
  176. #ifdef XS_DEBUG
  177. if (ptr != NULL) {
  178. FILE *f = fopen("xs_memory.out", "a");
  179. fprintf(f, "%p b\n", ptr);
  180. fclose(f);
  181. }
  182. #endif
  183. free(ptr);
  184. return NULL;
  185. }
  186. void _xs_destroy(char **var)
  187. {
  188. /*
  189. if (_xs_debug)
  190. printf("_xs_destroy %p\n", var);
  191. */
  192. xs_free(*var);
  193. }
  194. int _xs_blk_size(int sz)
  195. /* calculates the block size */
  196. {
  197. int blk_size = 4096;
  198. if (sz < 256)
  199. blk_size = 32;
  200. else
  201. if (sz < 4096)
  202. blk_size = 256;
  203. return ((((sz) + blk_size) / blk_size) * blk_size);
  204. }
  205. xstype xs_type(const xs_val *data)
  206. /* return the type of data */
  207. {
  208. xstype t;
  209. if (data == NULL)
  210. t = XSTYPE_NULL;
  211. else
  212. switch (data[0]) {
  213. case XSTYPE_NULL:
  214. case XSTYPE_TRUE:
  215. case XSTYPE_FALSE:
  216. case XSTYPE_LIST:
  217. case XSTYPE_LITEM:
  218. case XSTYPE_DICT:
  219. case XSTYPE_KEYVAL:
  220. case XSTYPE_NUMBER:
  221. case XSTYPE_DATA:
  222. t = data[0];
  223. break;
  224. default:
  225. t = XSTYPE_STRING;
  226. break;
  227. }
  228. return t;
  229. }
  230. void _xs_put_size(xs_val *ptr, int i)
  231. /* must match _XS_TYPE_SIZE */
  232. {
  233. memcpy(ptr + 1, &i, sizeof(i));
  234. }
  235. int _xs_get_size(const xs_val *ptr)
  236. /* must match _XS_TYPE_SIZE */
  237. {
  238. int i;
  239. memcpy(&i, ptr + 1, sizeof(i));
  240. return i;
  241. }
  242. int xs_size(const xs_val *data)
  243. /* returns the size of data in bytes */
  244. {
  245. int len = 0;
  246. const char *p;
  247. if (data == NULL)
  248. return 0;
  249. switch (xs_type(data)) {
  250. case XSTYPE_STRING:
  251. len = strlen(data) + 1;
  252. break;
  253. case XSTYPE_LIST:
  254. case XSTYPE_DICT:
  255. case XSTYPE_DATA:
  256. len = _xs_get_size(data);
  257. break;
  258. case XSTYPE_KEYVAL:
  259. /* calculate the size of the key and the value */
  260. p = data + 1;
  261. p += xs_size(p);
  262. p += xs_size(p);
  263. len = p - data;
  264. break;
  265. case XSTYPE_LITEM:
  266. /* it's the size of the item + 1 */
  267. p = data + 1;
  268. p += xs_size(p);
  269. len = p - data;
  270. break;
  271. case XSTYPE_NUMBER:
  272. len = 1 + xs_size(data + 1);
  273. break;
  274. default:
  275. len = 1;
  276. }
  277. return len;
  278. }
  279. int xs_is_null(const xs_val *data)
  280. /* checks for null */
  281. {
  282. return (xs_type(data) == XSTYPE_NULL);
  283. }
  284. int xs_cmp(const xs_val *v1, const xs_val *v2)
  285. /* compares two values */
  286. {
  287. if (xs_type(v1) == XSTYPE_STRING && xs_type(v2) == XSTYPE_STRING)
  288. return strcmp(v1, v2);
  289. int s1 = xs_size(v1);
  290. int s2 = xs_size(v2);
  291. int d = s1 - s2;
  292. return d == 0 ? memcmp(v1, v2, s1) : d;
  293. }
  294. const xs_val *xs_or(const xs_val *v1, const xs_val *v2)
  295. /* returns v1 if it's not NULL, else v2 */
  296. {
  297. return v1 == NULL ? v2 : v1;
  298. }
  299. xs_val *xs_dup(const xs_val *data)
  300. /* creates a duplicate of data */
  301. {
  302. xs_val *s = NULL;
  303. if (data) {
  304. int sz = xs_size(data);
  305. s = xs_realloc(NULL, _xs_blk_size(sz));
  306. memcpy(s, data, sz);
  307. }
  308. return s;
  309. }
  310. xs_val *xs_expand(xs_val *data, int offset, int size)
  311. /* opens a hole in data */
  312. {
  313. int sz = xs_size(data);
  314. int n;
  315. sz += size;
  316. /* open room */
  317. data = xs_realloc(data, _xs_blk_size(sz));
  318. /* move up the rest of the data */
  319. for (n = sz - 1; n >= offset + size; n--)
  320. data[n] = data[n - size];
  321. if (xs_type(data) == XSTYPE_LIST ||
  322. xs_type(data) == XSTYPE_DICT ||
  323. xs_type(data) == XSTYPE_DATA)
  324. _xs_put_size(data, sz);
  325. return data;
  326. }
  327. xs_val *xs_collapse(xs_val *data, int offset, int size)
  328. /* shrinks data */
  329. {
  330. int sz = xs_size(data);
  331. int n;
  332. /* don't try to delete beyond the limit */
  333. if (offset + size > sz)
  334. size = sz - offset;
  335. /* shrink total size */
  336. sz -= size;
  337. for (n = offset; n < sz; n++)
  338. data[n] = data[n + size];
  339. if (xs_type(data) == XSTYPE_LIST ||
  340. xs_type(data) == XSTYPE_DICT ||
  341. xs_type(data) == XSTYPE_DATA)
  342. _xs_put_size(data, sz);
  343. return xs_realloc(data, _xs_blk_size(sz));
  344. }
  345. xs_val *xs_insert_m(xs_val *data, int offset, const char *mem, int size)
  346. /* inserts a memory block */
  347. {
  348. data = xs_expand(data, offset, size);
  349. memcpy(data + offset, mem, size);
  350. return data;
  351. }
  352. xs_val *xs_stock(int type)
  353. /* returns stock values */
  354. {
  355. static xs_val stock_null[] = { XSTYPE_NULL };
  356. static xs_val stock_true[] = { XSTYPE_TRUE };
  357. static xs_val stock_false[] = { XSTYPE_FALSE };
  358. static xs_val stock_0[] = { XSTYPE_NUMBER, '0', '\0' };
  359. static xs_val stock_1[] = { XSTYPE_NUMBER, '1', '\0' };
  360. static xs_list *stock_list = NULL;
  361. static xs_dict *stock_dict = NULL;
  362. switch (type) {
  363. case 0: return stock_0;
  364. case 1: return stock_1;
  365. case XSTYPE_NULL: return stock_null;
  366. case XSTYPE_TRUE: return stock_true;
  367. case XSTYPE_FALSE: return stock_false;
  368. case XSTYPE_LIST:
  369. if (stock_list == NULL)
  370. stock_list = xs_list_new();
  371. return stock_list;
  372. case XSTYPE_DICT:
  373. if (stock_dict == NULL)
  374. stock_dict = xs_dict_new();
  375. return stock_dict;
  376. }
  377. return NULL;
  378. }
  379. /** strings **/
  380. xs_str *xs_str_new(const char *str)
  381. /* creates a new string */
  382. {
  383. return xs_insert(NULL, 0, str ? str : "");
  384. }
  385. xs_str *xs_str_new_sz(const char *mem, int sz)
  386. /* creates a new string from a memory block, adding an asciiz */
  387. {
  388. xs_str *s = xs_realloc(NULL, _xs_blk_size(sz + 1));
  389. memcpy(s, mem, sz);
  390. s[sz] = '\0';
  391. return s;
  392. }
  393. xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix)
  394. /* wraps str with prefix and suffix */
  395. {
  396. XS_ASSERT_TYPE(str, XSTYPE_STRING);
  397. if (prefix)
  398. str = xs_insert_m(str, 0, prefix, strlen(prefix));
  399. if (suffix)
  400. str = xs_insert_m(str, strlen(str), suffix, strlen(suffix));
  401. return str;
  402. }
  403. xs_str *_xs_str_cat(xs_str *str, const char *strs[])
  404. /* concatenates all strings after str */
  405. {
  406. int o = strlen(str);
  407. while (*strs) {
  408. int sz = strlen(*strs);
  409. str = xs_insert_m(str, o, *strs, sz);
  410. o += sz;
  411. strs++;
  412. }
  413. return str;
  414. }
  415. xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times)
  416. /* replaces inline all sfrom with sto */
  417. {
  418. XS_ASSERT_TYPE(str, XSTYPE_STRING);
  419. int sfsz = strlen(sfrom);
  420. int stsz = strlen(sto);
  421. int diff = stsz - sfsz;
  422. char *ss;
  423. int offset = 0;
  424. while (times > 0 && (ss = strstr(str + offset, sfrom)) != NULL) {
  425. int n_offset = ss - str;
  426. if (diff < 0)
  427. str = xs_collapse(str, n_offset, -diff);
  428. else
  429. if (diff > 0)
  430. str = xs_expand(str, n_offset, diff);
  431. memcpy(str + n_offset, sto, stsz);
  432. offset = n_offset + stsz;
  433. times--;
  434. }
  435. return str;
  436. }
  437. xs_str *xs_fmt(const char *fmt, ...)
  438. /* formats a string with printf()-like marks */
  439. {
  440. int n;
  441. xs_str *s = NULL;
  442. va_list ap;
  443. va_start(ap, fmt);
  444. n = vsnprintf(s, 0, fmt, ap);
  445. va_end(ap);
  446. if (n > 0) {
  447. s = xs_realloc(NULL, _xs_blk_size(n + 1));
  448. va_start(ap, fmt);
  449. vsnprintf(s, n + 1, fmt, ap);
  450. va_end(ap);
  451. }
  452. return s;
  453. }
  454. int xs_str_in(const char *haystack, const char *needle)
  455. /* finds needle in haystack and returns the offset or -1 */
  456. {
  457. char *s;
  458. int r = -1;
  459. if ((s = strstr(haystack, needle)) != NULL)
  460. r = s - haystack;
  461. return r;
  462. }
  463. int xs_between(const char *prefix, const char *str, const char *suffix)
  464. /* returns true if str starts with prefix and ends with suffix */
  465. {
  466. int sz = strlen(str);
  467. int psz = prefix ? strlen(prefix) : 0;
  468. int ssz = suffix ? strlen(suffix) : 0;
  469. if (sz < psz || sz < ssz)
  470. return 0;
  471. if (prefix && memcmp(str, prefix, psz) != 0)
  472. return 0;
  473. if (suffix && memcmp(str + sz - ssz, suffix, ssz) != 0)
  474. return 0;
  475. return 1;
  476. }
  477. xs_str *xs_crop_i(xs_str *str, int start, int end)
  478. /* crops the string to be only from start to end */
  479. {
  480. XS_ASSERT_TYPE(str, XSTYPE_STRING);
  481. int sz = strlen(str);
  482. if (end <= 0)
  483. end = sz + end;
  484. /* crop from the top */
  485. str[end] = '\0';
  486. /* crop from the bottom */
  487. str = xs_collapse(str, 0, start);
  488. return str;
  489. }
  490. xs_str *xs_lstrip_chars_i(xs_str *str, const char *chars)
  491. /* strips all chars from the start of str */
  492. {
  493. int n;
  494. for (n = 0; str[n] && strchr(chars, str[n]); n++);
  495. if (n)
  496. str = xs_collapse(str, 0, n);
  497. return str;
  498. }
  499. xs_str *xs_rstrip_chars_i(xs_str *str, const char *chars)
  500. /* strips all chars from the end of str */
  501. {
  502. int n;
  503. for (n = strlen(str); n > 0 && strchr(chars, str[n - 1]); n--);
  504. str[n] = '\0';
  505. return str;
  506. }
  507. xs_str *xs_strip_chars_i(xs_str *str, const char *chars)
  508. /* strips the string of chars from the start and the end */
  509. {
  510. return xs_lstrip_chars_i(xs_rstrip_chars_i(str, chars), chars);
  511. }
  512. xs_str *xs_tolower_i(xs_str *str)
  513. /* convert to lowercase */
  514. {
  515. XS_ASSERT_TYPE(str, XSTYPE_STRING);
  516. int n;
  517. for (n = 0; str[n]; n++)
  518. str[n] = tolower(str[n]);
  519. return str;
  520. }
  521. /** lists **/
  522. xs_list *xs_list_new(void)
  523. /* creates a new list */
  524. {
  525. int sz = 1 + _XS_TYPE_SIZE + 1;
  526. xs_list *l = xs_realloc(NULL, sz);
  527. memset(l, '\0', sz);
  528. l[0] = XSTYPE_LIST;
  529. _xs_put_size(l, sz);
  530. return l;
  531. }
  532. xs_list *_xs_list_write_litem(xs_list *list, int offset, const char *mem, int dsz)
  533. /* writes a list item */
  534. {
  535. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  536. if (mem == NULL) {
  537. mem = xs_stock(XSTYPE_NULL);
  538. dsz = xs_size(mem);
  539. }
  540. list = xs_expand(list, offset, dsz + 1);
  541. list[offset] = XSTYPE_LITEM;
  542. memcpy(list + offset + 1, mem, dsz);
  543. return list;
  544. }
  545. xs_list *xs_list_append_m(xs_list *list, const char *mem, int dsz)
  546. /* adds a memory block to the list */
  547. {
  548. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  549. return _xs_list_write_litem(list, xs_size(list) - 1, mem, dsz);
  550. }
  551. xs_list *_xs_list_append(xs_list *list, const xs_val *vals[])
  552. /* adds several values to the list */
  553. {
  554. /* special case: if the first argument is NULL, just insert it */
  555. if (*vals == NULL)
  556. return xs_list_append_m(list, NULL, 0);
  557. while (*vals) {
  558. list = xs_list_append_m(list, *vals, xs_size(*vals));
  559. vals++;
  560. }
  561. return list;
  562. }
  563. int xs_list_iter(xs_list **list, const xs_val **value)
  564. /* iterates a list value */
  565. {
  566. int goon = 1;
  567. xs_val *p = *list;
  568. /* skip the start of the list */
  569. if (xs_type(p) == XSTYPE_LIST)
  570. p += 1 + _XS_TYPE_SIZE;
  571. /* an element? */
  572. if (xs_type(p) == XSTYPE_LITEM) {
  573. p++;
  574. *value = p;
  575. p += xs_size(*value);
  576. }
  577. else {
  578. /* end of list */
  579. goon = 0;
  580. }
  581. /* store back the pointer */
  582. *list = p;
  583. return goon;
  584. }
  585. int xs_list_next(const xs_list *list, const xs_val **value, int *ctxt)
  586. /* iterates a list, with context */
  587. {
  588. if (xs_type(list) != XSTYPE_LIST)
  589. return 0;
  590. int goon = 1;
  591. const char *p = list;
  592. /* skip the start of the list */
  593. if (*ctxt == 0)
  594. *ctxt = 1 + _XS_TYPE_SIZE;
  595. p += *ctxt;
  596. /* an element? */
  597. if (xs_type(p) == XSTYPE_LITEM) {
  598. p++;
  599. *value = p;
  600. p += xs_size(*value);
  601. }
  602. else {
  603. /* end of list */
  604. goon = 0;
  605. }
  606. /* update the context */
  607. *ctxt = p - list;
  608. return goon;
  609. }
  610. int xs_list_len(const xs_list *list)
  611. /* returns the number of elements in the list */
  612. {
  613. XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
  614. int c = 0;
  615. const xs_val *v;
  616. xs_list_foreach(list, v)
  617. c++;
  618. return c;
  619. }
  620. const xs_val *xs_list_get(const xs_list *list, int num)
  621. /* returns the element #num */
  622. {
  623. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  624. if (num < 0)
  625. num = xs_list_len(list) + num;
  626. int c = 0;
  627. const xs_val *v;
  628. xs_list_foreach(list, v) {
  629. if (c == num)
  630. return v;
  631. c++;
  632. }
  633. return NULL;
  634. }
  635. xs_list *xs_list_del(xs_list *list, int num)
  636. /* deletes element #num */
  637. {
  638. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  639. const xs_val *v;
  640. if ((v = xs_list_get(list, num)) != NULL)
  641. list = xs_collapse(list, v - 1 - list, xs_size(v - 1));
  642. return list;
  643. }
  644. xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data)
  645. /* inserts an element at #num position */
  646. {
  647. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  648. const xs_val *v;
  649. int offset;
  650. if ((v = xs_list_get(list, num)) != NULL)
  651. offset = v - list;
  652. else
  653. offset = xs_size(list);
  654. return _xs_list_write_litem(list, offset - 1, data, xs_size(data));
  655. }
  656. xs_list *xs_list_set(xs_list *list, int num, const xs_val *data)
  657. /* sets the element at #num position */
  658. {
  659. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  660. list = xs_list_del(list, num);
  661. list = xs_list_insert(list, num, data);
  662. return list;
  663. }
  664. xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last)
  665. /* gets a copy of the first or last element of a list, shrinking it */
  666. {
  667. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  668. int ct = 0;
  669. const xs_val *v = NULL;
  670. if (!last) {
  671. /* get the first */
  672. xs_list_next(list, &v, &ct);
  673. }
  674. else {
  675. /* iterate to the end */
  676. while (xs_list_next(list, &v, &ct));
  677. }
  678. if (v != NULL) {
  679. *data = xs_dup(v);
  680. /* collapse from the address of the element */
  681. list = xs_collapse(list, v - 1 - list, xs_size(v - 1));
  682. }
  683. return list;
  684. }
  685. int xs_list_in(const xs_list *list, const xs_val *val)
  686. /* returns the position of val in list or -1 */
  687. {
  688. XS_ASSERT_TYPE_NULL(list, XSTYPE_LIST);
  689. int n = 0;
  690. const xs_val *v;
  691. int sz = xs_size(val);
  692. xs_list_foreach(list, v) {
  693. if (sz == xs_size(v) && memcmp(val, v, sz) == 0)
  694. return n;
  695. n++;
  696. }
  697. return -1;
  698. }
  699. xs_str *xs_join(const xs_list *list, const char *sep)
  700. /* joins a list into a string */
  701. {
  702. XS_ASSERT_TYPE(list, XSTYPE_LIST);
  703. xs_str *s = NULL;
  704. const xs_val *v;
  705. int c = 0;
  706. int offset = 0;
  707. int ssz = strlen(sep);
  708. xs_list_foreach(list, v) {
  709. /* refuse to join non-string values */
  710. if (xs_type(v) == XSTYPE_STRING) {
  711. int sz;
  712. /* add the separator */
  713. if (c != 0 && ssz) {
  714. s = xs_realloc(s, offset + ssz);
  715. memcpy(s + offset, sep, ssz);
  716. offset += ssz;
  717. }
  718. /* add the element */
  719. if ((sz = strlen(v)) > 0) {
  720. s = xs_realloc(s, offset + sz);
  721. memcpy(s + offset, v, sz);
  722. offset += sz;
  723. }
  724. c++;
  725. }
  726. }
  727. /* null-terminate */
  728. s = xs_realloc(s, _xs_blk_size(offset + 1));
  729. s[offset] = '\0';
  730. return s;
  731. }
  732. xs_list *xs_split_n(const char *str, const char *sep, int times)
  733. /* splits a string into a list upto n times */
  734. {
  735. int sz = strlen(sep);
  736. char *ss;
  737. xs_list *list;
  738. list = xs_list_new();
  739. while (times > 0 && (ss = strstr(str, sep)) != NULL) {
  740. /* create a new string with this slice and add it to the list */
  741. xs *s = xs_str_new_sz(str, ss - str);
  742. list = xs_list_append(list, s);
  743. /* skip past the separator */
  744. str = ss + sz;
  745. times--;
  746. }
  747. /* add the rest of the string */
  748. list = xs_list_append(list, str);
  749. return list;
  750. }
  751. xs_list *xs_list_cat(xs_list *l1, const xs_list *l2)
  752. /* concatenates list l2 to l1 */
  753. {
  754. XS_ASSERT_TYPE(l1, XSTYPE_LIST);
  755. XS_ASSERT_TYPE(l2, XSTYPE_LIST);
  756. /* inserts at the end of l1 the content of l2 (skipping header and footer) */
  757. return xs_insert_m(l1, xs_size(l1) - 1,
  758. l2 + 1 + _XS_TYPE_SIZE, xs_size(l2) - (1 + _XS_TYPE_SIZE + 1));
  759. }
  760. /** keyvals **/
  761. int xs_keyval_size(const xs_str *key, const xs_val *value)
  762. /* returns the needed size for a keyval */
  763. {
  764. return 1 + xs_size(key) + xs_size(value);
  765. }
  766. xs_str *xs_keyval_key(const xs_keyval *keyval)
  767. /* returns a pointer to the key of the keyval */
  768. {
  769. return (xs_str *)&keyval[1];
  770. }
  771. xs_val *xs_keyval_value(const xs_keyval *keyval)
  772. /* returns a pointer to the value of the keyval */
  773. {
  774. return (xs_val *)&keyval[1 + xs_size(xs_keyval_key(keyval))];
  775. }
  776. xs_keyval *xs_keyval_make(xs_keyval *keyval, const xs_str *key, const xs_val *value)
  777. /* builds a keyval into mem (should have enough size) */
  778. {
  779. keyval[0] = XSTYPE_KEYVAL;
  780. memcpy(xs_keyval_key(keyval), key, xs_size(key));
  781. memcpy(xs_keyval_value(keyval), value, xs_size(value));
  782. return keyval;
  783. }
  784. /** dicts **/
  785. typedef struct {
  786. int value_offset; /* offset to value (from dict start) */
  787. int next; /* next node in sequential search */
  788. int child[4]; /* child nodes in hashed search */
  789. char key[]; /* C string key */
  790. } ditem_hdr;
  791. typedef struct {
  792. int size; /* size of full dict (_XS_TYPE_SIZE) */
  793. int first; /* first node for sequential search */
  794. int root; /* root node for hashed search */
  795. /* a bunch of ditem_hdr and value follows */
  796. } dict_hdr;
  797. xs_dict *xs_dict_new(void)
  798. /* creates a new dict */
  799. {
  800. /* size of dict */
  801. int sz = 1 + sizeof(dict_hdr);
  802. xs_dict *d = xs_realloc(NULL, sz);
  803. memset(d, '\0', sz);
  804. d[0] = XSTYPE_DICT;
  805. _xs_put_size(d, sz);
  806. return d;
  807. }
  808. static int *_xs_dict_locate(const xs_dict *dict, const char *key)
  809. /* locates a ditem */
  810. {
  811. unsigned int h = xs_hash_func(key, strlen(key));
  812. /* start from the root */
  813. dict_hdr *dh = (dict_hdr *)(dict + 1);
  814. int *off = &dh->root;
  815. while (*off) {
  816. /* pointer to ditem */
  817. ditem_hdr *di = (ditem_hdr *)(dict + *off);
  818. /* pointer to the key */
  819. const char *d_key = di->key;
  820. if (strcmp(key, d_key) == 0)
  821. break;
  822. off = &di->child[h >> 30];
  823. h <<= 2;
  824. }
  825. return off;
  826. }
  827. xs_dict *xs_dict_set(xs_dict *dict, const xs_str *key, const xs_val *value)
  828. /* sets a key/value pair */
  829. {
  830. if (value == NULL)
  831. value = xs_stock(XSTYPE_NULL);
  832. if (xs_type(dict) == XSTYPE_DICT) {
  833. int *o = _xs_dict_locate(dict, key);
  834. int end = xs_size(dict);
  835. if (!*o) {
  836. /* ditem does not exist yet: append to the end */
  837. *o = end;
  838. int ksz = xs_size(key);
  839. int vsz = xs_size(value);
  840. int dsz = sizeof(ditem_hdr) + ksz + vsz;
  841. /* open room in the dict for the full ditem */
  842. dict = xs_expand(dict, end, dsz);
  843. dict_hdr *dh = (dict_hdr *)(dict + 1);
  844. /* build the ditem */
  845. ditem_hdr *di = (ditem_hdr *)(dict + end);
  846. memset(di, '\0', dsz);
  847. /* set the offset to the value */
  848. di->value_offset = end + sizeof(ditem_hdr) + ksz;
  849. /* copy the key */
  850. memcpy(di->key, key, ksz);
  851. /* copy the value */
  852. memcpy(dict + di->value_offset, value, vsz);
  853. /* chain to the sequential list */
  854. di->next = dh->first;
  855. dh->first = end;
  856. }
  857. else {
  858. /* ditem already exists */
  859. ditem_hdr *di = (ditem_hdr *)(dict + *o);
  860. /* get pointer to the value offset */
  861. int *i = &di->value_offset;
  862. /* deleted? recover offset */
  863. if (*i < 0)
  864. *i *= -1;
  865. /* get old value */
  866. xs_val *o_value = dict + *i;
  867. /* will new value fit over the old one? */
  868. if (xs_size(value) <= xs_size(o_value)) {
  869. /* just overwrite */
  870. /* (difference is leaked inside the dict) */
  871. memcpy(o_value, value, xs_size(value));
  872. }
  873. else {
  874. /* not enough room: new value will live at the end of the dict */
  875. /* (old value is leaked inside the dict) */
  876. *i = end;
  877. dict = xs_insert(dict, end, value);
  878. }
  879. }
  880. }
  881. return dict;
  882. }
  883. xs_dict *xs_dict_append(xs_dict *dict, const xs_str *key, const xs_val *value)
  884. /* just an alias (for this implementation it's the same) */
  885. {
  886. return xs_dict_set(dict, key, value);
  887. }
  888. xs_dict *xs_dict_prepend(xs_dict *dict, const xs_str *key, const xs_val *value)
  889. /* just an alias (for this implementation it's the same) */
  890. {
  891. return xs_dict_set(dict, key, value);
  892. }
  893. xs_dict *xs_dict_del(xs_dict *dict, const xs_str *key)
  894. /* deletes a key/value pair */
  895. {
  896. if (xs_type(dict) == XSTYPE_DICT) {
  897. int *o = _xs_dict_locate(dict, key);
  898. if (*o) {
  899. /* found ditem */
  900. ditem_hdr *di = (ditem_hdr *)(dict + *o);
  901. /* deleted ditems have a negative value offset */
  902. di->value_offset *= -1;
  903. }
  904. }
  905. return dict;
  906. }
  907. const xs_val *xs_dict_get(const xs_dict *dict, const xs_str *key)
  908. /* gets a value by key, or NULL */
  909. {
  910. if (xs_type(dict) == XSTYPE_DICT) {
  911. int *o = _xs_dict_locate(dict, key);
  912. if (*o) {
  913. /* found ditem */
  914. ditem_hdr *di = (ditem_hdr *)(dict + *o);
  915. if (di->value_offset > 0)
  916. return dict + di->value_offset;
  917. }
  918. }
  919. return NULL;
  920. }
  921. int xs_dict_next(const xs_dict *dict, const xs_str **key, const xs_val **value, int *ctxt)
  922. /* dict iterator, with context */
  923. {
  924. if (xs_type(dict) != XSTYPE_DICT)
  925. return 0;
  926. if (*ctxt == 0) {
  927. /* at the beginning: get the first sequential item */
  928. const dict_hdr *dh = (dict_hdr *)(dict + 1);
  929. *ctxt = dh->first;
  930. }
  931. *value = NULL;
  932. while (*value == NULL && *ctxt > 0) {
  933. const ditem_hdr *di = (ditem_hdr *)(dict + *ctxt);
  934. /* get value */
  935. if (di->value_offset > 0) {
  936. *value = (xs_val *)(dict + di->value_offset);
  937. /* get key */
  938. *key = (xs_str *)&di->key;
  939. }
  940. /* get offset to next ditem */
  941. *ctxt = di->next ? di->next : -1;
  942. }
  943. return *value != NULL;
  944. }
  945. xs_dict *xs_dict_gc(const xs_dict *dict)
  946. /* creates a copy of dict, but garbage-collected */
  947. {
  948. xs_dict *nd = xs_dict_new();
  949. const xs_str *k;
  950. const xs_val *v;
  951. xs_dict_foreach(dict, k, v) {
  952. if (xs_type(v) == XSTYPE_DICT) {
  953. xs *sd = xs_dict_gc(v);
  954. nd = xs_dict_set(nd, k, sd);
  955. }
  956. else
  957. nd = xs_dict_set(nd, k, v);
  958. }
  959. return nd;
  960. }
  961. const xs_val *xs_dict_get_path_sep(const xs_dict *dict, const char *path, const char *sep)
  962. /* gets a value from dict given a path separated by sep */
  963. {
  964. /* split by the separator */
  965. xs *l = xs_split_n(path, sep, 1);
  966. /* only one part? just get */
  967. if (xs_list_len(l) == 1)
  968. return xs_dict_get(dict, path);
  969. const char *prefix = xs_list_get(l, 0);
  970. const char *rest = xs_list_get(l, 1);
  971. const xs_dict *sd = xs_dict_get(dict, prefix);
  972. if (xs_type(sd) == XSTYPE_DICT)
  973. return xs_dict_get_path_sep(sd, rest, sep);
  974. return NULL;
  975. }
  976. xs_dict *xs_dict_set_path_sep(xs_dict *dict, const char *path, const xs_val *value, const char *sep)
  977. /* sets a value into dict given a path separated by sep;
  978. intermediate dicts are created if needed */
  979. {
  980. /* split by the separator */
  981. xs *l = xs_split_n(path, sep, 1);
  982. /* only one part? just set */
  983. if (xs_list_len(l) == 1)
  984. return xs_dict_set(dict, path, value);
  985. const char *prefix = xs_list_get(l, 0);
  986. const char *rest = xs_list_get(l, 1);
  987. xs *nd = NULL;
  988. /* does the first part of path exist? */
  989. const xs_dict *cd = xs_dict_get(dict, prefix);
  990. if (xs_type(cd) == XSTYPE_DICT)
  991. nd = xs_dup(cd);
  992. else
  993. nd = xs_dict_new();
  994. /* move down the path */
  995. nd = xs_dict_set_path_sep(nd, rest, value, sep);
  996. /* set */
  997. return xs_dict_set(dict, prefix, nd);
  998. }
  999. /** other values **/
  1000. xs_val *xs_val_new(xstype t)
  1001. /* adds a new special value */
  1002. {
  1003. xs_val *v = xs_realloc(NULL, _xs_blk_size(1));
  1004. v[0] = t;
  1005. return v;
  1006. }
  1007. /** numbers */
  1008. xs_number *xs_number_new(double f)
  1009. /* adds a new number value */
  1010. {
  1011. xs_number *v;
  1012. char tmp[64];
  1013. snprintf(tmp, sizeof(tmp), "%.15lf", f);
  1014. /* strip useless zeros */
  1015. if (strchr(tmp, '.') != NULL) {
  1016. char *ptr;
  1017. for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--);
  1018. if (*ptr != '.')
  1019. ptr++;
  1020. *ptr = '\0';
  1021. }
  1022. /* alloc for the marker and the full string */
  1023. v = xs_realloc(NULL, _xs_blk_size(1 + xs_size(tmp)));
  1024. v[0] = XSTYPE_NUMBER;
  1025. memcpy(&v[1], tmp, xs_size(tmp));
  1026. return v;
  1027. }
  1028. double xs_number_get(const xs_number *v)
  1029. /* gets the number as a double */
  1030. {
  1031. double f = 0.0;
  1032. if (xs_type(v) == XSTYPE_NUMBER)
  1033. f = atof(&v[1]);
  1034. else
  1035. if (xs_type(v) == XSTYPE_STRING)
  1036. f = atof(v);
  1037. return f;
  1038. }
  1039. const char *xs_number_str(const xs_number *v)
  1040. /* gets the number as a string */
  1041. {
  1042. const char *p = NULL;
  1043. if (xs_type(v) == XSTYPE_NUMBER)
  1044. p = &v[1];
  1045. return p;
  1046. }
  1047. /** raw data blocks **/
  1048. xs_data *xs_data_new(const void *data, int size)
  1049. /* returns a new raw data value */
  1050. {
  1051. xs_data *v;
  1052. /* add the overhead (data type + size) */
  1053. int total_size = size + 1 + _XS_TYPE_SIZE;
  1054. v = xs_realloc(NULL, _xs_blk_size(total_size));
  1055. v[0] = XSTYPE_DATA;
  1056. _xs_put_size(v, total_size);
  1057. memcpy(&v[1 + _XS_TYPE_SIZE], data, size);
  1058. return v;
  1059. }
  1060. int xs_data_size(const xs_data *value)
  1061. /* returns the size of the data stored inside value */
  1062. {
  1063. return _xs_get_size(value) - (1 + _XS_TYPE_SIZE);
  1064. }
  1065. void xs_data_get(void *data, const xs_data *value)
  1066. /* copies the raw data stored inside value into data */
  1067. {
  1068. memcpy(data, &value[1 + _XS_TYPE_SIZE], xs_data_size(value));
  1069. }
  1070. void *xs_memmem(const char *haystack, int h_size, const char *needle, int n_size)
  1071. /* clone of memmem */
  1072. {
  1073. char *p, *r = NULL;
  1074. int offset = 0;
  1075. while (!r && h_size - offset > n_size &&
  1076. (p = memchr(haystack + offset, *needle, h_size - offset))) {
  1077. if (memcmp(p, needle, n_size) == 0)
  1078. r = p;
  1079. else
  1080. offset = p - haystack + 1;
  1081. }
  1082. return r;
  1083. }
  1084. unsigned int xs_hash_func(const char *data, int size)
  1085. /* a general purpose hashing function */
  1086. {
  1087. unsigned int hash = 0x666;
  1088. int n;
  1089. for (n = 0; n < size; n++) {
  1090. hash ^= (unsigned char)data[n];
  1091. hash *= 111111111;
  1092. }
  1093. return hash ^ hash >> 16;
  1094. }
  1095. #endif /* XS_IMPLEMENTATION */
  1096. #endif /* _XS_H */