Feature #8394

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

Added by Kazuhiro NISHIYAMA almost 2 years ago. Updated over 1 year ago.

[ruby-dev:47336]
Status:Closed
Priority:Normal
Assignee:Masatoshi Seki

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

Associated revisions

Revision 40786
Added by Takeyuki FUJIOKA almost 2 years ago

  • lib/cgi/util.rb: Use String#b instead of dup.force_encoding("ASCII-8BIT") [Feature #8394]

Revision 40786
Added by Takeyuki FUJIOKA almost 2 years ago

  • lib/cgi/util.rb: Use String#b instead of dup.force_encoding("ASCII-8BIT") [Feature #8394]

Revision 41030
Added by Zachary Scott over 1 year ago

  • lib/cgi/util.rb, lib/erb.rb: Use String#b [Feature #8394] by znz

Revision 41030
Added by Zachary Scott over 1 year ago

  • lib/cgi/util.rb, lib/erb.rb: Use String#b [Feature #8394] by znz

History

#1 Updated by Takeyuki FUJIOKA almost 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Takeyuki FUJIOKA

私はいいと思うので、確認してコミットしようと思います。
終わったら担当者を関さんに振ります。

#2 Updated by Takeyuki FUJIOKA almost 2 years ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r40786.
Kazuhiro, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • lib/cgi/util.rb: Use String#b instead of dup.force_encoding("ASCII-8BIT") [Feature #8394]

#3 Updated by Takeyuki FUJIOKA almost 2 years ago

  • Status changed from Closed to Assigned
  • Assignee changed from Takeyuki FUJIOKA to Masatoshi Seki

#4 Updated by Zachary Scott over 1 year ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r41030.
Kazuhiro, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • lib/cgi/util.rb, lib/erb.rb: Use String#b [Feature #8394] by znz

Also available in: Atom PDF