class Address

class Address

lib/net/ip/address.tya:2

Address provides the net/ip/Address standard library API.

Source
# Address provides the net/ip/Address standard library API.
class Address
  # Address.bytes stores instance state.
  # @type Nil
  bytes: nil

  # Address.groups stores instance state.
  # @type Nil
  groups: nil

  # Address.mapped stores instance state.
  # @type Nil
  mapped: nil

  # Address.version stores instance state.
  # @type Nil
  version: nil

  # Address.initialize provides the net/ip/Address standard library operation.
  # @param version Any version value.
  # @param bytes Array bytes value.
  # @param groups Any groups value.
  # @param mapped Any mapped value.
  # @return Self the initialized object.
  initialize: version = nil, bytes = nil, groups = nil, mapped = false ->
    self.version = version
    self.bytes = bytes
    self.groups = groups
    self.mapped = mapped

  # Address.decimal? provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Boolean whether the condition is true.
  decimal?: text ->
    i = 0
    while i < text.byte_len()
      c = ord(text[i])
      if c < 48 or c > 57
        return false
      i = i + 1
    true

  # Address.format_v6 provides the net/ip/Address standard library operation.
  # @param groups Any groups value.
  # @param mapped Any mapped value.
  # @return Any the resulting value.
  format_v6: groups, mapped ->
    if (
      mapped and groups[0] == 0 and groups[1] == 0 and groups[2] == 0 and groups[3] == 0 and groups[4] == 0 and groups[5] == 65535
    )
      b1 = groups[6] / 256.to_i()
      b2 = groups[6] % 256
      b3 = groups[7] / 256.to_i()
      b4 = groups[7] % 256
      return "::ffff:" + b1.to_s() + "." + b2.to_s() + "." + b3.to_s() + "." + b4.to_s()
    run = self.zero_run(groups)
    out = ""
    i = 0
    while i < 8
      if run["len"] > 1 and i == run["start"]
        if out == ""
          out = "::"
        else
          out = out + "::"
        i = i + run["len"]
        if i >= 8
          return out
      else
        if out != "" and not out.ends_with(":")
          out = out + ":"
        out = out + self.int_to_hex(groups[i])
        i = i + 1
    out

  # Address.hex_digit provides the net/ip/Address standard library operation.
  # @param c Any c value.
  # @return Any the resulting value.
  hex_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("ip.Address.parse: invalid IPv6 address")

  # Address.hex_to_int provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  hex_to_int: text ->
    n = 0
    i = 0
    while i < text.byte_len()
      n = n * 16 + self.hex_digit(text[i])
      i = i + 1
    n

  # Address.int_to_hex provides the net/ip/Address standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  int_to_hex: n ->
    digits = "0123456789abcdef"
    if n == 0
      return "0"
    out = ""
    value = n
    while value > 0
      out = digits[value % 16] + out
      value = value / 16.to_i()
    out

  # Address.last_index provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @param needle Any needle value.
  # @return Int the resulting value.
  last_index: text, needle ->
    i = text.byte_len() - 1
    while i >= 0
      if text[i] == needle
        return i
      i = i - 1
    -1

  # Address.loopback? provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Boolean whether the condition is true.
  loopback?: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      return addr.bytes[0] == 127
    groups = addr.groups
    i = 0
    while i < 7
      if groups[i] != 0
        return false
      i = i + 1
    groups[7] == 1

  # Address.parse provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse: text ->
    if text.contains(":")
      return self.parse_v6(text)
    self.parse_v4(text)

  # Address.parse_v4 provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse_v4: text ->
    parts = text.split(".")
    if parts.len() != 4
      raise error("ip.Address.parse: invalid IPv4 address")
    bytes = []
    for part in parts
      if part == "" or not self.decimal?(part)
        raise error("ip.Address.parse: invalid IPv4 address")
      n = part.to_i()
      if n < 0 or n > 255
        raise error("ip.Address.parse: invalid IPv4 address")
      if n.to_s() != part and not (n == 0 and part == "0")
        raise error("ip.Address.parse: invalid IPv4 address")
      bytes.push(n)
    Address(4, bytes, nil, false)

  # Address.parse_v6 provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse_v6: text ->
    if text == ""
      raise error("ip.Address.parse: invalid IPv6 address")
    mapped = false
    ipv4_groups = []
    if text.contains(".")
      last_colon = self.last_index(text, ":")
      if last_colon < 0
        raise error("ip.Address.parse: invalid IPv6 address")
      ipv4_text = text.slice(last_colon + 1, text.len())
      ipv4 = self.parse_v4(ipv4_text)
      b = ipv4.bytes
      ipv4_groups = [b[0] * 256 + b[1], b[2] * 256 + b[3]]
      text = text.slice(0, last_colon)
      mapped = true
    parts = text.split("::")
    if parts.len() > 2
      raise error("ip.Address.parse: invalid IPv6 address")
    left = []
    right = []
    if parts[0] != ""
      left = self.parse_v6_side(parts[0])
    if parts.len() == 2 and parts[1] != ""
      right = self.parse_v6_side(parts[1])
    for g in ipv4_groups
      right.push(g)
    groups = []
    if parts.len() == 1
      if left.len() + ipv4_groups.len() != 8
        raise error("ip.Address.parse: invalid IPv6 address")
      groups = left
      for g in ipv4_groups
        groups.push(g)
    else
      zeros = 8 - left.len() - right.len()
      if zeros < 1
        raise error("ip.Address.parse: invalid IPv6 address")
      for g in left
        groups.push(g)
      i = 0
      while i < zeros
        groups.push(0)
        i = i + 1
      for g in right
        groups.push(g)
    if groups.len() != 8
      raise error("ip.Address.parse: invalid IPv6 address")
    Address(6, nil, groups, mapped)

  # Address.parse_v6_side provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse_v6_side: text ->
    parts = text.split(":")
    out = []
    for part in parts
      if part == "" or part.byte_len() > 4
        raise error("ip.Address.parse: invalid IPv6 address")
      out.push(self.hex_to_int(part))
    out

  # Address.private? provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Boolean whether the condition is true.
  private_address?: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      b = addr.bytes
      if b[0] == 10
        return true
      if b[0] == 172 and b[1] >= 16 and b[1] <= 31
        return true
      if b[0] == 192 and b[1] == 168
        return true
      return false
    g0 = addr.groups[0]
    g0 >= 64512 and g0 <= 65023

  # Address.to_s provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return String the resulting value.
  to_s: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      parts = []
      for b in addr.bytes
        parts.push(b.to_s())
      return parts.join(".")
    self.format_v6(addr.groups, addr.mapped)

  # Address.unspecified? provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Boolean whether the condition is true.
  unspecified?: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      return addr.bytes[0] == 0 and addr.bytes[1] == 0 and addr.bytes[2] == 0 and addr.bytes[3] == 0
    for g in addr.groups
      if g != 0
        return false
    true

  # Address.valid? provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Boolean whether the condition is true.
  valid?: text ->
    try
      self.parse(text)
      return true
    catch _
      return false

  # Address.version provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Any the resulting value.
  version_of: addr = nil ->
    if addr == nil
      addr = self
    addr.version

  # Address.zero_run provides the net/ip/Address standard library operation.
  # @param groups Any groups value.
  # @return Any the resulting value.
  zero_run: groups ->
    best_start = -1
    best_len = 0
    i = 0
    while i < groups.len()
      if groups[i] == 0
        start = i
        while i < groups.len() and groups[i] == 0
          i = i + 1
        n = i - start
        if n > best_len
          best_start = start
          best_len = n
      else
        i = i + 1
    { start: best_start, len: best_len }

