[前][次][番号順一覧][スレッド一覧]

ruby-changes:68139

From: Joao <ko1@a...>
Date: Mon, 27 Sep 2021 15:29:37 +0900 (JST)
Subject: [ruby-changes:68139] 05a28ce5b1 (master): [ruby/base64] Improve Base64.urlsafe_encode64 performance

https://git.ruby-lang.org/ruby.git/commit/?id=05a28ce5b1

From 05a28ce5b11d0e0ca48bae799ef65e9657dc4f6a Mon Sep 17 00:00:00 2001
From: Joao Fernandes <joao@h...>
Date: Thu, 2 Sep 2021 15:53:01 +0100
Subject: [ruby/base64] Improve Base64.urlsafe_encode64 performance

Improves the method's performance when asked to remove padding.

str.delete!("=") iterates over the entire string looking for the equals
character, but we know that we will, at most, find two at the end of the
string.

https://github.com/ruby/base64/commit/544e0c2cf7
---
 lib/base64.rb | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/base64.rb b/lib/base64.rb
index 5c8df84..6b04998 100644
--- a/lib/base64.rb
+++ b/lib/base64.rb
@@ -82,8 +82,14 @@ module Base64 https://github.com/ruby/ruby/blob/trunk/lib/base64.rb#L82
   # You can remove the padding by setting +padding+ as false.
   def urlsafe_encode64(bin, padding: true)
     str = strict_encode64(bin)
+    unless padding
+      if str.end_with?("==")
+        str.delete_suffix!("==")
+      elsif str.end_with?("=")
+        str.chop!
+      end
+    end
     str.tr!("+/", "-_")
-    str.delete!("=") unless padding
     str
   end
 
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]