diff --git a/docs/pages/libraries.md b/docs/pages/libraries.md index 7961041d..dbf745a6 100644 --- a/docs/pages/libraries.md +++ b/docs/pages/libraries.md @@ -626,7 +626,7 @@ Like `filestream:writef` but also flushes the standard output. function filestream:print(...: varargs): void ``` -Like `filestream:print`, but writes to `io.stdout` using `io.write`. +Like `print`, but writes to a filestream. ### filestream:lines @@ -2581,6 +2581,9 @@ function utf8.char(...: varargs): string Receives zero or more integers, converts each one to its corresponding UTF-8 byte sequence, and returns a string with the concatenation of all these sequences. +Containers of integers (e.g vector, sequence or span) are also accepted as arguments, +returning a string with the concatenation of all their elements. + ### utf8.codes ```nelua @@ -4134,6 +4137,7 @@ Values smaller than 100 mean the collector will not wait to start a new cycle. ```nelua global GCFlags: type = @enum(usize) { MARK = 1 << 16, -- Marked for collection (used only internally). + FINALIZE = 1 << 17, -- Finalizer should be called before deallocating. ROOT = AllocatorFlags.GCRoot, -- Allocation always scanned and never collected. LEAF = AllocatorFlags.GCLeaf, -- Allocation never scanned (contains no pointers). BRANCH = AllocatorFlags.GCBranch, -- Allocation should be scanned despite being detected as leaf (contains pointers). diff --git a/lib/filestream.nelua b/lib/filestream.nelua index eb516614..35f90780 100644 --- a/lib/filestream.nelua +++ b/lib/filestream.nelua @@ -451,7 +451,7 @@ function filestream:printf(...: varargs): void self:flush() end --- Like `filestream:print`, but writes to `io.stdout` using `io.write`. +-- Like `print`, but writes to a filestream. function filestream:print(...: varargs): void ## for i=1,select('#', ...) do ## if i > 1 then diff --git a/lib/utf8.nelua b/lib/utf8.nelua index 87b0856d..496ee134 100644 --- a/lib/utf8.nelua +++ b/lib/utf8.nelua @@ -110,12 +110,23 @@ end --[[ Receives zero or more integers, converts each one to its corresponding UTF-8 byte sequence, and returns a string with the concatenation of all these sequences. + +Containers of integers (e.g vector, sequence or span) are also accepted as arguments, +returning a string with the concatenation of all their elements. ]] function utf8.char(...: varargs): string local sb: stringbuilder ## for i=1,select('#', ...) do - local buf: [8]byte, len: int32 = utf8esc((@uint32)(#[select(i, ...)]#)) - sb:write(string{data=&buf[0], size=(@usize)(len)}) + local val: auto = #[select(i, ...)]# + ## if val.type.is_container then + for i: integer, v: integer in ipairs(val) do + local buf: [8]byte, len: int32 = utf8esc((@uint32)(v)) + sb:write(string{data=&buf[0], size=(@usize)(len)}) + end + ## else + local buf: [8]byte, len: int32 = utf8esc((@uint32)(val)) + sb:write(string{data=&buf[0], size=(@usize)(len)}) + ## end ## end return sb:promote() end diff --git a/tests/utf8_test.nelua b/tests/utf8_test.nelua index df6f727c..79e72400 100644 --- a/tests/utf8_test.nelua +++ b/tests/utf8_test.nelua @@ -1,4 +1,6 @@ require 'utf8' +require 'vector' +require 'sequence' do -- utf8.charpattern local b: boolean, s: sequence(string) @@ -18,6 +20,18 @@ do -- utf8.char s = utf8.char(0x7fffffff); assert(s == '\xFD\xBF\xBF\xBF\xBF\xBF') s:destroy() end +do -- utf8.char over containers + local s: string + local arr: [3]uint32 = {0x41,0xe3,0xff} + local spn: span(uint32) = {data=&arr, size=#arr} + local vec: vector(uint32) = {0x41,0xe3,0xff} + local seq: sequence(uint32) = {0x41,0xe3,0xff} + s = utf8.char(arr); assert(s == 'A\u{e3}\u{ff}') s:destroy() + s = utf8.char(spn); assert(s == 'A\u{e3}\u{ff}') s:destroy() + s = utf8.char(vec); assert(s == 'A\u{e3}\u{ff}') s:destroy() + s = utf8.char(seq); assert(s == 'A\u{e3}\u{ff}') s:destroy() +end + do -- utf8.codes local i: isize = 1 for p: isize, c: uint32 in utf8.codes('A\u{e3}\u{ff}0') do