Instance Variables

bytes

Address.bytes

lib/net/ip/address.tya:5

Address.bytes stores instance state.

Source
  # Address.bytes stores instance state.
  # @type Nil
  bytes: nil

groups

Address.groups

lib/net/ip/address.tya:9

Address.groups stores instance state.

Source
  # Address.groups stores instance state.
  # @type Nil
  groups: nil

mapped

Address.mapped

lib/net/ip/address.tya:13

Address.mapped stores instance state.

Source
  # Address.mapped stores instance state.
  # @type Nil
  mapped: nil

version

Address.version

lib/net/ip/address.tya:17

Address.version stores instance state.

Source
  # Address.version stores instance state.
  # @type Nil
  version: nil

Methods

decimal?

Address.decimal?(text)

lib/net/ip/address.tya:34

Address.decimal? provides the net/ip/Address standard library operation.

Source
  # Address.decimal? provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Boolean whether the condition is true.
  decimal?: text ->
    i = 0
    while i < text.byte_len()
      c = ord(text[i])
      if c < 48 or c > 57
        return false
      i = i + 1
    true

format_v6

Address.format_v6(groups, mapped)

lib/net/ip/address.tya:47

Address.format_v6 provides the net/ip/Address standard library operation.

Source
  # Address.format_v6 provides the net/ip/Address standard library operation.
  # @param groups Any groups value.
  # @param mapped Any mapped value.
  # @return Any the resulting value.
  format_v6: groups, mapped ->
    if (
      mapped and groups[0] == 0 and groups[1] == 0 and groups[2] == 0 and groups[3] == 0 and groups[4] == 0 and groups[5] == 65535
    )
      b1 = groups[6] / 256.to_i()
      b2 = groups[6] % 256
      b3 = groups[7] / 256.to_i()
      b4 = groups[7] % 256
      return "::ffff:" + b1.to_s() + "." + b2.to_s() + "." + b3.to_s() + "." + b4.to_s()
    run = self.zero_run(groups)
    out = ""
    i = 0
    while i < 8
      if run["len"] > 1 and i == run["start"]
        if out == ""
          out = "::"
        else
          out = out + "::"
        i = i + run["len"]
        if i >= 8
          return out
      else
        if out != "" and not out.ends_with(":")
          out = out + ":"
        out = out + self.int_to_hex(groups[i])
        i = i + 1
    out

