Hi, I've a module with two methods (thanks Jeff):
- hex_unescape(string)
- hex_scape(string)
as follows:
def self::hex_unescape(str)
str.gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) { sprintf("%%%02X", $&.unpack("C")[0]) }
end
"hex_escape" method is copied from CGI lib, and sincerelly I don't like too
much its approach using "sprintf". Is there other way more ellegant?
(performance is the mos important requeriment anyway).
Then I am sure you _measured_ it and came to the conclusion that it is
too slow, did you? What are your results and what are your
performance requirements?
Cheers
robert
···
2009/2/23 Iñaki Baz Castillo <ibc@aliax.net>:
Hi, I've a module with two methods (thanks Jeff):
- hex_unescape(string)
- hex_scape(string)
as follows:
def self::hex_unescape(str)
str.gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) { sprintf("%%%02X", $&.unpack("C")[0]) }
end
"hex_escape" method is copied from CGI lib, and sincerelly I don't like too
much its approach using "sprintf". Is there other way more ellegant?
(performance is the mos important requeriment anyway).
--
remember.guy do |as, often| as.you_can - without end
I did a Benchmark.realtime comparing hex_unescape and hex_escape
methods. hex_unescape takes ~2.5*10^(-5) while hex_escape takes
~4*10^(-5).
Anyway I've realized right now that "sprintf" is directly implemented
as C code so it can't be faster.
Thanks.
···
2009/2/23 Robert Klemme <shortcutter@googlemail.com>:
2009/2/23 Iñaki Baz Castillo <ibc@aliax.net>:
Hi, I've a module with two methods (thanks Jeff):
- hex_unescape(string)
- hex_scape(string)
as follows:
def self::hex_unescape(str)
str.gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) { sprintf("%%%02X", $&.unpack("C")[0]) }
end
"hex_escape" method is copied from CGI lib, and sincerelly I don't like too
much its approach using "sprintf". Is there other way more ellegant?
(performance is the mos important requeriment anyway).
Then I am sure you _measured_ it and came to the conclusion that it is
too slow, did you? What are your results and what are your
performance requirements?
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) {|m| sprintf("%%%02X", m[0]) }
end
And this in 1.9
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) {|m| sprintf("%%%02X", m.getbyte(0)) }
end
Cheers
robert
···
2009/2/23 Iñaki Baz Castillo <ibc@aliax.net>:
2009/2/23 Robert Klemme <shortcutter@googlemail.com>:
2009/2/23 Iñaki Baz Castillo <ibc@aliax.net>:
Hi, I've a module with two methods (thanks Jeff):
- hex_unescape(string)
- hex_scape(string)
as follows:
def self::hex_unescape(str)
str.gsub(/%([0-9a-fA-F]{2})/) { $1.to_i(16).chr }
end
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) { sprintf("%%%02X", $&.unpack("C")[0]) }
end
"hex_escape" method is copied from CGI lib, and sincerelly I don't like too
much its approach using "sprintf". Is there other way more ellegant?
(performance is the mos important requeriment anyway).
Then I am sure you _measured_ it and came to the conclusion that it is
too slow, did you? What are your results and what are your
performance requirements?
I did a Benchmark.realtime comparing hex_unescape and hex_escape
methods. hex_unescape takes ~2.5*10^(-5) while hex_escape takes
~4*10^(-5).
Anyway I've realized right now that "sprintf" is directly implemented
as C code so it can't be faster.
--
remember.guy do |as, often| as.you_can - without end
Thanks, do you mean that "m[0]" in Ruby 1.9 has a different behaviour
than in 1.8? maybe in 1.9 "m[0]" returns the first character (even if
it's more than two bytes as "ñ", "€"...) while in 1.8 it returns just
the firrst two bytes?
PD: I've Ruby 1.9 (2007-12-25 revision 14709) and I don't have
"getbyte()" method for String.
Thanks a lot.
···
2009/2/23 Robert Klemme <shortcutter@googlemail.com>:
Well, you can at least do this in 1.8
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) {|m| sprintf("%%%02X", m[0]) }
end
And this in 1.9
def self::hex_escape(str)
str.gsub(/[^a-zA-Z0-9_\-.]/n) {|m| sprintf("%%%02X", m.getbyte(0)) }
end
2009/2/23 Robert Klemme <shortcutter@googlemail.com>:
> Well, you can at least do this in 1.8
>
> def self::hex_escape(str)
> str.gsub(/[^a-zA-Z0-9_\-.]/n) {|m| sprintf("%%%02X", m[0]) }
> end
>
> And this in 1.9
>
> def self::hex_escape(str)
> str.gsub(/[^a-zA-Z0-9_\-.]/n) {|m| sprintf("%%%02X", m.getbyte(0)) }
> end
Thanks, do you mean that "m[0]" in Ruby 1.9 has a different behaviour
than in 1.8? maybe in 1.9 "m[0]" returns the first character (even if
it's more than two bytes as "ñ", "€"...) while in 1.8 it returns just
the firrst two bytes?
PD: I've Ruby 1.9 (2007-12-25 revision 14709) and I don't have
"getbyte()" method for String.
--
remember.guy do |as, often| as.you_can - without end