class Bytes

class Bytes

lib/bytes.tya:2

Bytes provides the bytes/Bytes standard library API.

Source
# Bytes provides the bytes/Bytes standard library API.
class Bytes
  # Bytes.value stores instance state.
  # @type Nil
  value: nil

  # Bytes.at returns a byte at a zero-based byte index.
  # @param value String value value.
  # @param index Int index value.
  # @return Any the resulting value.
  static at: value, index ->
    if index.class != Number or index != index.to_i()
      raise error("bytes.at: index must be an integer")
    arr = Self.array_of(value)
    if index < 0 or index >= arr.len()
      raise error("bytes.at: index out of bounds")
    arr[index]

  # Bytes.index_of returns the byte index of byte in a string or bytes value.
  # @param value String value value.
  # @param byte Int byte value.
  # @param start Int start value.
  # @return Any the resulting value.
  static index_of: value, byte, start = 0 ->
    if start == nil
      start = 0
    if start.class != Number or start != start.to_i()
      raise error("bytes.index_of: start must be an integer")
    arr = Self.array_of(value)
    target = Self.byte_of(byte)
    i = start
    if i < 0
      i = 0
    while i < arr.len()
      if arr[i] == target
        return i
      i = i + 1
    nil

  # Bytes.len returns the byte length of a string or bytes value.
  # @param value String value value.
  # @return Int the resulting value.
  static len: value ->
    Self.array_of(value).len()

  # Bytes.array_of returns a byte array for a string or bytes value.
  private static array_of: value ->
    if value.class == String
      return bytes_array(bytes_of(value))
    if serialization_kind(value) == "bytes"
      return bytes_array(value)
    raise error("bytes: value must be string or bytes")

  # Bytes.byte_of normalizes a byte argument.
  private static byte_of: byte ->
    if byte.class == Number
      if byte != byte.to_i() or byte < 0 or byte > 255
        raise error("bytes.index_of: byte must be 0..255")
      return byte.to_i()
    arr = Self.array_of(byte)
    if arr.len() != 1
      raise error("bytes.index_of: byte must be exactly one byte")
    arr[0]

  # Bytes.initialize stores an optional value for byte conversion.
  # @param value String value value.
  # @return Self the initialized object.
  initialize: value = nil ->
    self.value = value

  # Bytes.array is a compatibility alias for Bytes.to_array.
  # @param value String value value.
  # @return Array the resulting value.
  array: value = nil ->
    self.to_array(value)

  # Bytes.concat provides the bytes/Bytes standard library operation.
  # @param left Any left value.
  # @param right Any right value.
  # @return Any the resulting value.
  concat: left, right ->
    bytes_concat(left, right)

  # Bytes.from_array builds bytes from an array of byte values.
  # @param values Array values value.
  # @return Array the resulting value.
  from_array: values = nil ->
    if values == nil
      values = self.value
    bytes(values)

  # Bytes.from_text encodes text as bytes.
  # @param text String text value.
  # @return String the resulting value.
  from_text: text = nil ->
    if text == nil
      text = self.value
    bytes_of(text)

  # Bytes.new is a compatibility alias for Bytes.from_array.
  # @param values Array values value.
  # @return Self the resulting value.
  new: values = nil ->
    self.from_array(values)

  # Bytes.of is a compatibility alias for Bytes.from_text.
  # @param text String text value.
  # @return Any the resulting value.
  of: text = nil ->
    self.from_text(text)

  # Bytes.slice provides the bytes/Bytes standard library operation.
  # @param value String value value.
  # @param start Int start value.
  # @param end Int end value.
  # @return Any the resulting value.
  slice: value, start, end ->
    bytes_slice(value, start, end)

  # Bytes.text is a compatibility alias for Bytes.to_text.
  # @param value String value value.
  # @return String the resulting value.
  text: value = nil ->
    self.to_text(value)

  # Bytes.to_array returns bytes as an array of byte values.
  # @param value String value value.
  # @return Array the resulting value.
  to_array: value = nil ->
    if value == nil
      value = self.value
    bytes_array(value)

  # Bytes.to_text decodes bytes as text.
  # @param value String value value.
  # @return String the resulting value.
  to_text: value = nil ->
    if value == nil
      value = self.value
    bytes_text(value)

Instance Variables

value

Bytes.value

lib/bytes.tya:5

Bytes.value stores instance state.

Source
  # Bytes.value stores instance state.
  # @type Nil
  value: nil

Static Methods

at

static Bytes.at(value, index)

lib/bytes.tya:11

Bytes.at returns a byte at a zero-based byte index.

Source
  # Bytes.at returns a byte at a zero-based byte index.
  # @param value String value value.
  # @param index Int index value.
  # @return Any the resulting value.
  static at: value, index ->
    if index.class != Number or index != index.to_i()
      raise error("bytes.at: index must be an integer")
    arr = Self.array_of(value)
    if index < 0 or index >= arr.len()
      raise error("bytes.at: index out of bounds")
    arr[index]

