Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use Rack::Utils::bytesize, not String#size for multibyte characters i…

…n Ruby1.9

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit ce26ede59d9e833ee233edff8d9ec73c6ecb0998 1 parent eecb124
Masayoshi Takahashi takahashim authored josh committed
Showing with 10 additions and 1 deletion.
  1. +1 −1  lib/rack/utils.rb
  2. +9 −0 test/spec_rack_utils.rb
2  lib/rack/utils.rb
View
@@ -13,7 +13,7 @@ module Utils
# version since it's faster. (Stolen from Camping).
def escape(s)
s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
- '%'+$1.unpack('H2'*$1.size).join('%').upcase
+ '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
}.tr(' ', '+')
end
module_function :escape
9 test/spec_rack_utils.rb
View
@@ -12,6 +12,15 @@
should.equal "q1%212%22%27w%245%267%2Fz8%29%3F%5C"
end
+ specify "should escape correctly for multibyte characters" do
+ matz_name = "\xE3\x81\xBE\xE3\x81\xA4\xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsumoto
+ matz_name.force_encoding("UTF-8") if matz_name.respond_to? :force_encoding
+ Rack::Utils.escape(matz_name).should.equal '%E3%81%BE%E3%81%A4%E3%82%82%E3%81%A8'
+ matz_name_sep = "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsu moto
+ matz_name_sep.force_encoding("UTF-8") if matz_name_sep.respond_to? :force_encoding
+ Rack::Utils.escape(matz_name_sep).should.equal '%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8'
+ end
+
specify "should unescape correctly" do
Rack::Utils.unescape("fo%3Co%3Ebar").should.equal "fo<o>bar"
Rack::Utils.unescape("a+space").should.equal "a space"
Please sign in to comment.
Something went wrong with that request. Please try again.