Sha256: fe1288e0a76c66efec605dc0b6c3d8390f5f67b071e195eae6e6f3ae64e85f80

Contents?: true

Size: 1.04 KB

Versions: 11

Compression:

Stored size: 1.04 KB

Contents

# coding: utf-8
# This file is part of PacketGen
# See https://github.com/sdaubert/packetgen for more informations
# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
# This program is published under MIT license.

module PacketGen
  module Types

    # This class is just like regular String. It only adds {#read} and {#sz} methods
    # to be compatible with others {Types}.
    # @author Sylvain Daubert
    class String < ::String

      # @param [String] str
      # @param [Hash] options
      # @option options [Types::Int,Proc] :length_from object or proc from which
      #   takes length when reading
      def initialize(str='', options={})
        super(str)
        @length_from = options[:length_from]
      end

      # @param [::String] str
      # @return [String] self
      def read(str)
        s = str.to_s
        s = case @length_from
            when Int
              s[0, @length_from.to_i]
            else
              s
            end
        self.replace s
        self
      end

      alias sz length
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
packetgen-2.1.4 lib/packetgen/types/string.rb
packetgen-2.1.3 lib/packetgen/types/string.rb
packetgen-2.1.2 lib/packetgen/types/string.rb
packetgen-2.1.1 lib/packetgen/types/string.rb
packetgen-2.1.0 lib/packetgen/types/string.rb
packetgen-2.0.1 lib/packetgen/types/string.rb
packetgen-2.0.0 lib/packetgen/types/string.rb
packetgen-1.4.3 lib/packetgen/types/string.rb
packetgen-1.4.2 lib/packetgen/types/string.rb
packetgen-1.4.1 lib/packetgen/types/string.rb
packetgen-1.4.0 lib/packetgen/types/string.rb