commit: e720efd50ccb69b14a173a9906c5f5b1c25e4f6c
parent 61871d137af5276d333d91e3eee11132a1c45d8b
Author: Justin Tormey <jrtormey@gmail.com>
Date: Thu, 20 Aug 2020 13:39:59 -0500
Accumulate as iodata, only convert to binary if necessary
Diffstat:
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex
@@ -62,28 +62,25 @@ defmodule Linkify.Parser do
def parse(input, opts) do
opts = Map.merge(@default_opts, opts)
- acc = if opts[:iodata], do: [], else: ""
- do_parse(input, opts, {"", acc, :parsing})
+
+ {buffer, user_acc} = do_parse(input, opts, {"", [], :parsing})
+
+ if opts[:iodata] do
+ {buffer, user_acc}
+ else
+ {IO.iodata_to_binary(buffer), user_acc}
+ end
end
- defp accumulate(acc, buffer) when is_list(acc),
+ defp accumulate(acc, buffer),
do: [buffer | acc]
- defp accumulate(acc, buffer) when is_binary(acc),
- do: acc <> buffer
-
- defp accumulate(acc, buffer, trailing) when is_list(acc),
+ defp accumulate(acc, buffer, trailing),
do: [trailing, buffer | acc]
- defp accumulate(acc, buffer, trailing) when is_binary(acc),
- do: acc <> buffer <> trailing
-
- defp do_parse({"", user_acc}, _opts, {"", acc, _}) when is_list(acc),
+ defp do_parse({"", user_acc}, _opts, {"", acc, _}),
do: {Enum.reverse(acc), user_acc}
- defp do_parse({"", user_acc}, _opts, {"", acc, _}) when is_binary(acc),
- do: {acc, user_acc}
-
defp do_parse({"@" <> text, user_acc}, opts, {buffer, acc, :skip}),
do: do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer, "@"), :skip})
@@ -146,7 +143,7 @@ defmodule Linkify.Parser do
)
defp do_parse({text, user_acc}, opts, {buffer, acc, {:open, level}}) do
- do_parse({text, user_acc}, opts, {"", acc <> buffer, {:attrs, level}})
+ do_parse({text, user_acc}, opts, {"", accumulate(acc, buffer), {:attrs, level}})
end
defp do_parse(