Class: Unisec::Size — Documentation by YARD 0.9.44

# Class: Unisec::Size

Inherits:
  Object
  - Object
  - Unisec::Size
  show all

Defined in:
  lib/unisec/size.rb

## Overview

All kinf of size information about a Unicode string

## Instance Attribute Summary collapse

- #code_points_size ⇒ Integer readonly

  Number of code points.

- #grapheme_size ⇒ Integer readonly

  Number of graphemes.

- #utf16_bytesize ⇒ Integer readonly

  UTF-16 size in bytes.

- #utf16_unitsize ⇒ Integer readonly

  Number of UTF-16 units.

- #utf32_bytesize ⇒ Integer readonly

  UTF-32 size in bytes.

- #utf32_unitsize ⇒ Integer readonly

  Number of UTF-32 units.

- #utf8_bytesize ⇒ Integer readonly

  UTF-8 size in bytes.

- #utf8_unitsize ⇒ Integer readonly

  Number of UTF-8 units.

## Class Method Summary collapse

- .code_points_size(str) ⇒ Integer

  Number of code points.

- .grapheme_size(str) ⇒ Integer

  Number of graphemes.

- .utf16_bytesize(str) ⇒ Integer

  UTF-16 size in bytes.

- .utf16_unitsize(str) ⇒ Integer

  Number of UTF-16 units.

- .utf32_bytesize(str) ⇒ Integer

  UTF-32 size in bytes.

- .utf32_unitsize(str) ⇒ Integer

  Number of UTF-32 units.

- .utf8_bytesize(str) ⇒ Integer

  UTF-8 size in bytes.

- .utf8_unitsize(str) ⇒ Integer

  Number of UTF-8 units.

## Instance Method Summary collapse

- #display ⇒ Object

  Display a CLI-friendly output summurizing the size information about a Unicode string.

- #initialize(str) ⇒ Size constructor

  A new instance of Size.

## Constructor Details

### #initialize(str) ⇒ `Size`

Returns a new instance of Size.

    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    # File 'lib/unisec/size.rb', line 64

    def initialize(str)
      @code_points_size = Size.code_points_size(str)
      @grapheme_size = Size.grapheme_size(str)
      @utf8_bytesize = Size.utf8_bytesize(str)
      @utf16_bytesize = Size.utf16_bytesize(str)
      @utf32_bytesize = Size.utf32_bytesize(str)
      @utf8_unitsize = Size.utf8_unitsize(str)
      @utf16_unitsize = Size.utf16_unitsize(str)
      @utf32_unitsize = Size.utf32_unitsize(str)
    end

## Instance Attribute Details

### #code_points_size ⇒ `Integer` (readonly)

Number of code points

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.code_points_size # => 6

Returns:

- (`Integer`) —

  number of code points

    13
    14
    15
    # File 'lib/unisec/size.rb', line 13

    def code_points_size
      @code_points_size
    end

### #grapheme_size ⇒ `Integer` (readonly)

Number of graphemes

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.grapheme_size # => 1

Returns:

- (`Integer`) —

  number of graphemes

    20
    21
    22
    # File 'lib/unisec/size.rb', line 20

    def grapheme_size
      @grapheme_size
    end

### #utf16_bytesize ⇒ `Integer` (readonly)

UTF-16 size in bytes

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.utf16_bytesize # => 16

Returns:

- (`Integer`) —

  UTF-16 size in bytes

    34
    35
    36
    # File 'lib/unisec/size.rb', line 34

    def utf16_bytesize
      @utf16_bytesize
    end

### #utf16_unitsize ⇒ `Integer` (readonly)

Number of UTF-16 units

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.utf16_unitsize # => 8

Returns:

- (`Integer`) —

  number of UTF-16 units

    55
    56
    57
    # File 'lib/unisec/size.rb', line 55

    def utf16_unitsize
      @utf16_unitsize
    end

### #utf32_bytesize ⇒ `Integer` (readonly)

UTF-32 size in bytes

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.utf32_bytesize # => 24

Returns:

- (`Integer`) —

  UTF-32 size in bytes

    41
    42
    43
    # File 'lib/unisec/size.rb', line 41

    def utf32_bytesize
      @utf32_bytesize
    end

### #utf32_unitsize ⇒ `Integer` (readonly)

Number of UTF-32 units

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.utf32_unitsize # => 6

Returns:

- (`Integer`) —

  number of UTF-32 units

    62
    63
    64
    # File 'lib/unisec/size.rb', line 62

    def utf32_unitsize
      @utf32_unitsize
    end

### #utf8_bytesize ⇒ `Integer` (readonly)

UTF-8 size in bytes

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.utf8_bytesize # => 20

Returns:

- (`Integer`) —

  UTF-8 size in bytes

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

    def utf8_bytesize
      @utf8_bytesize
    end

