View Source EDTF.Parser.Helpers (EDTF v1.3.0)

Helper functions for parsing EDTF dates

Summary

Functions

Apply a parsed qualifier to a single value.

Apply a parsed sign to a parsed integer value.

Calculate the appropriate qualifier bitmasks for a given YYYY, MM, or DD. Bits are calculated from the left and shifted left to account for the specific component.

Reduce a list of components and bitmasks to a single list of values with their mask attributes ORed together.

Convert a parsed numeric bitstring to an integer

Functions

apply_qualifier(rest, value, context, line, offset)

Apply a parsed qualifier to a single value.

Example:

  iex> apply_qualifier("", [value: 2000, qualifier: ~c"%"], %{}, nil, nil)
  {"", [attributes: [approximate: true, uncertain: true], value: 2000], %{}}

  iex> apply_qualifier("", [value: 2000], %{}, nil, nil)
  {"", [attributes: [], value: 2000], %{}}

apply_sign(rest, value, context, line, offset)

Apply a parsed sign to a parsed integer value.

Example:

  iex> apply_sign("", [value: 2000, sign: ~c"-"], %{}, nil, nil)
  {"", [value: -2000], %{}}

  iex> apply_sign("", [value: 2000], %{}, nil, nil)
  {"", [value: 2000], %{}}

bitmask(bitstring, sign, arg3, shift)

bitmask(rest, value, context, line, offset, shift)

Calculate the appropriate qualifier bitmasks for a given YYYY, MM, or DD. Bits are calculated from the left and shifted left to account for the specific component.

  • The digits of YYYY are 1, 2, 4, 8
  • The digits of MM are 16, 32
  • The digits of DD are 64, 128

A full component qualifier (leading ~, ?, or %) results in the component being fully masked (15 for year, 48 for month, or 192 for day). Unspecified digits (X) flip individual bits.

A pre-component qualifier results in only that component being masked. A post- component qualifier results in that component plus all components to the left being masked.

Example:

  iex> bitmask("-%02", [value: ~c"200X", sign: ~c"-"], %{}, nil, nil, 0)
  {"-%02", [[value: -2000, attributes: [unspecified: 8]]], %{}}

  iex> bitmask("", [value: ~c"02", pre_qualifier: ~c"%"], %{}, nil, nil, 4)
  {"", [[value: 2, attributes: [approximate: 48, uncertain: 48]]], %{}}

  iex> bitmask("", [value: ~c"02", post_qualifier: ~c"%"], %{}, nil, nil, 4)
  {"", [[value: 2, attributes: [approximate: 63, uncertain: 63]]], %{}}

reduce(rest, values, context, line, offset)

Reduce a list of components and bitmasks to a single list of values with their mask attributes ORed together.

Example:

  iex> reduce("", [
  ...>   [value: 10, attributes: [unspecified: 128]],
  ...>   [value: 1, attributes: [unspecified: 32]],
  ...>   [value: 0, attributes: [unspecified: 5]]
  ...> ], %{}, nil, nil)
  {"", [values: [0, 1, 10], attributes: [unspecified: 165]], %{}}

  iex> reduce("", [
  ...>   [value: 10, attributes: [unspecified: 128]],
  ...>   [value: 1, attributes: [approximate: 48]],
  ...>   [value: 0, attributes: [approximate: 15, uncertain: 15]]
  ...> ], %{}, nil, nil)
  {"", [values: [0, 1, 10], attributes: [unspecified: 128, approximate: 63, uncertain: 15]], %{}}

to_integer(rest, value, context, line, offset)

Convert a parsed numeric bitstring to an integer

Example:

  iex> to_integer("", ~c"4321", %{}, nil, nil)
  {"", [1234], %{}}