Skip to content

Commit babbae7

Browse files
committed
Handle more unicode sequences in split_at, closes #11617
1 parent 0f0d4c0 commit babbae7

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/elixir/lib/string.ex

+8-4
Original file line numberDiff line numberDiff line change
@@ -2723,12 +2723,16 @@ defmodule String do
27232723
graphemes_and_length: 1,
27242724
reverse_characters_to_binary: 1}
27252725

2726-
defp byte_size_remaining_at(binary, 0) do
2727-
byte_size(binary)
2726+
defp byte_size_unicode(binary) when is_binary(binary), do: byte_size(binary)
2727+
defp byte_size_unicode([head]), do: byte_size_unicode(head)
2728+
defp byte_size_unicode([head | tail]), do: byte_size_unicode(head) + byte_size_unicode(tail)
2729+
2730+
defp byte_size_remaining_at(unicode, 0) do
2731+
byte_size_unicode(unicode)
27282732
end
27292733

2730-
defp byte_size_remaining_at(binary, n) do
2731-
case :unicode_util.gc(binary) do
2734+
defp byte_size_remaining_at(unicode, n) do
2735+
case :unicode_util.gc(unicode) do
27322736
[_] -> 0
27332737
[_ | rest] -> byte_size_remaining_at(rest, n - 1)
27342738
[] -> 0

lib/elixir/test/elixir/string_test.exs

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ defmodule StringTest do
131131
end
132132
end
133133

134+
test "split_at/2 with invalid guard" do
135+
assert String.split_at(<<?a, 195, 10, ?a>>, 2) == {<<?a, 195>>, <<10, ?a>>}
136+
assert String.split_at(<<107, 205, 135, 184>>, 1) == {<<107, 205, 135>>, <<184>>}
137+
end
138+
134139
test "upcase/1" do
135140
assert String.upcase("123 abcd 456 efg hij ( %$#) kl mnop @ qrst = -_ uvwxyz") ==
136141
"123 ABCD 456 EFG HIJ ( %$#) KL MNOP @ QRST = -_ UVWXYZ"

0 commit comments

Comments
 (0)