Skip to content

Instantly share code, notes, and snippets.

@arBmind
Last active March 30, 2021 23:29
Show Gist options
  • Save arBmind/65dc9043f0bd819a6a6c to your computer and use it in GitHub Desktop.
Save arBmind/65dc9043f0bd819a6a6c to your computer and use it in GitHub Desktop.
Elixir implementation of split line feed aware
defp split_lf_aware(str, 0, "", line), do: { line, to_string(str) }
defp split_lf_aware(str, 0, acc, line), do: { acc, line <> to_string(str) }
defp split_lf_aware("", _, acc, line), do: { acc + line, "" }
defp split_lf_aware("\n" <> str, pos, acc, line) do
split_lf_aware(str, pos-1, acc <> line <> "\n", "")
end
defp split_lf_aware(<<chr :: utf8>> <> str, pos, acc, line) do
split_lf_aware(str, pos-1, acc, line <> <<chr :: utf8>>)
end
def split_lf_aware(str, pos) do
split_lf_aware(str, pos, "", "")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment