Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string-undump.rb

Instance Method Summary collapse

Instance Method Details

#undump_badlyObject Also known as: undump

todo: eval in ‘#some_code’ or ‘#@var’ or ‘#$var’ ???



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/string-undump.rb', line 3

def undump_badly
  hex = /[0-9a-fA-F]/
  esctable = {
    '\n' => "\n",
    '\r' => "\r",
    '\t' => "\t",
    '\f' => "\f",
    '\v' => "\v",
    '\b' => "\b",
    '\a' => "\a",
    '\e' => "\e",
  }
  e = self.encoding
  s = if self[0] == '"' && self[-1] == '"'
      self[1..-2]
    else
      self.dup
    end
  s.gsub!(/\\"/, '"')
  s.gsub!(/\\\\/, '\\')
  s.gsub!(/\\[nrtfvbae]/) {|m| esctable[m]}
  s.gsub!(/\\u#{hex}{4}/) {|m| m[2..-1].hex.chr(e)}
  s.gsub!(/\\[ux]{#{hex}+}/) {|m| m[3..-1].hex.chr(e)}
  s.gsub!(/(?:\\x#{hex}{2})+/) {|m|
    m.gsub(/\\x/, '').scan(/../).map(&:hex).pack("C*").force_encoding(e)
  }
  s
end