Skip to content

Commit 090eb13

Browse files
JosephTLyonslpil
authored andcommitted
Fix string.slice to handle zero gracefully
1 parent b9b821e commit 090eb13

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/gleam/string.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn less_than(a: String, b: String) -> Bool
188188
/// ```
189189
///
190190
pub fn slice(from string: String, at_index idx: Int, length len: Int) -> String {
191-
case len < 0 {
191+
case len <= 0 {
192192
True -> ""
193193
False ->
194194
case idx < 0 {

test/gleam/string_test.gleam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ pub fn slice_test() {
335335

336336
assert string.slice("gleam", at_index: 2, length: -3) == ""
337337

338+
assert string.slice("gleam", at_index: 2, length: 0) == ""
339+
338340
assert string.slice("👶🏿", at_index: 0, length: 3) == "👶🏿"
339341
}
340342

0 commit comments

Comments
 (0)