Very nasty piece of code

Whilest writing my RTF tools library I had a need to convert from &#XX;
(XML entity style) into 'XX (RTF entity style). I ended up with this
monstrosity…

while string =~ /(&#([0-9a-fA-F]{2});)/ do

Converting from &#XX; to 'XX

string.sub!($1, “\\\’’#{$2}”)
end

It seems to work but what is going on here? It looks like Java or TCL on
a bad day and was only arrived at by adding random numbers of \ and '
until it worked.

Surely there is a better way to do this.

Hello Peter,

Monday, January 27, 2003, 12:44:41 PM, you wrote:

while string =~ /(&#([0-9a-fA-F]{2});)/ do

Converting from &#XX; to 'XX

string.sub!($1, “\\\''#{$2}”)

smth like

string.sub! $1 {“\'#{$2}”}

or even

string.sub! $1 {“\'” . $2}

2all: this may go to faq?

···


Best regards,
Bulat mailto:bulatz@integ.ru

[…]

while string =~ /(&#([0-9a-fA-F]{2});)/ do
# Converting from &#XX; to 'XX

string.sub!($1, "\\\\\\\''#{$2}")

end

string.gsub!(/&#[0-9a-fA-F]{2};/) {|x| “\'#{x[2,2]}” }

is faster (your code has quadratic complexity) and a bit more concise.

	Reimer Behrends
···

Peter Hickman (peter@semantico.com) wrote:

Hello Peter,

Monday, January 27, 2003, 12:44:41 PM, you wrote:

Converting from &#XX; to 'XX

Surely there is a better way to do this.

sorry. below is tested code :slight_smile:

string=" � "
string.gsub! (/&#([0-9a-fA-F]{2});/) {“\'” + $1}
print string

ps: see standart types/regular expressions/Backslash Sequences in the Substitution
in pickaxe book

···


Best regards,
Bulat mailto:bulatz@integ.ru

Bulat Ziganshin wrote:

Hello Peter,

Monday, January 27, 2003, 12:44:41 PM, you wrote:

while string =~ /(&#([0-9a-fA-F]{2});)/ do

Converting from &#XX; to 'XX

string.sub!($1, “\\\''#{$2}”)

smth like

string.sub! $1 {“\'#{$2}”}

or even

string.sub! $1 {“\'” . $2}

2all: this may go to faq?

Neither of those actually parse!

What version of Ruby are you using.

Hello Peter,

Monday, January 27, 2003, 1:02:35 PM, you wrote:

string.sub! $1 {“\'” . $2}

sorry, use

string.sub!($1) {“\'” . $2}

···


Best regards,
Bulat mailto:bulatz@integ.ru

Nope still broken. Remember string sub does some very nasty things with
\ and '.

[peter@wasabi peter]$ ruby fred
fred:6: parse error
string.sub!($1) {"\’" . $2}
^