Class: Zrip::Dictionary

Inherits:
Data
  • Object
show all
Defined in:
lib/zrip/dictionary.rb,
lib/zrip/dictionary.rb

Constant Summary collapse

ZDICT_MAGIC =
"\x37\xA4\x30\xEC".b.freeze
USER_DICT_ID_MIN =
32_768
USER_DICT_ID_MAX =
(2**31) - 1
USER_DICT_ID_SIZE =
USER_DICT_ID_MAX - USER_DICT_ID_MIN + 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes:, id: nil) ⇒ Dictionary

Returns a new instance of Dictionary.



15
16
17
18
19
20
21
22
23
24
# File 'lib/zrip/dictionary.rb', line 15

def initialize(bytes:, id: nil)
  b = bytes.b
  id ||= if b.byteslice(0, 4) == ZDICT_MAGIC
           b.byteslice(4, 4).unpack1("V")
         else
           raw = Digest::SHA256.digest(b).byteslice(0, 4).unpack1("V")
           USER_DICT_ID_MIN + (raw % USER_DICT_ID_SIZE)
         end
  super(bytes: b.freeze, id: id)
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes

Returns:

  • (Object)

    the current value of bytes



6
7
8
# File 'lib/zrip/dictionary.rb', line 6

def bytes
  @bytes
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



6
7
8
# File 'lib/zrip/dictionary.rb', line 6

def id
  @id
end

Instance Method Details

#sizeObject



27
28
29
# File 'lib/zrip/dictionary.rb', line 27

def size
  bytes.bytesize
end