Feature #8394

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

Added by Kazuhiro NISHIYAMA 12 months ago. Updated 11 months ago.

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

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.forceencoding('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.forceencoding("ASCII-8BIT") # don't use constant Enoding::ASCII8BIT for miniruby
+ s = s.b
enc = detectmagiccomment(s) || enc
out = Buffer.new(self, enc)

@@ -948,7 +948,7 @@ class ERB
# Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
#
def urlencode(s)
- s.to
s.dup.forceencoding("ASCII-8BIT").gsub(/[a-zA-Z0-9-.]/n) {
+ s.tos.b.gsub(/[a-zA-Z0-9-.]/n) {
sprintf("%%%02X", $&.unpack("C")[0])
}
end

Associated revisions

Revision 40786
Added by Takeyuki FUJIOKA 11 months ago

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

Revision 41030
Added by Zachary Scott 11 months ago

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

History

#1 Updated by Takeyuki FUJIOKA 12 months ago

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

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

#2 Updated by Takeyuki FUJIOKA 11 months 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 11 months ago

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

#4 Updated by Zachary Scott 11 months 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