View Source ExAequoBase.Text (ExAequoBase v0.1.2)

Text based tools

Summary

Functions

Removes a prefix (only if matching), or a given count of graphemes from the front of a string

Parses an input string up to a given string, returnig prefix and suffix

Types

@type atoms() :: [atom()]
@type binaries() :: [binary()]
@type either(lt, rt) :: ok_t(lt) | error_t(rt)
@type error_t() :: {:error, binary()}
@type error_t(t) :: {:error, t}
@type input_source_t() :: Enumerable.t() | binary() | binaries()
@type maybe(t) :: nil | t
@type natural() :: non_neg_integer()
@type numbered(t) :: {t, number()}
@type numbered_line_t() :: numbered(binary())
@type numbered_lines_t() :: [numbered_line_t()]
@type ok_t() :: {:ok, any()}
@type ok_t(t) :: {:ok, t}
@type reducer_result_t() :: {:halt, error_t()} | {:cont, ok_t()}
@type result_fun_t() :: (any() -> result_t())
@type result_fun_t(t) :: (any() -> result_t(t))
@type result_t() :: either(any(), binary())
@type result_t(t) :: either(t, binary())
@type stream_t() ::
  %IO.Stream{device: term(), line_or_bytes: term(), raw: term()}
  | %File.Stream{
      line_or_bytes: term(),
      modes: term(),
      node: term(),
      path: term(),
      raw: term()
    }

Functions

Link to this function

behead(string, by_string_or_length)

View Source
@spec behead(binary(), binary() | natural()) :: binary()

Removes a prefix (only if matching), or a given count of graphemes from the front of a string

iex(1)> behead("abc", "a")
"bc"


iex(2)> behead("abc", 2)
"c"

iex(3)> behead("abc", 0)
"abc"

But

iex(4)> assert_raise(
...(4)>   ExAequoBase.Text.Error,
...(4)>   fn -> behead("abc", "b")
...(4)> end)


iex(5)> assert_raise(
...(5)>   FunctionClauseError,
...(5)>   fn -> behead("abc", -1)
...(5)> end)
Link to this function

parse_up_to(input, delim, option \\ nil)

View Source
@spec parse_up_to(binary(), binary() | Regex.t(), atom()) ::
  maybe({binary(), binary()})

Parses an input string up to a given string, returnig prefix and suffix

iex(6)> parse_up_to("hello world", " ")
{"hello", "world"}

We can also use regular expressions

iex(7)> parse_up_to("hello  world", ~r/\s+/)
{"hello", "world"}

We can decide to keep the string we parse up to

iex(8)> parse_up_to("hello  world", ~r/\s+/, :keep)
{"hello", "  world"}

Or to include it into the macth

iex(9)> parse_up_to("hello  world", ~r/\s+/, :include)
{"hello  ", "world"}