class Hex

class Hex

lib/hex.tya:2

Hex provides the hex/Hex standard library API.

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

  # Hex.initialize stores an optional value for hex encoding or decoding.
  # @param value String value value.
  # @return Self the initialized object.
  initialize: value = nil ->
    self.value = value

  # Hex.decode provides the hex/Hex standard library operation.
  # @param text String text value.
  # @return String the resulting value.
  decode: text = nil ->
    if text == nil
      text = self.value
    n = text.byte_len()
    if n % 2 != 0
      raise error("hex.decode: odd-length input")
    arr = []
    i = 0
    while i < n
      hi = self.decode_digit(text[i])
      lo = self.decode_digit(text[i + 1])
      arr.push(hi * 16 + lo)
      i = i + 2
    bytes(arr)

  # Hex.decode_digit provides the hex/Hex standard library operation.
  # @param c Any c value.
  # @return Any the resulting value.
  decode_digit: c ->
    code = ord(c)
    if code >= 48 and code <= 57
      return code - 48
    if code >= 65 and code <= 70
      return code - 55
    if code >= 97 and code <= 102
      return code - 87
    raise error("hex.decode: invalid hex digit")

  # Hex.encode provides the hex/Hex standard library operation.
  # @param value String value value.
  # @return String the resulting value.
  encode: value = nil ->
    if value == nil
      value = self.value
    out = ""
    if value.class != String
      bytes = bytes_array(value)
      n = bytes.len()
      i = 0
      while i < n
        code = bytes[i]
        out = out + self.hex_digit(code / 16.to_i()) + self.hex_digit(code % 16)
        i = i + 1
      return out
    if value.class == String
      n = value.byte_len()
      i = 0
      while i < n
        code = ord(value[i])
        out = out + self.hex_digit(code / 16.to_i()) + self.hex_digit(code % 16)
        i = i + 1
      return out
    raise error("hex.encode: argument must be a string or bytes")

  # Hex.hex_digit provides the hex/Hex standard library operation.
  # @param d Any d value.
  # @return Any the resulting value.
  hex_digit: d ->
    if d < 10
      return chr(48 + d)
    chr(87 + d)

Instance Variables

value

Hex.value

lib/hex.tya:5

Hex.value stores instance state.

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

Methods

decode

Hex.decode(text = nil)

lib/hex.tya:16

Hex.decode provides the hex/Hex standard library operation.

Source
  # Hex.decode provides the hex/Hex standard library operation.
  # @param text String text value.
  # @return String the resulting value.
  decode: text = nil ->
    if text == nil
      text = self.value
    n = text.byte_len()
    if n % 2 != 0
      raise error("hex.decode: odd-length input")
    arr = []
    i = 0
    while i < n
      hi = self.decode_digit(text[i])
      lo = self.decode_digit(text[i + 1])
      arr.push(hi * 16 + lo)
      i = i + 2
    bytes(arr)

decode_digit

Hex.decode_digit(c)

lib/hex.tya:34

Hex.decode_digit provides the hex/Hex standard library operation.

Source
  # Hex.decode_digit provides the hex/Hex standard library operation.
  # @param c Any c value.
  # @return Any the resulting value.
  decode_digit: c ->
    code = ord(c)
    if code >= 48 and code <= 57
      return code - 48
    if code >= 65 and code <= 70
      return code - 55
    if code >= 97 and code <= 102
      return code - 87
    raise error("hex.decode: invalid hex digit")

encode

Hex.encode(value = nil)

lib/hex.tya:47

Hex.encode provides the hex/Hex standard library operation.

Source
  # Hex.encode provides the hex/Hex standard library operation.
  # @param value String value value.
  # @return String the resulting value.
  encode: value = nil ->
    if value == nil
      value = self.value
    out = ""
    if value.class != String
      bytes = bytes_array(value)
      n = bytes.len()
      i = 0
      while i < n
        code = bytes[i]
        out = out + self.hex_digit(code / 16.to_i()) + self.hex_digit(code % 16)
        i = i + 1
      return out
    if value.class == String
      n = value.byte_len()
      i = 0
      while i < n
        code = ord(value[i])
        out = out + self.hex_digit(code / 16.to_i()) + self.hex_digit(code % 16)
        i = i + 1
      return out
    raise error("hex.encode: argument must be a string or bytes")

hex_digit

Hex.hex_digit(d)

lib/hex.tya:73

Hex.hex_digit provides the hex/Hex standard library operation.

Source
  # Hex.hex_digit provides the hex/Hex standard library operation.
  # @param d Any d value.
  # @return Any the resulting value.
  hex_digit: d ->
    if d < 10
      return chr(48 + d)
    chr(87 + d)

initialize

Hex.initialize(value = nil)

lib/hex.tya:10

Hex.initialize stores an optional value for hex encoding or decoding.

Source
  # Hex.initialize stores an optional value for hex encoding or decoding.
  # @param value String value value.
  # @return Self the initialized object.
  initialize: value = nil ->
    self.value = value