module RuneRb::System::Patches::IntegerRefinements

A module adding overflow functions to integers to mimic the behavior of Java primitive overflow behavior.

Public Instance Methods

adjust(type) click to toggle source

Adjusts the integer based on the passed type @param type [Symbol] the type of adjustment to make to the integer.

# File deployment/app/system/patches/integer.rb, line 64
def adjust(type)
  case type
  when :Byte, :byte, :b, :B
    primitive_max = 2**7 - 1
    primitive_min = -2**7
  when :Short, :short, :s, :S
    primitive_max = 2**15 - 1
    primitive_min = -2**15
  when :Integer, :Int, :int, :i, :I
    primitive_max = 2**31 - 1
    primitive_min = -2**31
  when :Long, :long, :l, :L
    primitive_max = 2**63 - 1
    primitive_min = -2**63
  when :Nibble, :nibble, :n, :N
    primitive_max = 2**4
    primitive_min = -2**4
  else
    primitive_max = 2**31 - 1
    primitive_min = -2**31
  end
  self < -primitive_max ? -1 * (-self & primitive_max) : self
  self > primitive_min ? (self & primitive_max) : self
end
binary_representation() click to toggle source

Returns a binary representation in the form of an array of 1's and 0's in their respective digits. @return [Array] the binary representation

# File deployment/app/system/patches/integer.rb, line 8
def binary_representation
  to_s(2).chars.map(&:to_i)
end
from_binary_rep(representation) click to toggle source

Returns a base 10 numeric from the passed array representation @param representation [Array] the representation used to generate the numeric @returns [Integer] the base 10 numeric of the representation.

# File deployment/app/system/patches/integer.rb, line 17
def from_binary_rep(representation)
  res = 0
  representation.each_with_index do |bit, idx|
    res += bit * (2**idx)
  end
  res
end
nibble() click to toggle source
# File deployment/app/system/patches/integer.rb, line 56
def nibble
  adjust(:nibble)
end
signed(type) click to toggle source

Adjusts the value of the Integer to be signed. @param type [Symbol] the type of primitive the value will be returned as

# File deployment/app/system/patches/integer.rb, line 44
def signed(type)
  case type
  when :Byte, :byte, :b, :B then adjust(:byte)
  when :Short, :short, :s, :S then adjust(:short)
  when :Integer, :Int, :int, :integer, :i, :I then adjust(:integer)
  when :Long, :long, :l, :L then adjust(:long)
  else adjust(:integer)
  end
end
unsigned(type) click to toggle source

Adjusts the value of the Integer to be unsigned. @param type [Symbol] the type of primitive the value will be returned as

# File deployment/app/system/patches/integer.rb, line 29
def unsigned(type)
  0 unless positive?
  case type
  when :Byte, :byte, :b, :B then to_i > 0xFF ? 0xFF : to_i
  when :Short, :short, :s, :S then to_i > 0xFFFF ? 0xFFFF : to_i
  when :Integer, :Int, :int, :integer, :i, :I then to_i > 0xFFFFFFFF ? 0xFFFFFFFF : to_i
  when :Long, :long, :l, :L then to_i > 0xFFFFFFFFFFFFFFFF ? 0xFFFFFFFFFFFFFFFF : to_i
  else to_i > 0xFFFFFFFF ? 0xFFFFFFFF : to_i
  end
end