Class: Unisec::Decdump — Documentation by YARD 0.9.44

# Class: Unisec::Decdump

Inherits:
  Object
  - Object
  - Unisec::Decdump
  show all

Defined in:
  lib/unisec/decdump.rb

## Overview

Decimal dump (decdump) of all Unicode encodings.

## Instance Attribute Summary collapse

- #utf16be ⇒ String readonly

  UTF-16BE decdump.

- #utf16le ⇒ String readonly

  UTF-16LE decdump.

- #utf32be ⇒ String readonly

  UTF-32BE decdump.

- #utf32le ⇒ String readonly

  UTF-32LE decdump.

- #utf8 ⇒ String readonly

  UTF-8 decdump.

## Class Method Summary collapse

- .utf16be(str) ⇒ String

  Encode to UTF-16BE in decdump format (packed by code unit = every 2 bytes).

- .utf16le(str) ⇒ String

  Encode to UTF-16LE in decdump format (packed by code unit = every 2 bytes).

- .utf32be(str) ⇒ String

  Encode to UTF-32BE in decdump format (packed by code unit = every 4 bytes).

- .utf32le(str) ⇒ String

  Encode to UTF-32LE in decdump format (packed by code unit = every 4 bytes).

- .utf8(str) ⇒ String

  Encode to UTF-8 in decdump format (spaced at every code unit = every byte).

## Instance Method Summary collapse

- #display ⇒ String

  Display a CLI-friendly output summurizing the decdump in all Unicode encodings.

- #initialize(str) ⇒ Decdump constructor

  Init the decdump.

## Constructor Details

### #initialize(str) ⇒ `Decdump`

Init the decdump.

#### Examples:

    ded = Unisec::Decdump.new('I 💕 Ruby 💎')
    ded.utf8 # => "073 032 240 159 146 149 032 082 117 098 121 032 240 159 146 142"
    ded.utf16be # => "|000 073| |000 032| |216 061| |220 149| |000 032| |000 082| |000 117| |000 098| |000 121| |000 032| |216 061| |220 142|"
    ded.utf32be # => "|000 000 000 073| |000 000 000 032| |000 001 244 149| |000 000 000 032| |000 000 000 082| |000 000 000 117| |000 000 000 098| |000 000 000 121| |000 000 000 032| |000 001 244 142|"

Parameters:

- str (`String`) —

  Input string to encode

    36
    37
    38
    39
    40
    41
    42
    # File 'lib/unisec/decdump.rb', line 36

    def initialize(str)
      @utf8 = Decdump.utf8(str)
      @utf16be = Decdump.utf16be(str)
      @utf16le = Decdump.utf16le(str)
      @utf32be = Decdump.utf32be(str)
      @utf32le = Decdump.utf32le(str)
    end

## Instance Attribute Details

### #utf16be ⇒ `String` (readonly)

UTF-16BE decdump

Returns:

- (`String`) —

  UTF-16BE decdump

    15
    16
    17
    # File 'lib/unisec/decdump.rb', line 15

    def utf16be
      @utf16be
    end

### #utf16le ⇒ `String` (readonly)

UTF-16LE decdump

Returns:

- (`String`) —

  UTF-16LE decdump

    19
    20
    21
    # File 'lib/unisec/decdump.rb', line 19

    def utf16le
      @utf16le
    end

### #utf32be ⇒ `String` (readonly)

UTF-32BE decdump

Returns:

- (`String`) —

  UTF-32BE decdump

    23
    24
    25
    # File 'lib/unisec/decdump.rb', line 23

    def utf32be
      @utf32be
    end

### #utf32le ⇒ `String` (readonly)

UTF-32LE decdump

Returns:

- (`String`) —

  UTF-32LE decdump

    27
    28
    29
    # File 'lib/unisec/decdump.rb', line 27

    def utf32le
      @utf32le
    end

### #utf8 ⇒ `String` (readonly)

UTF-8 decdump

Returns:

- (`String`) —

  UTF-8 decdump

    11
    12
    13
    # File 'lib/unisec/decdump.rb', line 11

    def utf8
      @utf8
    end

## Class Method Details

### .utf16be(str) ⇒ `String`

Encode to UTF-16BE in decdump format (packed by code unit = every 2 bytes)

#### Examples:

    Unisec::Decdump.utf16be('🐋') # => "|216 061| |220 011|"

Parameters:

- str (`String`) —

  Input string to encode

