module Addr

Constants

BIN_MAG_DIGS
BIN_POSTFIX
BIN_PREFIX
BIN_SEP
DEC_MAG_DIGS
DEC_POSTFIX
DEC_SEP
HEX_MAG_DIGS
HEX_POSTFIX
HEX_PREFIX
HEX_SEP

Public Instance Methods

bit_range() click to toggle source
# File lib/ipxact/addr.rb, line 23
def bit_range
  [position, position+width]
end
to_bin(str, opts={}) click to toggle source

@param str [String] string representing number; can be in any base @param opts [Hash] options are:

:pre  => [boolean, String] # if String, notation, by default '0b'; hash key indicates position; if true,
:post => [boolean, String] # radix notation goes at end e.g. '10b'; cannot contradict :pre
:sep  => [boolean] # adds separator characters ('_' for hex) every 4 digits
:pad  => [Fixnum] # given value indicates total digit width of number e.g. 4 => '000Fh'

@return [String] string representation of hex number

# File lib/ipxact/addr.rb, line 59
def to_bin(str, opts={})
  s = str.to_dec.to_s(2)
  s = add_padding(s, 1) if opts[:pad]

  if opts[:sep]
    separator = opts[:sep] == true ? BIN_SEP : opts[:sep]
    s = add_separators(s, BIN_MAG_DIGS, separator)
  end

  if opts[:post]
    s += opts[:post] == true ? BIN_POSTFIX : opts[:post]
  end
  if opts[:pre]
    s = (opts[:pre] == true ? BIN_PREFIX : opts[:pre]) + s
  end
  s
end
to_dec(str, opts={}) click to toggle source

@param str [String] number as string; can be any base or notation @return [String] formatted string

# File lib/ipxact/addr.rb, line 79
def to_dec(str, opts={})
  s = str.to_dec.to_s
  s = add_padding(s, opts[:pad])     if opts[:pad]

  if opts[:sep]
    separator = opts[:sep] == true ? DEC_SEP : opts[:sep]
    s = add_separators(s, DEC_MAG_DIGS, separator)
  end

  if opts[:post]
    s += opts[:post] == true ? DEC_POSTFIX : opts[:post]
  end
  s
end
to_hex(str, opts={}) click to toggle source

@param str [String] string representing number; can be in any base @param opts [Hash] options are:

:pre  => true # radix notation goes at beginning e.g. '0x000F' - default setting
:post => true # radix notation goes at end e.g. 'Fh'
:sep  => true # adds separator characters ('_' for hex) every 4 digits
:pad  => [Fixnum] # given value indicates total digit width of number e.g. 4 => '000Fh'

@return [String] string representation of hex number

# File lib/ipxact/addr.rb, line 34
def to_hex(str, opts={})
  s = str.to_dec.to_s(16).upcase
  s = add_padding(s, opts[:pad]) if opts[:pad]

  if opts[:sep]
    separator = opts[:sep] == true ? HEX_SEP : opts[:sep]
    s = add_separators(s, HEX_MAG_DIGS, separator)
  end

  if opts[:post]
    s += opts[:post] == true ? HEX_POSTFIX : opts[:post]
  end
  if opts[:pre]
    s = opts[:pre] == true ? HEX_PREFIX : opts[:pre] + s
  end
  s
end

Private Instance Methods

add_padding(str, places) click to toggle source

@param str [String] number as string @param places [Fixnum] number of digits for the whole number @return [String] string representing number

# File lib/ipxact/addr.rb, line 97
def add_padding(str, places)
  padding = ''
  (places - str.size).times do padding << '0' end
  padding + str
end
add_separators(str, mag, sep) click to toggle source
# File lib/ipxact/addr.rb, line 103
def add_separators(str, mag, sep)
  expr = Regexp.new("\\w{#{mag}}")
  s = str.reverse.gsub(expr) do |m| m+sep end.reverse
  str.size%mag == 0 ? s[1..-1] : s
end
get_bin_value() click to toggle source
# File lib/ipxact/addr.rb, line 113
def get_bin_value
  av = reset_value.value
  self[:value].to_s(2).split(//)
end