Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
95c1c48f
Commit
95c1c48f
authored
Jun 22, 2012
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Speed up base64.urlsafe_{en,de}code().
parent
9436361e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
13 deletions
+13
-13
Lib/base64.py
Lib/base64.py
+11
-13
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/base64.py
View file @
95c1c48f
...
...
@@ -40,14 +40,6 @@ def _bytes_from_decode_data(s):
else
:
raise
TypeError
(
"argument should be bytes or ASCII string, not %s"
%
s
.
__class__
.
__name__
)
def
_translate
(
s
,
altchars
):
if
not
isinstance
(
s
,
bytes_types
):
raise
TypeError
(
"expected bytes, not %s"
%
s
.
__class__
.
__name__
)
translation
=
bytearray
(
range
(
256
))
for
k
,
v
in
altchars
.
items
():
translation
[
ord
(
k
)]
=
v
[
0
]
return
s
.
translate
(
translation
)
# Base64 encoding/decoding uses binascii
...
...
@@ -71,7 +63,7 @@ def b64encode(s, altchars=None):
raise
TypeError
(
"expected bytes, not %s"
%
altchars
.
__class__
.
__name__
)
assert
len
(
altchars
)
==
2
,
repr
(
altchars
)
return
_translate
(
encoded
,
{
'+'
:
altchars
[
0
:
1
],
'/'
:
altchars
[
1
:
2
]}
)
return
encoded
.
translate
(
bytes
.
maketrans
(
b'+/'
,
altchars
)
)
return
encoded
...
...
@@ -93,7 +85,7 @@ def b64decode(s, altchars=None, validate=False):
if
altchars
is
not
None
:
altchars
=
_bytes_from_decode_data
(
altchars
)
assert
len
(
altchars
)
==
2
,
repr
(
altchars
)
s
=
_translate
(
s
,
{
chr
(
altchars
[
0
]):
b'+'
,
chr
(
altchars
[
1
]):
b'/'
}
)
s
=
s
.
translate
(
bytes
.
maketrans
(
altchars
,
b'+/'
)
)
if
validate
and
not
re
.
match
(
b'^[A-Za-z0-9+/]*={0,2}$'
,
s
):
raise
binascii
.
Error
(
'Non-base64 digit found'
)
return
binascii
.
a2b_base64
(
s
)
...
...
@@ -116,6 +108,10 @@ def standard_b64decode(s):
"""
return
b64decode
(
s
)
_urlsafe_encode_translation
=
bytes
.
maketrans
(
b'+/'
,
b'-_'
)
_urlsafe_decode_translation
=
bytes
.
maketrans
(
b'-_'
,
b'+/'
)
def
urlsafe_b64encode
(
s
):
"""Encode a byte string using a url-safe Base64 alphabet.
...
...
@@ -123,7 +119,7 @@ def urlsafe_b64encode(s):
returned. The alphabet uses '-' instead of '+' and '_' instead of
'/'.
"""
return
b64encode
(
s
,
b'-_'
)
return
b64encode
(
s
).
translate
(
_urlsafe_encode_translation
)
def
urlsafe_b64decode
(
s
):
"""Decode a byte string encoded with the standard Base64 alphabet.
...
...
@@ -135,7 +131,9 @@ def urlsafe_b64decode(s):
The alphabet uses '-' instead of '+' and '_' instead of '/'.
"""
return
b64decode
(
s
,
b'-_'
)
s
=
_bytes_from_decode_data
(
s
)
s
=
s
.
translate
(
_urlsafe_decode_translation
)
return
b64decode
(
s
)
...
...
@@ -228,7 +226,7 @@ def b32decode(s, casefold=False, map01=None):
if
map01
is
not
None
:
map01
=
_bytes_from_decode_data
(
map01
)
assert
len
(
map01
)
==
1
,
repr
(
map01
)
s
=
_translate
(
s
,
{
b'0'
:
b'O'
,
b'1'
:
map01
}
)
s
=
s
.
translate
(
bytes
.
maketrans
(
b'01'
,
b'O'
+
map01
)
)
if
casefold
:
s
=
s
.
upper
()
# Strip off pad characters from the right. We need to count the pad
...
...
Misc/NEWS
View file @
95c1c48f
...
...
@@ -40,6 +40,8 @@ Core and Builtins
Library
-------
-
Issue
:
#
15138
:
base64
.
urlsafe_
{
en
,
de
}
code
()
are
now
3
-
4
x
faster
.
-
Issue
#
9527
:
datetime
.
astimezone
()
method
will
now
supply
a
class
timezone
instance
corresponding
to
the
system
local
timezone
when
called
with
no
arguments
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment