summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2024-05-27 20:20:14 -0700
committerKaz Kylheku <kaz@kylheku.com>2024-05-27 20:20:14 -0700
commit6bccfe8a55a12ef162fd945633c130f8b3362529 (patch)
tree2c0c75c5022003f0de0f8d6f20bfa70bc76d9266 /lib.c
parent55cf3cae3665cab1264fe00ec31859d8dba13027 (diff)
downloadtxr-6bccfe8a55a12ef162fd945633c130f8b3362529.tar.gz
txr-6bccfe8a55a12ef162fd945633c130f8b3362529.tar.bz2
txr-6bccfe8a55a12ef162fd945633c130f8b3362529.zip
quasiliterals: buffers in hex, separation for strings and buffers.
In this commit, output variables in the TXR Pattern language and in TXR Lisp quasiliterals now support separator strings for values that are strings and buffers. Values which are buffers appear
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 7a0d2522..363b55a3 100644
--- a/lib.c
+++ b/lib.c
@@ -6237,6 +6237,40 @@ val fmt_join(varg args)
return join_with(nil, args);
}
+val fmt_str_sep(val sep, val str, val self)
+{
+ ucnum lsep = c_unum(length_str(sep), self);
+ ucnum lstr = c_unum(length_str(str), self);
+ const wchar_t *csep = c_str(sep, self);
+ const wchar_t *cstr = c_str(str, self);
+ ucnum total = (lstr > 0 ? (lstr - 1) * lsep + lstr : 0) + 1;
+ ucnum i;
+ wchar_t *out = chk_wmalloc(total);
+ wchar_t *ptr = out, *end = out + total;
+
+ for (i = 0; i < lstr; i++) {
+ if (i > 0) {
+ if (end - ptr <= convert(ptrdiff_t, lsep))
+ break;
+ wcscpy(ptr, csep);
+ ptr += lsep;
+ }
+ if (end - ptr <= 1)
+ break;
+ *ptr++ = cstr[i];
+ }
+
+ if (i < lstr) {
+ free(out);
+ uw_throwf(error_s, lit("~a: string length overflow"), self, nao);
+ }
+
+ *ptr = 0;
+
+ return string_own(out);
+}
+
+
val split_str_keep(val str, val sep, val keep_sep_opt, val count_opt)
{
val self = lit("split-str");