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
-
#lstr(int_fmt, string) ⇒ void
Emits a length-prefixed string.
-
#strnz(string, maxpad) ⇒ void
Emits a string, NUL-padded up to the given length in bytes.
-
#strz(string) ⇒ void
Emits a null-terminated string.
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.
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.
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.
13 14 15 16 |
# File 'lib/bindef/extras/string.rb', line 13 def strz(string) str string str "\0" end |