hex_digit

Address.hex_digit(c)

lib/net/ip/address.tya:78

Address.hex_digit provides the net/ip/Address standard library operation.

Source
  # Address.hex_digit provides the net/ip/Address standard library operation.
  # @param c Any c value.
  # @return Any the resulting value.
  hex_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("ip.Address.parse: invalid IPv6 address")

hex_to_int

Address.hex_to_int(text)

lib/net/ip/address.tya:91

Address.hex_to_int provides the net/ip/Address standard library operation.

Source
  # Address.hex_to_int provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  hex_to_int: text ->
    n = 0
    i = 0
    while i < text.byte_len()
      n = n * 16 + self.hex_digit(text[i])
      i = i + 1
    n

initialize

Address.initialize(version = nil, bytes = nil, groups = nil, mapped = false)

lib/net/ip/address.tya:25

Address.initialize provides the net/ip/Address standard library operation.

Source
  # Address.initialize provides the net/ip/Address standard library operation.
  # @param version Any version value.
  # @param bytes Array bytes value.
  # @param groups Any groups value.
  # @param mapped Any mapped value.
  # @return Self the initialized object.
  initialize: version = nil, bytes = nil, groups = nil, mapped = false ->
    self.version = version
    self.bytes = bytes
    self.groups = groups
    self.mapped = mapped

int_to_hex

Address.int_to_hex(n)

lib/net/ip/address.tya:102

Address.int_to_hex provides the net/ip/Address standard library operation.

Source
  # Address.int_to_hex provides the net/ip/Address standard library operation.
  # @param n Int n value.
  # @return Any the resulting value.
  int_to_hex: n ->
    digits = "0123456789abcdef"
    if n == 0
      return "0"
    out = ""
    value = n
    while value > 0
      out = digits[value % 16] + out
      value = value / 16.to_i()
    out

last_index

Address.last_index(text, needle)

lib/net/ip/address.tya:117

Address.last_index provides the net/ip/Address standard library operation.

Source
  # Address.last_index provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @param needle Any needle value.
  # @return Int the resulting value.
  last_index: text, needle ->
    i = text.byte_len() - 1
    while i >= 0
      if text[i] == needle
        return i
      i = i - 1
    -1

loopback?

Address.loopback?(addr = nil)

lib/net/ip/address.tya:128

Address.loopback? provides the net/ip/Address standard library operation.

Source
  # Address.loopback? provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Boolean whether the condition is true.
  loopback?: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      return addr.bytes[0] == 127
    groups = addr.groups
    i = 0
    while i < 7
      if groups[i] != 0
        return false
      i = i + 1
    groups[7] == 1

parse

Address.parse(text)

lib/net/ip/address.tya:144

Address.parse provides the net/ip/Address standard library operation.

Source
  # Address.parse provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse: text ->
    if text.contains(":")
      return self.parse_v6(text)
    self.parse_v4(text)

parse_v4

Address.parse_v4(text)

lib/net/ip/address.tya:152

Address.parse_v4 provides the net/ip/Address standard library operation.

Source
  # Address.parse_v4 provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse_v4: text ->
    parts = text.split(".")
    if parts.len() != 4
      raise error("ip.Address.parse: invalid IPv4 address")
    bytes = []
    for part in parts
      if part == "" or not self.decimal?(part)
        raise error("ip.Address.parse: invalid IPv4 address")
      n = part.to_i()
      if n < 0 or n > 255
        raise error("ip.Address.parse: invalid IPv4 address")
      if n.to_s() != part and not (n == 0 and part == "0")
        raise error("ip.Address.parse: invalid IPv4 address")
      bytes.push(n)
    Address(4, bytes, nil, false)

parse_v6

Address.parse_v6(text)

lib/net/ip/address.tya:171

Address.parse_v6 provides the net/ip/Address standard library operation.

