xs.h 34 KB

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