Module: Solana::Ruby::Kit::Codecs::Bytes

Extended by:
T::Sig
Included in:
Solana::Ruby::Kit::Codecs
Defined in:
lib/solana/ruby/kit/codecs/bytes.rb

Overview

Low-level byte-string utilities. All methods work with binary-encoded Ruby Strings (encoding = ASCII-8BIT).

Class Method Summary collapse

Class Method Details

.contains_bytes?(outer, inner, offset: 0) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 49

def contains_bytes?(outer, inner, offset: 0)
  ob = outer.b
  ib = inner.b
  return false if offset + ib.bytesize > ob.bytesize

  ob.byteslice(offset, ib.bytesize) == ib
end

.fix_bytes(bytes, size) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 37

def fix_bytes(bytes, size)
  b = bytes.b
  unless b.bytesize == size
    Kernel.raise ArgumentError,
          "Expected #{size} bytes, got #{b.bytesize}"
  end

  b
end

.merge_bytes(*byte_strings) ⇒ Object



15
16
17
18
19
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 15

def merge_bytes(*byte_strings)
  result = ''.b
  byte_strings.each { |bs| result << bs.b }
  result
end

.pad_bytes(bytes, size, direction: :right, pad_byte: "\x00") ⇒ Object



27
28
29
30
31
32
33
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 27

def pad_bytes(bytes, size, direction: :right, pad_byte: "\x00")
  b = bytes.b
  return b if b.bytesize >= size

  padding = (pad_byte.b * (size - b.bytesize))
  direction == :left ? padding + b : b + padding
end