Skip to content

Commit

Permalink
use Rack::Utils::bytesize, not String#size for multibyte characters i…
Browse files Browse the repository at this point in the history
…n Ruby1.9
  • Loading branch information
takahashim committed Jun 24, 2009
1 parent 9326894 commit 15179f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/utils.rb
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/spec_rack_utils.rb
Expand Up @@ -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"
Expand Down

0 comments on commit 15179f6

Please sign in to comment.