Fix crashes in String.join/join-with-char with empty array.

This commit is contained in:
Jorge Acereda 2019-09-10 21:49:00 +02:00
parent 78181991f7
commit aa71647d02
2 changed files with 10 additions and 2 deletions

View File

@ -114,7 +114,7 @@
(defn join [sep strings]
(let-do [j 0
lstrings (Array.length strings)
num-seps (- lstrings 1)
num-seps (max 0 (- lstrings 1))
sep-length (String.length &sep)
seps-size (* num-seps sep-length)
result (String.allocate (+ seps-size (sum-length strings)) \ )]
@ -134,7 +134,7 @@
;; (= (join-with-char \ ["Hello" "world"]) (join " " ["Hello" "world"]))
(let-do [j 0
lstrings (Array.length strings)
sep-length (- lstrings 1)
sep-length (max 0 (- lstrings 1))
result (String.allocate (+ sep-length (sum-length strings)) \ )]
(for [i 0 lstrings]
(let-do [str (Array.nth strings i)

View File

@ -210,6 +210,10 @@
"hello aaaa there aaaa world"
&(join @" aaaa " &[@"hello" @"there" @"world"])
"join works correctly III")
(assert-equal test
""
&(join @" aaaa " &[])
"join works correctly IV")
(assert-equal test
"hello there world"
&(join-with-char \ &[@"hello" @"there" @"world"])
@ -218,6 +222,10 @@
"hello, there, world"
&(join-with-char \, &[@"hello" @" there" @" world"])
"join-with-char works correctly II")
(assert-equal test
""
&(join-with-char \, &[])
"join-with-char works correctly III")
(assert-equal test
"hellohellohello"
&(repeat 3 "hello")