From 8c82ee4e0484df4370200e13bd8b07a069b3191a Mon Sep 17 00:00:00 2001 From: Joao Fernandes Date: Fri, 24 Sep 2021 16:10:54 +0100 Subject: [PATCH 1/3] Add Gemfile.lock to .gitgnore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9106b2a..4ea5798 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /pkg/ /spec/reports/ /tmp/ +Gemfile.lock From 37192dc98835edaf31fc1ac05dc1cd2d1419b7b2 Mon Sep 17 00:00:00 2001 From: Joao Fernandes Date: Fri, 24 Sep 2021 16:11:11 +0100 Subject: [PATCH 2/3] Out with minitest, in with test-unit Maybe some long time ago this used minitest? That's not the case anymore. --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 8ba2339..eb86192 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source "https://rubygems.org" gem "rake" -gem "minitest" +gem "test-unit" From 6401ef58243efa65e0dbbfcddf9610a084b84886 Mon Sep 17 00:00:00 2001 From: Joao Fernandes Date: Fri, 24 Sep 2021 16:13:40 +0100 Subject: [PATCH 3/3] 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. --- lib/base64.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/base64.rb b/lib/base64.rb index de1e8c0..693aa1f 100644 --- a/lib/base64.rb +++ b/lib/base64.rb @@ -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