repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
zap
github_2023
zigzap
c
fio_timer_test_task
FIO_FUNC void fio_timer_test_task(void *arg) { ++(((size_t *)arg)[0]); }
/* ***************************************************************************** Testing fio_timers ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L9374-L9374
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_socket_test
FIO_FUNC void fio_socket_test(void) { /* initialize unix socket name */ fio_str_s sock_name = FIO_STR_INIT; #ifdef P_tmpdir fio_str_write(&sock_name, P_tmpdir, strlen(P_tmpdir)); if (fio_str_len(&sock_name) && fio_str_data(&sock_name)[fio_str_len(&sock_name) - 1] == '/') fio_str_resize(&sock_name, fio_str_len(&sock_name) - 1); #else fio_str_write(&sock_name, "/tmp", 4); #endif fio_str_printf(&sock_name, "/fio_test_sock-%d.sock", (int)getpid()); fprintf(stderr, "=== Testing facil.io listening socket creation (partial " "testing only).\n"); fprintf(stderr, "* testing on TCP/IP port 8765 and Unix socket: %s\n", fio_str_data(&sock_name)); intptr_t uuid = fio_socket(fio_str_data(&sock_name), NULL, 1); FIO_ASSERT(uuid != -1, "Failed to open unix socket\n"); FIO_ASSERT(uuid_data(uuid).open, "Unix socket not initialized"); intptr_t client1 = fio_socket(fio_str_data(&sock_name), NULL, 0); FIO_ASSERT(client1 != -1, "Failed to connect to unix socket."); intptr_t client2 = fio_accept(uuid); FIO_ASSERT(client2 != -1, "Failed to accept unix socket connection."); fprintf(stderr, "* Unix server addr %s\n", fio_peer_addr(uuid).data); fprintf(stderr, "* Unix client1 addr %s\n", fio_peer_addr(client1).data); fprintf(stderr, "* Unix client2 addr %s\n", fio_peer_addr(client2).data); { char tmp_buf[28]; ssize_t r = -1; ssize_t timer_junk; fio_write(client1, "Hello World", 11); if (0) { /* packet may have been sent synchronously, don't test */ if (!uuid_data(client1).packet) unlink(__FILE__ ".sock"); FIO_ASSERT(uuid_data(client1).packet, "fio_write error, no packet!") } /* prevent poll from hanging */ fio_run_every(5, 1, fio_timer_test_task, &timer_junk, fio_timer_test_task); errno = EAGAIN; for (size_t i = 0; i < 100 && r <= 0 && (r == 0 || errno == EAGAIN || errno == EWOULDBLOCK); ++i) { fio_poll(); fio_defer_perform(); fio_reschedule_thread(); errno = 0; r = fio_read(client2, tmp_buf, 28); } if (!(r > 0 && r <= 28) || memcmp("Hello World", tmp_buf, r)) { perror("* ernno"); unlink(__FILE__ ".sock"); } FIO_ASSERT(r > 0 && r <= 28, "Failed to read from unix socket " __FILE__ ".sock %zd", r); FIO_ASSERT(!memcmp("Hello World", tmp_buf, r), "Unix socket Read/Write cycle error (%zd: %.*s)", r, (int)r, tmp_buf); fprintf(stderr, "* Unix socket Read/Write cycle passed: %.*s\n", (int)r, tmp_buf); fio_data->last_cycle.tv_sec += 10; fio_timer_clear_all(); } fio_force_close(client1); fio_force_close(client2); fio_force_close(uuid); unlink(fio_str_data(&sock_name)); /* free unix socket name */ fio_str_free(&sock_name); uuid = fio_socket(NULL, "8765", 1); FIO_ASSERT(uuid != -1, "Failed to open TCP/IP socket on port 8765"); FIO_ASSERT(uuid_data(uuid).open, "TCP/IP socket not initialized"); fprintf(stderr, "* TCP/IP server addr %s\n", fio_peer_addr(uuid).data); client1 = fio_socket("Localhost", "8765", 0); FIO_ASSERT(client1 != -1, "Failed to connect to TCP/IP socket on port 8765"); fprintf(stderr, "* TCP/IP client1 addr %s\n", fio_peer_addr(client1).data); errno = EAGAIN; for (size_t i = 0; i < 100 && (errno == EAGAIN || errno == EWOULDBLOCK); ++i) { errno = 0; fio_reschedule_thread(); client2 = fio_accept(uuid); } if (client2 == -1) perror("accept error"); FIO_ASSERT(client2 != -1, "Failed to accept TCP/IP socket connection on port 8765"); fprintf(stderr, "* TCP/IP client2 addr %s\n", fio_peer_addr(client2).data); fio_force_close(client1); fio_force_close(client2); fio_force_close(uuid); fio_timer_clear_all(); fio_defer_clear_tasks(); fprintf(stderr, "* passed.\n"); }
/* ***************************************************************************** Testing listening socket ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L9440-L9537
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cycle_test_task
FIO_FUNC void fio_cycle_test_task(void *arg) { fio_stop(); (void)arg; }
/* ***************************************************************************** Testing listening socket ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L9543-L9546
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_riskyhash_speed_test
FIO_FUNC void fio_riskyhash_speed_test(void) { /* test based on code from BearSSL with credit to Thomas Pornin */ uint8_t buffer[8192]; memset(buffer, 'T', sizeof(buffer)); /* warmup */ uint64_t hash = 0; for (size_t i = 0; i < 4; i++) { hash += fio_risky_hash(buffer, 8192, 1); memcpy(buffer, &hash, sizeof(hash)); } /* loop until test runs for more than 2 seconds */ for (uint64_t cycles = 8192;;) { clock_t start, end; start = clock(); for (size_t i = cycles; i > 0; i--) { hash += fio_risky_hash(buffer, 8192, 1); __asm__ volatile("" ::: "memory"); } end = clock(); memcpy(buffer, &hash, sizeof(hash)); if ((end - start) >= (2 * CLOCKS_PER_SEC) || cycles >= ((uint64_t)1 << 62)) { fprintf(stderr, "%-20s %8.2f MB/s\n", "fio_risky_hash", (double)(sizeof(buffer) * cycles) / (((end - start) * 1000000.0 / CLOCKS_PER_SEC))); break; } cycles <<= 2; } }
/* ***************************************************************************** Bad Hash (risky hash) tests ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L9930-L9959
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_siphash_speed_test
FIO_FUNC void fio_siphash_speed_test(void) { /* test based on code from BearSSL with credit to Thomas Pornin */ uint8_t buffer[8192]; memset(buffer, 'T', sizeof(buffer)); /* warmup */ uint64_t hash = 0; for (size_t i = 0; i < 4; i++) { hash += fio_siphash24(buffer, sizeof(buffer), 0, 0); memcpy(buffer, &hash, sizeof(hash)); } /* loop until test runs for more than 2 seconds */ for (uint64_t cycles = 8192;;) { clock_t start, end; start = clock(); for (size_t i = cycles; i > 0; i--) { hash += fio_siphash24(buffer, sizeof(buffer), 0, 0); __asm__ volatile("" ::: "memory"); } end = clock(); memcpy(buffer, &hash, sizeof(hash)); if ((end - start) >= (2 * CLOCKS_PER_SEC) || cycles >= ((uint64_t)1 << 62)) { fprintf(stderr, "%-20s %8.2f MB/s\n", "fio SipHash24", (double)(sizeof(buffer) * cycles) / (((end - start) * 1000000.0 / CLOCKS_PER_SEC))); break; } cycles <<= 2; } /* loop until test runs for more than 2 seconds */ for (uint64_t cycles = 8192;;) { clock_t start, end; start = clock(); for (size_t i = cycles; i > 0; i--) { hash += fio_siphash13(buffer, sizeof(buffer), 0, 0); __asm__ volatile("" ::: "memory"); } end = clock(); memcpy(buffer, &hash, sizeof(hash)); if ((end - start) >= (2 * CLOCKS_PER_SEC) || cycles >= ((uint64_t)1 << 62)) { fprintf(stderr, "%-20s %8.2f MB/s\n", "fio SipHash13", (double)(sizeof(buffer) * cycles) / (((end - start) * 1000000.0 / CLOCKS_PER_SEC))); break; } cycles <<= 2; } }
/* ***************************************************************************** SipHash tests ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L9988-L10036
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_sha1_speed_test
FIO_FUNC void fio_sha1_speed_test(void) { /* test based on code from BearSSL with credit to Thomas Pornin */ uint8_t buffer[8192]; uint8_t result[21]; fio_sha1_s sha1; memset(buffer, 'T', sizeof(buffer)); /* warmup */ for (size_t i = 0; i < 4; i++) { sha1 = fio_sha1_init(); fio_sha1_write(&sha1, buffer, sizeof(buffer)); memcpy(result, fio_sha1_result(&sha1), 21); } /* loop until test runs for more than 2 seconds */ for (size_t cycles = 8192;;) { clock_t start, end; sha1 = fio_sha1_init(); start = clock(); for (size_t i = cycles; i > 0; i--) { fio_sha1_write(&sha1, buffer, sizeof(buffer)); __asm__ volatile("" ::: "memory"); } end = clock(); fio_sha1_result(&sha1); if ((end - start) >= (2 * CLOCKS_PER_SEC)) { fprintf(stderr, "%-20s %8.2f MB/s\n", "fio SHA-1", (double)(sizeof(buffer) * cycles) / (((end - start) * 1000000.0 / CLOCKS_PER_SEC))); break; } cycles <<= 1; } }
/* ***************************************************************************** SHA-1 tests ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L10051-L10082
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_base64_speed_test
FIO_FUNC void fio_base64_speed_test(void) { /* test based on code from BearSSL with credit to Thomas Pornin */ char buffer[8192]; char result[8192 * 2]; memset(buffer, 'T', sizeof(buffer)); /* warmup */ for (size_t i = 0; i < 4; i++) { fio_base64_encode(result, buffer, sizeof(buffer)); memcpy(buffer, result, sizeof(buffer)); } /* loop until test runs for more than 2 seconds */ for (size_t cycles = 8192;;) { clock_t start, end; start = clock(); for (size_t i = cycles; i > 0; i--) { fio_base64_encode(result, buffer, sizeof(buffer)); __asm__ volatile("" ::: "memory"); } end = clock(); if ((end - start) >= (2 * CLOCKS_PER_SEC)) { fprintf(stderr, "%-20s %8.2f MB/s\n", "fio Base64 Encode", (double)(sizeof(buffer) * cycles) / (((end - start) * 1000000.0 / CLOCKS_PER_SEC))); break; } cycles <<= 2; } /* speed test decoding */ const int encoded_len = fio_base64_encode(result, buffer, (int)(sizeof(buffer) - 2)); /* warmup */ for (size_t i = 0; i < 4; i++) { fio_base64_decode(buffer, result, encoded_len); __asm__ volatile("" ::: "memory"); } /* loop until test runs for more than 2 seconds */ for (size_t cycles = 8192;;) { clock_t start, end; start = clock(); for (size_t i = cycles; i > 0; i--) { fio_base64_decode(buffer, result, encoded_len); __asm__ volatile("" ::: "memory"); } end = clock(); if ((end - start) >= (2 * CLOCKS_PER_SEC)) { fprintf(stderr, "%-20s %8.2f MB/s\n", "fio Base64 Decode", (double)(encoded_len * cycles) / (((end - start) * 1000000.0 / CLOCKS_PER_SEC))); break; } cycles <<= 2; } }
/* ***************************************************************************** Base64 tests ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L10406-L10459
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_test_random
FIO_FUNC void fio_test_random(void) { fprintf(stderr, "=== Testing random generator\n"); uint64_t rnd = fio_rand64(); FIO_ASSERT((rnd != fio_rand64() && rnd != fio_rand64()), "fio_rand64 returned the same result three times in a row."); #if NODEBUG uint64_t buffer1[8]; uint8_t buffer2[8192]; clock_t start, end; start = clock(); for (size_t i = 0; i < (8388608 / (64 / 8)); i++) { buffer1[i & 7] = fio_rand64(); __asm__ volatile("" ::: "memory"); } end = clock(); fprintf(stderr, "+ Random generator available\n+ created 8Mb using 64bits " "Random %lu CPU clock count ~%.2fMb/s\n", end - start, (8.0) / (((double)(end - start)) / CLOCKS_PER_SEC)); start = clock(); for (size_t i = 0; i < (8388608 / (8192)); i++) { fio_rand_bytes(buffer2, 8192); __asm__ volatile("" ::: "memory"); } end = clock(); fprintf(stderr, "+ created 8Mb using 8,192 Bytes " "Random %lu CPU clock count ~%.2fMb/s\n", end - start, (8.0) / (((double)(end - start)) / CLOCKS_PER_SEC)); (void)buffer1; (void)buffer2; #endif }
/******************************************************************************* Random Testing ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L10535-L10567
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_uuid_link_test_on_close
FIO_FUNC void fio_uuid_link_test_on_close(void *obj) { fio_atomic_add((uintptr_t *)obj, 1); }
/* ***************************************************************************** Test UUID Linking ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L10617-L10619
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_str2u_test
FIO_FUNC void fio_str2u_test(void) { fprintf(stderr, "=== Testing fio_u2strX and fio_u2strX functions.\n"); char buffer[32]; for (int64_t i = -1024; i < 1024; ++i) { fio_u2str64(buffer, i); __asm__ volatile("" ::: "memory"); FIO_ASSERT((int64_t)fio_str2u64(buffer) == i, "fio_u2str64 / fio_str2u64 mismatch %zd != %zd", (ssize_t)fio_str2u64(buffer), (ssize_t)i); } for (int32_t i = -1024; i < 1024; ++i) { fio_u2str32(buffer, i); __asm__ volatile("" ::: "memory"); FIO_ASSERT((int32_t)fio_str2u32(buffer) == i, "fio_u2str32 / fio_str2u32 mismatch %zd != %zd", (ssize_t)(fio_str2u32(buffer)), (ssize_t)i); } for (int16_t i = -1024; i < 1024; ++i) { fio_u2str16(buffer, i); __asm__ volatile("" ::: "memory"); FIO_ASSERT((int16_t)fio_str2u16(buffer) == i, "fio_u2str16 / fio_str2u16 mismatch %zd != %zd", (ssize_t)(fio_str2u16(buffer)), (ssize_t)i); } fprintf(stderr, "* passed.\n"); }
/* ***************************************************************************** Byte Order Testing ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L10644-L10669
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_atof_test
FIO_FUNC void fio_atof_test(void) { fprintf(stderr, "=== Testing fio_ftoa and fio_ftoa (partial)\n"); #define TEST_DOUBLE(s, d, must) \ do { \ char *p = (char *)(s); \ double r = fio_atof(&p); \ if (r != (d)) { \ FIO_LOG_DEBUG("Double Test Error! %s => %.19g (not %.19g)", \ ((char *)(s)), r, d); \ if (must) { \ FIO_ASSERT(0, "double test failed on %s", ((char *)(s))); \ exit(-1); \ } \ } \ } while (0) /* The numbers were copied from https://github.com/miloyip/rapidjson */ TEST_DOUBLE("0.0", 0.0, 1); TEST_DOUBLE("-0.0", -0.0, 1); TEST_DOUBLE("1.0", 1.0, 1); TEST_DOUBLE("-1.0", -1.0, 1); TEST_DOUBLE("1.5", 1.5, 1); TEST_DOUBLE("-1.5", -1.5, 1); TEST_DOUBLE("3.1416", 3.1416, 1); TEST_DOUBLE("1E10", 1E10, 1); TEST_DOUBLE("1e10", 1e10, 1); TEST_DOUBLE("1E+10", 1E+10, 1); TEST_DOUBLE("1E-10", 1E-10, 1); TEST_DOUBLE("-1E10", -1E10, 1); TEST_DOUBLE("-1e10", -1e10, 1); TEST_DOUBLE("-1E+10", -1E+10, 1); TEST_DOUBLE("-1E-10", -1E-10, 1); TEST_DOUBLE("1.234E+10", 1.234E+10, 1); TEST_DOUBLE("1.234E-10", 1.234E-10, 1); TEST_DOUBLE("1.79769e+308", 1.79769e+308, 1); TEST_DOUBLE("2.22507e-308", 2.22507e-308, 1); TEST_DOUBLE("-1.79769e+308", -1.79769e+308, 1); TEST_DOUBLE("-2.22507e-308", -2.22507e-308, 1); TEST_DOUBLE("4.9406564584124654e-324", 4.9406564584124654e-324, 0); TEST_DOUBLE("2.2250738585072009e-308", 2.2250738585072009e-308, 0); TEST_DOUBLE("2.2250738585072014e-308", 2.2250738585072014e-308, 1); TEST_DOUBLE("1.7976931348623157e+308", 1.7976931348623157e+308, 1); TEST_DOUBLE("1e-10000", 0.0, 0); TEST_DOUBLE("18446744073709551616", 18446744073709551616.0, 0); TEST_DOUBLE("-9223372036854775809", -9223372036854775809.0, 0); TEST_DOUBLE("0.9868011474609375", 0.9868011474609375, 0); TEST_DOUBLE("123e34", 123e34, 1); TEST_DOUBLE("45913141877270640000.0", 45913141877270640000.0, 1); TEST_DOUBLE("2.2250738585072011e-308", 2.2250738585072011e-308, 0); TEST_DOUBLE("1e-214748363", 0.0, 1); TEST_DOUBLE("1e-214748364", 0.0, 1); TEST_DOUBLE("0.017976931348623157e+310, 1", 1.7976931348623157e+308, 0); TEST_DOUBLE("2.2250738585072012e-308", 2.2250738585072014e-308, 0); TEST_DOUBLE("2.22507385850720113605740979670913197593481954635164565e-308", 2.2250738585072014e-308, 0); TEST_DOUBLE("0.999999999999999944488848768742172978818416595458984375", 1.0, 0); TEST_DOUBLE("0.999999999999999944488848768742172978818416595458984376", 1.0, 0); TEST_DOUBLE("1.00000000000000011102230246251565404236316680908203125", 1.0, 0); TEST_DOUBLE("1.00000000000000011102230246251565404236316680908203124", 1.0, 0); TEST_DOUBLE("72057594037927928.0", 72057594037927928.0, 0); TEST_DOUBLE("72057594037927936.0", 72057594037927936.0, 0); TEST_DOUBLE("72057594037927932.0", 72057594037927936.0, 0); TEST_DOUBLE("7205759403792793200001e-5", 72057594037927936.0, 0); TEST_DOUBLE("9223372036854774784.0", 9223372036854774784.0, 0); TEST_DOUBLE("9223372036854775808.0", 9223372036854775808.0, 0); TEST_DOUBLE("9223372036854775296.0", 9223372036854775808.0, 0); TEST_DOUBLE("922337203685477529600001e-5", 9223372036854775808.0, 0); TEST_DOUBLE("10141204801825834086073718800384", 10141204801825834086073718800384.0, 0); TEST_DOUBLE("10141204801825835211973625643008", 10141204801825835211973625643008.0, 0); TEST_DOUBLE("10141204801825834649023672221696", 10141204801825835211973625643008.0, 0); TEST_DOUBLE("1014120480182583464902367222169600001e-5", 10141204801825835211973625643008.0, 0); TEST_DOUBLE("5708990770823838890407843763683279797179383808", 5708990770823838890407843763683279797179383808.0, 0); TEST_DOUBLE("5708990770823839524233143877797980545530986496", 5708990770823839524233143877797980545530986496.0, 0); TEST_DOUBLE("5708990770823839207320493820740630171355185152", 5708990770823839524233143877797980545530986496.0, 0); TEST_DOUBLE("5708990770823839207320493820740630171355185152001e-3", 5708990770823839524233143877797980545530986496.0, 0); fprintf(stderr, "\n* passed.\n"); }
/* ***************************************************************************** String 2 Float and Float 2 String (partial) testing ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L10910-L11005
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_test
void fio_test(void) { FIO_ASSERT(fio_capa(), "facil.io initialization error!"); fio_malloc_test(); fio_state_callback_test(); fio_str_test(); fio_atol_test(); fio_atof_test(); fio_str2u_test(); fio_llist_test(); fio_ary_test(); fio_set_test(); fio_defer_test(); fio_timer_test(); fio_poll_test(); fio_socket_test(); fio_uuid_link_test(); fio_cycle_test(); fio_riskyhash_test(); fio_siphash_test(); fio_sha1_test(); fio_sha2_test(); fio_base64_test(); fio_test_random(); fio_pubsub_test(); (void)fio_sentinel_task; (void)deferred_on_shutdown; (void)fio_poll; }
/* ***************************************************************************** Run all tests ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L11010-L11037
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
websocket_subscribe_zigcompat
uintptr_t websocket_subscribe_zigcompat(struct websocket_subscribe_s_zigcompat args) { return websocket_subscribe(args.ws, // .ws = args.ws, .channel = args.channel, .on_message = args.on_message, .on_unsubscribe = args.on_unsubscribe, .udata = args.udata, .match = args.match, .force_binary = args.force_binary & 1, .force_text = args.force_text & 1, ); }
/** * Subscribes to a channel. See {struct websocket_subscribe_s} for possible * arguments. * * Returns a subscription ID on success and 0 on failure. * * All subscriptions are automatically revoked once the websocket is closed. * * If the connections subscribes to the same channel more than once, messages * will be merged. However, another subscription ID will be assigned, since two * calls to {websocket_unsubscribe} will be required in order to unregister from * the channel. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio_zig.c#L120-L132
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cli_map_line2alias
static void fio_cli_map_line2alias(char const *line) { cstr_s n = {.data = line}; while (n.data[0] == '-') { while (n.data[n.len] && n.data[n.len] != ' ' && n.data[n.len] != ',') { ++n.len; } const char *old = NULL; fio_cli_hash_insert(&fio_aliases, FIO_CLI_HASH_VAL(n), n, (void *)line, &old); if (old) { FIO_LOG_WARNING("CLI argument name conflict detected\n" " The following two directives conflict:\n" "\t%s\n\t%s\n", old, line); } while (n.data[n.len] && (n.data[n.len] == ' ' || n.data[n.len] == ',')) { ++n.len; } n.data += n.len; n.len = 0; } }
/* ***************************************************************************** CLI Parsing ***************************************************************************** */ /* ***************************************************************************** CLI Parsing ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/cli/fio_cli.c#L63-L85
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cli_get_i
int fio_cli_get_i(char const *name) { char const *val = fio_cli_get(name); if (!val) return 0; int i = (int)fio_atol((char **)&val); return i; }
/** Returns the argument's value as an integer. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/cli/fio_cli.c#L400-L406
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cli_unnamed_count
unsigned int fio_cli_unnamed_count(void) { return (unsigned int)fio_unnamed_count; }
/** Returns the number of unrecognized argument. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/cli/fio_cli.c#L409-L411
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_cli_set
void fio_cli_set(char const *name, char const *value) { cstr_s n = (cstr_s){.data = name, .len = strlen(name)}; fio_cli_hash_insert(&fio_values, FIO_CLI_HASH_VAL(n), n, value, NULL); }
/** * Sets the argument's value as a NUL terminated C String (no copy!). * * Note: this does NOT copy the C strings to memory. Memory should be kept * alive until `fio_cli_end` is called. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/cli/fio_cli.c#L428-L431
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_dealloc
static void fiobj_ary_dealloc(FIOBJ o, void (*task)(FIOBJ, void *), void *arg) { FIO_ARY_FOR((&obj2ary(o)->ary), i) { task(*i, arg); } fio_ary___free(&obj2ary(o)->ary); fio_free(FIOBJ2PTR(o)); }
/* ***************************************************************************** VTable ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L32-L36
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_count
size_t fiobj_ary_count(const FIOBJ ary) { assert(FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); return fio_ary___count(&obj2ary(ary)->ary); }
/** Returns the number of elements in the Array. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L52-L55
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_alloc
static inline FIOBJ fiobj_ary_alloc(size_t capa) { fiobj_ary_s *ary = fio_malloc(sizeof(*ary)); if (!ary) { perror("ERROR: fiobj array couldn't allocate memory"); exit(errno); } *ary = (fiobj_ary_s){ .head = { .ref = 1, .type = FIOBJ_T_ARRAY, }, }; if (capa) fio_ary_____require_on_top(&ary->ary, capa); return (FIOBJ)ary; }
/* ***************************************************************************** Allocation ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L81-L97
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_new
FIOBJ fiobj_ary_new(void) { return fiobj_ary_alloc(0); }
/** Creates a mutable empty Array object. Use `fiobj_free` when done. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L100-L100
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_new2
FIOBJ fiobj_ary_new2(size_t capa) { return fiobj_ary_alloc(capa); }
/** Creates a mutable empty Array object with the requested capacity. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L102-L102
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_capa
size_t fiobj_ary_capa(FIOBJ ary) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); return fio_ary___capa(&obj2ary(ary)->ary); }
/* ***************************************************************************** Array direct entry access API ***************************************************************************** */ /** Returns the current, temporary, array capacity (it's dynamic). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L109-L112
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_index
FIOBJ fiobj_ary_index(FIOBJ ary, int64_t pos) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); return fio_ary___get(&obj2ary(ary)->ary, pos); }
/** * Returns a temporary object owned by the Array. * * Negative values are retrieved from the end of the array. i.e., `-1` * is the last item. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L131-L134
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_set
void fiobj_ary_set(FIOBJ ary, FIOBJ obj, int64_t pos) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); FIOBJ old = FIOBJ_INVALID; fio_ary___set(&obj2ary(ary)->ary, pos, obj, &old); fiobj_free(old); }
/** * Sets an object at the requested position. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L139-L144
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_push
void fiobj_ary_push(FIOBJ ary, FIOBJ obj) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); fio_ary___push(&obj2ary(ary)->ary, obj); }
/* ***************************************************************************** Array push / shift API ***************************************************************************** */ /** * Pushes an object to the end of the Array. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L153-L156
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_pop
FIOBJ fiobj_ary_pop(FIOBJ ary) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); FIOBJ ret = FIOBJ_INVALID; fio_ary___pop(&obj2ary(ary)->ary, &ret); return ret; }
/** Pops an object from the end of the Array. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L159-L164
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_unshift
void fiobj_ary_unshift(FIOBJ ary, FIOBJ obj) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); fio_ary___unshift(&obj2ary(ary)->ary, obj); }
/** * Unshifts an object to the beginning of the Array. This could be * expensive. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L170-L173
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_shift
FIOBJ fiobj_ary_shift(FIOBJ ary) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); FIOBJ ret = FIOBJ_INVALID; fio_ary___shift(&obj2ary(ary)->ary, &ret); return ret; }
/** Shifts an object from the beginning of the Array. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L176-L181
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_replace
FIOBJ fiobj_ary_replace(FIOBJ ary, FIOBJ obj, int64_t pos) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); FIOBJ old = fiobj_ary_index(ary, pos); fiobj_ary_set(ary, obj, pos); return old; }
/* ***************************************************************************** Array Find / Remove / Replace ***************************************************************************** */ /** * Replaces the object at a specific position, returning the old object - * remember to `fiobj_free` the old object. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L191-L196
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_find
int64_t fiobj_ary_find(FIOBJ ary, FIOBJ data) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); return (int64_t)fio_ary___find(&obj2ary(ary)->ary, data); }
/** * Finds the index of a specifide object (if any). Returns -1 if the object * isn't found. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L202-L205
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_remove
int fiobj_ary_remove(FIOBJ ary, int64_t pos) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); FIOBJ old = FIOBJ_INVALID; if (fio_ary___remove(&obj2ary(ary)->ary, (intptr_t)pos, &old)) { return -1; } fiobj_free(old); return 0; }
/** * Removes the object at the index (if valid), changing the index of any * following objects. * * Returns 0 on success or -1 (if no object or out of bounds). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L213-L221
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_remove2
int fiobj_ary_remove2(FIOBJ ary, FIOBJ data) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); if (-1 == fio_ary___remove2(&obj2ary(ary)->ary, data, &data)) { return -1; } fiobj_free(data); return 0; }
/** * Removes the first instance of an object from the Array (if any), changing the * index of any following objects. * * Returns 0 on success or -1 (if the object wasn't found). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L229-L236
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_ary_compact
void fiobj_ary_compact(FIOBJ ary) { assert(ary && FIOBJ_TYPE_IS(ary, FIOBJ_T_ARRAY)); fio_ary___compact(&obj2ary(ary)->ary); }
/* ***************************************************************************** Array compacting (untested) ***************************************************************************** */ /** * Removes any NULL *pointers* from an Array, keeping all Objects (including * explicit NULL objects) in the array. * * This action is O(n) where n in the length of the array. * It could get expensive. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_ary.c#L249-L252
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
swallow_ch
static inline int swallow_ch(uint8_t **buffer, register uint8_t *const limit, const uint8_t c) { if (**buffer == c) return 1; #if !ALLOW_UNALIGNED_MEMORY_ACCESS || !defined(__x86_64__) /* too short for this mess */ if ((uintptr_t)limit <= 16 + ((uintptr_t)*buffer & (~(uintptr_t)7))) goto finish; /* align memory */ { const uint8_t *alignment = (uint8_t *)(((uintptr_t)(*buffer) & (~(uintptr_t)7)) + 8); if (limit >= alignment) { while (*buffer < alignment) { if (**buffer == c) { (*buffer)++; return 1; } *buffer += 1; } } } const uint8_t *limit64 = (uint8_t *)((uintptr_t)limit & (~(uintptr_t)7)); #else const uint8_t *limit64 = (uint8_t *)limit - 7; #endif uint64_t wanted1 = 0x0101010101010101ULL * c; for (; *buffer < limit64; *buffer += 8) { const uint64_t eq1 = ~((*((uint64_t *)*buffer)) ^ wanted1); const uint64_t t0 = (eq1 & 0x7f7f7f7f7f7f7f7fllu) + 0x0101010101010101llu; const uint64_t t1 = (eq1 & 0x8080808080808080llu); if ((t0 & t1)) { break; } } #if !ALLOW_UNALIGNED_MEMORY_ACCESS || !defined(__x86_64__) finish: #endif while (*buffer < limit) { if (**buffer == c) { (*buffer)++; return 1; } (*buffer)++; } return 0; }
/** * This seems to be faster on some systems, especially for smaller distances. * * On newer systems, `memchr` should be faster. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L253-L302
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newstr
FIOBJ fiobj_data_newstr(void) { FIOBJ o = fiobj_data_alloc(fio_malloc(4096), -1); REQUIRE_MEM(obj2io(o)->buffer); obj2io(o)->capa = 4096; obj2io(o)->source.dealloc = fio_free; return o; }
/* ***************************************************************************** Creating the IO object ***************************************************************************** */ /** Creates a new local in-memory IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L326-L332
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newstr2
FIOBJ fiobj_data_newstr2(void *buffer, uintptr_t length, void (*dealloc)(void *)) { FIOBJ o = fiobj_data_alloc(buffer, -1); obj2io(o)->capa = length; obj2io(o)->len = length; obj2io(o)->source.dealloc = dealloc; return o; }
/** * Creates a IO object from an existing buffer. The buffer will be deallocated * using the provided `dealloc` function pointer. Use a NULL `dealloc` function * pointer if the buffer is static and shouldn't be freed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L339-L346
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newfd
FIOBJ fiobj_data_newfd(int fd) { FIOBJ o = fiobj_data_alloc(fio_malloc(4096), fd); REQUIRE_MEM(obj2io(o)->buffer); obj2io(o)->source.fpos = 0; return o; }
/** Creates a new local file IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L349-L354
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newtmpfile
FIOBJ fiobj_data_newtmpfile(void) { // create a temporary file to contain the data. int fd = fio_tmpfile(); if (fd == -1) return 0; return fiobj_data_newfd(fd); }
/** Creates a new local tempfile IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L357-L363
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_slice
FIOBJ fiobj_data_slice(FIOBJ parent, intptr_t offset, uintptr_t length) { /* cut from the end */ if (offset < 0) { size_t parent_len = fiobj_data_len(parent); offset = parent_len + 1 + offset; } if (offset < 0) offset = 0; while (obj2io(parent)->fd == -2) { /* don't slice a slice... climb the parent chain. */ offset += obj2io(parent)->capa; parent = obj2io(parent)->source.parent; } size_t parent_len = fiobj_data_len(parent); if (parent_len <= (size_t)offset) { length = 0; offset = parent_len; } else if (parent_len < offset + length) { length = parent_len - offset; } /* make the object */ FIOBJ o = fiobj_data_alloc(NULL, -2); obj2io(o)->capa = offset; obj2io(o)->len = length; obj2io(o)->source.parent = fiobj_dup(parent); return o; }
/** Creates a slice from an existing Data object. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L366-L392
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_save_str
static int fiobj_data_save_str(FIOBJ o, const char *filename) { int target = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0777); if (target == -1) return -1; errno = 0; size_t total = 0; do { ssize_t act = write(target, obj2io(o)->buffer + total, obj2io(o)->len - total); if (act < 0) goto error; total += act; } while (total < obj2io(o)->len); close(target); return 0; error: close(target); unlink(filename); return -1; }
/* ***************************************************************************** Saving the IO object ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L398-L417
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_save
int fiobj_data_save(FIOBJ o, const char *filename) { switch (obj2io(o)->fd) { case -1: return fiobj_data_save_str(o, filename); break; case -2: return fiobj_data_save_slice(o, filename); break; default: return fiobj_data_save_file(o, filename); } }
/** Creates a new local file IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L475-L486
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_read_str
static fio_str_info_s fiobj_data_read_str(FIOBJ io, intptr_t length) { if (obj2io(io)->pos == obj2io(io)->len) { /* EOF */ return (fio_str_info_s){.data = NULL, .len = 0}; } if (length <= 0) { /* read to EOF - length */ length = (obj2io(io)->len - obj2io(io)->pos) + length; } if (length <= 0) { /* We are at EOF - length or beyond */ return (fio_str_info_s){.data = NULL, .len = 0}; } /* reading length bytes */ register size_t pos = obj2io(io)->pos; obj2io(io)->pos = pos + length; if (obj2io(io)->pos > obj2io(io)->len) obj2io(io)->pos = obj2io(io)->len; return (fio_str_info_s){ .data = (char *)(obj2io(io)->buffer + pos), .len = (obj2io(io)->pos - pos), }; }
/* ***************************************************************************** Reading API ***************************************************************************** */ /** Reads up to `length` bytes */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L493-L518
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_read_slice
static fio_str_info_s fiobj_data_read_slice(FIOBJ io, intptr_t length) { if (obj2io(io)->pos == obj2io(io)->len) { /* EOF */ return (fio_str_info_s){.data = NULL, .len = 0}; } if (length <= 0) { /* read to EOF - length */ length = (obj2io(io)->len - obj2io(io)->pos) + length; } if (length <= 0) { /* We are at EOF - length or beyond */ return (fio_str_info_s){.data = NULL, .len = 0}; } register size_t pos = obj2io(io)->pos; obj2io(io)->pos = pos + length; if (obj2io(io)->pos > obj2io(io)->len) obj2io(io)->pos = obj2io(io)->len; return fiobj_data_pread(obj2io(io)->source.parent, pos + obj2io(io)->capa, (obj2io(io)->pos - pos)); }
/** Reads up to `length` bytes */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L521-L541
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_read_file
static fio_str_info_s fiobj_data_read_file(FIOBJ io, intptr_t length) { uintptr_t fsize = fiobj_data_get_fd_size(io); if (length <= 0) { /* read to EOF - length */ length = (fsize - obj2io(io)->source.fpos) + length; } if (length <= 0) { /* We are at EOF - length or beyond */ errno = 0; return (fio_str_info_s){.data = NULL, .len = 0}; } /* reading length bytes */ if (length + obj2io(io)->pos <= obj2io(io)->len) { /* the data already exists in the buffer */ // fprintf(stderr, "in_buffer...\n"); fio_str_info_s data = {.data = (char *)(obj2io(io)->buffer + obj2io(io)->pos), .len = (uintptr_t)length}; obj2io(io)->pos += length; obj2io(io)->source.fpos += length; return data; } else { /* read the data into the buffer - internal counting gets invalidated */ // fprintf(stderr, "populate buffer...\n"); obj2io(io)->len = 0; obj2io(io)->pos = 0; fiobj_data_pre_write(io, length); ssize_t l; retry_int: l = pread(obj2io(io)->fd, obj2io(io)->buffer, length, obj2io(io)->source.fpos); if (l == -1 && errno == EINTR) goto retry_int; if (l == -1 || l == 0) return (fio_str_info_s){.data = NULL, .len = 0}; obj2io(io)->source.fpos += l; return (fio_str_info_s){.data = (char *)obj2io(io)->buffer, .len = l}; } }
/** Reads up to `length` bytes */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L544-L585
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_read
fio_str_info_s fiobj_data_read(FIOBJ io, intptr_t length) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) { errno = EFAULT; return (fio_str_info_s){.data = NULL, .len = 0}; } errno = 0; switch (obj2io(io)->fd) { case -1: return fiobj_data_read_str(io, length); break; case -2: return fiobj_data_read_slice(io, length); break; default: return fiobj_data_read_file(io, length); } }
/** * Reads up to `length` bytes and returns a temporary(!) C string object. * * The C string object will be invalidate the next time a function call to the * IO object is made. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L593-L609
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_read2ch_str
static fio_str_info_s fiobj_data_read2ch_str(FIOBJ io, uint8_t token) { if (obj2io(io)->pos == obj2io(io)->len) /* EOF */ return (fio_str_info_s){.data = NULL, .len = 0}; uint8_t *pos = obj2io(io)->buffer + obj2io(io)->pos; uint8_t *lim = obj2io(io)->buffer + obj2io(io)->len; swallow_ch(&pos, lim, token); fio_str_info_s ret = (fio_str_info_s){ .data = (char *)obj2io(io)->buffer + obj2io(io)->pos, .len = (uintptr_t)(pos - obj2io(io)->buffer) - obj2io(io)->pos, }; obj2io(io)->pos = (uintptr_t)(pos - obj2io(io)->buffer); return ret; }
/* ***************************************************************************** Tokenize (read2ch) ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L615-L628
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_read2ch
fio_str_info_s fiobj_data_read2ch(FIOBJ io, uint8_t token) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) { errno = EFAULT; return (fio_str_info_s){.data = NULL, .len = 0}; } switch (obj2io(io)->fd) { case -1: return fiobj_data_read2ch_str(io, token); break; case -2: return fiobj_data_read2ch_slice(io, token); break; default: return fiobj_data_read2ch_file(io, token); } }
/** * Reads until the `token` byte is encountered or until the end of the stream. * * Returns a temporary(!) C string including the end of line marker. * * Careful when using this call on large file streams, as the whole file * stream might be loaded into the memory. * * The C string object will be invalidate the next time a function call to the * IO object is made. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L709-L724
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_pos
intptr_t fiobj_data_pos(FIOBJ io) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) return -1; switch (obj2io(io)->fd) { case -1: /* fallthrough */ case -2: return obj2io(io)->pos; break; default: return obj2io(io)->source.fpos; } }
/* ***************************************************************************** Position / Seeking ***************************************************************************** */ /** * Returns the current reading position. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L733-L744
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_len
intptr_t fiobj_data_len(FIOBJ io) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) return -1; return fiobj_data_i(io); }
/** * Returns the length of the stream. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L749-L753
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_seek
void fiobj_data_seek(FIOBJ io, intptr_t position) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) return; switch (obj2io(io)->fd) { case -1: /* fallthrough */ case -2: /* String / Slice code */ if (position == 0) { obj2io(io)->pos = 0; return; } if (position > 0) { if ((uintptr_t)position > obj2io(io)->len) position = obj2io(io)->len; obj2io(io)->pos = position; return; } position = (0 - position); if ((uintptr_t)position > obj2io(io)->len) position = 0; else position = obj2io(io)->len - position; obj2io(io)->pos = position; return; break; default: /* File code */ obj2io(io)->pos = 0; obj2io(io)->len = 0; if (position == 0) { obj2io(io)->source.fpos = 0; return; } int64_t len = fiobj_data_get_fd_size(io); if (len < 0) len = 0; if (position > 0) { if (position > len) position = len; obj2io(io)->source.fpos = position; return; } position = (0 - position); if (position > len) position = 0; else position = len - position; obj2io(io)->source.fpos = position; return; } }
/** * Moves the reading position to the requested position. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L758-L810
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_pread_str
static fio_str_info_s fiobj_data_pread_str(FIOBJ io, intptr_t start_at, uintptr_t length) { if (start_at < 0) start_at = obj2io(io)->len + start_at; if (start_at < 0) start_at = 0; if ((size_t)start_at > obj2io(io)->len) start_at = obj2io(io)->len; if (length + start_at > obj2io(io)->len) length = obj2io(io)->len - start_at; if (length == 0) return (fio_str_info_s){ .data = NULL, .len = 0, }; return (fio_str_info_s){ .data = (char *)obj2io(io)->buffer + start_at, .len = length, }; }
/* ***************************************************************************** `fiobj_data_pread` ***************************************************************************** */ // switch(obj2io(o)->fd) { // case -1: // break; // case -2: // break; // default: // }
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L823-L842
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_pread
fio_str_info_s fiobj_data_pread(FIOBJ io, intptr_t start_at, uintptr_t length) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA)) { errno = EFAULT; return (fio_str_info_s){ .data = NULL, .len = 0, }; } errno = 0; switch (obj2io(io)->fd) { case -1: return fiobj_data_pread_str(io, start_at, length); break; case -2: return fiobj_data_pread_slice(io, start_at, length); break; default: return fiobj_data_pread_file(io, start_at, length); } }
/** * Reads up to `length` bytes starting at `start_at` position and returns a * temporary(!) C string object. The reading position is ignored and * unchanged. * * The C string object will be invalidate the next time a function call to the * IO object is made. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L904-L924
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_assert_dynamic
void fiobj_data_assert_dynamic(FIOBJ io) { if (!io) { errno = ENFILE; return; } assert(FIOBJ_TYPE(io) == FIOBJ_T_DATA); fiobj_data_pre_write(io, 0); return; }
/* ***************************************************************************** Writing API ***************************************************************************** */ /** * Makes sure the IO object isn't attached to a static or external string. * * If the IO object is attached to a static or external string, the data will be * copied to a new memory block. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L936-L944
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_write
intptr_t fiobj_data_write(FIOBJ io, void *buffer, uintptr_t length) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA) || (!buffer && length)) { errno = EFAULT; return -1; } errno = 0; /* Unslice slices */ if (obj2io(io)->fd == -2) fiobj_data_assert_dynamic(io); if (obj2io(io)->fd == -1) { /* String Code */ fiobj_data_pre_write(io, length + 1); memcpy(obj2io(io)->buffer + obj2io(io)->len, buffer, length); obj2io(io)->len = obj2io(io)->len + length; obj2io(io)->buffer[obj2io(io)->len] = 0; return length; } /* File Code */ return pwrite(obj2io(io)->fd, buffer, length, fiobj_data_get_fd_size(io)); }
/** * Writes `length` bytes at the end of the IO stream, ignoring the reading * position. * * Behaves and returns the same value as the system call `write`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L952-L973
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_puts
intptr_t fiobj_data_puts(FIOBJ io, void *buffer, uintptr_t length) { if (!io || !FIOBJ_TYPE_IS(io, FIOBJ_T_DATA) || (!buffer && length)) { errno = EFAULT; return -1; } /* Unslice slices */ if (obj2io(io)->fd == -2) fiobj_data_assert_dynamic(io); if (obj2io(io)->fd == -1) { /* String Code */ fiobj_data_pre_write(io, length + 2); if (length) { memcpy(obj2io(io)->buffer + obj2io(io)->len, buffer, length); } obj2io(io)->len = obj2io(io)->len + length + 2; obj2io(io)->buffer[obj2io(io)->len - 2] = '\r'; obj2io(io)->buffer[obj2io(io)->len - 1] = '\n'; return length + 2; } /* File Code */ uintptr_t end = fiobj_data_get_fd_size(io); ssize_t t1 = 0, t2 = 0; if (length) { t1 = pwrite(obj2io(io)->fd, buffer, length, end); if (t1 < 0) return t1; end += t1; } t2 = pwrite(obj2io(io)->fd, buffer, length, end); if (t2 < 0) return t1; return t1 + t2; }
/** * Writes `length` bytes at the end of the IO stream, ignoring the reading * position, adding an EOL marker ("\r\n") to the end of the stream. * * Behaves and returns the same value as the system call `write`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L981-L1015
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newstr
FIOBJ fiobj_data_newstr(void) { return FIOBJ_INVALID; }
/** Creates a new local in-memory IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L1163-L1163
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newstr2
FIOBJ fiobj_data_newstr2(void *buffer, uintptr_t length, void (*dealloc)(void *)) { return FIOBJ_INVALID; }
/** * Creates a IO object from an existing buffer. The buffer will be deallocated * using the provided `dealloc` function pointer. Use a NULL `dealloc` function * pointer if the buffer is static and shouldn't be freed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L1170-L1173
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newtmpfile
FIOBJ fiobj_data_newtmpfile(void) { return FIOBJ_INVALID; }
/** Creates a new local tempfile IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L1176-L1176
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_data_newfd
FIOBJ fiobj_data_newfd(int fd) { return FIOBJ_INVALID; }
/** Creates a new local file IO object */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_data.c#L1179-L1179
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_dealloc
static void fiobj_hash_dealloc(FIOBJ o, void (*task)(FIOBJ, void *), void *arg) { FIO_SET_FOR_LOOP(&obj2hash(o)->hash, i) { if (i->obj.key) task((FIOBJ)i->obj.obj, arg); fiobj_free((FIOBJ)i->obj.key); i->obj.key = FIOBJ_INVALID; i->obj.obj = FIOBJ_INVALID; } obj2hash(o)->hash.count = 0; fio_hash___free(&obj2hash(o)->hash); fio_free(FIOBJ2PTR(o)); }
/* ***************************************************************************** Hash alloc + VTable ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L58-L70
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_count
size_t fiobj_hash_count(const FIOBJ o) { assert(o && FIOBJ_TYPE_IS(o, FIOBJ_T_HASH)); return fio_hash___count(&obj2hash(o)->hash); }
/** Returns the number of elements in the Array. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L123-L126
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_new
FIOBJ fiobj_hash_new(void) { fiobj_hash_s *h = fio_malloc(sizeof(*h)); FIO_ASSERT_ALLOC(h); *h = (fiobj_hash_s){.head = {.ref = 1, .type = FIOBJ_T_HASH}, .hash = FIO_SET_INIT}; return (FIOBJ)h | FIOBJECT_HASH_FLAG; }
/* ***************************************************************************** Hash API ***************************************************************************** */ /** * Creates a mutable empty Hash object. Use `fiobj_free` when done. * * Notice that these Hash objects are designed for smaller collections and * retain order of object insertion. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L160-L166
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_new2
FIOBJ fiobj_hash_new2(size_t capa) { fiobj_hash_s *h = fio_malloc(sizeof(*h)); FIO_ASSERT_ALLOC(h); *h = (fiobj_hash_s){.head = {.ref = 1, .type = FIOBJ_T_HASH}, .hash = FIO_SET_INIT}; fio_hash___capa_require(&h->hash, capa); return (FIOBJ)h | FIOBJECT_HASH_FLAG; }
/** * Creates a mutable empty Hash object with an initial capacity of `capa`. Use * `fiobj_free` when done. * * Notice that these Hash objects are designed for smaller collections and * retain order of object insertion. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L175-L182
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_capa
size_t fiobj_hash_capa(const FIOBJ hash) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); return fio_hash___capa(&obj2hash(hash)->hash); }
/** * Returns a temporary theoretical Hash map capacity. * This could be used for testing performance and memory consumption. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L188-L191
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_set
int fiobj_hash_set(FIOBJ hash, FIOBJ key, FIOBJ obj) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); if (FIOBJ_TYPE_IS(key, FIOBJ_T_STRING)) fiobj_str_freeze(key); fio_hash___insert(&obj2hash(hash)->hash, fiobj_obj2hash(key), key, obj, NULL); fiobj_free(obj); /* take ownership - free the user's reference. */ return 0; }
/** * Sets a key-value pair in the Hash, duplicating the Symbol and **moving** * the ownership of the object to the Hash. * * Returns -1 on error. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L199-L206
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_pop
FIOBJ fiobj_hash_pop(FIOBJ hash, FIOBJ *key) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); FIOBJ old; if (fio_hash___count(&obj2hash(hash)->hash)) return FIOBJ_INVALID; old = fiobj_dup(fio_hash___last(&obj2hash(hash)->hash).obj); if (key) *key = fiobj_dup(fio_hash___last(&obj2hash(hash)->hash).key); fio_hash___pop(&obj2hash(hash)->hash); return old; }
/** * Allows the Hash to be used as a stack. * * If a pointer `key` is provided, it will receive ownership of the key * (remember to free). * * Returns FIOBJ_INVALID on error. * * Returns and object if successful (remember to free). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L218-L228
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_replace
FIOBJ fiobj_hash_replace(FIOBJ hash, FIOBJ key, FIOBJ obj) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); FIOBJ old = FIOBJ_INVALID; fio_hash___insert(&obj2hash(hash)->hash, fiobj_obj2hash(key), key, obj, &old); fiobj_free(obj); /* take ownership - free the user's reference. */ return old; }
/** * Replaces the value in a key-value pair, returning the old value (and it's * ownership) to the caller. * * A return value of NULL indicates that no previous object existed (but a new * key-value pair was created. * * Errors are silently ignored. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L239-L245
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_remove
FIOBJ fiobj_hash_remove(FIOBJ hash, FIOBJ key) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); FIOBJ old = FIOBJ_INVALID; fio_hash___remove(&obj2hash(hash)->hash, fiobj_obj2hash(key), key, &old); return old; }
/** * Removes a key-value pair from the Hash, if it exists. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L250-L255
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_remove2
FIOBJ fiobj_hash_remove2(FIOBJ hash, uint64_t hash_value) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); FIOBJ old = FIOBJ_INVALID; fio_hash___remove(&obj2hash(hash)->hash, hash_value, -1, &old); return old; }
/** * Removes a key-value pair from the Hash, if it exists, returning the old * object (instead of freeing it). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L261-L266
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_delete
int fiobj_hash_delete(FIOBJ hash, FIOBJ key) { return fio_hash___remove(&obj2hash(hash)->hash, fiobj_obj2hash(key), key, NULL); }
/** * Deletes a key-value pair from the Hash, if it exists, freeing the * associated object. * * Returns -1 on type error or if the object never existed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L274-L277
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_delete2
int fiobj_hash_delete2(FIOBJ hash, uint64_t key_hash) { return fio_hash___remove(&obj2hash(hash)->hash, key_hash, -1, NULL); }
/** * Deletes a key-value pair from the Hash, if it exists, freeing the * associated object. * * This function takes a `uintptr_t` Hash value (see `fio_siphash`) to * perform a lookup in the HashMap, which is slightly faster than the other * variations. * * Returns -1 on type error or if the object never existed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L289-L291
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_get
FIOBJ fiobj_hash_get(const FIOBJ hash, FIOBJ key) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); return fio_hash___find(&obj2hash(hash)->hash, fiobj_obj2hash(key), key); ; }
/** * Returns a temporary handle to the object associated with the Symbol, NULL * if none. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L297-L301
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_get2
FIOBJ fiobj_hash_get2(const FIOBJ hash, uint64_t key_hash) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); return fio_hash___find(&obj2hash(hash)->hash, key_hash, -1); ; }
/** * Returns a temporary handle to the object associated hashed key value. * * This function takes a `uintptr_t` Hash value (see `fio_siphash`) to * perform a lookup in the HashMap. * * Returns NULL if no object is associated with this hashed key value. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L311-L315
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_haskey
int fiobj_hash_haskey(const FIOBJ hash, FIOBJ key) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); return fio_hash___find(&obj2hash(hash)->hash, fiobj_obj2hash(key), key) != FIOBJ_INVALID; }
/** * Returns 1 if the key (Symbol) exists in the Hash, even if value is NULL. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L320-L324
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_clear
void fiobj_hash_clear(const FIOBJ hash) { assert(hash && FIOBJ_TYPE_IS(hash, FIOBJ_T_HASH)); fio_hash___free(&obj2hash(hash)->hash); }
/** * Empties the Hash. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_hash.c#L329-L332
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_json_add2parser
static inline void fiobj_json_add2parser(fiobj_json_parser_s *p, FIOBJ o) { if (p->top) { if (p->is_hash) { if (p->key) { fiobj_hash_set(p->top, p->key, o); fiobj_free(p->key); p->key = FIOBJ_INVALID; } else { p->key = o; } } else { fiobj_ary_push(p->top, o); } } else { p->top = o; } }
/* ***************************************************************************** FIOBJ Callacks ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L49-L65
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_null
static void fio_json_on_null(json_parser_s *p) { fiobj_json_add2parser((fiobj_json_parser_s *)p, fiobj_null()); }
/** a NULL object was detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L68-L70
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_true
static void fio_json_on_true(json_parser_s *p) { fiobj_json_add2parser((fiobj_json_parser_s *)p, fiobj_true()); }
/** a TRUE object was detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L72-L74
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_false
static void fio_json_on_false(json_parser_s *p) { fiobj_json_add2parser((fiobj_json_parser_s *)p, fiobj_false()); }
/** a FALSE object was detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L76-L78
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_number
static void fio_json_on_number(json_parser_s *p, long long i) { fiobj_json_add2parser((fiobj_json_parser_s *)p, fiobj_num_new(i)); }
/** a Numberl was detected (long long). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L80-L82
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_float
static void fio_json_on_float(json_parser_s *p, double f) { fiobj_json_add2parser((fiobj_json_parser_s *)p, fiobj_float_new(f)); }
/** a Float was detected (double). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L84-L86
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_string
static void fio_json_on_string(json_parser_s *p, void *start, size_t length) { FIOBJ str = fiobj_str_buf(length); fiobj_str_resize( str, fio_json_unescape_str(fiobj_obj2cstr(str).data, start, length)); fiobj_json_add2parser((fiobj_json_parser_s *)p, str); }
/** a String was detected (int / float). update `pos` to point at ending */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L88-L93
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_start_object
static int fio_json_on_start_object(json_parser_s *p) { fiobj_json_parser_s *pr = (fiobj_json_parser_s *)p; if (pr->target) { /* push NULL, don't free the objects */ fio_json_stack_push(&pr->stack, pr->top); pr->top = pr->target; pr->target = FIOBJ_INVALID; } else { FIOBJ hash = fiobj_hash_new(); fiobj_json_add2parser(pr, hash); fio_json_stack_push(&pr->stack, pr->top); pr->top = hash; } pr->is_hash = 1; return 0; }
/** a dictionary object was detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L95-L110
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_end_object
static void fio_json_on_end_object(json_parser_s *p) { fiobj_json_parser_s *pr = (fiobj_json_parser_s *)p; if (pr->key) { FIO_LOG_WARNING("(JSON parsing) malformed JSON, " "ignoring dangling Hash key."); fiobj_free(pr->key); pr->key = FIOBJ_INVALID; } pr->top = FIOBJ_INVALID; fio_json_stack_pop(&pr->stack, &pr->top); pr->is_hash = FIOBJ_TYPE_IS(pr->top, FIOBJ_T_HASH); }
/** a dictionary object closure detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L112-L123
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_start_array
static int fio_json_on_start_array(json_parser_s *p) { fiobj_json_parser_s *pr = (fiobj_json_parser_s *)p; if (pr->target) return -1; FIOBJ ary = fiobj_ary_new(); fiobj_json_add2parser(pr, ary); fio_json_stack_push(&pr->stack, pr->top); pr->top = ary; pr->is_hash = 0; return 0; }
/** an array object was detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L125-L135
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_end_array
static void fio_json_on_end_array(json_parser_s *p) { fiobj_json_parser_s *pr = (fiobj_json_parser_s *)p; pr->top = FIOBJ_INVALID; fio_json_stack_pop(&pr->stack, &pr->top); pr->is_hash = FIOBJ_TYPE_IS(pr->top, FIOBJ_T_HASH); }
/** an array closure was detected */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L137-L142
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_json
static void fio_json_on_json(json_parser_s *p) { // fiobj_json_parser_s *pr = (fiobj_json_parser_s *)p; // FIO_ARY_FOR(&pr->stack, pos) { fiobj_free((FIOBJ)pos.obj); } // fio_json_stack_free(&pr->stack); (void)p; /* nothing special... right? */ }
/** the JSON parsing is complete */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L144-L149
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_json_on_error
static void fio_json_on_error(json_parser_s *p) { fiobj_json_parser_s *pr = (fiobj_json_parser_s *)p; #if DEBUG FIO_LOG_DEBUG("JSON on error called."); #endif fiobj_free((FIOBJ)fio_json_stack_get(&pr->stack, 0)); fiobj_free(pr->key); fio_json_stack_free(&pr->stack); *pr = (fiobj_json_parser_s){.top = FIOBJ_INVALID}; }
/** the JSON parsing is complete */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L151-L160
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
write_safe_str
static void write_safe_str(FIOBJ dest, const FIOBJ str) { fio_str_info_s s = fiobj_obj2cstr(str); fio_str_info_s t = fiobj_obj2cstr(dest); t.data[t.len] = '"'; t.len++; fiobj_str_resize(dest, t.len); t = fiobj_obj2cstr(dest); const uint8_t *restrict src = (const uint8_t *)s.data; size_t len = s.len; uint64_t end = t.len; /* make sure we have some room */ size_t added = 0; size_t capa = fiobj_str_capa(dest); if (capa <= end + s.len + 64) { if (0) { capa = (((capa >> 12) + 1) << 12) - 1; capa = fiobj_str_capa_assert(dest, capa); } else { capa = fiobj_str_capa_assert(dest, (end + s.len + 64)); } fio_str_info_s tmp = fiobj_obj2cstr(dest); t = tmp; } while (len) { char *restrict writer = (char *)t.data; while (len && src[0] > 32 && src[0] != '"' && src[0] != '\\') { len--; writer[end++] = *(src++); } if (!len) break; switch (src[0]) { case '\b': writer[end++] = '\\'; writer[end++] = 'b'; added++; break; /* from switch */ case '\f': writer[end++] = '\\'; writer[end++] = 'f'; added++; break; /* from switch */ case '\n': writer[end++] = '\\'; writer[end++] = 'n'; added++; break; /* from switch */ case '\r': writer[end++] = '\\'; writer[end++] = 'r'; added++; break; /* from switch */ case '\t': writer[end++] = '\\'; writer[end++] = 't'; added++; break; /* from switch */ case '"': case '\\': case '/': writer[end++] = '\\'; writer[end++] = src[0]; added++; break; /* from switch */ default: if (src[0] <= 31) { /* MUST escape all control values less than 32 */ writer[end++] = '\\'; writer[end++] = 'u'; writer[end++] = '0'; writer[end++] = '0'; writer[end++] = hex_chars[src[0] >> 4]; writer[end++] = hex_chars[src[0] & 15]; added += 4; } else writer[end++] = src[0]; break; /* from switch */ } src++; len--; if (added >= 48 && capa <= end + len + 64) { writer[end] = 0; fiobj_str_resize(dest, end); fiobj_str_capa_assert(dest, (end + len + 64)); t = fiobj_obj2cstr(dest); writer = (char *)t.data; capa = t.capa; added = 0; } } t.data[end++] = '"'; fiobj_str_resize(dest, end); }
/* ***************************************************************************** JSON formatting ***************************************************************************** */ /** Writes a JSON friendly version of the src String */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L167-L259
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_json2obj
size_t fiobj_json2obj(FIOBJ *pobj, const void *data, size_t len) { fiobj_json_parser_s p = {.top = FIOBJ_INVALID}; size_t consumed = fio_json_parse(&p.p, data, len); if (!consumed || p.p.depth) { fiobj_free(fio_json_stack_get(&p.stack, 0)); p.top = FIOBJ_INVALID; } fio_json_stack_free(&p.stack); fiobj_free(p.key); *pobj = p.top; return consumed; }
/* ***************************************************************************** FIOBJ API ***************************************************************************** */ /** * Parses JSON, setting `pobj` to point to the new Object. * * Returns the number of bytes consumed. On Error, 0 is returned and no data is * consumed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L377-L388
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_hash_update_json
size_t fiobj_hash_update_json(FIOBJ hash, const void *data, size_t len) { if (!hash) return 0; fiobj_json_parser_s p = {.top = FIOBJ_INVALID, .target = hash}; size_t consumed = fio_json_parse(&p.p, data, len); fio_json_stack_free(&p.stack); fiobj_free(p.key); if (p.top != hash) fiobj_free(p.top); return consumed; }
/** * Updates a Hash using JSON data. * * Parsing errors and non-dictionar object JSON data are silently ignored, * attempting to update the Hash as much as possible before any errors * encountered. * * Conflicting Hash data is overwritten (prefering the new over the old). * * Returns the number of bytes consumed. On Error, 0 is returned and no data is * consumed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L402-L412
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_obj2json2
FIOBJ fiobj_obj2json2(FIOBJ dest, FIOBJ o, uint8_t pretty) { assert(dest && FIOBJ_TYPE_IS(dest, FIOBJ_T_STRING)); if (!o) { fiobj_str_write(dest, "null", 4); return 0; } fio_json_stack_s stack = FIO_ARY_INIT; obj2json_data_s data = { .dest = dest, .stack = &stack, .pretty = pretty, .count = 1, }; if (!o || !FIOBJ_IS_ALLOCATED(o) || !FIOBJECT2VTBL(o)->each) { fiobj_obj2json_task(o, &data); return dest; } fiobj_each2(o, fiobj_obj2json_task, &data); fio_json_stack_free(&stack); return dest; }
/** * Formats an object into a JSON string, appending the JSON string to an * existing String. Remember to `fiobj_free`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L418-L438
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_obj2json
FIOBJ fiobj_obj2json(FIOBJ obj, uint8_t pretty) { return fiobj_obj2json2(fiobj_str_buf(128), obj, pretty); }
/* Formats an object into a JSON string. Remember to `fiobj_free`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_json.c#L441-L443
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_mustache_free
void fiobj_mustache_free(mustache_s *mustache) { mustache_free(mustache); }
/** Free the mustache template */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L50-L50
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_mustache_build2
FIOBJ fiobj_mustache_build2(FIOBJ dest, mustache_s *mustache, FIOBJ data) { mustache_build(mustache, .udata1 = (void *)dest, .udata2 = (void *)data); return dest; }
/** * Renders a template into an existing FIOBJ String (`dest`'s end), using the * information in the `data` object. * * Returns FIOBJ_INVALID if an error occured and a FIOBJ String on success. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L58-L61
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_mustache_build
FIOBJ fiobj_mustache_build(mustache_s *mustache, FIOBJ data) { if (!mustache) return FIOBJ_INVALID; return fiobj_mustache_build2(fiobj_str_buf(mustache->u.read_only.data_length), mustache, data); }
/** * Creates a FIOBJ String containing the rendered template using the information * in the `data` object. * * Returns FIOBJ_INVALID if an error occured and a FIOBJ String on success. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L69-L74
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fiobj_mustache_find_obj_absolute
static inline FIOBJ fiobj_mustache_find_obj_absolute(FIOBJ parent, FIOBJ key) { if (!FIOBJ_TYPE_IS(parent, FIOBJ_T_HASH)) return FIOBJ_INVALID; FIOBJ o = FIOBJ_INVALID; o = fiobj_hash_get(parent, key); return o; }
/* ***************************************************************************** Mustache Callbacks ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L80-L86
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
mustache_on_arg
static int mustache_on_arg(mustache_section_s *section, const char *name, uint32_t name_len, unsigned char escape) { FIOBJ o = fiobj_mustache_find_obj(section, name, name_len); if (!o) return 0; fio_str_info_s i = fiobj_obj2cstr(o); if (!i.len) return 0; return mustache_write_text(section, i.data, i.len, escape); }
/** * Called when an argument name was detected in the current section. * * A conforming implementation will search for the named argument both in the * existing section and all of it's parents (walking backwards towards the root) * until a value is detected. * * A missing value should be treated the same as an empty string. * * A conforming implementation will output the named argument's value (either * HTML escaped or not, depending on the `escape` flag) as a string. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L154-L163
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
mustache_on_text
static int mustache_on_text(mustache_section_s *section, const char *data, uint32_t data_len) { FIOBJ dest = (FIOBJ)section->udata1; fiobj_str_write(dest, data, data_len); return 0; }
/** * Called when simple template text (string) is detected. * * A conforming implementation will output data as a string (no escaping). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fiobj/fiobj_mustache.c#L170-L175
675c65b509d48c21a8d1fa4c5ec53fc407643a3b