Created
June 3, 2014 15:15
-
-
Save suut/35fef96b1ddcd35c498d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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