module RuneRb::System::Patches::StringRefinements

A module adding new functions to the String class in the form of a refinement. The functions assist when a string is used as a Stream container/buffer.

Constants

VALID_CHARS

Public Instance Methods

byte_from(position) click to toggle source

returns the next byte from the provided position. @param position [Integer] the offset/position to begin reading from.

# File deployment/app/system/patches/string.rb, line 24
def byte_from(position)
  slice!(position).unpack1('c')
end
bytes_from(position, amount) click to toggle source

Returns x amount of bytes from a given position. @param position [Integer] the offset/position to begin reading from. @param amount [Integer] the amount of bytes to read.

# File deployment/app/system/patches/string.rb, line 31
def bytes_from(position, amount)
  amount.times.inject('') { |str| str << slice!(position) }.unpack('c*')
end
from_base37(base37) click to toggle source

Returns a string from the provided b37 numeric @param base37 [Integer] the base_37 numeric to build a string from.

# File deployment/app/system/patches/string.rb, line 135
def from_base37(base37)
  return self unless empty?

  while base37 != 0
    original = base37.signed(:long)
    base37 = (base37 / 37).signed(:long)
    self << RuneRb::System::Patches::StringRefinements::VALID_CHARS[(original - base37 * 37).signed(:int)]
  end

  reverse!
end
int_from(position) click to toggle source

returns the next int from the provided position. @param position [Integer] the offset/position to begin reading from.

# File deployment/app/system/patches/string.rb, line 72
def int_from(position)
  slice!(position..(position + 3)).unpack1('l')
end
ints_from(position, amount) click to toggle source

Returns x amount of integers from a given position @param position [Integer] the offset/position to begin reading from. @param amount [Integer] the amount of integers to read.

# File deployment/app/system/patches/string.rb, line 79
def ints_from(position, amount)
  amount.times.inject('') { |str| str << slice!(position..(position + 3)) }.unpack('l*')
end
long_from(position) click to toggle source

returns the next long from the provided position. @param position [Integer] the offset/position to begin reading from.

# File deployment/app/system/patches/string.rb, line 96
def long_from(position)
  slice!(position..(position + 7)).unpack1('q')
end
longs_from(position, amount) click to toggle source

Returns x amount of longs from a given position. @param position [Integer] the offset/position to begin reading from. @param amount [Integer] the amount of longs to read.

# File deployment/app/system/patches/string.rb, line 103
def longs_from(position, amount)
  amount.times.inject('') { |str| str << slice!(position..(position + 7)) }.unpack('q*')
end
next_byte() click to toggle source

returns the next byte.

# File deployment/app/system/patches/string.rb, line 12
def next_byte
  slice!(0)&.unpack1('c')
end
next_bytes(amount) click to toggle source

Returns the next x amount of byte values. @param amount [Integer] the amount of bytes.

# File deployment/app/system/patches/string.rb, line 18
def next_bytes(amount)
  amount.times.inject('') { |str| str << slice!(0) }.unpack('c*')
end
next_int() click to toggle source

returns the next integer.

# File deployment/app/system/patches/string.rb, line 60
def next_int
  slice!(0..3).unpack1('l')
end
next_ints(amount) click to toggle source

Returns the next x amount of integer values. @param amount [Integer] the amount of integers.

# File deployment/app/system/patches/string.rb, line 66
def next_ints(amount)
  amount.times.inject('') { |str| str << slice!(0..3) }.unpack('l*')
end
next_long() click to toggle source

returns the next long.

# File deployment/app/system/patches/string.rb, line 84
def next_long
  slice!(0..7).unpack1('q')
end
next_longs(amount) click to toggle source

Returns the next x amount of long values. @param amount [Integer] the amount of longs.

# File deployment/app/system/patches/string.rb, line 90
def next_longs(amount)
  amount.times.inject('') { |str| str << slice!(0..7) }.unpack('q*')
end
next_short() click to toggle source

returns the next short.

# File deployment/app/system/patches/string.rb, line 36
def next_short
  slice!(0..1).unpack1('n')
end
next_shorts(amount) click to toggle source

Returns the next x amount of short values. @param amount [Integer] the amount of shorts.

# File deployment/app/system/patches/string.rb, line 42
def next_shorts(amount)
  amount.times.inject('') { |str| str << slice!(0..1) }.unpack('n*')
end
next_tstring() click to toggle source

Returns the next terminated string.

# File deployment/app/system/patches/string.rb, line 108
def next_tstring
  val = ''
  while (res = next_byte)
    break if res == 10

    val << res
  end
  val
end
short_from(position) click to toggle source

returns the next short from the provided position. @param position [Integer] the offset/position to begin reading from.

# File deployment/app/system/patches/string.rb, line 48
def short_from(position)
  slice!(position..(position + 1)).unpack1('n')
end
shorts_from(position, amount) click to toggle source

Returns x amount of shorts from a given position @param position [Integer] the offset/position to begin reading from. @param amount [Integer] the amount of shorts to read.

# File deployment/app/system/patches/string.rb, line 55
def shorts_from(position, amount)
  amount.times.inject('') { |str| str << slice!(position..(position + 1)) }.unpack('n*')
end
to_base37() click to toggle source

Returns a base37 numeric representation of the String. @return [Integer] a base37 number representing the String

# File deployment/app/system/patches/string.rb, line 120
def to_base37
  l = 0
  (0...length).each do |i|
    c = self[i].chr
    l *= 37
    l += (1  + self[i].bytes.first) - 65 if (c >= 'A') && (c <= 'Z')
    l += (1  + self[i].bytes.first) - 97 if (c >= 'a') && (c <= 'z')
    l += (27 + self[i].bytes.first) - 48 if (c >= '0') && (c <= '9')
  end
  l /= 37 while (l % 37).zero? && l != 0
  l
end