Source
  # Address.parse_v6 provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse_v6: text ->
    if text == ""
      raise error("ip.Address.parse: invalid IPv6 address")
    mapped = false
    ipv4_groups = []
    if text.contains(".")
      last_colon = self.last_index(text, ":")
      if last_colon < 0
        raise error("ip.Address.parse: invalid IPv6 address")
      ipv4_text = text.slice(last_colon + 1, text.len())
      ipv4 = self.parse_v4(ipv4_text)
      b = ipv4.bytes
      ipv4_groups = [b[0] * 256 + b[1], b[2] * 256 + b[3]]
      text = text.slice(0, last_colon)
      mapped = true
    parts = text.split("::")
    if parts.len() > 2
      raise error("ip.Address.parse: invalid IPv6 address")
    left = []
    right = []
    if parts[0] != ""
      left = self.parse_v6_side(parts[0])
    if parts.len() == 2 and parts[1] != ""
      right = self.parse_v6_side(parts[1])
    for g in ipv4_groups
      right.push(g)
    groups = []
    if parts.len() == 1
      if left.len() + ipv4_groups.len() != 8
        raise error("ip.Address.parse: invalid IPv6 address")
      groups = left
      for g in ipv4_groups
        groups.push(g)
    else
      zeros = 8 - left.len() - right.len()
      if zeros < 1
        raise error("ip.Address.parse: invalid IPv6 address")
      for g in left
        groups.push(g)
      i = 0
      while i < zeros
        groups.push(0)
        i = i + 1
      for g in right
        groups.push(g)
    if groups.len() != 8
      raise error("ip.Address.parse: invalid IPv6 address")
    Address(6, nil, groups, mapped)

parse_v6_side

Address.parse_v6_side(text)

lib/net/ip/address.tya:223

Address.parse_v6_side provides the net/ip/Address standard library operation.

Source
  # Address.parse_v6_side provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Any the resulting value.
  parse_v6_side: text ->
    parts = text.split(":")
    out = []
    for part in parts
      if part == "" or part.byte_len() > 4
        raise error("ip.Address.parse: invalid IPv6 address")
      out.push(self.hex_to_int(part))
    out

private_address?

Address.private_address?(addr = nil)

lib/net/ip/address.tya:235

Address.private? provides the net/ip/Address standard library operation.

Source
  # Address.private? provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Boolean whether the condition is true.
  private_address?: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      b = addr.bytes
      if b[0] == 10
        return true
      if b[0] == 172 and b[1] >= 16 and b[1] <= 31
        return true
      if b[0] == 192 and b[1] == 168
        return true
      return false
    g0 = addr.groups[0]
    g0 >= 64512 and g0 <= 65023

to_s

Address.to_s(addr = nil)

lib/net/ip/address.tya:253

Address.to_s provides the net/ip/Address standard library operation.

Source
  # Address.to_s provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return String the resulting value.
  to_s: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      parts = []
      for b in addr.bytes
        parts.push(b.to_s())
      return parts.join(".")
    self.format_v6(addr.groups, addr.mapped)

unspecified?

Address.unspecified?(addr = nil)

lib/net/ip/address.tya:266

Address.unspecified? provides the net/ip/Address standard library operation.

Source
  # Address.unspecified? provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Boolean whether the condition is true.
  unspecified?: addr = nil ->
    if addr == nil
      addr = self
    if addr.version == 4
      return addr.bytes[0] == 0 and addr.bytes[1] == 0 and addr.bytes[2] == 0 and addr.bytes[3] == 0
    for g in addr.groups
      if g != 0
        return false
    true

valid?

Address.valid?(text)

lib/net/ip/address.tya:279

Address.valid? provides the net/ip/Address standard library operation.

Source
  # Address.valid? provides the net/ip/Address standard library operation.
  # @param text String text value.
  # @return Boolean whether the condition is true.
  valid?: text ->
    try
      self.parse(text)
      return true
    catch _
      return false

version_of

Address.version_of(addr = nil)

lib/net/ip/address.tya:289

Address.version provides the net/ip/Address standard library operation.

Source
  # Address.version provides the net/ip/Address standard library operation.
  # @param addr Any addr value.
  # @return Any the resulting value.
  version_of: addr = nil ->
    if addr == nil
      addr = self
    addr.version

zero_run

Address.zero_run(groups)

lib/net/ip/address.tya:297

Address.zero_run provides the net/ip/Address standard library operation.

Source
  # Address.zero_run provides the net/ip/Address standard library operation.
  # @param groups Any groups value.
  # @return Any the resulting value.
  zero_run: groups ->
    best_start = -1
    best_len = 0
    i = 0
    while i < groups.len()
      if groups[i] == 0
        start = i
        while i < groups.len() and groups[i] == 0
          i = i + 1
        n = i - start
        if n > best_len
          best_start = start
          best_len = n
      else
        i = i + 1
    { start: best_start, len: best_len }