さて、文字列を 32 文字の乗数にするこの小さな関数を作成しましたが、String .replace を使用すると、非常に奇妙なバグが発生します。髪を引っ張らなければならないので、何が欠けているのか見てください。
Variables:
self.blockSize = 32
self.interrupt = '$^EnD#Block^$'
self.filler = '#'
Functions:
def pad(self, data):
joint1 = ''.join([data, self.interrupt])
joint2 = self.filler * ((self.blockSize - len(joint1)) % self.blockSize)
return ''.join([joint1, joint2])
def unpad(self, data):
data = str(data).rstrip(self.interrupt)
return data.replace(self.filler, '')
Call:
p = e.pad('this is not a very good idea yo')
print(p)
print(e.unpad(p))
Output:
Jans-MacBook-Pro:test2 jan$ ../../bin/python3 data.py
this is not a very good idea yo123$^EnD#Block^$################
this is not a very good idea yo123
Jans-MacBook-Pro:test2 jan$ ../../bin/python3 data.py
this is not a very good idea yo$^EnD#Block^$###################
this is not a very good idea y
Jans-MacBook-Pro:test2 jan$
お中を無くします。ああ!しかし、後で乱数を追加しても何も消えません。
解決策 - 編集: 悪い。self.filler と self.interrupt を置き忘れました。私は今とても恥ずかしいです。コードは次のようになります。
def unpad(self, data):
data = str(data).rstrip(self.filler)
return data.replace(self.interrupt, '')