A
ako...
hello,
i need to write a function that would parse a string literal in another
language. a string literal in this language is:
STRING = "CHAR*"
CHAR = any character except for " and \
| \"
| \\
| \/
| \u four hexadecimal digits
the \u sequence specifies a character in UTF-16 encoding.
for example: "abc", "", "a\"bc", "a\\b", "a\u12bfc"
below is the code that i wrote. is this Ruby enough? can someone
suggest improvements? a better style?
thanks
konstantin
def parselit(s)
r = %r{\\"|\\/|\\\\|\\u[\da-f][\da-f][\da-f][\da-f]}i
s =~ /^"((?:[^"\\]|#{r})*)"$/ && $1.gsub(r) { |x| x =~ /\\u(.*)/ ?
[$1.hex].pack('U*') : x[1..-1] }
end
puts parselit('"\u004e\"a"')
i need to write a function that would parse a string literal in another
language. a string literal in this language is:
STRING = "CHAR*"
CHAR = any character except for " and \
| \"
| \\
| \/
| \u four hexadecimal digits
the \u sequence specifies a character in UTF-16 encoding.
for example: "abc", "", "a\"bc", "a\\b", "a\u12bfc"
below is the code that i wrote. is this Ruby enough? can someone
suggest improvements? a better style?
thanks
konstantin
def parselit(s)
r = %r{\\"|\\/|\\\\|\\u[\da-f][\da-f][\da-f][\da-f]}i
s =~ /^"((?:[^"\\]|#{r})*)"$/ && $1.gsub(r) { |x| x =~ /\\u(.*)/ ?
[$1.hex].pack('U*') : x[1..-1] }
end
puts parselit('"\u004e\"a"')