xs.h 34 KB

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