Skip to content

Commit

Permalink
Add support for passing containers to utf8.char
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 16, 2023
1 parent d005568 commit 596fcca
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/pages/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion lib/filestream.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions lib/utf8.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/utf8_test.nelua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'utf8'
require 'vector'
require 'sequence'

do -- utf8.charpattern
local b: boolean, s: sequence(string)
Expand All @@ -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) <close> = {0x41,0xe3,0xff}
local seq: sequence(uint32) <close> = {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
Expand Down

0 comments on commit 596fcca

Please sign in to comment.