### #utf8_unitsize ⇒ `Integer` (readonly)

Number of UTF-8 units

#### Examples:

    us = Unisec::Size.new('👩‍❤️‍👩')
    us.utf8_unitsize # => 20

Returns:

- (`Integer`) —

  number of UTF-8 units

    48
    49
    50
    # File 'lib/unisec/size.rb', line 48

    def utf8_unitsize
      @utf8_unitsize
    end

## Class Method Details

### .code_points_size(str) ⇒ `Integer`

Number of code points

#### Examples:

    Unisec::Size.code_points_size('👩‍❤️‍👩') # => 6

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  number of code points

    80
    81
    82
    # File 'lib/unisec/size.rb', line 80

    def self.code_points_size(str)
      str.size
    end

### .grapheme_size(str) ⇒ `Integer`

Number of graphemes

#### Examples:

    Unisec::Size.grapheme_size('👩‍❤️‍👩') # => 1

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  number of graphemes

    89
    90
    91
    # File 'lib/unisec/size.rb', line 89

    def self.grapheme_size(str)
      str.grapheme_clusters.size
    end

### .utf16_bytesize(str) ⇒ `Integer`

UTF-16 size in bytes

#### Examples:

    Unisec::Size.utf16_bytesize('👩‍❤️‍👩') # => 16

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  UTF-16 size in bytes

    107
    108
    109
    # File 'lib/unisec/size.rb', line 107

    def self.utf16_bytesize(str)
      str.encode('UTF-16BE').bytesize
    end

### .utf16_unitsize(str) ⇒ `Integer`

Number of UTF-16 units

#### Examples:

    Unisec::Size.utf16_unitsize('👩‍❤️‍👩') # => 8

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  number of UTF-16 units

    134
    135
    136
    # File 'lib/unisec/size.rb', line 134

    def self.utf16_unitsize(str)
      utf16_bytesize(str) / 2
    end

### .utf32_bytesize(str) ⇒ `Integer`

UTF-32 size in bytes

#### Examples:

    Unisec::Size.utf32_bytesize('👩‍❤️‍👩') # => 24

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  UTF-32 size in bytes

    116
    117
    118
    # File 'lib/unisec/size.rb', line 116

    def self.utf32_bytesize(str)
      str.encode('UTF-32BE').bytesize
    end

### .utf32_unitsize(str) ⇒ `Integer`

Number of UTF-32 units

#### Examples:

    Unisec::Size.utf32_unitsize('👩‍❤️‍👩') # => 6

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  number of UTF-32 units

    143
    144
    145
    # File 'lib/unisec/size.rb', line 143

    def self.utf32_unitsize(str)
      utf32_bytesize(str) / 4
    end

### .utf8_bytesize(str) ⇒ `Integer`

UTF-8 size in bytes

#### Examples:

    Unisec::Size.utf8_bytesize('👩‍❤️‍👩') # => 20

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  UTF-8 size in bytes

    98
    99
    100
    # File 'lib/unisec/size.rb', line 98

    def self.utf8_bytesize(str)
      str.bytesize
    end

### .utf8_unitsize(str) ⇒ `Integer`

Number of UTF-8 units

#### Examples:

    Unisec::Size.utf8_unitsize('👩‍❤️‍👩') # => 20

Parameters:

- str (`String`) —

  Input sting we want to know the size of

Returns:

- (`Integer`) —

  number of UTF-8 units

    125
    126
    127
    # File 'lib/unisec/size.rb', line 125

    def self.utf8_unitsize(str)
      utf8_bytesize(str)
    end

## Instance Method Details

### #display ⇒ `Object`

Display a CLI-friendly output summurizing the size information about a Unicode string.

#### Examples:

    Unisec::Size.new('👩‍❤️‍👨').display
    # =>
    # Code point(s):   6
    # Grapheme(s):     1
    # UTF-8 byte(s):   20
    # UTF-16 byte(s):  16
    # UTF-32 byte(s):  24
    # UTF-8 unit(s):   20
    # UTF-16 unit(s):  8
    # UTF-32 unit(s):  6

    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    # File 'lib/unisec/size.rb', line 159

    def display
      display = ->(key, value) { puts Paint[key, :red, :bold].ljust(27) + " #{value}" }
      display.call('Code point(s):', @code_points_size)
      display.call('Grapheme(s):', @grapheme_size)
      display.call('UTF-8 byte(s):', @utf8_bytesize)
      display.call('UTF-16 byte(s):', @utf16_bytesize)
      display.call('UTF-32 byte(s):', @utf32_bytesize)
      display.call('UTF-8 unit(s):', @utf8_unitsize)
      display.call('UTF-16 unit(s):', @utf16_unitsize)
      display.call('UTF-32 unit(s):', @utf32_unitsize)
    end