Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6401ef5

Browse files
committed
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.
1 parent 37192dc commit 6401ef5

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/base64.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ def urlsafe_decode64(str)
9999
# NOTE: RFC 4648 does say nothing about unpadded input, but says that
100100
# "the excess pad characters MAY also be ignored", so it is inferred that
101101
# unpadded input is also acceptable.
102-
str = str.tr("-_", "+/")
103102
if !str.end_with?("=") && str.length % 4 != 0
104103
str = str.ljust((str.length + 3) & ~3, "=")
104+
str.tr!("-_", "+/")
105+
else
106+
str = str.tr("-_", "+/")
105107
end
106108
strict_decode64(str)
107109
end

0 commit comments

Comments
 (0)