diff options
| author | 2025-08-23 20:41:16 +0200 | |
|---|---|---|
| committer | 2025-08-23 20:41:16 +0200 | |
| commit | 75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b (patch) | |
| tree | e8988779f9a6ac8d93695ad0e6adbf7b2c3aac29 /xs.h | |
| parent | Ensure the tag is a string in server_get_handler(). (diff) | |
| download | penes-snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.tar.gz penes-snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.tar.xz penes-snac2-75cc7ae7a3799a27a5647a2c7d912e6a6e2fbb5b.zip | |
Added some more helping functions.
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 77 |
1 files changed, 60 insertions, 17 deletions
| @@ -134,7 +134,9 @@ xs_dict *xs_dict_set_path_sep(xs_dict *dict, const char *path, const xs_val *val | |||
| 134 | 134 | ||
| 135 | xs_val *xs_val_new(xstype t); | 135 | xs_val *xs_val_new(xstype t); |
| 136 | xs_number *xs_number_new(double f); | 136 | xs_number *xs_number_new(double f); |
| 137 | xs_number *xs_number_new_l(long l); | ||
| 137 | double xs_number_get(const xs_number *v); | 138 | double xs_number_get(const xs_number *v); |
| 139 | long xs_number_get_l(const xs_number *v); | ||
| 138 | const char *xs_number_str(const xs_number *v); | 140 | const char *xs_number_str(const xs_number *v); |
| 139 | 141 | ||
| 140 | xs_data *xs_data_new(const void *data, int size); | 142 | xs_data *xs_data_new(const void *data, int size); |
| @@ -163,6 +165,7 @@ uint64_t xs_hash64_func(const char *data, int size); | |||
| 163 | #define xs_is_string(v) (xs_type((v)) == XSTYPE_STRING) | 165 | #define xs_is_string(v) (xs_type((v)) == XSTYPE_STRING) |
| 164 | #define xs_is_list(v) (xs_type((v)) == XSTYPE_LIST) | 166 | #define xs_is_list(v) (xs_type((v)) == XSTYPE_LIST) |
| 165 | #define xs_is_dict(v) (xs_type((v)) == XSTYPE_DICT) | 167 | #define xs_is_dict(v) (xs_type((v)) == XSTYPE_DICT) |
| 168 | #define xs_is_number(v) (xs_type((v)) == XSTYPE_NUMBER) | ||
| 166 | 169 | ||
| 167 | #define xs_list_foreach(l, v) for (int ct_##__LINE__ = 0; xs_list_next(l, &v, &ct_##__LINE__); ) | 170 | #define xs_list_foreach(l, v) for (int ct_##__LINE__ = 0; xs_list_next(l, &v, &ct_##__LINE__); ) |
| 168 | #define xs_dict_foreach(l, k, v) for (int ct_##__LINE__ = 0; xs_dict_next(l, &k, &v, &ct_##__LINE__); ) | 171 | #define xs_dict_foreach(l, k, v) for (int ct_##__LINE__ = 0; xs_dict_next(l, &k, &v, &ct_##__LINE__); ) |
| @@ -362,9 +365,22 @@ int xs_is_null(const xs_val *data) | |||
| 362 | int xs_cmp(const xs_val *v1, const xs_val *v2) | 365 | int xs_cmp(const xs_val *v1, const xs_val *v2) |
| 363 | /* compares two values */ | 366 | /* compares two values */ |
| 364 | { | 367 | { |
| 365 | if (xs_type(v1) == XSTYPE_STRING && xs_type(v2) == XSTYPE_STRING) | 368 | xstype t1 = xs_type(v1); |
| 369 | xstype t2 = xs_type(v2); | ||
| 370 | |||
| 371 | if (t1 == XSTYPE_NULL || t2 == XSTYPE_NULL) | ||
| 372 | return 0; | ||
| 373 | |||
| 374 | if (t1 == XSTYPE_STRING && t2 == XSTYPE_STRING) | ||
| 366 | return strcmp(v1, v2); | 375 | return strcmp(v1, v2); |
| 367 | 376 | ||
| 377 | if (t1 == XSTYPE_NUMBER && t2 == XSTYPE_NUMBER) { | ||
| 378 | double d1 = xs_number_get(v1); | ||
| 379 | double d2 = xs_number_get(v2); | ||
| 380 | |||
| 381 | return d1 == d2 ? 0 : d1 < d2 ? -1 : 1; | ||
| 382 | } | ||
| 383 | |||
| 368 | int s1 = xs_size(v1); | 384 | int s1 = xs_size(v1); |
| 369 | int s2 = xs_size(v2); | 385 | int s2 = xs_size(v2); |
| 370 | int d = s1 - s2; | 386 | int d = s1 - s2; |
| @@ -975,6 +991,9 @@ xs_str *xs_join(const xs_list *list, const char *sep) | |||
| 975 | 991 | ||
| 976 | xs_list_foreach(list, v) { | 992 | xs_list_foreach(list, v) { |
| 977 | /* refuse to join non-string values */ | 993 | /* refuse to join non-string values */ |
| 994 | if (xs_type(v) == XSTYPE_NUMBER) | ||
| 995 | v = xs_number_str(v); | ||
| 996 | |||
| 978 | if (xs_type(v) == XSTYPE_STRING) { | 997 | if (xs_type(v) == XSTYPE_STRING) { |
| 979 | int sz; | 998 | int sz; |
| 980 | 999 | ||
| @@ -1396,10 +1415,20 @@ xs_val *xs_val_new(xstype t) | |||
| 1396 | 1415 | ||
| 1397 | /** numbers */ | 1416 | /** numbers */ |
| 1398 | 1417 | ||
| 1418 | xs_number *_xs_number_new(const char *str) | ||
| 1419 | { | ||
| 1420 | xs_number *v = xs_realloc(NULL, 1 + xs_size(str)); | ||
| 1421 | |||
| 1422 | v[0] = XSTYPE_NUMBER; | ||
| 1423 | memcpy(&v[1], str, xs_size(str)); | ||
| 1424 | |||
| 1425 | return v; | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | |||
| 1399 | xs_number *xs_number_new(double f) | 1429 | xs_number *xs_number_new(double f) |
| 1400 | /* adds a new number value */ | 1430 | /* creates a new number value from a double */ |
| 1401 | { | 1431 | { |
| 1402 | xs_number *v; | ||
| 1403 | char tmp[64]; | 1432 | char tmp[64]; |
| 1404 | 1433 | ||
| 1405 | snprintf(tmp, sizeof(tmp), "%.15lf", f); | 1434 | snprintf(tmp, sizeof(tmp), "%.15lf", f); |
| @@ -1416,40 +1445,54 @@ xs_number *xs_number_new(double f) | |||
| 1416 | *ptr = '\0'; | 1445 | *ptr = '\0'; |
| 1417 | } | 1446 | } |
| 1418 | 1447 | ||
| 1419 | /* alloc for the marker and the full string */ | 1448 | return _xs_number_new(tmp); |
| 1420 | v = xs_realloc(NULL, _xs_blk_size(1 + xs_size(tmp))); | 1449 | } |
| 1421 | 1450 | ||
| 1422 | v[0] = XSTYPE_NUMBER; | ||
| 1423 | memcpy(&v[1], tmp, xs_size(tmp)); | ||
| 1424 | 1451 | ||
| 1425 | return v; | 1452 | xs_number *xs_number_new_l(long l) |
| 1453 | /* creates a new number value from a long */ | ||
| 1454 | { | ||
| 1455 | char tmp[64]; | ||
| 1456 | |||
| 1457 | snprintf(tmp, sizeof(tmp), "%ld", l); | ||
| 1458 | |||
| 1459 | return _xs_number_new(tmp); | ||
| 1426 | } | 1460 | } |
| 1427 | 1461 | ||
| 1428 | 1462 | ||
| 1429 | double xs_number_get(const xs_number *v) | 1463 | double xs_number_get(const xs_number *v) |
| 1430 | /* gets the number as a double */ | 1464 | /* gets the number as a double */ |
| 1431 | { | 1465 | { |
| 1432 | double f = 0.0; | 1466 | if (xs_type(v) == XSTYPE_NUMBER) |
| 1467 | v++; | ||
| 1468 | |||
| 1469 | if (xs_type(v) == XSTYPE_STRING) | ||
| 1470 | return atof(v); | ||
| 1433 | 1471 | ||
| 1472 | return 0.0; | ||
| 1473 | } | ||
| 1474 | |||
| 1475 | |||
| 1476 | long xs_number_get_l(const xs_number *v) | ||
| 1477 | /* gets the number as a long */ | ||
| 1478 | { | ||
| 1434 | if (xs_type(v) == XSTYPE_NUMBER) | 1479 | if (xs_type(v) == XSTYPE_NUMBER) |
| 1435 | f = atof(&v[1]); | 1480 | v++; |
| 1436 | else | 1481 | |
| 1437 | if (xs_type(v) == XSTYPE_STRING) | 1482 | if (xs_type(v) == XSTYPE_STRING) |
| 1438 | f = atof(v); | 1483 | return atol(v); |
| 1439 | 1484 | ||
| 1440 | return f; | 1485 | return 0; |
| 1441 | } | 1486 | } |
| 1442 | 1487 | ||
| 1443 | 1488 | ||
| 1444 | const char *xs_number_str(const xs_number *v) | 1489 | const char *xs_number_str(const xs_number *v) |
| 1445 | /* gets the number as a string */ | 1490 | /* gets the number as a string */ |
| 1446 | { | 1491 | { |
| 1447 | const char *p = NULL; | ||
| 1448 | |||
| 1449 | if (xs_type(v) == XSTYPE_NUMBER) | 1492 | if (xs_type(v) == XSTYPE_NUMBER) |
| 1450 | p = &v[1]; | 1493 | return v + 1; |
| 1451 | 1494 | ||
| 1452 | return p; | 1495 | return NULL; |
| 1453 | } | 1496 | } |
| 1454 | 1497 | ||
| 1455 | 1498 | ||