Project

General

Profile

Actions

Feature #8394

closed

use String#b instead of dup.force_encoding("ASCII-8BIT")

Added by znz (Kazuhiro NISHIYAMA) over 11 years ago. Updated over 11 years ago.

Status:
Closed
Target version:
-
[ruby-dev:47336]

Description

dup.force_encoding("ASCII-8BIT") を使っているところは b を使えば良いのではないかと思ったのですが、どうでしょうか?

diff --git a/lib/cgi/util.rb b/lib/cgi/util.rb
index 7de6fc7..bb5df37 100644
--- a/lib/cgi/util.rb
+++ b/lib/cgi/util.rb
@@ -6,7 +6,7 @@ module CGI::Util

# => "%27Stop%21%27+said+Fred"

def escape(string)
encoding = string.encoding

  • string.dup.force_encoding('ASCII-8BIT').gsub(/([^ a-zA-Z0-9_.-]+)/) do
  • string.b.gsub(/([^ a-zA-Z0-9_.-]+)/) do
    '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
    end.tr(' ', '+').force_encoding(encoding)
    end
    diff --git a/lib/erb.rb b/lib/erb.rb
    index 51b128e..9cb3999 100644
    --- a/lib/erb.rb
    +++ b/lib/erb.rb
    @@ -596,7 +596,7 @@ class ERB
    def compile(s)
    enc = s.encoding
    raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
  •  s = s.dup.force_encoding("ASCII-8BIT") # don't use constant Enoding::ASCII_8BIT for miniruby
    
  •  s = s.b
     enc = detect_magic_comment(s) || enc
     out = Buffer.new(self, enc)
    

@@ -948,7 +948,7 @@ class ERB
# Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
#
def url_encode(s)

  •  s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
    
  •  s.to_s.b.gsub(/[^a-zA-Z0-9_\-.]/n) {
       sprintf("%%%02X", $&.unpack("C")[0])
     }
    
    end
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0