Sha256: 81835f4bd9d20cb795bea2352661f712ea7d30b8eca721fecb87bc22bbcbd099

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'delegate'

# A netstring parser and emitter.
#
# @see http://cr.yp.to/proto/netstrings.txt
class Netstring < SimpleDelegator
  class Error < StandardError; end

  # Returns the netstring.
  #
  # @return [String] the netstring
  attr_reader :netstring

  # Dumps a string to a netstring.
  #
  # @param [String] s a string
  # @return [String] a netstring
  def self.dump(s)
    unless String === s
      raise Error, "#{s.inspect} is not a String"
    end
    "#{s.size}:#{s},"
  end

  # 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
  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[match.end(0) + size] == ','
      raise Error, 'expected "," delimiter'
    end
    Netstring.new(n, match.end(0), size)
  end

  # 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
  def initialize(n, start, length)
    super(n[start, length])

    @netstring = n[0, start + length + 1]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
netstring-0.0.2 lib/netstring.rb