class Prism::Pack::Directive

pack 模板语言中的指令。

常量

ENDIAN_DESCRIPTIONS

各种字节序类型的描述。

SIGNED_DESCRIPTIONS

各种有符号类型的描述。

SIZE_DESCRIPTIONS

各种大小类型的描述。

属性

endian[R]

指令的字节序类型。

length[R]

此指令的长度(用于整数)。

length_type[R]

此指令的长度类型(用于整数)。

signed[R]

指令的有符号类型。

size[R]

指令的大小。

source[R]

表示此指令的源字符串的字节切片。

type[R]

指令的类型。

variant[R]

一个符号,表示我们是打包还是解包。

version[R]

一个符号,表示 Ruby 的版本。

公共类方法

new(version, variant, source, type, signed, endian, size, length_type, length) 点击切换源代码

使用给定的值初始化一个新的指令。

# File prism/pack.rb, line 89
def initialize(version, variant, source, type, signed, endian, size, length_type, length)
  @version = version
  @variant = variant
  @source = source
  @type = type
  @signed = signed
  @endian = endian
  @size = size
  @length_type = length_type
  @length = length
end

公共实例方法

describe() 点击切换源代码

提供指令的人类可读描述。

# File prism/pack.rb, line 131
def describe
  case type
  when SPACE
    "whitespace"
  when COMMENT
    "comment"
  when INTEGER
    if size == SIZE_8
      base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} integer"
    else
      base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} integer"
    end
    case length_type
    when LENGTH_FIXED
      if length > 1
        base + ", x#{length}"
      else
        base
      end
    when LENGTH_MAX
      base + ", as many as possible"
    else
      raise
    end
  when UTF8
    "UTF-8 character"
  when BER
    "BER-compressed integer"
  when FLOAT
    "#{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} float"
  when STRING_SPACE_PADDED
    "arbitrary binary string (space padded)"
  when STRING_NULL_PADDED
    "arbitrary binary string (null padded, count is width)"
  when STRING_NULL_TERMINATED
    "arbitrary binary string (null padded, count is width), except that null is added with *"
  when STRING_MSB
    "bit string (MSB first)"
  when STRING_LSB
    "bit string (LSB first)"
  when STRING_HEX_HIGH
    "hex string (high nibble first)"
  when STRING_HEX_LOW
    "hex string (low nibble first)"
  when STRING_UU
    "UU-encoded string"
  when STRING_MIME
    "quoted printable, MIME encoding"
  when STRING_BASE64
    "base64 encoded string"
  when STRING_FIXED
    "pointer to a structure (fixed-length string)"
  when STRING_POINTER
    "pointer to a null-terminated string"
  when MOVE
    "move to absolute position"
  when BACK
    "back up a byte"
  when NULL
    "null byte"
  else
    raise
  end
end