View Source Elnom.Strings.Complete (elnom v0.1.0)
Parsers recognising utf-8 strings.
If you're working with binary (non-utf8) data, use Elnom.Bytes.Complete
instead.
Summary
Functions
Matches a string with escaped characters.
Matches a byte string with escaped characters.
Returns the longest substring of the matches the pattern.
Parse till certain characters are met.
Recognizes a pattern
Recognizes a case insensitive pattern.
Returns an input substring containing the first count
utf-8 input characters.
Returns the longest input substring (at least 1) till a predicate is met.
Returns the longest input substring (if any) till a predicate is met.
Returns the non empty input substring up to the first occurrence of the pattern.
Returns the input substring up to the first occurrence of the pattern.
Returns the longest (at least 1) input substring that matches the predicate.
Returns the longest input substring (if any) that matches the predicate.
Returns the longest (min <= len <= max) input substring that matches the predicate.
Functions
Matches a string with escaped characters.
The first argument matches the normal characters (it must not accept the control character) The second argument is the control character (like \ in most languages) The third argument matches the escaped characters
iex> esc = escaped(digit1(), "\\", one_of("rn;"))
iex> esc.("123;")
{:ok, ";", "123"}
iex> esc.("123\\;456;")
{:ok, ";", "123\\;456"}
Matches a byte string with escaped characters.
- The first argument matches the normal characters (it must not match the control character)
- The second argument is the control character (like \ in most languages)
- The third argument matches the escaped characters and transforms them
As an example, the chain abc\tdef
could be abc def
(it also consumes the control character)
iex> esc = escaped_transform(
...> alpha1(),
...> "\\",
...> alt({
...> value("\\", tag("\\")),
...> value("\"", tag("\"")),
...> value("\n", tag("n"))
...> })
...> )
iex> esc.("abc")
{:ok, "", "abc"}
iex> esc.("ab\\\"cd")
{:ok, "", "ab\"cd"}
iex> esc.("ab\\ncd")
{:ok, "", "ab\ncd"}
Returns the longest substring of the matches the pattern.
The parser will return the longest substring consisting of the characters in provided in the combinator’s argument.
It will return an Error if the pattern wasn’t met.
iex> hex = is_a("0123456789ABCDEF")
iex> hex.("123 and voila")
{:ok, " and voila", "123"}
iex> hex.("DEADBEEF and others")
{:ok, " and others", "DEADBEEF"}
iex> hex.("D15EA5E")
{:ok, "", "D15EA5E"}
iex> hex.("other")
{:error, %Error{kind: :is_a, buffer: "other"}}
iex> hex.("")
{:error, %Error{kind: :is_a, buffer: ""}}
Parse till certain characters are met.
The parser will return the longest substring till one of the characters of the combinator’s argument are met.
It doesn’t consume the matched character.
iex> not_space = is_not(" \t\r\n")
iex> not_space.("Hello, World!")
{:ok, " World!", "Hello,"}
iex> not_space.("Sometimes\t")
{:ok, "\t", "Sometimes"}
iex> not_space.("Nospace")
{:ok, "", "Nospace"}
iex> not_space.(" hello")
{:error, %Error{kind: :is_not, buffer: " hello"}}
iex> not_space.("")
{:error, %Error{kind: :is_not, buffer: ""}}
Recognizes a pattern
The input data will be compared to the tag combinator’s argument and will return the part of the input that matches the argument.
It will return an Error if the input doesn’t match the pattern.
iex> tag("hello").("hello there")
{:ok, " there", "hello"}
iex> tag("hello").("bye")
{:error, %Error{kind: :tag, buffer: "bye"}}
iex> tag("").("bye")
{:ok, "bye", ""}
iex> tag("💙").("💙💙")
{:ok, "💙", "💙"}
Recognizes a case insensitive pattern.
The input data will be compared to the tag combinator’s argument and will return the part of the input that matches the argument with no regard to case.
It will return an Error if the input doesn’t match the pattern.
iex> parser = tag_no_case("hello")
iex> parser.("Hello, World!")
{:ok, ", World!", "Hello"}
iex> parser.("hello, World!")
{:ok, ", World!", "hello"}
iex> parser.("HeLlO, World!")
{:ok, ", World!", "HeLlO"}
iex> parser.("Something")
{:error, %Error{kind: :tag, buffer: "Something"}}
iex> parser.("")
{:error, %Error{kind: :tag, buffer: ""}}
Returns an input substring containing the first count
utf-8 input characters.
It will return Error if the input is shorter than the argument.
iex> take6 = take(6)
iex> take6.("1234567")
{:ok, "7", "123456"}
iex> take6.("things")
{:ok, "", "things"}
iex> take6.("short")
{:error, %Error{kind: :eof, buffer: "short"}}
iex> take6.("")
{:error, %Error{kind: :eof, buffer: ""}}
iex> take(1).("💙")
{:ok, "", "💙"}
Returns the longest input substring (at least 1) till a predicate is met.
The parser will return the longest substring till the given predicate (a function that takes the input and returns a bool).
It will return Error if the input is empty or the predicate matches the first input.
iex> till_colon = take_till1(fn char -> char == ?: end)
iex> till_colon.("latin:123")
{:ok, ":123", "latin"}
iex> till_colon.(":empty matched")
{:error, %Error{kind: :take_till1, buffer: ":empty matched"}}
iex> till_colon.("12345")
{:ok, "", "12345"}
iex> till_colon.("")
{:error, %Error{kind: :take_till1, buffer: ""}}
Returns the longest input substring (if any) till a predicate is met.
The parser will return the longest substring till the given predicate (a function that takes the input and returns a bool).
iex> till_colon = take_till(fn char -> char == ?\: end)
iex> till_colon.("latin:123")
{:ok, ":123", "latin"}
iex> till_colon.(":empty matched")
{:ok, ":empty matched", ""}
iex> till_colon.("12345")
{:ok, "", "12345"}
iex> till_colon.("")
{:ok, "", ""}
Returns the non empty input substring up to the first occurrence of the pattern.
It doesn’t consume the pattern. It will return Error if the pattern wasn’t met.
iex> until_eof = take_until1("eof")
iex> until_eof.("hello, worldeof")
{:ok, "eof", "hello, world"}
iex> until_eof.("hello, world")
{:error, %Error{kind: :take_until, buffer: "hello, world"}}
iex> until_eof.("eof")
{:error, %Error{kind: :take_until, buffer: "eof"}}
iex> until_eof.("")
{:error, %Error{kind: :take_until, buffer: ""}}
iex> until_eof.("1eof2eof")
{:ok, "eof2eof", "1"}
Returns the input substring up to the first occurrence of the pattern.
It doesn’t consume the pattern. It will return Error if the pattern wasn’t met.
iex> until_eof = take_until("eof")
iex> until_eof.("hello, worldeof")
{:ok, "eof", "hello, world"}
iex> until_eof.("hello, world")
{:error, %Error{kind: :take_until, buffer: "hello, world"}}
iex> until_eof.("eof")
{:ok, "eof", ""}
iex> until_eof.("")
{:error, %Error{kind: :take_until, buffer: ""}}
iex> until_eof.("1eof2eof")
{:ok, "eof2eof", "1"}
Returns the longest (at least 1) input substring that matches the predicate.
The parser will return the longest substring that matches the given predicate (a function that takes the input and returns a bool).
It will return an Error if the pattern wasn’t met.
iex> alpha = take_while1(&is_alphabetic/1)
iex> alpha.("latin123")
{:ok, "123", "latin"}
iex> alpha.("12345")
{:error, %Error{kind: :take_while1, buffer: "12345"}}
iex> alpha.("latin")
{:ok, "", "latin"}
iex> alpha.("")
{:error, %Error{kind: :take_while1, buffer: ""}}
Returns the longest input substring (if any) that matches the predicate.
The parser will return the longest substring that matches the given predicate (a function that takes the input and returns a bool).
iex> alpha = take_while(&is_alphabetic/1)
iex> alpha.("latin123")
{:ok, "123", "latin"}
iex> alpha.("12345")
{:ok, "12345", ""}
iex> alpha.("latin")
{:ok, "", "latin"}
iex> alpha.("") == {:ok, "", ""}
Returns the longest (min <= len <= max) input substring that matches the predicate.
The parser will return the longest substring that matches the given predicate (a function that takes the input and returns a bool).
It will return an Error if the pattern wasn’t met or is out of range.
iex> short_alpha = take_while_m_n(3, 6, &is_alphabetic/1)
iex> short_alpha.("latin123")
{:ok, "123", "latin"}
iex> short_alpha.("lengthy")
{:ok, "y", "length"}
iex> short_alpha.("latin")
{:ok, "", "latin"}
iex> short_alpha.("ed")
{:error, %Error{kind: :take_while_m_n, buffer: "ed"}}
iex> short_alpha.("12345")
{:error, %Error{kind: :take_while_m_n, buffer: "12345"}}