Fix utf-8 handling in stdout_stream/2
parent
3fb1018803
commit
c99e2ccaff
@ -0,0 +1,22 @@
|
||||
defmodule Asciinema.StringUtils do
|
||||
def valid_part(invalid_str, str) do
|
||||
case String.chunk(invalid_str <> str, :valid) do
|
||||
[] ->
|
||||
{"", ""}
|
||||
chunks ->
|
||||
str =
|
||||
chunks
|
||||
|> Enum.take(Enum.count(chunks) - 1)
|
||||
|> Enum.filter(&String.valid?/1)
|
||||
|> Enum.join
|
||||
|
||||
last = Enum.at(chunks, -1)
|
||||
|
||||
if String.valid?(last) do
|
||||
{str <> last, ""}
|
||||
else
|
||||
{str, last}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,27 @@
|
||||
defmodule Asciinema.StringUtilsTest do
|
||||
use ExUnit.Case
|
||||
|
||||
describe "valid_part/2" do
|
||||
import Asciinema.StringUtils, only: [valid_part: 2]
|
||||
|
||||
test "no accumulator, valid string" do
|
||||
assert valid_part("", "foo") == {"foo", ""}
|
||||
end
|
||||
|
||||
test "no accumulator, partial utf-8 seq" do
|
||||
assert valid_part("", <<0xc5>>) == {"", <<0xc5>>}
|
||||
end
|
||||
|
||||
test "no accumulator, valid string + partial utf-8 seq at the end" do
|
||||
assert valid_part("", "foo" <> <<0xc5>>) == {"foo", <<0xc5>>}
|
||||
end
|
||||
|
||||
test "with accumulator, rest of utf-8 seq + valid string at the end" do
|
||||
assert valid_part(<<0xc5>>, <<0x82>> <> "ćfoo") == {"łćfoo", ""}
|
||||
end
|
||||
|
||||
test "with accumulator, mixed valid/invalid string + partial utf-8 seq at the end" do
|
||||
assert valid_part(<<0xc5>>, "x" <> <<0xc5, 0xc5>> <> "y" <> <<0xc5>>) == {"xy", <<0xc5>>}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue