From c5134a9ddba4fd14703d506b0cccd51d823e013e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 30 May 2020 07:38:12 -0700 Subject: quasistrings: reduce consing. Quasistrings compile to code that requires on the sys:fmt-join function to glue strings together. Rewriting that function to avoid converting its arguments from struct args * to a list. * eval.c (fmt_join): Static function removed. * lib.c (cat_str_measure, cat_str_append): more_p parameter changed to int type, which better matches the C style Boolean values it takes. (fmt_join): New external function. * lib.h: Declared. * args.h (args_more_nozap, args_get_nozap): New inline functions allowing multiple iterations over arguments without making a copy. --- lib.c | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 6180d87a..ef3d511c 100644 --- a/lib.c +++ b/lib.c @@ -4101,7 +4101,7 @@ static void cat_str_init(struct cat_str *cs, val sep, wchar_t *onech) } } -static void cat_str_measure(struct cat_str *cs, val item, val more_p) +static void cat_str_measure(struct cat_str *cs, val item, int more_p) { if (!item) return; @@ -4143,7 +4143,7 @@ static void cat_str_alloc(struct cat_str *cs) cs->ptr = cs->str = chk_wmalloc(cs->total); } -static void cat_str_append(struct cat_str *cs, val item, val more_p) +static void cat_str_append(struct cat_str *cs, val item, int more_p) { if (!item) return; @@ -4176,12 +4176,12 @@ val cat_str(val list, val sep) cat_str_init(&cs, sep, onech); for (iter = list; iter != nil; iter = cdr(iter)) - cat_str_measure(&cs, car(iter), cdr(iter)); + cat_str_measure(&cs, car(iter), cdr(iter) != nil); cat_str_alloc(&cs); for (iter = list; iter != nil; iter = cdr(iter)) - cat_str_append(&cs, car(iter), cdr(iter)); + cat_str_append(&cs, car(iter), cdr(iter) != nil); return cat_str_get(&cs); } @@ -4197,7 +4197,7 @@ static val vscat(val sep, va_list vl1, va_list vl2) for (item = va_arg(vl1, val); item != nao; item = next) { next = va_arg(vl1, val); - cat_str_measure(&cs, item, tnil(next != nao)); + cat_str_measure(&cs, item, next != nao); } cat_str_alloc(&cs); @@ -4205,7 +4205,7 @@ static val vscat(val sep, va_list vl1, va_list vl2) for (item = va_arg(vl2, val); item != nao; item = next) { next = va_arg(vl2, val); - cat_str_append(&cs, item, tnil(next != nao)); + cat_str_append(&cs, item, next != nao); } return cat_str_get(&cs); @@ -4223,6 +4223,34 @@ val scat(val sep, ...) return ret; } +val fmt_join(struct args *args) +{ + cnum index; + val iter; + int more; + struct cat_str cs; + + cat_str_init(&cs, nil, 0); + + for (index = 0, iter = args->list, more = args_more_nozap(args, index, iter); + more;) + { + val item = args_get_nozap(args, &index, &iter); + cat_str_measure(&cs, item, more = args_more_nozap(args, index, iter)); + } + + cat_str_alloc(&cs); + + for (index = 0, iter = args->list, more = args_more_nozap(args, index, iter); + more;) + { + val item = args_get_nozap(args, &index, &iter); + cat_str_append(&cs, item, more = args_more_nozap(args, index, iter)); + } + + return cat_str_get(&cs); +} + val split_str_keep(val str, val sep, val keep_sep) { keep_sep = default_null_arg(keep_sep); -- cgit v1.2.3