View Source Elnom.Number (elnom v0.1.0)

Parsers recognizing numbers in binary and numeric-string formats

Summary

Functions

Recognizes a big endian 4 bytes floating point number.

Recognizes a big endian 8 bytes floating point number.

Recognizes an signed 2 byte integer in big endian format.

Recognizes an signed 3 byte integer in big endian format.

Recognizes an signed 4 byte integer in big endian format.

Recognizes an signed 8 byte integer in big endian format.

Recognizes an signed 16 byte integer in big endian format.

Recognizes an unsigned 2 byte integer in big endian format.

Recognizes an unsigned 3 byte integer in big endian format.

Recognizes an unsigned 4 byte integer in big endian format.

Recognizes an unsigned 8 byte integer in big endian format.

Recognizes an unsigned 16 byte integer in big endian format.

Recognizes floating point number in text format and returns a float.

Parse a 32-bit float as big endian if the argument is :big and little endian if :little.

Parse a 64-bit double as big endian if the argument is :big and little endian if :little.

Elixir does not have a float type, so this function aliases to double/0.

Parses a hex-encoded integer.

Recognizes a signed 1 byte integer.

Parse a i16 integer as big endian if the argument is :big and little endian if :little.

Parse a i24 integer as big endian if the argument is :big and little endian if :little.

Parse a i32 integer as big endian if the argument is :big and little endian if :little.

Parse a i64 integer as big endian if the argument is :big and little endian if :little.

Parse a i128 integer as big endian if the argument is :big and little endian if :little.

Recognizes a little endian 4 bytes floating point number.

Recognizes a little endian 8 bytes floating point number.

Recognizes an signed 2 byte integer in little endian format.

Recognizes an signed 3 byte integer in little endian format.

Recognizes an signed 4 byte integer in little endian format.

Recognizes an signed 8 byte integer in little endian format.

Recognizes an signed 16 byte integer in little endian format.

Recognizes an unsigned 2 byte integer in little endian format.

Recognizes an unsigned 3 byte integer in little endian format.

Recognizes an unsigned 4 byte integer in little endian format.

Recognizes an unsigned 8 byte integer in little endian format.

Recognizes an unsigned 16 byte integer in little endian format.

Recognizes floating point number in a byte string and returns the corresponding string.

Recognizes an unsigned 1 byte integer.

Parse a u16 integer as big endian if the argument is :big and little endian if :little.

Parse a u24 integer as big endian if the argument is :big and little endian if :little.

Parse a u32 integer as big endian if the argument is :big and little endian if :little.

Parse a u64 integer as big endian if the argument is :big and little endian if :little.

Parse a u128 integer as big endian if the argument is :big and little endian if :little.

Functions

Recognizes a big endian 4 bytes floating point number.

iex> be_f32().(<<0x41, 0x48, 0x00, 0x00>>)
{:ok, "", 12.5}

iex> be_f32().("abc")
{:error, %Elnom.Error{buffer: "abc", kind: :eof}}

Recognizes a big endian 8 bytes floating point number.

iex> be_f64().(<<0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00>>)
{:ok, "", 12.5}

iex> be_f64().("abcdefg")
{:error, %Elnom.Error{buffer: "abcdefg", kind: :eof}}

Recognizes an signed 2 byte integer in big endian format.

iex> be_i16().(<<1, 3, "abcefg">>)
{:ok, "abcefg", 0x0103}

iex> be_i16().(<<0>>)
{:error, %Elnom.Error{buffer: <<0>>, kind: :eof}}

Recognizes an signed 3 byte integer in big endian format.

iex> be_i24().(<<1, 3, 5, "abcefg">>)
{:ok, "abcefg", 0x010305}

iex> be_i24().(<<0, 1>>)
{:error, %Elnom.Error{buffer: <<0, 1>>, kind: :eof}}

Recognizes an signed 4 byte integer in big endian format.

iex> be_i32().(<<1, 3, 5, 7, "abcefg">>)
{:ok, "abcefg", 0x01030507}

iex> be_i32().(<<0, 1, 2>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2>>, kind: :eof}}

Recognizes an signed 8 byte integer in big endian format.

