Module: Bindef::Extras::String

Included in:
Bindef
Defined in:
lib/bindef/extras/string.rb

Overview

Potentially useful extra string emission commands.

Instance Method Summary collapse

Instance Method Details

#lstr(int_fmt, string) ⇒ void

Note:

Like Bindef#str, uses the :encoding Bindef#pragma

Note:

Like Bindef#u16 and wider, the :endian Bindef#pragma

This method returns an undefined value.

Emits a length-prefixed string.

Examples:

lstr :u8, "foo" # Emits "\x03foo"

Parameters:

  • int_fmt (Symbol)

    the width of the length prefix, as one of the integer commands

  • string (String)

    the string to emit



48
49
50
51
52
# File 'lib/bindef/extras/string.rb', line 48

def lstr(int_fmt, string)
  str string do |enc_string|
    send int_fmt, enc_string.bytesize
  end
end

#strnz(string, maxpad) ⇒ void

Note:

Like Bindef#str, uses the :encoding Bindef#pragma

This method returns an undefined value.

Emits a string, NUL-padded up to the given length in bytes.

Examples:

strnz "foo", 5 # Emits "foo\x00\x00"

Parameters:

  • string (String)

    the string to emit

  • maxpad (Integer)

    the maximum number of padding NULs

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bindef/extras/string.rb', line 25

def strnz(string, maxpad)
  pad = maxpad

  str string do |enc_string|
    pad = maxpad - enc_string.bytesize
  end

  raise CommandError, "maxpad < encoded string len" if pad.negative?

  # Reset our encoding temporarily, to make sure we emit the right number of NULs.
  pragma encoding: "utf-8" do
    str("\x00" * pad)
  end
end

#strz(string) ⇒ void

Note:

Like Bindef#str, uses the :encoding Bindef#pragma

This method returns an undefined value.

Emits a null-terminated string.

Examples:

strz "foobar" # Emits "foobar\x00"

Parameters:

  • string (String)

    the string to emit



13
14
15
16
# File 'lib/bindef/extras/string.rb', line 13

def strz(string)
  str string
  str "\0"
end