From d66a50ac7414cdbe5c572e371ce98e99cfd43b43 Mon Sep 17 00:00:00 2001 From: "carpentry-heartbeat[bot]" Date: Mon, 15 Jun 2026 15:46:42 +0200 Subject: [PATCH] Add append-long, append-float, and append-bool functions --- src/strbuf.h | 29 +++++++++++++++++++++++++++++ strbuf.carp | 10 ++++++++++ test/strbuf.carp | 26 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/src/strbuf.h b/src/strbuf.h index bd8f2f1..9e80020 100644 --- a/src/strbuf.h +++ b/src/strbuf.h @@ -3,6 +3,7 @@ #include #include +#include #define STRBUF_DEFAULT_CAP 256 @@ -72,6 +73,34 @@ void StringBuf_append_MINUS_double(StringBuf* sb, double n) { sb->len += slen; } +void StringBuf_append_MINUS_long(StringBuf* sb, Long n) { + char tmp[24]; + int slen = snprintf(tmp, sizeof(tmp), "%" PRId64, n); + strbuf_grow(sb, slen); + memcpy(sb->data + sb->len, tmp, slen); + sb->len += slen; +} + +void StringBuf_append_MINUS_float(StringBuf* sb, float n) { + char tmp[64]; + int slen = snprintf(tmp, sizeof(tmp), "%g", (double)n); + strbuf_grow(sb, slen); + memcpy(sb->data + sb->len, tmp, slen); + sb->len += slen; +} + +void StringBuf_append_MINUS_bool(StringBuf* sb, bool b) { + if (b) { + strbuf_grow(sb, 4); + memcpy(sb->data + sb->len, "true", 4); + sb->len += 4; + } else { + strbuf_grow(sb, 5); + memcpy(sb->data + sb->len, "false", 5); + sb->len += 5; + } +} + void StringBuf_append_MINUS_crlf(StringBuf* sb) { strbuf_grow(sb, 2); sb->data[sb->len++] = '\r'; diff --git a/strbuf.carp b/strbuf.carp index 1ad1cc0..099af5b 100644 --- a/strbuf.carp +++ b/strbuf.carp @@ -47,6 +47,16 @@ amortized O(1) appends. (doc append-double "appends a double as its string representation.") (register append-double (Fn [&StringBuf Double] ())) + (doc append-long "appends a long as its decimal string representation.") + (register append-long (Fn [&StringBuf Long] ())) + + (doc append-float "appends a float as its string representation.") + (register append-float (Fn [&StringBuf Float] ())) + + (doc append-bool + "appends a bool as \\342\\200\\234true\\342\\200\\235 or \\342\\200\\234false\\342\\200\\235.") + (register append-bool (Fn [&StringBuf Bool] ())) + (doc append-crlf "appends \\r\\n (HTTP line ending).") (register append-crlf (Fn [&StringBuf] ())) diff --git a/test/strbuf.carp b/test/strbuf.carp index 9cb7644..5a244a6 100644 --- a/test/strbuf.carp +++ b/test/strbuf.carp @@ -92,6 +92,32 @@ (let-do [s (StringBuf.to-string &sb)] (StringBuf.delete sb) s)) "append-double") + (assert-equal test + "n=42" + &(let-do [sb (StringBuf.create)] + (StringBuf.append-str &sb "n=") + (StringBuf.append-long &sb 42l) + (let-do [s (StringBuf.to-string &sb)] (StringBuf.delete sb) s)) + "append-long") + + (assert-equal test + "x=1.5" + &(let-do [sb (StringBuf.create)] + (StringBuf.append-str &sb "x=") + (StringBuf.append-float &sb 1.5f) + (let-do [s (StringBuf.to-string &sb)] (StringBuf.delete sb) s)) + "append-float") + + (assert-equal test + "yes=true no=false" + &(let-do [sb (StringBuf.create)] + (StringBuf.append-str &sb "yes=") + (StringBuf.append-bool &sb true) + (StringBuf.append-str &sb " no=") + (StringBuf.append-bool &sb false) + (let-do [s (StringBuf.to-string &sb)] (StringBuf.delete sb) s)) + "append-bool") + (assert-equal test "abc" &(let-do [sb (StringBuf.create)