recovery us-ascii encoding

This commit is contained in:
mmacia committed 2026-09-25 23:33:36 +00:00
1 parent 7200ef5215
commit d030408ab8
3 files changed
+16 -3

No files matched your search

+2 -2
View File
@@ -8,14 +8,14 @@ defmodule FeedReader.Recovery.Encoding do
to UTF-8, including declarations split across input chunks.
"""
def transcode(input, :utf8), do: input
def transcode(input, encoding) when encoding in [:utf8, :ascii], do: input
def transcode(input, :latin1),
do: Stream.map(input, &:unicode.characters_to_binary(&1, :latin1, :utf8))
def normalize_declaration(input, :utf8), do: input
def normalize_declaration(input, :latin1) do
def normalize_declaration(input, encoding) when encoding in [:latin1, :ascii] do
Stream.transform(input, fn -> {:prolog, <<>>} end, &rewrite_chunk/2, &flush/1)
end
+2 -1
View File
@@ -10,7 +10,7 @@ defmodule FeedReader.Recovery do
"""
@doc "Returns the encoding declared by an XML prefix, defaulting to UTF-8."
@spec declared_encoding(binary()) :: :utf8 | :latin1
@spec declared_encoding(binary()) :: :utf8 | :latin1 | :ascii
def declared_encoding(prefix) do
case Regex.run(~r/<\?xml\b[^>]*\bencoding\s*=\s*["']([^"']+)["']/is, prefix,
capture: :all_but_first
@@ -34,6 +34,7 @@ defmodule FeedReader.Recovery do
defp normalize_encoding(encoding) do
case String.downcase(encoding) do
value when value in ["iso-8859-1", "latin1", "latin-1"] -> :latin1
"us-ascii" -> :ascii
_utf8 -> :utf8
end
end