Skip to content

Commit 7e76039

Browse files
committed
More tests for malformed sequences
1 parent 375f2bd commit 7e76039

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lib/elixir/lib/string.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,11 @@ defmodule String do
524524
defp parts_to_index(n) when is_integer(n) and n > 0, do: n
525525

526526
defp split_empty("", true, 1), do: []
527-
defp split_empty(string, _, 1) when is_binary(string), do: [IO.iodata_to_binary(string)]
527+
defp split_empty(string, _, 1), do: [IO.iodata_to_binary(string)]
528528

529529
defp split_empty(string, trim, count) do
530530
case :unicode_util.gc(string) do
531+
[gc] -> [grapheme_to_binary(gc) | split_empty("", trim, 1)]
531532
[gc | rest] -> [grapheme_to_binary(gc) | split_empty(rest, trim, count - 1)]
532533
[] -> split_empty("", trim, 1)
533534
{:error, <<byte, rest::bits>>} -> [<<byte>> | split_empty(rest, trim, count - 1)]

lib/elixir/test/elixir/string_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ defmodule StringTest do
115115
["", <<225, 158, 128, 225, 158, 185, 225>>]
116116

117117
assert String.split(<<225, 158, 128, 225, 158, 185, 225>>, "", parts: 3) ==
118+
["", "កឹ", <<225>>]
119+
120+
assert String.split(<<225, 158, 128, 225, 158, 185, 225>>, "", parts: 4) ==
118121
["", "កឹ", <<225>>, ""]
119122
end
120123

0 commit comments

Comments
 (0)