From 524adfef0b903ff25faa62936bbdfd417dfb5694 Mon Sep 17 00:00:00 2001 From: Gordon Tisher Date: Tue, 8 Oct 2024 14:41:06 -0700 Subject: [PATCH] Fix `String/decode_utf8` returning a non-empty string when given an empty list (#701) Previously calling `(String/decode_utf8 [])` would return `"\0"`. This PR makes it return `""`. Adds this case to `utf8.bend` and updated the snapshot. Fixes #701 --- src/fun/builtins.bend | 4 +++- tests/golden_tests/io/utf8.bend | 3 ++- tests/snapshots/io__utf8.bend.snap | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index baa3a102..29d94f0e 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -561,7 +561,9 @@ Utf8/REPLACEMENT_CHARACTER : u24 = '\u{FFFD}' # Decodes a sequence of bytes to a String using utf-8 encoding. # Invalid utf-8 sequences are replaced with Utf8/REPLACEMENT_CHARACTER. -String/decode_utf8 (bytes: (List u24)) : String = +String/decode_utf8 (bytes: (List u24)) : String +String/decode_utf8 [] = String/Nil +String/decode_utf8 bytes = let (got, rest) = (Utf8/decode_character bytes) match rest { List/Nil: (String/Cons got String/Nil) diff --git a/tests/golden_tests/io/utf8.bend b/tests/golden_tests/io/utf8.bend index b3e16168..80618d6e 100644 --- a/tests/golden_tests/io/utf8.bend +++ b/tests/golden_tests/io/utf8.bend @@ -5,5 +5,6 @@ v2 = (to-and-back "(位f ((位x (f (x x))) (位x (f (x x)))))") v3 = (to-and-back "馃専") v4 = (to-and-back "Hello 馃寧!") v5 = (to-and-back "饟唸 饟唹 饟唺 饟唻 饟叝") +v6 = (String/decode_utf8 []) -main = [v1, v2, v3, v4, v5] +main = [v1, v2, v3, v4, v5, v6] diff --git a/tests/snapshots/io__utf8.bend.snap b/tests/snapshots/io__utf8.bend.snap index f4ec8ef3..19479507 100644 --- a/tests/snapshots/io__utf8.bend.snap +++ b/tests/snapshots/io__utf8.bend.snap @@ -3,4 +3,4 @@ source: tests/golden_tests.rs input_file: tests/golden_tests/io/utf8.bend --- Strict mode: -["hi", "(位f ((位x (f (x x))) (位x (f (x x)))))", "馃専", "Hello 馃寧!", "饟唸 饟唹 饟唺 饟唻 饟叝"] +["hi", "(位f ((位x (f (x x))) (位x (f (x x)))))", "馃専", "Hello 馃寧!", "饟唸 饟唹 饟唺 饟唻 饟叝", ""]