Last active
March 30, 2021 23:29
-
-
Save arBmind/65dc9043f0bd819a6a6c to your computer and use it in GitHub Desktop.
Elixir implementation of split line feed aware
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
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