Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > interpolation of escaped characters in strings

Reply
Thread Tools

interpolation of escaped characters in strings

 
 
Eyal Oren
Guest
Posts: n/a
 
      02-20-2007
Hi,

I've searched through the newsgroup, found some related stuff (about
'string interpolation at will') but couldn't quite figure it out
completely

My question: I read a string from file, using 'readlines', containing
special characters, especially \n. These characters are not
substituted into their binary control characters, e.g. my strings act
as if created with 'test\abc' instead of "test\nabc". I would like
to convert these strings and have Ruby take account of the special
meaning of the escaped characters, so that I can print them out
nicely.
I see two ways:
- use gsub: str.gsub('\n',"\n"), but this solves only \n, so I need to
handle each possible escaped character manually
- use eval: eval('"' + str + '"'), but this is dangerous, for example
I had one string that ends with \, thereby breaking the eval code
since the slash escapes the closing quote.

Is there a better solution? I have the feeling that unpack would help,
but I simply can't understand the documentation of unpack. I found
some discussions on str.interpolate, but I don't care for
interpolation of Ruby code, I only want replacement of these special
characters.

(I had the same problem for unicode escapes \uxxxx, but I solved that
with some unpack code from the web)

 
Reply With Quote
 
 
 
 
Xavier Noria
Guest
Posts: n/a
 
      02-20-2007

On Feb 20, 2007, at 7:10 PM, Eyal Oren wrote:

> Hi,
>
> I've searched through the newsgroup, found some related stuff (about
> 'string interpolation at will') but couldn't quite figure it out
> completely
>
> My question: I read a string from file, using 'readlines', containing
> special characters, especially \n. These characters are not
> substituted into their binary control characters, e.g. my strings act
> as if created with 'test\abc' instead of "test\nabc". I would like
> to convert these strings and have Ruby take account of the special
> meaning of the escaped characters, so that I can print them out
> nicely.


Looks like you want something close to String#inspect.

puts "foo\tbar\n".inspect # -> "foo\tbar\n"

-- fxn


 
Reply With Quote
 
 
 
 
Eyal Oren
Guest
Posts: n/a
 
      02-21-2007
On Feb 20, 6:23 pm, Xavier Noria <(E-Mail Removed)> wrote:
> Looks like you want something close to String#inspect.
> puts "foo\tbar\n".inspect # -> "foo\tbar\n"


no, that doesn't work. what I would like is that puts would actually
print a tab and a newline, instead of \t and \n.

E.g. a = 'test\nbreak'. Then "puts a.inspect" doesn't actually print a
line break, it prints the characters '\n'. I would like to transform
"a" into a string that really includes a linebreak instead of the
characters '\n'.

Does anybody know how to do it, without eval or a manually crafted
gsub?

 
Reply With Quote
 
Harold Hausman
Guest
Posts: n/a
 
      02-21-2007
On 2/21/07, Eyal Oren <(E-Mail Removed)> wrote:
> E.g. a = 'test\nbreak'. Then "puts a.inspect" doesn't actually print a
> line break, it prints the characters '\n'. I would like to transform
> "a" into a string that really includes a linebreak instead of the
> characters '\n'.
>


