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

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
Next Next commit
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.
  • Loading branch information
jcmfernandes committed Sep 2, 2021
commit 544e0c2cf735acd5c61bd3d26247350fc7a86691
8 changes: 7 additions & 1 deletion lib/base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ def strict_decode64(str)
# 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

Expand Down