This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Xgit.Util.ParseCharlist.decode_ambiguous_charlist/1. (#196)
* Implement Xgit.Util.ParseCharlist.decode_ambiguous_charlist/1. * Replace uses of RawParseUtils.decode/1 with ParseCharlist.decode_ambiguous_charlist/1. * A few small fixes.
- Loading branch information
Showing
4 changed files
with
38 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Xgit.Util.ParseCharlist do | ||
@moduledoc false | ||
# Internal utility for parsing charlists with ambiguous encodings. | ||
|
||
import Xgit.Util.ForceCoverage | ||
|
||
@doc ~S""" | ||
Convert a list of bytes to an Elixir (UTF-8) string when the encoding is not | ||
definitively known. Try parsing as a UTF-8 byte array first, then try ISO-8859-1. | ||
""" | ||
@spec decode_ambiguous_charlist(b :: [byte]) :: String.t() | ||
def decode_ambiguous_charlist(b) when is_list(b) do | ||
raw = :erlang.list_to_binary(b) | ||
|
||
case :unicode.characters_to_binary(raw) do | ||
utf8 when is_binary(utf8) -> cover(utf8) | ||
_ -> :unicode.characters_to_binary(raw, :latin1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Xgit.Util.ParseCharlistTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Xgit.Util.ParseCharlist | ||
|
||
test "decode/1" do | ||
assert ParseCharlist.decode_ambiguous_charlist([64, 65, 66]) == "@AB" | ||
assert ParseCharlist.decode_ambiguous_charlist([228, 105, 116, 105]) == "äiti" | ||
assert ParseCharlist.decode_ambiguous_charlist([195, 164, 105, 116, 105]) == "äiti" | ||
assert ParseCharlist.decode_ambiguous_charlist([66, 106, 246, 114, 110]) == "Björn" | ||
assert ParseCharlist.decode_ambiguous_charlist([66, 106, 195, 182, 114, 110]) == "Björn" | ||
end | ||
end |