summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-06-12 19:35:06 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-06-12 19:35:06 -0700
commitecb2cd4a54505d3068cad1faa9bf02c28162bc55 (patch)
tree91663dc250a746daa35d1dab5007da34b6d29119 /lib.c
parente72f960431aa61185c40cf38a471b6d8b0924a58 (diff)
downloadtxr-ecb2cd4a54505d3068cad1faa9bf02c28162bc55.tar.gz
txr-ecb2cd4a54505d3068cad1faa9bf02c28162bc55.tar.bz2
txr-ecb2cd4a54505d3068cad1faa9bf02c28162bc55.zip
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.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c33
1 files changed, 33 insertions, 0 deletions
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)