Method: String#unescape

Defined in:
lib/ronin/formatting/extensions/text/string.rb

#unescapeString Also known as: hex_unescape

Unescapes the escaped String.

Examples:

"\\x68\\x65\\x6c\\x6c\\x6f".unescape
# => "hello"

Returns:

  • (String)

    The unescaped version of the hex escaped String.

Since:

  • 0.5.0



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/ronin/formatting/extensions/text/string.rb', line 269

def unescape
  buffer     = ''
  hex_index  = 0
  hex_length = length

  while (hex_index < hex_length)
    hex_substring = self[hex_index..-1]

    if hex_substring =~ /^\\[0-7]{3}/
      buffer    << hex_substring[0,4].to_i(8)
      hex_index += 3
    elsif hex_substring =~ /^\\x[0-9a-fA-F]{1,2}/
      hex_substring[2..-1].scan(/^[0-9a-fA-F]{1,2}/) do |hex_byte|
        buffer    << hex_byte.to_i(16)
        hex_index += (2 + hex_byte.length)
      end
    elsif hex_substring =~ /^\\./
      buffer    << UNESCAPE_CHARS[hex_substring[0,2]]
      hex_index += 2
    else
      buffer    << hex_substring[0,1]
      hex_index += 1
    end
  end

  return buffer
end