Returns:

- (`String`) —

  decdump (UTF-16BE encoded)

    58
    59
    60
    61
    62
    63
    # File 'lib/unisec/decdump.rb', line 58

    def self.utf16be(str)
      dec_chuncks = str.encode('UTF-16BE').to_hex.scan(/.{2}/).map do |x|
        x.hex2dec(padding: 3)
      end
      dec_chuncks.join(' ').scan(/\d+ \d+/).map { |x| "|#{x}|" }.join(' ')
    end

### .utf16le(str) ⇒ `String`

Encode to UTF-16LE in decdump format (packed by code unit = every 2 bytes)

#### Examples:

    Unisec::Decdump.utf16le('🐋') # => "|061 216| |011 220|"

Parameters:

- str (`String`) —

  Input string to encode

Returns:

- (`String`) —

  decdump (UTF-16LE encoded)

    70
    71
    72
    73
    74
    75
    # File 'lib/unisec/decdump.rb', line 70

    def self.utf16le(str)
      dec_chuncks = str.encode('UTF-16LE').to_hex.scan(/.{2}/).map do |x|
        x.hex2dec(padding: 3)
      end
      dec_chuncks.join(' ').scan(/\d+ \d+/).map { |x| "|#{x}|" }.join(' ')
    end

### .utf32be(str) ⇒ `String`

Encode to UTF-32BE in decdump format (packed by code unit = every 4 bytes)

#### Examples:

    Unisec::Decdump.utf32be('🐋') # => "|000 001 244 011|"

Parameters:

- str (`String`) —

  Input string to encode

Returns:

- (`String`) —

  decdump (UTF-32BE encoded)

    82
    83
    84
    85
    86
    87
    # File 'lib/unisec/decdump.rb', line 82

    def self.utf32be(str)
      dec_chuncks = str.encode('UTF-32BE').to_hex.scan(/.{2}/).map do |x|
        x.hex2dec(padding: 3)
      end
      dec_chuncks.join(' ').scan(/\d+ \d+ \d+ \d+/).map { |x| "|#{x}|" }.join(' ')
    end

### .utf32le(str) ⇒ `String`

Encode to UTF-32LE in decdump format (packed by code unit = every 4 bytes)

#### Examples:

    Unisec::Decdump.utf32le('🐋') # => "|011 244 001 000|"

Parameters:

- str (`String`) —

  Input string to encode

Returns:

- (`String`) —

  decdump (UTF-32LE encoded)

    94
    95
    96
    97
    98
    99
    # File 'lib/unisec/decdump.rb', line 94

    def self.utf32le(str)
      dec_chuncks = str.encode('UTF-32LE').to_hex.scan(/.{2}/).map do |x|
        x.hex2dec(padding: 3)
      end
      dec_chuncks.join(' ').scan(/\d+ \d+ \d+ \d+/).map { |x| "|#{x}|" }.join(' ')
    end

### .utf8(str) ⇒ `String`

Encode to UTF-8 in decdump format (spaced at every code unit = every byte)

#### Examples:

    Unisec::Decdump.utf8('🐋') # => "240 159 144 139"

Parameters:

- str (`String`) —

  Input string to encode

Returns:

- (`String`) —

  decdump (UTF-8 encoded)

    49
    50
    51
    # File 'lib/unisec/decdump.rb', line 49

    def self.utf8(str)
      str.encode('UTF-8').to_hex.scan(/.{2}/).map { |x| x.hex2dec(padding: 3) }.join(' ')
    end

## Instance Method Details

### #display ⇒ `String`

Display a CLI-friendly output summurizing the decdump in all Unicode encodings

#### Examples:

    puts Unisec::Decdump.new('K').display # =>
    # UTF-8: 226 132 170
    # UTF-16BE: |033 042|
    # UTF-16LE: |042 033|
    # UTF-32BE: |000 000 033 042|
    # UTF-32LE: |042 033 000 000|

Returns:

- (`String`) —

  CLI-ready output

    110
    111
    112
    113
    114
    115
    116
    # File 'lib/unisec/decdump.rb', line 110

    def display
      "UTF-8: #{@utf8}\n" \
      "UTF-16BE: #{@utf16be}\n" \
      "UTF-16LE: #{@utf16le}\n" \
      "UTF-32BE: #{@utf32be}\n" \
      "UTF-32LE: #{@utf32le}".gsub('|', Paint['|', :red])
    end