Skip to content

Commit

Permalink
Add string.concat
Browse files Browse the repository at this point in the history
It servers the same purposes as Lua's `table.concat`
  • Loading branch information
edubart committed May 23, 2022
1 parent 0b29e34 commit a1619a9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/pages/clibraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -2858,15 +2858,15 @@ function C._Exit(status: cint): void
### C.atexit

```nelua
function C.atexit(func: pointer): cint
function C.atexit(func: function(): void): cint
```



### C.at_quick_exit

```nelua
function C.at_quick_exit(func: pointer): cint
function C.at_quick_exit(func: function(): void): cint
```


Expand Down
16 changes: 12 additions & 4 deletions docs/pages/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,14 @@ Converts a string to a span of bytes.

Remarks: Similar to `subview` a reference of the current string data is returned.

### string.concat

```nelua
function string.concat(list: span(string), sep: facultative(string)): string
```

Concatenates a span of a strings into a single string.

### string:__close

```nelua
Expand Down Expand Up @@ -2596,7 +2604,7 @@ The coroutine handle.
### coroutine.destroy

```nelua
function coroutine.destroy(co: coroutine): void
function coroutine.destroy(co: coroutine): (boolean, string)
```

Destroy the coroutine `co`, freeing its stack memory and resources.
Expand All @@ -2616,7 +2624,7 @@ Effectively the same as `destroy`, called when a to-be-closed variable goes out
### coroutine.create

```nelua
function coroutine.create(f: function_concept): coroutine
function coroutine.create(f: function_concept): (coroutine, string)
```

Returns a new coroutine with body function `f`.
Expand Down Expand Up @@ -2677,7 +2685,7 @@ Starts or continues the execution of the coroutine `co`.
### coroutine.spawn

```nelua
function coroutine.spawn(f: function_concept, ...: varargs): coroutine
function coroutine.spawn(f: function_concept, ...: varargs): (coroutine, string)
```

Creates and immediately starts a new coroutine with body function `f`.
Expand All @@ -2688,7 +2696,7 @@ This is effectively the same as calling `coroutine.create` and then `coroutine.r
### coroutine.yield

```nelua
function coroutine.yield(...: varargs): void
function coroutine.yield(...: varargs): (boolean, string)
```

Suspends the execution of the running coroutine.
Expand Down
29 changes: 29 additions & 0 deletions lib/string.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ function string.span(s: string): span(byte) <inline>
return (@span(byte)){data=s.data, size=s.size}
end

-- Concatenates a span of a strings into a single string.
function string.concat(list: span(string), sep: facultative(string)): string
## if sep.type.is_niltype then
local sep: string = ""
## end
local size: usize
for i: usize=0,<list.size do
if i > 0 then size = size + sep.size end
size = size + list[i].size
end
if size == 0 then return (@string){} end
local s: string = string.create(size)
local pos: usize = 0
for i: usize=0,<list.size do
if sep.size > 0 and i > 0 then
for j=0,<sep.size do
s.data[pos] = sep.data[j]
pos = pos + 1
end
end
local part: string = list[i]
for j=0,<part.size do
s.data[pos] = part.data[j]
pos = pos + 1
end
end
return s
end

----------------------------------------------------------------------------------------------------
-- String metamethods

Expand Down
12 changes: 11 additions & 1 deletion tests/string_test.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ do -- string.gsub
sub:destroy()
end

do -- string format
do -- string.format
local s: string
assert_string_eq(string.format(''), '')
assert_string_eq(string.format('%s', (@string){}), '')
Expand Down Expand Up @@ -480,6 +480,16 @@ do -- string format
assert_string_eq(string.format('|%4s|', ''), '| |')
end

do -- string.concat
assert(string.concat({}) == "")
assert(string.concat({}, " ") == "")

local s: string <close> = string.concat({"hello", "world"}) assert(s == "helloworld")
local s: string <close> = string.concat({"hello", "world"}, " ") print(s) assert(s == "hello world")
local s: string <close> = string.concat({"hello", "huge", "world"}, ",") assert(s == "hello,huge,world")
local s: string <close> = string.concat({"a", "b", "c"}, ";:") assert(s == "a;:b;:c")
end

do -- print
print('hello')
local s: string = 'world'
Expand Down

0 comments on commit a1619a9

Please sign in to comment.