class Netstring
A netstring parser and emitter.
Attributes
netstring[R]
Returns the netstring.
@return [String] the netstring
Public Class Methods
dump(s)
click to toggle source
Dumps a string to a netstring.
@param [String] s a string @return [String] a netstring
# File lib/netstring.rb, line 18 def self.dump(s) unless String === s raise Error, "#{s.inspect} is not a String" end "#{s.bytesize}:#{s}," end
load(n)
click to toggle source
Loads a string from a netstring.
The netstring may be multiple concatenated netstrings.
The return value is a ‘Netstring` object, whose `#to_s` method returns the string, and whose `#offset` method returns the length of the netstring.
@param [String] n a netstring @return [Netstring] a string
# File lib/netstring.rb, line 34 def self.load(n) unless String === n raise Error, "#{n.inspect} is not a String" end match = n.match(/\A(\d+):/) unless match raise Error, 'bad netstring header' end size = Integer(match[1]) unless n.byteslice(match.end(0) + size) == ',' raise Error, 'expected "," delimiter' end new(n, match.end(0), size) end
new(n, start, length)
click to toggle source
Initializes a netstring.
The netstring may be multiple concatenated netstrings.
@param [String] n a netstring @param [Integer] start the start of the string in the netstring @param [Integer] length the length of the string in the netstring
Calls superclass method
# File lib/netstring.rb, line 56 def initialize(n, start, length) super(n.byteslice(start, length)) @netstring = n.byteslice(0, start + length + 1) end