summaryrefslogtreecommitdiff
path: root/lib/base64.rb
diff options
context:
space:
mode:
authorJoao Fernandes <joao@hopin.to>2021-09-24 16:13:40 +0100
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2021-09-27 15:23:25 +0900
commitf8000e293141a9af9f51effc76007aab21b0dede (patch)
treef08dc76327e82eebb44105735574866e630e5d85 /lib/base64.rb
parent1b004ba0db2b8e4e0a6b3362dd7681e0c642cab0 (diff)
downloadruby-f8000e293141a9af9f51effc76007aab21b0dede.tar.gz
[ruby/base64] Avoid unnecessary string duplication
String#ljust returns a new string, so whenever we need to add padding, we can replace "-/" in place with String#tr! and avoid creating yet another copy of the string. https://github.com/ruby/base64/commit/6401ef5824
Diffstat (limited to 'lib/base64.rb')
-rw-r--r--lib/base64.rb4
1 files changed, 3 insertions, 1 deletions
diff --git a/lib/base64.rb b/lib/base64.rb
index de1e8c0e55..693aa1f519 100644
--- a/lib/base64.rb
+++ b/lib/base64.rb
@@ -99,9 +99,11 @@ module Base64
# NOTE: RFC 4648 does say nothing about unpadded input, but says that
# "the excess pad characters MAY also be ignored", so it is inferred that
# unpadded input is also acceptable.
- str = str.tr("-_", "+/")
if !str.end_with?("=") && str.length % 4 != 0
str = str.ljust((str.length + 3) & ~3, "=")
+ str.tr!("-_", "+/")
+ else
+ str = str.tr("-_", "+/")
end
strict_decode64(str)
end