index_of

static Bytes.index_of(value, byte, start = 0)

lib/bytes.tya:24

Bytes.index_of returns the byte index of byte in a string or bytes value.

Source
  # Bytes.index_of returns the byte index of byte in a string or bytes value.
  # @param value String value value.
  # @param byte Int byte value.
  # @param start Int start value.
  # @return Any the resulting value.
  static index_of: value, byte, start = 0 ->
    if start == nil
      start = 0
    if start.class != Number or start != start.to_i()
      raise error("bytes.index_of: start must be an integer")
    arr = Self.array_of(value)
    target = Self.byte_of(byte)
    i = start
    if i < 0
      i = 0
    while i < arr.len()
      if arr[i] == target
        return i
      i = i + 1
    nil

len

static Bytes.len(value)

lib/bytes.tya:43

Bytes.len returns the byte length of a string or bytes value.

Source
  # Bytes.len returns the byte length of a string or bytes value.
  # @param value String value value.
  # @return Int the resulting value.
  static len: value ->
    Self.array_of(value).len()

Methods

array

Bytes.array(value = nil)

lib/bytes.tya:74

Bytes.array is a compatibility alias for Bytes.to_array.

Source
  # Bytes.array is a compatibility alias for Bytes.to_array.
  # @param value String value value.
  # @return Array the resulting value.
  array: value = nil ->
    self.to_array(value)

concat

Bytes.concat(left, right)

lib/bytes.tya:81

Bytes.concat provides the bytes/Bytes standard library operation.

Source
  # Bytes.concat provides the bytes/Bytes standard library operation.
  # @param left Any left value.
  # @param right Any right value.
  # @return Any the resulting value.
  concat: left, right ->
    bytes_concat(left, right)

from_array

Bytes.from_array(values = nil)

lib/bytes.tya:87

Bytes.from_array builds bytes from an array of byte values.

Source
  # Bytes.from_array builds bytes from an array of byte values.
  # @param values Array values value.
  # @return Array the resulting value.
  from_array: values = nil ->
    if values == nil
      values = self.value
    bytes(values)

from_text

Bytes.from_text(text = nil)

lib/bytes.tya:95

Bytes.from_text encodes text as bytes.

Source
  # Bytes.from_text encodes text as bytes.
  # @param text String text value.
  # @return String the resulting value.
  from_text: text = nil ->
    if text == nil
      text = self.value
    bytes_of(text)

initialize

Bytes.initialize(value = nil)

lib/bytes.tya:68

Bytes.initialize stores an optional value for byte conversion.

Source
  # Bytes.initialize stores an optional value for byte conversion.
  # @param value String value value.
  # @return Self the initialized object.
  initialize: value = nil ->
    self.value = value

new

Bytes.new(values = nil)

lib/bytes.tya:103

Bytes.new is a compatibility alias for Bytes.from_array.

Source
  # Bytes.new is a compatibility alias for Bytes.from_array.
  # @param values Array values value.
  # @return Self the resulting value.
  new: values = nil ->
    self.from_array(values)

of

Bytes.of(text = nil)

lib/bytes.tya:109

Bytes.of is a compatibility alias for Bytes.from_text.

Source
  # Bytes.of is a compatibility alias for Bytes.from_text.
  # @param text String text value.
  # @return Any the resulting value.
  of: text = nil ->
    self.from_text(text)

slice

Bytes.slice(value, start, end)

lib/bytes.tya:117

Bytes.slice provides the bytes/Bytes standard library operation.

Source
  # Bytes.slice provides the bytes/Bytes standard library operation.
  # @param value String value value.
  # @param start Int start value.
  # @param end Int end value.
  # @return Any the resulting value.
  slice: value, start, end ->
    bytes_slice(value, start, end)

text

Bytes.text(value = nil)

lib/bytes.tya:123

Bytes.text is a compatibility alias for Bytes.to_text.

Source
  # Bytes.text is a compatibility alias for Bytes.to_text.
  # @param value String value value.
  # @return String the resulting value.
  text: value = nil ->
    self.to_text(value)

to_array

Bytes.to_array(value = nil)

lib/bytes.tya:129

Bytes.to_array returns bytes as an array of byte values.

Source
  # Bytes.to_array returns bytes as an array of byte values.
  # @param value String value value.
  # @return Array the resulting value.
  to_array: value = nil ->
    if value == nil
      value = self.value
    bytes_array(value)

to_text

Bytes.to_text(value = nil)

lib/bytes.tya:137

Bytes.to_text decodes bytes as text.

Source
  # Bytes.to_text decodes bytes as text.
  # @param value String value value.
  # @return String the resulting value.
  to_text: value = nil ->
    if value == nil
      value = self.value
    bytes_text(value)