Class: Ruckus::Str
Overview
A Ruckus::Str is a bag of bytes, wrapping a Ruby string
Constant Summary
Constants inherited from Parsel
Instance Attribute Summary
Attributes inherited from Parsel
#name, #parent, #rendered_offset, #rendering, #tag, #value
Instance Method Summary collapse
-
#capture(str) ⇒ Object
As with Parsel; take a string, return what’s left, capture the value in @value.
-
#initialize(opts = {}) ⇒ Str
constructor
- Options include: size
- :min = :max = :size min
- string will be padded to this size max
- string will be cut off at this size padding
- (Default: “x00”) — what to pad with value
- Normally a string unicode
-
Convert to UTF-16-LE before rendering.
-
#to_s(off = nil) ⇒ Object
As per Parsel, write the string.
Methods inherited from Parsel
bytes_for_bits, coerce, #each_matching_selector, endian?, factory?, #find_containing, #find_tag, #find_tag_struct, #fixup, #in, #incomplete!, #index_for_selectors, #inspect, #matches_selector?, #method_missing, #native?, native?, #next, #out, #parent_structure, #permute, #prev, #resolve, #respond_to?, #root, #size, #visit, #where_am_i?
Constructor Details
#initialize(opts = {}) ⇒ Str
Options include:
- size
-
:min = :max = :size
- min
-
string will be padded to this size
- max
-
string will be cut off at this size
- padding
-
(Default: “x00”) — what to pad with
- value
-
Normally a string
- unicode
-
Convert to UTF-16-LE before rendering
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruckus/str.rb', line 20 def initialize(opts={}) opts[:bounded_by] ||= -1 if opts[:bounded] if opts[:bounded_by] opts[:size] = { :offset => opts[:bounded_by], :meth => :value } end if opts[:size] opts[:min] = opts[:size] opts[:max] = opts[:size] @in_size = opts[:size] end opts[:padding] ||= "\x00" opts[:min] ||= 0 super(opts) @value = @value.clone if @value @value ||= "" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Ruckus::Parsel
Instance Method Details
#capture(str) ⇒ Object
As with Parsel; take a string, return what’s left, capture the value in @value.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ruckus/str.rb', line 44 def capture(str) if @in_size max = resolve(@in_size) min = resolve(@in_size) end max ||= resolve(@max) min ||= resolve(@min) pad = resolve(@padding) nul = resolve(@nul_terminated) uni = resolve(@unicode) del = resolve(@delimiter) @value = nil incomplete! if not str if (s = size) incomplete! if str.size < s cap = str[0...s] elsif nul nterm = str.index(uni ? "\000\000" : "\000") if nterm cap = str[0...nterm] else cap = str end elsif del if((idx = str.index(del))) cap = str[0...idx] else cap = str end else cap = str end cap = cap[0...max] if max while cap.size < min cap << pad end # must work on a dup of str here or @value may get clobbered by str.slice! @value = uni ? cap.to_ascii : cap.dup fin = -1 mod = nul ? 1 : 0 str.slice! 0, (cap.size + mod) return str end |
#to_s(off = nil) ⇒ Object
As per Parsel, write the string
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruckus/str.rb', line 97 def to_s(off=nil) @rendered_offset = off || 0 min = resolve(@min) max = resolve(@max) uni = resolve(@unicode) val = (resolve(@value) || "").to_s val = val.clone # gross! pad = resolve(@padding) nul = resolve(@nul_terminated) pto = resolve(@pad_to) val << "\x00" if nul and val[-1] != 0 val = val.to_utf16 if uni while min and val.size < min val << pad end if pto while ((val.size % pto) != 0) # this is some shameful shit right here val << pad end end val = val[0...max] if max if off return val, off + val.size else return val end end |