Class: Rex::Struct2::SString
- Inherits:
-
Object
- Object
- Rex::Struct2::SString
- Includes:
- Element
- Defined in:
- lib/rex/struct2/s_string.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#pad ⇒ Object
Returns the value of attribute pad.
-
#size ⇒ Object
Returns the value of attribute size.
Attributes included from Element
#container, #restraint, #value
Instance Method Summary collapse
- #from_s(bytes) ⇒ Object
-
#initialize(size = nil, default = nil, pad = nil) ⇒ SString
constructor
A new instance of SString.
- #reset ⇒ Object
- #to_s ⇒ Object
Methods included from Element
Constructor Details
#initialize(size = nil, default = nil, pad = nil) ⇒ SString
Returns a new instance of SString.
16 17 18 19 20 21 |
# File 'lib/rex/struct2/s_string.rb', line 16 def initialize(size=nil, default=nil, pad=nil) self.size = size @default = default @pad = pad reset() end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
13 14 15 |
# File 'lib/rex/struct2/s_string.rb', line 13 def default @default end |
#pad ⇒ Object
Returns the value of attribute pad.
13 14 15 |
# File 'lib/rex/struct2/s_string.rb', line 13 def pad @pad end |
#size ⇒ Object
Returns the value of attribute size.
13 14 15 |
# File 'lib/rex/struct2/s_string.rb', line 13 def size @size end |
Instance Method Details
#from_s(bytes) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rex/struct2/s_string.rb', line 53 def from_s(bytes) # we don't have enough bytes to satisfy our minimum if restraint && restraint.min && bytes.length < restraint.min return end if restraint && restraint.max self.value = bytes.slice(0, restraint.max) else self.value = bytes.dup end return(self.slength) end |
#reset ⇒ Object
32 33 34 |
# File 'lib/rex/struct2/s_string.rb', line 32 def reset self.value = @default end |
#to_s ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rex/struct2/s_string.rb', line 36 def to_s string = self.value return if !string # pad if short if restraint && restraint.min && self.pad && restraint.min > string.length string += self.pad * (restraint.min - string.length) end # truncate if long if restraint && restraint.max string = string.slice(0, restraint.max) end return string end |