Skip to content

Instantly share code, notes, and snippets.

@suut
Created June 3, 2014 15:15
Show Gist options
  • Select an option

  • Save suut/35fef96b1ddcd35c498d to your computer and use it in GitHub Desktop.

Select an option

Save suut/35fef96b1ddcd35c498d to your computer and use it in GitHub Desktop.
def split(txt, target):
# split according to \n in text
# split in 512 bytes (and be careful not to split in the middle of a UTF-8 control code)
final_text = []
for i in txt.split('\n'):
if len(i.encode())+11+len(target) >= 512:
s = i.encode()
splitted = []
cursor = 512-11-len(target)
while ''.join(j.decode() for j in splitted) != i:
try:
s[:cursor].decode()
except UnicodeDecodeError:
cursor -= 1
splitted.append(s[:cursor])
s = s[cursor:]
cursor -= len(s)
final_text += [k.decode() for k in splitted]
else:
final_text.append(i)
return final_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment