Skip to content

Commit

Permalink
Do not double encode \n, \r and \t [fixes #70]
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrichartz committed Sep 12, 2020
1 parent 87c30b9 commit e3044b5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/csv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ defmodule CSV do
iex> [[\"a\\nb\", \"\\tc\"], [\"de\", \"\\tf\\\"\"]]
iex> |> CSV.encode(separator: ?\\t, delimiter: \"\\n\")
iex> |> Enum.take(2)
[\"\\\"a\\\\nb\\\"\\t\\\"\\\\tc\\\"\\n\", \"de\\t\\\"\\\\tf\\\"\\\"\\\"\\n\"]
[\"\\\"a\\nb\\\"\\t\\\"\\tc\\\"\\n\", \"de\\t\\\"\\tf\\\"\\\"\\\"\\n\"]
"""

def encode(stream, options \\ []) do
Expand Down
11 changes: 1 addition & 10 deletions lib/csv/encoding/encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,12 @@ defimpl CSV.Encode, for: BitString do
]) ->
<<@double_quote::utf8>> <>
(data
|> escape
|> String.replace(
<<@double_quote::utf8>>,
<<@double_quote::utf8>> <> <<@double_quote::utf8>>
)) <> <<@double_quote::utf8>>

true ->
data |> escape
true -> data
end
end

defp escape(cell) do
cell
|> String.replace(<<@newline::utf8>>, "\\n")
|> String.replace(<<@carriage_return::utf8>>, "\\r")
|> String.replace("\t", "\\t")
end
end
2 changes: 1 addition & 1 deletion lib/csv/encoding/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule CSV.Encoding.Encoder do
iex> [[\"a\\nb\", \"\\tc\"], [\"de\", \"\\tf\\\"\"]]
iex> |> CSV.Encoding.Encoder.encode(separator: ?\\t, delimiter: \"\\n\")
iex> |> Enum.take(2)
[\"\\\"a\\\\nb\\\"\\t\\\"\\\\tc\\\"\\n\", \"de\\t\\\"\\\\tf\\\"\\\"\\\"\\n\"]
[\"\\\"a\\nb\\\"\\t\\\"\\tc\\\"\\n\", \"de\\t\\\"\\tf\\\"\\\"\\\"\\n\"]
"""

def encode(stream, options \\ []) do
Expand Down
4 changes: 2 additions & 2 deletions test/encoding/escaped_fields_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule EncodingTests.EscapedFieldsTest do

test "encodes streams to csv strings and escapes them" do
result = Encoder.encode([["a,", "b\re"], ["c,f\"", "dg"]]) |> Enum.to_list()
assert result == ["\"a,\",\"b\\re\"\r\n", "\"c,f\"\"\",dg\r\n"]
assert result == ["\"a,\",\"b\re\"\r\n", "\"c,f\"\"\",dg\r\n"]
end

test "encodes streams of various content to csv strings and escapes them" do
Expand All @@ -17,6 +17,6 @@ defmodule EncodingTests.EscapedFieldsTest do
Encoder.encode([["a\t", "b\re"], ["c\tf\"", "dg"]], separator: ?\t, delimiter: "\n")
|> Enum.to_list()

assert result == ["\"a\\t\"\t\"b\\re\"\n", "\"c\\tf\"\"\"\tdg\n"]
assert result == ["\"a\t\"\t\"b\re\"\n", "\"c\tf\"\"\"\tdg\n"]
end
end

0 comments on commit e3044b5

Please sign in to comment.