A
Andrew S. Townley
Hi,
I was wondering if there was a better/safer way to parse string data
that has been dumped using the String#dump method. At the moment, I've
been using regular expressions to do it, but that doesn't seem to work
with unicode characters, since they get dumped as follows:
irb(main):007:0> s =3D "=E2=82=AC"
=3D> "=E2=82=AC"
irb(main):008:0> s.dump
=3D> "\"\\342\\202\\254\""
On a lark, I figured that Ruby would be able to get them back, so I
tried this:
irb(main):009:0> x =3D eval s.dump
=3D> "=E2=82=AC"
Which, of course, works. However, I'm a bit leery of doing this from a
safety perspective, because I really don't have any control over these
strings, and I'd prefer not to allow the execution of arbitrary Ruby
code every time I'm trying to restore strings (I need them serialized as
appropriately escaped quoted literals).
Has anyone else ever needed to do this, and, if so, how did you solve
the problem. I guess I could do another pass on the string looking for
'\\[\n]+' values and try and combine them some way, but I'm not really
sure how to do that either.
Any ideas?
Cheers,
ast
--=20
Andrew S. Townley <[email protected]>
http://atownley.org
I was wondering if there was a better/safer way to parse string data
that has been dumped using the String#dump method. At the moment, I've
been using regular expressions to do it, but that doesn't seem to work
with unicode characters, since they get dumped as follows:
irb(main):007:0> s =3D "=E2=82=AC"
=3D> "=E2=82=AC"
irb(main):008:0> s.dump
=3D> "\"\\342\\202\\254\""
On a lark, I figured that Ruby would be able to get them back, so I
tried this:
irb(main):009:0> x =3D eval s.dump
=3D> "=E2=82=AC"
Which, of course, works. However, I'm a bit leery of doing this from a
safety perspective, because I really don't have any control over these
strings, and I'd prefer not to allow the execution of arbitrary Ruby
code every time I'm trying to restore strings (I need them serialized as
appropriately escaped quoted literals).
Has anyone else ever needed to do this, and, if so, how did you solve
the problem. I guess I could do another pass on the string looking for
'\\[\n]+' values and try and combine them some way, but I'm not really
sure how to do that either.
Any ideas?
Cheers,
ast
--=20
Andrew S. Townley <[email protected]>
http://atownley.org