Support inttypes in JDN (#1905)

Co-authored-by: sogaiu <983021772@users.noreply.github.com>
This commit is contained in:
sogaiu
2026-10-05 21:46:28 -05:00
committed by GitHub
co-authored by sogaiu
parent 820ad0780c
commit 809355fcf7
2 changed files with 36 additions and 1 deletions
+27
View File
@@ -463,6 +463,33 @@ static int print_jdn_one(struct pretty *S, Janet x, int depth) {
janet_buffer_push_u8(S->buffer, '}');
}
break;
#ifdef JANET_INT_TYPES
case JANET_ABSTRACT: {
void *abst = janet_unwrap_abstract(x);
if (janet_abstract_type(abst) == &janet_s64_type) {
janet_buffer_ensure(S->buffer, S->buffer->count + BUFSIZE, 2);
int64_t s64 = janet_unwrap_s64(x);
int count = snprintf((char *)S->buffer->data + S->buffer->count,
BUFSIZE, "%" PRId64 ":s", s64);
if (count < 0) {
return 1;
}
S->buffer->count += count;
} else if (janet_abstract_type(abst) == &janet_u64_type) {
janet_buffer_ensure(S->buffer, S->buffer->count + BUFSIZE, 2);
uint64_t u64 = janet_unwrap_u64(x);
int count = snprintf((char *)S->buffer->data + S->buffer->count,
BUFSIZE, "%" PRIu64 ":u", u64);
if (count < 0) {
return 1;
}
S->buffer->count += count;
} else {
return 1;
}
}
break;
#endif
default:
return 1;
}
+9 -1
View File
@@ -50,7 +50,8 @@
# 5c364e0
(defn check-jdn [x]
(assert (deep= (parse (string/format "%j" x)) x) "round trip jdn"))
(assert (true? (first (protect (deep= (parse (string/format "%j" x)) x))))
(string/format "round trip jdn: %n" x)))
(check-jdn 0)
(check-jdn nil)
@@ -60,6 +61,13 @@
(check-jdn 12837192371923)
(check-jdn "a string")
(check-jdn @"a buffer")
# Issue 1899
(check-jdn -258:s)
(check-jdn 3_141_592:u)
# Issue 1899
(assert-error "non-inttype abstract should fail"
(string/format "%j" (peg/compile 1)))
# Issue 1737
(assert (deep= "@[]" (string/format "%M" @[])))