Last active
July 27, 2020 20:58
-
-
Save xfenix/84b678ade81ab920f84f292636a1f466 to your computer and use it in GitHub Desktop.
Space/speed effective string by word cutting algorithm
This file contains 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
from ctypes import c_uint32 | |
def ellipsis(string: str, str_length: int, suffix: str = '...') -> str: | |
max_len: int = str_length + 1 | |
space: str = ' ' | |
if len(string) > max_len: | |
if string[max_len] == space: | |
return string[:max_len] + suffix | |
else: | |
cursor: c_uint32 = c_uint32(str_length) | |
while cursor.value > 0: | |
if string[cursor.value] != space: | |
cursor.value -= 1 | |
else: | |
return string[:cursor.value] + suffix | |
return None | |
else: | |
return string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment