summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoao Fernandes <joao@hopin.to>2021-09-02 15:53:01 +0100
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2021-09-27 15:23:04 +0900
commit05a28ce5b11d0e0ca48bae799ef65e9657dc4f6a (patch)
tree9820c4f57a143a26f4ecb2c118f28674e20ef44f /lib
parentf7ffe9dbdeb2bebb4c9155fc391f0bab198bfb51 (diff)
[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
Diffstat (limited to 'lib')
-rw-r--r--lib/base64.rb8
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/base64.rb b/lib/base64.rb
index 5c8df841f1..6b049982cf 100644
--- a/lib/base64.rb
+++ b/lib/base64.rb
@@ -82,8 +82,14 @@ module Base64
# 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