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.
17 18 19 20 21 22 |
# File 'lib/rex/struct2/s_string.rb', line 17 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.
14 15 16 |
# File 'lib/rex/struct2/s_string.rb', line 14 def default @default end |
#pad ⇒ Object
Returns the value of attribute pad.
14 15 16 |
# File 'lib/rex/struct2/s_string.rb', line 14 def pad @pad end |
#size ⇒ Object
Returns the value of attribute size.
14 15 16 |
# File 'lib/rex/struct2/s_string.rb', line 14 def size @size end |
Instance Method Details
#from_s(bytes) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rex/struct2/s_string.rb', line 54 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
33 34 35 |
# File 'lib/rex/struct2/s_string.rb', line 33 def reset self.value = @default end |
#to_s ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rex/struct2/s_string.rb', line 37 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 |