Module: KSUID::Utils Private

Defined in:
lib/ksuid/utils.rb

Overview

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

Utility functions for converting between different encodings

Constant Summary collapse

BYTES =

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

A regular expression for splitting bytes out of a “binary” string

Returns:

  • (Regexp)

    the splitter

/.{8}/.freeze
PAIRS =

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

A regular expression for splitting a String into pairs of characters

Returns:

  • (Regexp)

    the splitter

/.{2}/.freeze

Class Method Summary collapse

Class Method Details

.byte_string_from_array(bytes) ⇒ Array<Integer>

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.

Converts a byte array into a byte string

Parameters:

  • bytes (String)

    a byte string

Returns:

  • (Array<Integer>)

    an array of bytes from the byte string



24
25
26
# File 'lib/ksuid/utils.rb', line 24

def self.byte_string_from_array(bytes)
  bytes.pack('C*')
end

.byte_string_from_hex(hex, bits = 32) ⇒ 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.

Converts a hex string into a byte string

Parameters:

  • hex (String)

    a hex-encoded KSUID

  • bits (Integer) (defaults to: 32)

    the expected number of bits for the result

Returns:

  • (String)

    the byte string



33
34
35
36
37
38
39
40
41
# File 'lib/ksuid/utils.rb', line 33

def self.byte_string_from_hex(hex, bits = 32)
  byte_array =
    hex
    .rjust(bits, '0')
    .scan(PAIRS)
    .map { |bytes| bytes.to_i(16) }

  byte_string_from_array(byte_array)
end

.bytes_to_hex_string(bytes) ⇒ 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.

Converts a byte string or byte array into a hex-encoded string

Parameters:

  • bytes (String, Array<Integer>)

    the byte string or array

Returns:

  • (String)

    the byte string as a hex-encoded string



47
48
49
50
51
52
53
# File 'lib/ksuid/utils.rb', line 47

def self.bytes_to_hex_string(bytes)
  bytes = bytes.bytes if bytes.is_a?(String)

  byte_string_from_array(bytes)
    .unpack1('H*')
    .upcase
end

.int_from_bytes(bytes) ⇒ Integer

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.

Converts a byte string or byte array into an integer

Parameters:

  • bytes (String, Array<Integer>)

    the byte string or array

Returns:

  • (Integer)

    the resulting integer



59
60
61
62
63
64
65
66
# File 'lib/ksuid/utils.rb', line 59

def self.int_from_bytes(bytes)
  bytes = bytes.bytes if bytes.is_a?(String)

  bytes
    .map { |byte| byte.to_s(2).rjust(8, '0') }
    .join
    .to_i(2)
end

.int_to_bytes(int, bits = 32) ⇒ 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.

Converts an integer into a network-ordered (big endian) byte string

Parameters:

  • int (Integer)

    the integer to convert

  • bits (Integer) (defaults to: 32)

    the expected number of bits for the result

Returns:

  • (String)

    the byte string



73
74
75
76
77
78
79
80
# File 'lib/ksuid/utils.rb', line 73

def self.int_to_bytes(int, bits = 32)
  int
    .to_s(2)
    .rjust(bits, '0')
    .scan(BYTES)
    .map { |digits| digits.to_i(2) }
    .pack("C#{bits / 8}")
end