iex> be_i64().(<<1, 3, 5, 7, 9, 11, 13, 15, "abcefg">>)
{:ok, "abcefg", 0x01030507090b0d0f}

iex> be_i64().(<<0, 1, 2, 3, 4, 5, 6>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6>>, kind: :eof}}

Recognizes an signed 16 byte integer in big endian format.

iex> be_i128().(<<1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, "abcefg">>)
{:ok, "abcefg", 0x01030507090b0d0f11131517191b1d1f}

iex> be_i128().(<<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>, kind: :eof}}

Recognizes an unsigned 2 byte integer in big endian format.

iex> be_u16().(<<1, 3, "abcefg">>)
{:ok, "abcefg", 0x0103}

iex> be_u16().(<<0>>)
{:error, %Elnom.Error{buffer: <<0>>, kind: :eof}}

Recognizes an unsigned 3 byte integer in big endian format.

iex> be_u24().(<<1, 3, 5, "abcefg">>)
{:ok, "abcefg", 0x010305}

iex> be_u24().(<<0, 1>>)
{:error, %Elnom.Error{buffer: <<0, 1>>, kind: :eof}}

Recognizes an unsigned 4 byte integer in big endian format.

iex> be_u32().(<<1, 3, 5, 7, "abcefg">>)
{:ok, "abcefg", 0x01030507}

iex> be_u32().(<<0, 1, 2>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2>>, kind: :eof}}

Recognizes an unsigned 8 byte integer in big endian format.

iex> be_u64().(<<1, 3, 5, 7, 9, 11, 13, 15, "abcefg">>)
{:ok, "abcefg", 0x01030507090b0d0f}

iex> be_u64().(<<0, 1, 2, 3, 4, 5, 6>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6>>, kind: :eof}}

Recognizes an unsigned 16 byte integer in big endian format.

iex> be_u128().(<<1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, "abcefg">>)
{:ok, "abcefg", 0x01030507090b0d0f11131517191b1d1f}

iex> be_u128().(<<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>, kind: :eof}}

Recognizes floating point number in text format and returns a float.

iex> float().("11e-1")
{:ok, "", 1.1}

iex> float().("123E-02")
{:ok, "", 1.23}

iex> float().("123K-01")
{:ok, "K-01", 123.0}

iex> float().("abc")
{:error, %Elnom.Error{buffer: "abc", kind: :float}}

Parse a 32-bit float as big endian if the argument is :big and little endian if :little.

Parse a 64-bit double as big endian if the argument is :big and little endian if :little.

Elixir does not have a float type, so this function aliases to double/0.

Parses a hex-encoded integer.

iex> hex_u32().("01AE")
{:ok, "", 0x01AE}

iex> hex_u32().("abc")
{:ok, "", 0x0ABC}

iex> hex_u32().("012345678")
{:ok, "8", 0x01234567}

iex> hex_u32().("ggg")
{:error, %Elnom.Error{buffer: "ggg", kind: :take_while_m_n}}

Recognizes a signed 1 byte integer.

iex> i8().(<<0, 3, "abcefg">>)
{:ok, <<3, "abcefg">>, 0}

iex> i8().(<<255, 3, "abcefg">>)
{:ok, <<3, "abcefg">>, -1}

iex> i8().("")
{:error, %Elnom.Error{buffer: "", kind: :eof}}

Parse a i16 integer as big endian if the argument is :big and little endian if :little.

Parse a i24 integer as big endian if the argument is :big and little endian if :little.

Parse a i32 integer as big endian if the argument is :big and little endian if :little.

Parse a i64 integer as big endian if the argument is :big and little endian if :little.

Parse a i128 integer as big endian if the argument is :big and little endian if :little.

Recognizes a little endian 4 bytes floating point number.

iex> le_f32().(<<0x00, 0x00, 0x48, 0x41>>)
{:ok, "", 12.5}

iex> le_f32().("abc")
{:error, %Elnom.Error{buffer: "abc", kind: :eof}}

Recognizes a little endian 8 bytes floating point number.

iex> le_f64().(<<0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40>>)
{:ok, "", 12.5}

