Module: Unisec::Utils::String
- Defined in:
- lib/unisec/utils.rb
Overview
About string conversion and manipulation.
Class Method Summary collapse
-
.autodetect(str) ⇒ Symbol
Internal method used for String.convert.
-
.convert(input, target_type) ⇒ Variable
Convert a string input into the chosen type.
-
.convert_to_integer(input) ⇒ Integer
Internal method used for String.convert.
-
.grapheme_reverse(str) ⇒ String
Reverse a string by graphemes (not by code points).
Class Method Details
.autodetect(str) ⇒ Symbol
Internal method used for convert.
Autodetect the representation type of the string input.
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.
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.
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)
108 109 110 |
# File 'lib/unisec/utils.rb', line 108 def self.grapheme_reverse(str) str.grapheme_clusters.reverse.join end |