dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] stdlib: avoid extra calls to eliminate "\n" from Base64
@ 2016-01-02  4:59 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-01-02  4:59 UTC (permalink / raw)
  To: spew

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 | 2 +-
 lib/net/http.rb                           | 3 +--
 lib/net/http/header.rb                    | 2 +-
 lib/net/imap.rb                           | 6 +++---
 lib/net/smtp.rb                           | 2 +-
 lib/open-uri.rb                           | 3 ++-
 lib/rss/rss.rb                            | 2 +-
 lib/securerandom.rb                       | 5 ++---
 lib/webrick/httpauth/digestauth.rb        | 2 +-
 lib/webrick/httpproxy.rb                  | 4 ++--
 10 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb
index e7c7a99..7c40050 100644
--- a/ext/psych/lib/psych/visitors/yaml_tree.rb
+++ b/ext/psych/lib/psych/visitors/yaml_tree.rb
@@ -314,7 +314,7 @@ def visit_String o
         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
diff --git a/lib/net/http.rb b/lib/net/http.rb
index f77df5e..f6499f2 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -912,8 +912,7 @@ def connect
             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"
diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb
index 5d99e8f..3a76dcf 100644
--- a/lib/net/http/header.rb
+++ b/lib/net/http/header.rb
@@ -427,7 +427,7 @@ def proxy_basic_auth(account, password)
   end
 
   def basic_encode(account, password)
-    'Basic ' + ["#{account}:#{password}"].pack('m').delete("\r\n")
+    'Basic ' + ["#{account}:#{password}"].pack('m0')
   end
   private :basic_encode
 
diff --git a/lib/net/imap.rb b/lib/net/imap.rb
index 439ca13..9895ed6 100644
--- a/lib/net/imap.rb
+++ b/lib/net/imap.rb
@@ -419,7 +419,7 @@ def authenticate(auth_type, *args)
       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 @@ def self.encode_utf7(s)
         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
diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb
index d634274..250293b 100644
--- a/lib/net/smtp.rb
+++ b/lib/net/smtp.rb
@@ -788,7 +788,7 @@ def check_auth_args(user, secret, authtype = DEFAULT_AUTH_TYPE)
 
     def base64_encode(str)
       # expects "str" may not become too long
-      [str].pack('m').gsub(/\s+/, '')
+      [str].pack('m0')
     end
 
     IMASK = 0x36
diff --git a/lib/open-uri.rb b/lib/open-uri.rb
index 9e4a5e2..192f1e5 100644
--- a/lib/open-uri.rb
+++ b/lib/open-uri.rb
@@ -285,7 +285,8 @@ def OpenURI.open_http(buf, target, proxy, options) # :nodoc:
       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
 
diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb
index 1bb0f6e..5b9c5d4 100644
--- a/lib/rss/rss.rb
+++ b/lib/rss/rss.rb
@@ -1241,7 +1241,7 @@ def xmled_content
         __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
diff --git a/lib/securerandom.rb b/lib/securerandom.rb
index b08b62b..198f273 100644
--- a/lib/securerandom.rb
+++ b/lib/securerandom.rb
@@ -136,7 +136,7 @@ def hex(n=nil)
   #
   # 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 @@ def base64(n=nil)
   #
   # 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
diff --git a/lib/webrick/httpauth/digestauth.rb b/lib/webrick/httpauth/digestauth.rb
index 018989e..98bdbd1 100644
--- a/lib/webrick/httpauth/digestauth.rb
+++ b/lib/webrick/httpauth/digestauth.rb
@@ -312,7 +312,7 @@ def split_param_value(string)
       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
 
diff --git a/lib/webrick/httpproxy.rb b/lib/webrick/httpproxy.rb
index 79a2e8f..083720f 100644
--- a/lib/webrick/httpproxy.rb
+++ b/lib/webrick/httpproxy.rb
@@ -143,7 +143,7 @@ def do_CONNECT(req, res)
       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 @@ def setup_upstream_proxy_authentication(req, res, header)
       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
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-01-02  4:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-02  4:59 [PATCH] stdlib: avoid extra calls to eliminate "\n" from Base64 Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).