From ba5c64bca426ce8b2eea9d1e3fec190d74cac0fe Mon Sep 17 00:00:00 2001 From: preciz Date: Thu, 1 Oct 2026 09:55:16 +0200 Subject: [PATCH] Avoid UTF-8 re-encoding in StringIO character reads --- lib/elixir/lib/string_io.ex | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/elixir/lib/string_io.ex b/lib/elixir/lib/string_io.ex index aa5460b511c..1cfb83f4ab5 100644 --- a/lib/elixir/lib/string_io.ex +++ b/lib/elixir/lib/string_io.ex @@ -343,23 +343,24 @@ defmodule StringIO do end defp get_chars(input, :unicode, count) do - with {:ok, count} <- split_at(input, count, 0) do - <> = input + with {:ok, rest} <- split_at(input, count) do + size = byte_size(input) - byte_size(rest) + <> = input {chars, rest} end end - defp split_at(_, 0, acc), - do: {:ok, acc} + defp split_at(input, 0), + do: {:ok, input} - defp split_at(<>, count, acc), - do: split_at(t, count - 1, acc + byte_size(<>)) + defp split_at(<<_::utf8, rest::binary>>, count), + do: split_at(rest, count - 1) - defp split_at(<<_, _::binary>>, _count, _acc), + defp split_at(<<_, _::binary>>, _count), do: {:error, :invalid_unicode} - defp split_at(<<>>, _count, acc), - do: {:ok, acc} + defp split_at(<<>>, _count), + do: {:ok, <<>>} ## get_line