Sha256: f572856e5a9723b1335e5cfd59b4c78e4bd3782f903f4beb2768276c8fedb0d4

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 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.

# frozen_string_literal: true

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
      include LengthFrom

      # @return [Integer]
      attr_reader :static_length

      # @param [Hash] options
      # @option options [Types::Int,Proc] :length_from object or proc from which
      #   takes length when reading
      # @option options [Integer] :static_length set a static length for this string
      def initialize(options={})
        super()
        initialize_length_from(options)
        @static_length = options[:static_length]
      end

      # @param [::String] str
      # @return [String] self
      def read(str)
        s = read_with_length_from(str)
        s = s[0, static_length] if static_length
        self.replace(s)
        self
      end

      alias sz length
      alias to_human to_s
      alias from_human read
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
packetgen-3.0.2 lib/packetgen/types/string.rb
packetgen-3.0.1 lib/packetgen/types/string.rb
packetgen-3.0.0 lib/packetgen/types/string.rb