Last active
September 22, 2015 19:09
-
-
Save hanabokuro/10104634 to your computer and use it in GitHub Desktop.
String#undump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def undump | |
self.sub(/\A"/, '').sub(/"\z/, '').gsub(/\\(x([0-9a-f]{2})) | \\(u\{([0-9a-f]{4})\}) | \\(.) /ix) { | |
if $1 # \xXX | |
[$2.hex].pack("C") | |
elsif $3 # \u{xxxx} | |
[$4.hex].pack("U") | |
else # \. | |
case $5 | |
when 't' | |
"\t" | |
when 'v' | |
"\v" | |
when 'n' | |
"\n" | |
when 'r' | |
"\r" | |
when 'f' | |
"\f" | |
when 'b' | |
"\b" | |
when 'a' | |
"\a" | |
when 'e' | |
"\e" | |
when 's' | |
" " | |
else | |
$5 | |
end | |
end | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment