Skip to content

Commit 95acc97

Browse files
author
Kevin J Walters
committed
Removing the C-esque pad removal in strUnpad() and replacing with rstrip based on review.
Note: CP 5.3.0 bytes(xx) always make a new object. adafruit#1185
1 parent e3dd2d2 commit 95acc97

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

CLUE_Rock_Paper_Scissors/advanced/rps_crypto.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ def bytesPad(text, size=8, pad=0):
3939

4040
def strUnpad(text_as_bytes, pad=0):
4141
"""Convert a bytes or bytearray
42-
to a str removing trailing characters matching pad."""
42+
to a str removing trailing bytes matching int pad."""
43+
text_b = bytes(text_as_bytes) # bytes type has the useful methods
4344
if pad is not None:
44-
end_ex = len(text_as_bytes)
45-
while end_ex > 0 and text_as_bytes[end_ex - 1] == pad:
46-
end_ex -= 1
45+
text_b = text_b.rstrip(bytes([pad]))
4746

48-
# bytearray does not have decode method so need to convert here
49-
return bytes(text_as_bytes[0:end_ex]).decode("utf-8")
47+
return text_b.decode("utf-8")
5048

5149

5250
def enlargeKey(small_key, mult):

0 commit comments

Comments
 (0)