iex> le_f64().("abcdefg")
{:error, %Elnom.Error{buffer: "abcdefg", kind: :eof}}

Recognizes an signed 2 byte integer in little endian format.

iex> le_i16().(<<1, 3, "abcefg">>)
{:ok, "abcefg", 0x0301}

iex> le_i16().(<<0>>)
{:error, %Elnom.Error{buffer: <<0>>, kind: :eof}}

Recognizes an signed 3 byte integer in little endian format.

iex> le_i24().(<<1, 3, 5, "abcefg">>)
{:ok, "abcefg", 0x050301}

iex> le_i24().(<<0, 1>>)
{:error, %Elnom.Error{buffer: <<0, 1>>, kind: :eof}}

Recognizes an signed 4 byte integer in little endian format.

iex> le_i32().(<<1, 3, 5, 7, "abcefg">>)
{:ok, "abcefg", 0x07050301}

iex> le_i32().(<<0, 1, 2>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2>>, kind: :eof}}

Recognizes an signed 8 byte integer in little endian format.

iex> le_i64().(<<1, 3, 5, 7, 9, 11, 13, 15, "abcefg">>)
{:ok, "abcefg", 0x0f0d0b0907050301}

iex> le_i64().(<<0, 1, 2, 3, 4, 5, 6>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6>>, kind: :eof}}

Recognizes an signed 16 byte integer in little endian format.

iex> le_i128().(<<1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, "abcefg">>)
{:ok, "abcefg", 0x1f1d1b19171513110f0d0b0907050301}

iex> le_i128().(<<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>, kind: :eof}}

Recognizes an unsigned 2 byte integer in little endian format.

iex> le_u16().(<<1, 3, "abcefg">>)
{:ok, "abcefg", 0x0301}

iex> le_u16().(<<0>>)
{:error, %Elnom.Error{buffer: <<0>>, kind: :eof}}

Recognizes an unsigned 3 byte integer in little endian format.

iex> le_u24().(<<1, 3, 5, "abcefg">>)
{:ok, "abcefg", 0x050301}

iex> le_u24().(<<0, 1>>)
{:error, %Elnom.Error{buffer: <<0, 1>>, kind: :eof}}

Recognizes an unsigned 4 byte integer in little endian format.

iex> le_u32().(<<1, 3, 5, 7, "abcefg">>)
{:ok, "abcefg", 0x07050301}

iex> le_u32().(<<0, 1, 2>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2>>, kind: :eof}}

Recognizes an unsigned 8 byte integer in little endian format.

iex> le_u64().(<<1, 3, 5, 7, 9, 11, 13, 15, "abcefg">>)
{:ok, "abcefg", 0x0f0d0b0907050301}

iex> le_u64().(<<0, 1, 2, 3, 4, 5, 6>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6>>, kind: :eof}}

Recognizes an unsigned 16 byte integer in little endian format.

iex> le_u128().(<<1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, "abcefg">>)
{:ok, "abcefg", 0x1f1d1b19171513110f0d0b0907050301}

iex> le_u128().(<<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>)
{:error, %Elnom.Error{buffer: <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14>>, kind: :eof}}

Recognizes floating point number in a byte string and returns the corresponding string.

iex> recognize_float().("11e-1")
{:ok, "", "11e-1"}

iex> recognize_float().("123E-02")
{:ok, "", "123E-02"}

iex> recognize_float().("123K-01")
{:ok, "K-01", "123"}

iex> recognize_float().("abc")
{:error, %Elnom.Error{buffer: "abc", kind: :float}}

Recognizes an unsigned 1 byte integer.

iex> u8().(<<0, 3, "abcefg">>)
{:ok, <<3, "abcefg">>, 0}

iex> u8().("")
{:error, %Elnom.Error{buffer: "", kind: :eof}}

Parse a u16 integer as big endian if the argument is :big and little endian if :little.

Parse a u24 integer as big endian if the argument is :big and little endian if :little.

Parse a u32 integer as big endian if the argument is :big and little endian if :little.

Parse a u64 integer as big endian if the argument is :big and little endian if :little.

Parse a u128 integer as big endian if the argument is :big and little endian if :little.