Sha256: d2f491f68efe5896960ed78b91cc1ba4a75ab02253a4fdcd34f235d156a1e4d6

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'hexdump/numeric/exceptions'
require 'hexdump/format_string'

module Hexdump
  module Numeric
    #
    # @api private
    #
    # @since 1.0.0
    #
    class Octal < FormatString

      SIZE_TO_WIDTH = {
        1 => 3,  # 0xff.to_s(7).length
        2 => 6,  # 0xffff.to_s(7).length
        4 => 11, # 0xffffffff.to_s(7).length
        8 => 22  # 0xffffffffffffffff.to_s(7).length
      }

      # @return [Integer]
      attr_reader :width

      #
      # Initializes the octal format.
      #
      # @param [Type::Int, Type::UInt] type
      #
      # @raise [NotImplementedError]
      #
      # @raise [IncompatibleTypeError]
      #
      # @raise [TypeError]
      #
      def initialize(type)
        case type
        when Type::Int, Type::UInt
          @width = SIZE_TO_WIDTH.fetch(type.size) do
            raise(NotImplementedError,"type #{type} with unsupported size #{type.size}")
          end

          if type.signed?
            super("% .#{@width}o"); @width += 1
          else
            super("%.#{@width}o")
          end
        when Type::Float
          raise(IncompatibleTypeError,"cannot format floating-point numbers in octal")
        else
          raise(TypeError,"unsupported type: #{type.inspect}")
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexdump-1.0.0 lib/hexdump/numeric/octal.rb