From ecb2cd4a54505d3068cad1faa9bf02c28162bc55 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 12 Jun 2022 19:35:06 -0700 Subject: New function: str The str function is like mkstring but allows a fill pattern to be specified. * eval.c (eval_init): str intrinsic registered. * lib.[ch[ (str): New function. * tests/015/str.tl: New file. * txr.1: Documented. * stdlib/doc-syms.tl: Updated. --- lib.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 23fd5008..cc0f923e 100644 --- a/lib.c +++ b/lib.c @@ -4885,6 +4885,39 @@ val init_str(val str, const wchar_t *data, val self) return str; } +val str(val len, val pattern) +{ + if (chrp(pattern) || null_or_missing_p(pattern)) { + return mkstring(len, pattern); + } else { + val self = lit("str"); + const wchar_t *pat = c_str(pattern, self); + ucnum pl = c_unum(length(pattern), self); + + if (pl <= 1) { + val ch = if3(pl == 0, chr(' '), chr(pat[0])); + return mkstring(len, ch); + } else { + ucnum l = c_unum(len, self); + val str = mkustring(len); + ucnum offs = 0; + + str->st.str[l] = 0; + + for (;;) { + wmemcpy(str->st.str + offs, pat, min(l, pl)); + if (pl < l) { + l -= pl; + offs += pl; + continue; + } + break; + } + return str; + } + } +} + static val copy_lazy_str(val lstr); val copy_str(val str) -- cgit v1.2.3