ruby-changes:41417
From: normal <ko1@a...>
Date: Sun, 10 Jan 2016 09:35:29 +0900 (JST)
Subject: [ruby-changes:41417] normal:r53488 (trunk): stdlib: avoid extra calls to eliminate "\n" from Base64
normal 2016-01-10 09:35:43 +0900 (Sun, 10 Jan 2016) New Revision: 53488 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53488 Log: stdlib: avoid extra calls to eliminate "\n" from Base64 We may use the '0' (zero) to avoid adding the line feed. Furthermore, the '*' (asterisk) modifier is not needed for a single-element arrays. * ext/psych/lib/psych/visitors/yaml_tree.rb (visit_String): eliminate chomp * lib/net/http.rb (connect): eliminate delete * lib/net/http/header.rb (basic_encode): ditto * lib/net/imap.rb (authenticate): eliminate gsub (self.encode_utf7): shorten delete arg * lib/net/smtp.rb (base64_encode): eliminate gsub * lib/open-uri.rb (OpenURI.open_http): eliminate delete * lib/rss/rss.rb: ditto * lib/securerandom.rb (base64): ditto (urlsafe_base64): eliminate delete! * lib/webrick/httpauth/digestauth.rb (split_param_value): eliminate chop * lib/webrick/httpproxy.rb (do_CONNECT): eliminate delete (setup_upstream_proxy_authentication): ditto [ruby-core:72666] [Feature #11938] Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/yaml_tree.rb trunk/lib/net/http/header.rb trunk/lib/net/http.rb trunk/lib/net/imap.rb trunk/lib/net/smtp.rb trunk/lib/open-uri.rb trunk/lib/rss/rss.rb trunk/lib/securerandom.rb trunk/lib/webrick/httpauth/digestauth.rb trunk/lib/webrick/httpproxy.rb Index: lib/webrick/httpproxy.rb =================================================================== --- lib/webrick/httpproxy.rb (revision 53487) +++ lib/webrick/httpproxy.rb (revision 53488) @@ -143,7 +143,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpproxy.rb#L143 if proxy = proxy_uri(req, res) proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0" if proxy.userinfo - credentials = "Basic " + [proxy.userinfo].pack("m").delete("\n") + credentials = "Basic " + [proxy.userinfo].pack("m0") end host, port = proxy.host, proxy.port end @@ -294,7 +294,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpproxy.rb#L294 if upstream = proxy_uri(req, res) if upstream.userinfo header['proxy-authorization'] = - "Basic " + [upstream.userinfo].pack("m").delete("\n") + "Basic " + [upstream.userinfo].pack("m0") end return upstream end Index: lib/webrick/httpauth/digestauth.rb =================================================================== --- lib/webrick/httpauth/digestauth.rb (revision 53487) +++ lib/webrick/httpauth/digestauth.rb (revision 53488) @@ -312,7 +312,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpauth/digestauth.rb#L312 def generate_next_nonce(req) now = "%012d" % req.request_time.to_i pk = hexdigest(now, @instance_key)[0,32] - nonce = [now + ":" + pk].pack("m*").chop # it has 60 length of chars. + nonce = [now + ":" + pk].pack("m0") # it has 60 length of chars. nonce end Index: lib/open-uri.rb =================================================================== --- lib/open-uri.rb (revision 53487) +++ lib/open-uri.rb (revision 53488) @@ -285,7 +285,8 @@ module OpenURI https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb#L285 target_port = proxy_uri.port request_uri = target.to_s if proxy_user && proxy_pass - header["Proxy-Authorization"] = 'Basic ' + ["#{proxy_user}:#{proxy_pass}"].pack('m').delete("\r\n") + header["Proxy-Authorization"] = + 'Basic ' + ["#{proxy_user}:#{proxy_pass}"].pack('m0') end end Index: lib/rss/rss.rb =================================================================== --- lib/rss/rss.rb (revision 53487) +++ lib/rss/rss.rb (revision 53488) @@ -1241,7 +1241,7 @@ EOC https://github.com/ruby/ruby/blob/trunk/lib/rss/rss.rb#L1241 __send__(self.class.xml_getter).to_s else _content = content - _content = [_content].pack("m").delete("\n") if need_base64_encode? + _content = [_content].pack("m0") if need_base64_encode? h(_content) end end Index: lib/net/smtp.rb =================================================================== --- lib/net/smtp.rb (revision 53487) +++ lib/net/smtp.rb (revision 53488) @@ -788,7 +788,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/smtp.rb#L788 def base64_encode(str) # expects "str" may not become too long - [str].pack('m').gsub(/\s+/, '') + [str].pack('m0') end IMASK = 0x36 Index: lib/net/imap.rb =================================================================== --- lib/net/imap.rb (revision 53487) +++ lib/net/imap.rb (revision 53488) @@ -419,7 +419,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L419 send_command("AUTHENTICATE", auth_type) do |resp| if resp.instance_of?(ContinuationRequest) data = authenticator.process(resp.data.text.unpack("m")[0]) - s = [data].pack("m").gsub(/\n/, "") + s = [data].pack("m0") send_string_data(s) put_string(CRLF) end @@ -1007,8 +1007,8 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L1007 if $1 "&-" else - base64 = [$&.encode(Encoding::UTF_16BE)].pack("m") - "&" + base64.delete("=\n").tr("/", ",") + "-" + base64 = [$&.encode(Encoding::UTF_16BE)].pack("m0") + "&" + base64.delete("=").tr("/", ",") + "-" end }.force_encoding("ASCII-8BIT") end Index: lib/net/http/header.rb =================================================================== --- lib/net/http/header.rb (revision 53487) +++ lib/net/http/header.rb (revision 53488) @@ -427,7 +427,7 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L427 end def basic_encode(account, password) - 'Basic ' + ["#{account}:#{password}"].pack('m').delete("\r\n") + 'Basic ' + ["#{account}:#{password}"].pack('m0') end private :basic_encode Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 53487) +++ lib/net/http.rb (revision 53488) @@ -912,8 +912,7 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L912 buf = "CONNECT #{@address}:#{@port} HTTP/#{HTTPVersion}\r\n" buf << "Host: #{@address}:#{@port}\r\n" if proxy_user - credential = ["#{proxy_user}:#{proxy_pass}"].pack('m') - credential.delete!("\r\n") + credential = ["#{proxy_user}:#{proxy_pass}"].pack('m0') buf << "Proxy-Authorization: Basic #{credential}\r\n" end buf << "\r\n" Index: lib/securerandom.rb =================================================================== --- lib/securerandom.rb (revision 53487) +++ lib/securerandom.rb (revision 53488) @@ -136,7 +136,7 @@ module Random::Formatter https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L136 # # See RFC 3548 for the definition of base64. def base64(n=nil) - [random_bytes(n)].pack("m*").delete("\n") + [random_bytes(n)].pack("m0") end # SecureRandom.urlsafe_base64 generates a random URL-safe base64 string. @@ -166,8 +166,7 @@ module Random::Formatter https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L166 # # See RFC 3548 for the definition of URL-safe base64. def urlsafe_base64(n=nil, padding=false) - s = [random_bytes(n)].pack("m*") - s.delete!("\n") + s = [random_bytes(n)].pack("m0") s.tr!("+/", "-_") s.delete!("=") unless padding s Index: ChangeLog =================================================================== --- ChangeLog (revision 53487) +++ ChangeLog (revision 53488) @@ -1,3 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Jan 10 09:14:42 2016 Eric Wong <e@8...> + + * ext/psych/lib/psych/visitors/yaml_tree.rb (visit_String): + eliminate chomp + * lib/net/http.rb (connect): eliminate delete + * lib/net/http/header.rb (basic_encode): ditto + * lib/net/imap.rb (authenticate): eliminate gsub + (self.encode_utf7): shorten delete arg + * lib/net/smtp.rb (base64_encode): eliminate gsub + * lib/open-uri.rb (OpenURI.open_http): eliminate delete + * lib/rss/rss.rb: ditto + * lib/securerandom.rb (base64): ditto + (urlsafe_base64): eliminate delete! + * lib/webrick/httpauth/digestauth.rb (split_param_value): + eliminate chop + * lib/webrick/httpproxy.rb (do_CONNECT): eliminate delete + (setup_upstream_proxy_authentication): ditto + [ruby-core:72666] [Feature #11938] + Sat Jan 9 23:19:14 2016 Kuniaki IGARASHI <igaiga@g...> * test/ruby/test_hash.rb (test_try_convert): Add test for Index: ext/psych/lib/psych/visitors/yaml_tree.rb =================================================================== --- ext/psych/lib/psych/visitors/yaml_tree.rb (revision 53487) +++ ext/psych/lib/psych/visitors/yaml_tree.rb (revision 53488) @@ -314,7 +314,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L314 tag = nil if binary?(o) - o = [o].pack('m').chomp + o = [o].pack('m0') tag = '!binary' # FIXME: change to below when syck is removed #tag = 'tag:yaml.org,2002:binary' style = Nodes::Scalar::LITERAL -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/