Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
jcmfernandes committed Sep 24, 2021
commit 6401ef58243efa65e0dbbfcddf9610a084b84886
4 changes: 3 additions & 1 deletion lib/base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ def urlsafe_decode64(str)
# 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
Expand Down