PassiveSupport.String (passive_support v0.8.4)
Helper functions for working with strings and UTF-8 binary data.
Link to this section Summary
Functions
Returns a copy of string
with a newline removed from the end.
Splits a string by a given length or lengths.
Converts the provided pattern to a regular expression, if necessary,
and then invokes Regex.run
on the expression and the string.
Safely casts the string to an atom, returning {:ok, atom}
if successful
and :error
if not.
Converts the provided pattern to a regular expression, if necessary,
and then invokes Regex.scan
on the expression and the string.
Link to this section Functions
chomp(string)
Specs
Returns a copy of string
with a newline removed from the end.
If there is no newline at the end of string
, then it is returned unchanged
Examples
iex> chomp("hello world!\n")
"hello world!"
iex> chomp("hello\nworld!")
"hello\nworld!"
iex> chomp("multiline!\n\n")
"multiline!\n"
iex> chomp("single line!")
"single line!"
length_split(string, lengths, opts \\ [first_split: false])
Specs
length_split(String.t(), integer() | [integer()], [{:first_split, boolean()}]) :: String.t() | [String.t()] | [[String.t()]]
Splits a string by a given length or lengths.
When one length is given, splits the string into a list of substrings of that length.
When a list of lengths is given, returns a list of lists of substrings of the given lengths.
If the string does not fit within the given length(s), the final substring will be the length of the remainder of the string.
To retrieve only the first length
or lengths
of the string,
pass first_split: true
. Note that in the case of a single length
,
this is equivalent to calling String.slice(string, 0..length)
, or
binary_part(string, 0, length)
. This is useful when, while supplying
multiple lengths, only the first lengths
of the given string are important
to the program, or when the sum of lengths
is equal to the length
of the original string.
Examples
iex> length_split("hello world!", 3)
["hel", "lo ", "wor", "ld!"]
iex> length_split("hello world!", 5)
["hello", " worl", "d!"]
iex> length_split("hello world!", 5, first_split: true)
"hello"
iex> length_split("Life, the universe, and everything... is pattern-matchable", [10, 9, 7])
[
["Life, the ", "universe,", " and ev"],
["erything..", ". is patt", "ern-mat"],
["chable"]
]
iex> length_split("Life, the universe, and everything... is pattern-matchable", [10, 9, 7], first_split: true)
["Life, the ", "universe,", " and ev"]
match(string, pattern, opts \\ [])
Specs
Converts the provided pattern to a regular expression, if necessary,
and then invokes Regex.run
on the expression and the string.
Useful for invoking regular expressions on strings in the middle of transformation pipelines.
Examples
iex> match("footwear, fun, and fondue", "((f[ou])[no]).+")
["footwear, fun, and fondue", "foo", "fo"]
iex> match("fööd!", "öö")
["öö"]
iex> match("footwear, fun, and fondue", ~r/((f[ou])[no]).+/U)
["foot", "foo", "fo"]
safe_existing_atom(arg)
Specs
Safely casts the string to an atom, returning {:ok, atom}
if successful
and :error
if not.
Examples
iex> safe_existing_atom("ok")
{:ok, :ok}
iex> safe_existing_atom("not_particularly_ok")
:error
scan(string, pattern, opts \\ [])
Specs
Converts the provided pattern to a regular expression, if necessary,
and then invokes Regex.scan
on the expression and the string.
Useful for invoking regular expressions on strings in the middle of transformation pipelines.
Examples
iex> scan("footwear, fun, and fondue", "((f[ou])[no]).+")
[["footwear, fun, and fondue", "foo", "fo"]]
iex> scan("fööd!", "öö")
[["öö"]]
iex> scan("footwear, fun, and fondue", ~r/((f[ou])[no]).+/U)
[["foot", "foo", "fo"], ["fun,", "fun", "fu"], ["fond", "fon", "fo"]]