in library/scripts/string_pack.py [0:0]
def add_string(self, string):
string_bytes = string.encode(encoding=self.encoding)
bytes_len = len(string_bytes)
if bytes_len == 0: # empty string
return 0, 0
location = self.store.find(string_bytes)
if location == -1:
# Not found. But before trying to add it, see if a prefix of the new string
# is at the end of the store buffer. If that's the case, we can save a few bytes
# by sharing that.
prefix = bytearray(string_bytes[:-1])
while prefix and not self.store.endswith(prefix):
del prefix[-1]
if prefix:
# Some part of the prefix remains, which means it matches the end of the buffer.
start = len(self.store) - len(prefix)
self.store += string_bytes[len(prefix) :]
else:
# Add the string to the end of the buffer.
start = len(self.store)
self.store += string_bytes
return start, bytes_len
else:
return location, bytes_len