Skip to content

Commit

Permalink
handle inf and nan special floats
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Dec 27, 2023
1 parent f6dd284 commit cb733ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added

- Support for escape and unicode characters [#98](https://github.com/ufirstgroup/ymlr/pull/98)
- Refactor `Ymlr.Encode` to make it faster.
- Refactor `Ymlr.Encode` to make it faster [#171](https://github.com/ufirstgroup/ymlr/pull/171).
- Handle special floats `.Inf` and `.Nan`. [#170](https://github.com/ufirstgroup/ymlr/issues/170), [#172](https://github.com/ufirstgroup/ymlr/issues/172)

### Fixed

Expand Down
25 changes: 24 additions & 1 deletion lib/ymlr/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,28 @@ defmodule Ymlr.Encode do
"? ",
"0b",
"0o",
"0x"
"0x",
".inf",
".Inf",
".INF",
"+.inf",
"+.Inf",
"+.INF",
"-.inf",
"-.Inf",
"-.INF",
".nan",
".Nan",
".NAN"
]

@special_atom_mapping %{
:nan => ".nan",
:inf => ".inf",
:"-inf" => "-.inf",
:"+inf" => ".inf"
}

@quote_when_contains_string [" #", ": "]

@quote_when_last_char ~c":"
Expand Down Expand Up @@ -223,6 +242,10 @@ defmodule Ymlr.Encode do
end

@spec atom(atom()) :: iodata()
for {input, encoded} <- @special_atom_mapping do
def atom(unquote(input)), do: unquote(encoded)
end

def atom(data), do: Atom.to_string(data)

@spec string(binary(), integer) :: iodata()
Expand Down
16 changes: 16 additions & 0 deletions test/ymlr/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ defmodule Ymlr.EncodeTest do
assert_identity_and_output("0o777", ~S('0o777'))
assert_identity_and_output("0x1F", ~S('0x1F'))
assert_identity_and_output("null", ~S('null'))
assert_identity_and_output(".inf", ~S('.inf'))
assert_identity_and_output(".Inf", ~S('.Inf'))
assert_identity_and_output(".INF", ~S('.INF'))
assert_identity_and_output("-.inf", ~S('-.inf'))
assert_identity_and_output("-.Inf", ~S('-.Inf'))
assert_identity_and_output("-.INF", ~S('-.INF'))
assert_identity_and_output("+.inf", ~S('+.inf'))
assert_identity_and_output("+.Inf", ~S('+.Inf'))
assert_identity_and_output("+.INF", ~S('+.INF'))
assert_identity_and_output(".nan", ~S('.nan'))
assert_identity_and_output(".Nan", ~S('.Nan'))
assert_identity_and_output(".NAN", ~S('.NAN'))
end

test "quoted strings - avoid mapping confusion" do
Expand Down Expand Up @@ -228,6 +240,10 @@ defmodule Ymlr.EncodeTest do

test "floats" do
assert_identity_and_output(1.2, "1.2")
assert_identity_and_output(:nan, ".nan")
assert_output(:inf, ".inf")
assert_identity_and_output(:"+inf", ".inf")
assert_identity_and_output(:"-inf", "-.inf")
end

test "decimals" do
Expand Down

0 comments on commit cb733ce

Please sign in to comment.