Class: Hexdump::Numeric::CharOrInt Private

Inherits:
FormatString show all
Defined in:
lib/hexdump/numeric/char_or_int.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FormatString

#to_s

Constructor Details

#initialize(base, encoding = nil) ⇒ CharOrInt

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the character format.

Parameters:

  • base (Hexadecimal, Decimal, Octal, Binary)

    The numeric base format to fallback to if a value does not map to a character.

  • encoding (Encoding, nil) (defaults to: nil)

    The optional encoding to convert bytes to.

Since:

  • 1.0.0



28
29
30
31
32
33
# File 'lib/hexdump/numeric/char_or_int.rb', line 28

def initialize(base,encoding=nil)
  @base     = base
  @encoding = encoding
  
  super("%#{@base.width}s")
end

Instance Attribute Details

#baseHexadecimal, ... (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.0.0



13
14
15
# File 'lib/hexdump/numeric/char_or_int.rb', line 13

def base
  @base
end

#encodingEncoding? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Encoding, nil)

Since:

  • 1.0.0



16
17
18
# File 'lib/hexdump/numeric/char_or_int.rb', line 16

def encoding
  @encoding
end

Instance Method Details

#%(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats a given ASCII byte value to a character or numeric format.

Parameters:

  • value (Integer)

    The ASCII byte value.

Returns:

  • (String)

    The character or numeric formatted value.

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hexdump/numeric/char_or_int.rb', line 53

def %(value)
  if value == 0x00
    super("\\0")
  elsif value == 0x07
    super("\\a")
  elsif value == 0x08
    super("\\b")
  elsif value == 0x09
    super("\\t")
  elsif value == 0x0a
    super("\\n")
  elsif value == 0x0b
    super("\\v")
  elsif value == 0x0c
    super("\\f")
  elsif value == 0x0d
    super("\\r")
  else
    if @encoding
      if value >= 0x00
        char = value.chr(@encoding) rescue nil

        if char && char =~ /[[:print:]]/
          super(char)
        else
          @base % value
        end
      else
        @base % value
      end
    else
      if (value >= 0x20 && value <= 0x7e)
        super(value.chr)
      else
        @base % value
      end
    end
  end
end

#widthInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The string width associated with the numeric base.

Returns:

  • (Integer)

Since:

  • 1.0.0



40
41
42
# File 'lib/hexdump/numeric/char_or_int.rb', line 40

def width
  @base.width
end