Module: Unisec::Utils::String

Defined in:
lib/unisec/utils.rb

Overview

About string conversion and manipulation.

Class Method Summary collapse

Class Method Details

.autodetect(str) ⇒ Symbol

Internal method used for convert.

Autodetect the representation type of the string input.

Examples:

# Hexadecimal
Unisec::Utils::String.autodetect('0x1f4a9') # => :hexadecimal
# Decimal
Unisec::Utils::String.autodetect('0d128169') # => :decimal
# Binary
Unisec::Utils::String.autodetect('0b11111010010101001') # => :binary
# Unicode string
Unisec::Utils::String.autodetect('💩') # => :string

Parameters:

Returns:

  • (Symbol)

    the detected type: :hexadecimal, :decimal, :binary, :string.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/unisec/utils.rb', line 89

def self.autodetect(str)
  case str
  when /0x[0-9a-fA-F]/
    :hexadecimal
  when /0d[0-9]+/
    :decimal
  when /0b[0-1]+/
    :binary
  else
    :string
  end
end

.convert(input, target_type) ⇒ Variable

Convert a string input into the chosen type.

Examples:

Unisec::Utils::String.convert('0x1f4a9', :integer) # => 128169

Parameters:

  • input (String)

    If the target type is :integer, the string must represent a number encoded in hexadecimal, decimal, binary. If it's a Unicode string, only the first code point will be taken into account.

  • target_type (Symbol)

    Convert to the chosen type. Currently only supports :integer.

Returns:

  • (Variable)

    The type of the output depends on the chosen target_type.



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

def self.convert(input, target_type)
  case target_type
  when :integer
    convert_to_integer(input)
  else
    raise TypeError, "Target type \"#{target_type}\" not avaible"
  end
end

.convert_to_integer(input) ⇒ Integer

Internal method used for convert.

Convert a string input into integer.

Examples:

# Hexadecimal
Unisec::Utils::String.convert_to_integer('0x1f4a9') # => 128169
# Decimal
Unisec::Utils::String.convert_to_integer('0d128169') # => 128169
# Binary
Unisec::Utils::String.convert_to_integer('0b11111010010101001') # => 128169
# Unicode string
Unisec::Utils::String.convert_to_integer('💩') # => 128169

Parameters:

  • input (String)

    The string must represent a number encoded in hexadecimal, decimal, binary. If it's a Unicode string, only the first code point will be taken into account. The input type is determined automatically based on the prefix.

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/unisec/utils.rb', line 60

def self.convert_to_integer(input)
  case autodetect(input)
  when :hexadecimal
    input.hex2dec(prefix: '0x').to_i
  when :decimal
    input.to_i
  when :binary
    input.bin2hex.hex2dec.to_i
  when :string
    input.codepoints.first
  else
    raise TypeError, "Input \"#{input}\" is not of the expected type"
  end
end

.grapheme_reverse(str) ⇒ String

Reverse a string by graphemes (not by code points)

Examples:

b = "\u{1f1eb}\u{1f1f7}\u{1F413}" # => "🇫🇷🐓"
b.reverse # => "🐓🇷🇫"
Unisec::Utils::String.grapheme_reverse(b) # => "🐓🇫🇷"

Returns:

  • (String)

    the reversed string



108
109
110
# File 'lib/unisec/utils.rb', line 108

def self.grapheme_reverse(str)
  str.grapheme_clusters.reverse.join
end