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.
-
.char2codepoint(chr) ⇒ String
Display the code point in Unicode format for a given character (code point as string).
-
.chars2codepoints(chrs) ⇒ String
Display the code points in Unicode format for the given characters (code points as string).
-
.chars2intcodepoints(chrs) ⇒ String
Display the code points in integer format for the given characters (code points as string).
-
.convert(input, target_type) ⇒ Variable
Convert a string input into the chosen type.
-
.convert_to_char(input) ⇒ String
Internal method used for String.convert.
-
.convert_to_integer(input) ⇒ Integer
Internal method used for String.convert.
-
.grapheme_reverse(str) ⇒ String
Reverse a string by graphemes (not by code points).
-
.to_range(range_str) ⇒ Range
Convert a string of hex encoded Unicode code points range to actual integer Ruby range.
Class Method Details
.autodetect(str) ⇒ Symbol
Internal method used for convert.
Autodetect the representation type of the string input.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/unisec/utils.rb', line 155 def self.autodetect(str) case str when /0x[0-9a-fA-F]+/ :hexadecimal when /U\+[0-9A-F]+/ :stdcp when /0d[0-9]+/ :decimal when /0b[0-1]+/ :binary else :string end end |
.char2codepoint(chr) ⇒ String
Replace this method by target type :stdcp in String.convert()
Display the code point in Unicode format for a given character (code point as string)
186 187 188 |
# File 'lib/unisec/utils.rb', line 186 def self.char2codepoint(chr) Integer.deccp2stdhexcp(chr.codepoints.first) end |
.chars2codepoints(chrs) ⇒ String
Display the code points in Unicode format for the given characters (code points as string)
196 197 198 199 200 201 202 |
# File 'lib/unisec/utils.rb', line 196 def self.chars2codepoints(chrs) out = [] chrs.each_char do |chr| out << char2codepoint(chr) end out.join(' ') end |
.chars2intcodepoints(chrs) ⇒ String
Display the code points in integer format for the given characters (code points as string)
209 210 211 |
# File 'lib/unisec/utils.rb', line 209 def self.chars2intcodepoints(chrs) chrs.codepoints.map(&:to_s).join(' ') end |
.convert(input, target_type) ⇒ Variable
Convert a string input into the chosen type.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/unisec/utils.rb', line 66 def self.convert(input, target_type) case target_type when :integer convert_to_integer(input) when :char convert_to_char(input) else raise TypeError, "Target type \"#{target_type}\" not avaible" end end |
.convert_to_char(input) ⇒ String
Internal method used for convert.
Convert a string input into a character.
130 131 132 133 134 135 136 137 |
# File 'lib/unisec/utils.rb', line 130 def self.convert_to_char(input) case autodetect(input) when :hexadecimal, :stdcp, :decimal, :binary, :string [convert(input, :integer)].pack('U') else raise TypeError, "Input \"#{input}\" is not of the expected type" end end |
.convert_to_integer(input) ⇒ Integer
Internal method used for convert.
Convert a string input into integer.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/unisec/utils.rb', line 95 def self.convert_to_integer(input) case autodetect(input) when :hexadecimal input.hex2dec(prefix: '0x').to_i when :stdcp input.hex2dec(prefix: 'U+').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)
176 177 178 |
# File 'lib/unisec/utils.rb', line 176 def self.grapheme_reverse(str) str.grapheme_clusters.reverse.join end |