It looks like you're running into the fact that when you create a
string literal with a single quote (') it's treated differently than
when you construct the literal with double quotes...

Observe:
irb(main):001:0> a = 'test\nbreak'
=> "test\\nbreak"
irb(main):002:0> puts a
test\nbreak
=> nil
irb(main):003:0> puts 'whoops'
whoops
=> nil
irb(main):004:0> a = "test\n\break"
=> "test\n\break"
irb(main):005:0> puts a
test
reak
=> nil
irb(main):006:0> puts 'sweet!'
sweet!
=> nil

hth,
-Harold

 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      02-21-2007
On Feb 21, 2007, at 1:10 PM, Harold Hausman wrote:

> On 2/21/07, Eyal Oren <(E-Mail Removed)> wrote:
>> E.g. a = 'test\nbreak'. Then "puts a.inspect" doesn't actually
>> print a
>> line break, it prints the characters '\n'. I would like to transform
>> "a" into a string that really includes a linebreak instead of the
>> characters '\n'.
>>

>
> It looks like you're running into the fact that when you create a
> string literal with a single quote (') it's treated differently than
> when you construct the literal with double quotes...


Problem is he reads the string 'foo\nbar' from an external source,
that is, the string has a literal slash and a literal n within. He
wants a nice way to convert that and any other escape sequence into
an actual escape sequence.

-- fxn


 
Reply With Quote
 
Eyal Oren
Guest
Posts: n/a
 
      02-21-2007
On 02/21/07/02/07 21:10 +0900, Harold Hausman wrote:
>It looks like you're running into the fact that when you create a
>string literal with a single quote (') it's treated differently than
>when you construct the literal with double quotes...

Yes. I know. My question is: how to convert the one variant ('') into the
other variant ("") at runtime?

As I said in my first post, I can do that using eval or using gsub, but
both have problems. Does anybody know another way of converting ''-strings
into ""-ones?

-eyal

 
Reply With Quote
 
Harold Hausman
Guest
Posts: n/a
 
      02-21-2007
On 2/21/07, Xavier Noria <(E-Mail Removed)> wrote:
> On Feb 21, 2007, at 1:10 PM, Harold Hausman wrote:
>
> > On 2/21/07, Eyal Oren <(E-Mail Removed)> wrote:
> >> E.g. a = 'test\nbreak'. Then "puts a.inspect" doesn't actually
> >> print a
> >> line break, it prints the characters '\n'. I would like to transform
> >> "a" into a string that really includes a linebreak instead of the
> >> characters '\n'.
> >>

> >
> > It looks like you're running into the fact that when you create a
> > string literal with a single quote (') it's treated differently than
> > when you construct the literal with double quotes...

>
> Problem is he reads the string 'foo\nbar' from an external source,
> that is, the string has a literal slash and a literal n within. He
> wants a nice way to convert that and any other escape sequence into
> an actual escape sequence.
>


Dah!

I didn't even see that there were even earlier messages. I apologize.
What's worse, is that even after looking at this for 20+ mins, I still
don't see a way to cleanly do it. Once that backslash is escaped in
that string, you're pretty much hosed.

I think gsub is your best option here. Though, I'd love to to surprised. (:

Apologies,
-Harold

 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      02-21-2007
On Wed, 21 Feb 2007, Eyal Oren wrote:

> On Feb 20, 6:23 pm, Xavier Noria <(E-Mail Removed)> wrote:
>> Looks like you want something close to String#inspect.
>> puts "foo\tbar\n".inspect # -> "foo\tbar\n"

>
> no, that doesn't work. what I would like is that puts would actually
> print a tab and a newline, instead of \t and \n.
>
> E.g. a = 'test\nbreak'. Then "puts a.inspect" doesn't actually print a
> line break, it prints the characters '\n'. I would like to transform
> "a" into a string that really includes a linebreak instead of the
> characters '\n'.
>
> Does anybody know how to do it, without eval or a manually crafted
> gsub?
>
>


why are you avoiding eval?

harp:~ > cat a.rb
class String
def double_quote
Thread.new do
$SAFE = 12
begin
eval('"%s"' % self)
rescue Exception => e
e
end
end.value
end
end

s = 'foo\tbar\nfoobar'
p s
p s.double_quote
puts s.double_quote


harp:~ > ruby a.rb
"foo\\tbar\\nfoobar"
"foo\tbar\nfoobar"
foo bar
foobar


seems like the obvious thing to so

$SAFE is.

regards.

-a
--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

 
Reply With Quote
 
Eyal Oren
Guest
Posts: n/a
 
      02-21-2007
On 02/22/07/02/07 01:17 +0900, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>why are you avoiding eval?
> harp:~ > cat a.rb
> class String
> def double_quote
> Thread.new do
> $SAFE = 12
> begin
> eval('"%s"' % self)
> rescue Exception => e
> e
> end
> end.value
> end
> end
>
>seems like the obvious thing to so $SAFE is.

Thanks, I hadn't thought of using SAFE. My problem is that I encounter
not-wellformed strings in the data, e.g. 'hello\', which break evaluation.
But now I catch the exception and just return the string un-evaluated,
which is fine, because those escape syntaxes are not allowed anyway.

So thanks, I've used your solution.

-eyal

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
servlet request handling - escaped characters in request Thea Java 2 09-22-2006 12:08 PM
Convert Raw Text Escaped Characters to Characters nicholas.wakefield@gmail.com Java 2 07-11-2005 09:17 PM
Minidom output of XML escaped characters Derek Basch Python 2 03-25-2005 05:25 PM
Translating escaped characters Henrik S. Hansen Python 2 11-02-2004 07:12 AM
Translate escaped characters in a string Ben Finney Python 2 04-03-2004 07:46 AM



Advertisments