Skip to content

Commit 87dc792

Browse files
committed
Properly handle list output of :unicode_util.gc/1, closes #12160
1 parent 78d6b88 commit 87dc792

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lib/elixir/lib/string.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,7 @@ defmodule String do
19301930
def next_grapheme(string) when is_binary(string) do
19311931
case :unicode_util.gc(string) do
19321932
[gc] -> {grapheme_to_binary(gc), <<>>}
1933+
[gc, rest] -> {grapheme_to_binary(gc), rest}
19331934
[gc | rest] -> {grapheme_to_binary(gc), rest}
19341935
[] -> nil
19351936
{:error, <<byte, rest::bits>>} -> {<<byte>>, rest}

lib/elixir/test/elixir/string_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@ defmodule StringTest do
585585
test "next_grapheme/1" do
586586
assert String.next_grapheme("Ā̀stute") == {"Ā̀", "stute"}
587587
assert String.next_grapheme("") == nil
588+
589+
for _ <- 1..10 do
590+
bin = :crypto.strong_rand_bytes(100)
591+
assert bin |> Stream.unfold(&String.next_grapheme/1) |> Enum.all?(&is_binary/1),
592+
"cannot build graphemes for #{inspect(bin)}"
593+
end
588594
end
589595

590596
test "first/1" do

0 commit comments

Comments
 (0)