From 3ccc3ac8b0cc090f5315b23effd4ce6c8cad783d Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Sat, 29 Jan 2022 23:50:30 +0000 Subject: =?UTF-8?q?Add=20monadic=20format=20=E2=8D=95.=20It=20doesn't=20wo?= =?UTF-8?q?rk=20for=20nested=20arrays=20yet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'functions.c') diff --git a/functions.c b/functions.c index 0f40a9a..a0090a9 100644 --- a/functions.c +++ b/functions.c @@ -58,7 +58,7 @@ fnmonad monadfunctiondefs[] = { fnReverseFirst, /* ⊖ */ fnTranspose, /* ⍉ */ fnExecute, /* ⍎ */ - 0, /* ⍕ */ + fnFormat, /* ⍕ */ fnSelfReference1, /* ∇ */ }; @@ -840,6 +840,46 @@ fnExecute(Array *right) return res; } +Array * +fnFormat(Array *right) +{ + if(right->type == AtypeArray) + throwerror(L"Format for nested arrays", ENotImplemented); + /* TODO add support for formatting nested arrays */ + + right = right->rank == 0 ? fnRavel(right) : fnSame(right); + + Rune *str = pparray(right); + int maxwidth = 0; + int tmpwidth = 0; + for(int i = 0; str[i] != 0; i++){ + if(str[i] != '\n') + tmpwidth++; + if(str[i] == '\n' || str[i+1] == 0){ + if(tmpwidth > maxwidth) + maxwidth = tmpwidth; + tmpwidth = 0; + } + } + vlong size; + if(right->shape[right->rank-1] == 0) + size = 0; + else + size = maxwidth * right->size / right->shape[right->rank-1]; + + Array *result = allocarray(AtypeRune, right->rank, size); + result->shape[result->rank-1] = maxwidth; + for(int i = 0; i < result->rank-1; i++) + result->shape[i] = right->shape[i]; + int offset = 0; + for(int i = 0; str[i] != 0; i++) + if(str[i] != '\n') + result->runedata[offset++] = str[i]; + free(str); + freearray(right); + return result; +} + Array * fnSelfReference1(Array *right) { -- cgit v1.2.3