diff options
| author | 2023-05-07 17:57:49 -0400 | |
|---|---|---|
| committer | 2023-05-07 17:57:49 -0400 | |
| commit | 46fdc953c70c1dc0afb12b52d8c68a6a7a078887 (patch) | |
| tree | 310a712cf1f6f7387d3d4e448aea1571ea95cf16 | |
| parent | git: also ignore compile_flags.txt (diff) | |
native: polyfills for get/opt cbytes
| -rw-r--r-- | project.janet | 1 | ||||
| -rw-r--r-- | src/jurl.h | 11 | ||||
| -rw-r--r-- | src/polyfill.c | 19 | ||||
| -rw-r--r-- | src/util.c | 8 |
4 files changed, 30 insertions, 9 deletions
diff --git a/project.janet b/project.janet index b3cfe27..e4d530e 100644 --- a/project.janet +++ b/project.janet @@ -69,5 +69,6 @@ "src/errors.c" "src/getinfo.c" "src/mime.c" + "src/polyfill.c" "src/setopt.c" "src/util.c"]) @@ -2,6 +2,16 @@ #include <curl/curl.h> #include <janet.h> +// = polyfills +#if JANET_VERSION_MAJOR < 2 && JANET_VERSION_MINOR < 28 +#define POLYFILL_CBYTES +#endif + +#ifdef POLYFILL_CBYTES +const char *janet_getcbytes(const Janet *argv, int32_t n); +const char *janet_optcbytes(const Janet *argv, int32_t argc, int32_t n, const char *dflt); +#endif + // = structures // cleanup.c @@ -90,7 +100,6 @@ JANET_CFUN(jurl_setopt); // util.c int janet_getslist(struct curl_slist **slist, Janet *argv, int32_t n); -const char *janet_getcbytes(const Janet *argv, int32_t n); JANET_CFUN(jurl_escape); JANET_CFUN(jurl_unescape); JANET_CFUN(jurl_wrap_error); diff --git a/src/polyfill.c b/src/polyfill.c new file mode 100644 index 0000000..f7db312 --- /dev/null +++ b/src/polyfill.c @@ -0,0 +1,19 @@ +#include "jurl.h" + +#ifdef POLYFILL_CBYTES +const char* janet_getcbytes(const Janet *argv, int32_t n) { + JanetByteView view = janet_getbytes(argv, n); + const char *cstr = (const char *)view.bytes; + if (strlen(cstr) != (size_t) view.len) { + janet_panic("bytes contain embedded 0s"); + } + return cstr; +} + +const char *janet_optcbytes(const Janet *argv, int32_t argc, int32_t n, const char *dflt) { + if (n >= argc || janet_checktype(argv[n], JANET_NIL)) { + return dflt; + } + return janet_getcbytes(argv, n); +} +#endif @@ -14,14 +14,6 @@ int janet_getslist(struct curl_slist **slist, Janet *argv, int32_t n) { return 1; } -const char *janet_getcbytes(const Janet *argv, int32_t n) { - JanetByteView b = janet_getbytes(argv, n); - if (strlen((const char*) b.bytes) != (size_t) b.len) { - janet_panic("bytes contain embedded 0s"); - } - return (const char*)b.bytes; -} - JANET_CFUN(jurl_escape) { janet_fixarity(argc, 1); JanetByteView b = janet_getbytes(argv, 0); |
