Skip to content

Instantly share code, notes, and snippets.

@xfenix
Last active July 27, 2020 20:58
Show Gist options
  • Save xfenix/84b678ade81ab920f84f292636a1f466 to your computer and use it in GitHub Desktop.
Save xfenix/84b678ade81ab920f84f292636a1f466 to your computer and use it in GitHub Desktop.
Space/speed effective string by word cutting algorithm
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