Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/elixir/unicode/unicode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,39 @@ defmodule String.Break do

# Split

def split(string) do
:binary.split(string, unquote(whitespace -- non_breakable), [:global, :trim_all])
splittable = whitespace -- non_breakable

def split(string) when is_binary(string) do
split_whitespace(string, string, 0)
end

for cp <- splittable do
defp split_whitespace(<<unquote(cp), rest::bits>>, string, pos),
do: split_whitespace(rest, string, pos + unquote(byte_size(cp)))
end

defp split_whitespace(<<>>, _string, _pos), do: []

defp split_whitespace(<<_, rest::bits>>, string, pos),
do: split_word(rest, string, pos, pos + 1)

defp split_word(<<byte, rest::bits>>, string, start, pos) when byte in 0x21..0x7F,
do: split_word(rest, string, start, pos + 1)

for cp <- splittable do
defp split_word(<<unquote(cp), rest::bits>>, string, start, pos) do
[
binary_part(string, start, pos - start)
| split_whitespace(rest, string, pos + unquote(byte_size(cp)))
]
end
end

defp split_word(<<>>, string, start, pos), do: [binary_part(string, start, pos - start)]

defp split_word(<<_, rest::bits>>, string, start, pos),
do: split_word(rest, string, start, pos + 1)

# Decompose

def decompose(entries, map) do
Expand Down