From 0350834cd3dab0491d79e90c473f678959bd97ce Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 15 May 2024 07:40:21 -0500 Subject: [PATCH 1/4] By default, require and import extend current env. --- src/boot/boot.janet | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index dfbe9ef7..f3dbfd2a 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2958,7 +2958,7 @@ :core/stream path (file/open path :rb))) (def path-is-file (= f path)) - (default env (make-env)) + (default env (make-env (curenv))) (def spath (string path)) (put env :source (or source (if-not path-is-file spath path))) (var exit-error nil) From 3e402d397e9f94a41cb70098f13d3495c241363f Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 15 May 2024 18:16:19 -0500 Subject: [PATCH 2/4] Use older openbsd build for CI. --- .builds/openbsd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.builds/openbsd.yml b/.builds/openbsd.yml index 5526b15c..c03faaf1 100644 --- a/.builds/openbsd.yml +++ b/.builds/openbsd.yml @@ -1,4 +1,4 @@ -image: openbsd/latest +image: openbsd/7.4 sources: - https://git.sr.ht/~bakpakin/janet packages: From c747e8d16c743ec2db8bd81cb7be563d80a8038f Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 15 May 2024 18:20:20 -0500 Subject: [PATCH 3/4] Address some compiler linter messages on openbsd --- src/core/inttypes.c | 4 ++-- src/core/pp.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/inttypes.c b/src/core/inttypes.c index b4c999b8..bed6dff1 100644 --- a/src/core/inttypes.c +++ b/src/core/inttypes.c @@ -73,13 +73,13 @@ static void *int64_unmarshal(JanetMarshalContext *ctx) { static void it_s64_tostring(void *p, JanetBuffer *buffer) { char str[32]; - sprintf(str, "%" PRId64, *((int64_t *)p)); + snprintf(str, sizeof(str), "%" PRId64, *((int64_t *)p)); janet_buffer_push_cstring(buffer, str); } static void it_u64_tostring(void *p, JanetBuffer *buffer) { char str[32]; - sprintf(str, "%" PRIu64, *((uint64_t *)p)); + snprintf(str, sizeof(str), "%" PRIu64, *((uint64_t *)p)); janet_buffer_push_cstring(buffer, str); } diff --git a/src/core/pp.c b/src/core/pp.c index 89ecc141..bba70b6b 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -830,7 +830,7 @@ static const char *scanformat( if (loc != NULL && *loc != '\0') { const char *mapping = get_fmt_mapping(*p2++); size_t len = strlen(mapping); - strcpy(form, mapping); + memcpy(form, mapping, len); form += len; } else { *(form++) = *(p2++); From 9946f3bdf4fc6aa7adfb321f8141490443042aff Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 15 May 2024 20:16:42 -0500 Subject: [PATCH 4/4] Add buffer/format-at Move changes over from bundle-tools branch and add testing. --- src/core/buffer.c | 22 ++++++++++++++++++++++ test/suite-buffer.janet | 15 +++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/core/buffer.c b/src/core/buffer.c index 5d9e7b3d..a34f29fb 100644 --- a/src/core/buffer.c +++ b/src/core/buffer.c @@ -655,6 +655,27 @@ JANET_CORE_FN(cfun_buffer_format, return argv[0]; } +JANET_CORE_FN(cfun_buffer_format_at, + "(buffer/format-at buffer at format & args)", + "Snprintf like functionality for printing values into a buffer. Returns " + "the modified buffer.") { + janet_arity(argc, 2, -1); + JanetBuffer *buffer = janet_getbuffer(argv, 0); + int32_t at = janet_getinteger(argv, 1); + if (at < 0) { + at += buffer->count + 1; + } + if (at > buffer->count || at < 0) janet_panicf("expected index at to be in range [0, %d), got %d", buffer->count, at); + int32_t oldcount = buffer->count; + buffer->count = at; + const char *strfrmt = (const char *) janet_getstring(argv, 2); + janet_buffer_format(buffer, strfrmt, 2, argc, argv); + if (buffer->count < oldcount) { + buffer->count = oldcount; + } + return argv[0]; +} + void janet_lib_buffer(JanetTable *env) { JanetRegExt buffer_cfuns[] = { JANET_CORE_REG("buffer/new", cfun_buffer_new), @@ -681,6 +702,7 @@ void janet_lib_buffer(JanetTable *env) { JANET_CORE_REG("buffer/bit-toggle", cfun_buffer_bittoggle), JANET_CORE_REG("buffer/blit", cfun_buffer_blit), JANET_CORE_REG("buffer/format", cfun_buffer_format), + JANET_CORE_REG("buffer/format-at", cfun_buffer_format_at), JANET_REG_END }; janet_core_cfuns_ext(env, NULL, buffer_cfuns); diff --git a/test/suite-buffer.janet b/test/suite-buffer.janet index 7d585680..9821e384 100644 --- a/test/suite-buffer.janet +++ b/test/suite-buffer.janet @@ -162,5 +162,20 @@ (assert (deep= @"abc423" (buffer/push-at @"abc123" 3 "4")) "buffer/push-at 3") +# buffer/format-at +(def start-buf (buffer/new-filled 100 (chr "x"))) +(buffer/format-at start-buf 50 "aa%dbb" 32) +(assert (= (string start-buf) (string (string/repeat "x" 50) "aa32bb" (string/repeat "x" 44))) + "buffer/format-at 1") +(assert + (deep= + (buffer/format @"" "%j" [1 2 3 :a :b :c]) + (buffer/format-at @"" 0 "%j" [1 2 3 :a :b :c])) + "buffer/format-at empty buffer") +(def buf @"xxxyyy") +(buffer/format-at buf -4 "xxx") +(assert (= (string buf) "xxxxxx") "buffer/format-at negative index") +(assert-error "expected index at to be in range [0, 0), got 1" (buffer/format-at @"" 1 "abc")) + (end-suite)