LeXtract.CharInterval (lextract v0.1.2)

View Source

Represents a character position interval in text.

Fields

  • :start_pos - Starting character position (0-based, inclusive)
  • :end_pos - Ending character position (0-based, exclusive)

Examples

iex> interval = %LeXtract.CharInterval{start_pos: 0, end_pos: 5}
iex> LeXtract.CharInterval.length(interval)
5

Summary

Functions

Extracts text from a string using this interval.

Returns the length of the interval.

Creates a new character interval.

Checks if two intervals overlap.

Types

t()

@type t() :: %LeXtract.CharInterval{
  end_pos: non_neg_integer(),
  start_pos: non_neg_integer()
}

Functions

extract(text, char_interval)

@spec extract(String.t(), t()) :: String.t()

Extracts text from a string using this interval.

Examples

iex> interval = LeXtract.CharInterval.new(0, 5)
iex> LeXtract.CharInterval.extract("Hello, world!", interval)
"Hello"

length(char_interval)

@spec length(t()) :: non_neg_integer()

Returns the length of the interval.

Examples

iex> interval = LeXtract.CharInterval.new(10, 20)
iex> LeXtract.CharInterval.length(interval)
10

new(start_pos, end_pos)

@spec new(non_neg_integer(), non_neg_integer()) :: t()

Creates a new character interval.

Examples

iex> LeXtract.CharInterval.new(0, 10)
%LeXtract.CharInterval{start_pos: 0, end_pos: 10}

overlaps?(char_interval1, char_interval2)

@spec overlaps?(t(), t()) :: boolean()

Checks if two intervals overlap.

Examples

iex> i1 = LeXtract.CharInterval.new(0, 5)
iex> i2 = LeXtract.CharInterval.new(3, 8)
iex> LeXtract.CharInterval.overlaps?(i1, i2)
true

iex> i1 = LeXtract.CharInterval.new(0, 5)
iex> i2 = LeXtract.CharInterval.new(5, 10)
iex> LeXtract.CharInterval.overlaps?(i1, i2)
false