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

Extended by:
Bytes, T::Sig
Included in:
Solana::Ruby::Kit::Codecs, Bytes
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).

Instance Method Summary collapse

Instance Method Details

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

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 53

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



41
42
43
44
45
46
47
48
49
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 41

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



19
20
21
22
23
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 19

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



31
32
33
34
35
36
37
# File 'lib/solana/ruby/kit/codecs/bytes.rb', line 31

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