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
commit1e78321bcd7838565fb6ea6ecf330db7d2422547 (patch)
tree2c0c75c5022003f0de0f8d6f20bfa70bc76d9266 /lib.c
parent89eb0e18c6af1dc2150e34af19e81b1ad3b52f81 (diff)
downloadtxr-1e78321bcd7838565fb6ea6ecf330db7d2422547.tar.gz
txr-1e78321bcd7838565fb6ea6ecf330db7d2422547.tar.bz2
txr-1e78321bcd7838565fb6ea6ecf330db7d2422547.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 differently: they are rendered as a sequence of lower case hex digit pairs. When a string-valued variable specifies a separator, the separator appears between characters of the string value. Previously, the separator was ignored. When a buffer-valued variable specifies a separator. the separator appears between pairs of digits, not between digits. For instance if ethaddr is a variable holding #b'08:00:27:79:c7:f5', then the quasiliteral `@ethaddr` produces "08002779c7f" whereas `@{ethaddr ":"}` produces "08:00:27:79:c7:f5". * buf.[ch] (buf_str_sep): New function. * lib.[ch] (fmt_str_sep): New function. * eval.c (fmt_cat): If the argument is a string, and a separator is present, replace the value with the result of calling fmt_str_sep. If the argument is a buffer, and a separator is present, use buf_str_sep to convert to a string, otherwise use tostringp. * txr.1: Section on Output Variables updated. * tests/012/readprint.tl: New tests.
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");