Skip to content

Instantly share code, notes, and snippets.

@kipcole9
Last active June 26, 2021 22:00
Show Gist options
  • Save kipcole9/9f9fc4f3f7db3bbc5c5c045d4de3d1e0 to your computer and use it in GitHub Desktop.
Save kipcole9/9f9fc4f3f7db3bbc5c5c045d4de3d1e0 to your computer and use it in GitHub Desktop.
defmodule Extract do
@moduledoc """
Split into words only when the words have a "%" as a prefix
or a suffix
"""
# Empty string returns empty list
def words("") do
[]
end
# Skip over spaces
def words(<<" ", rest::binary>>) do
words(rest)
end
# A word that starts with a "%" prefix
def words(<<"%", string::binary>>) do
{word, rest} = extract_word(string)
[word | words(rest)]
end
# Start of word, but no "%" prefix
def words(<<string::binary>>) do
{word, rest} = extract_word(string)
case rest do
# The word ends in "%" so keep it
<<"%", rest::binary>> ->
[word | words(rest)]
# Doesn't end in "%" so discard it
_other ->
words(rest)
end
end
# Extract a word from a string
def extract_word(rest, acc \\ "")
# There's no string left so return
# whatever we accumulated
def extract_word("" = rest, acc) do
{acc, rest}
end
def extract_word(<<char::utf8, rest::binary>>, acc) when char not in [?\s, ?%] do
extract_word(rest, acc <> <<char::utf8>>)
end
# End of word, return the word and the
# rest of the string
def extract_word(rest, acc) do
{acc, rest}
end
end
@kipcole9
Copy link
Author

Some examples:

iex> Extract.words "%abc def"
["abc"]
iex> Extract.words "%abc def%"
["abc", "def"]
iex> Extract.words "%abc def% xyx"
["abc", "def"]
iex> Extract.words "%abc def xyx%" 
["abc", "xyx"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment