Given an encoding, is there a preconceived way to 'cap' a string according to some given maximum size in bytes. Illustration:
>>> some_string = 'abc'
>>> size_limit = 2
>>> encoding = 'utf-8'
>>> capped_string = cap_to_size(some_string, size_limit, encoding)
>>> capped_string
'ab'
That is, the function cap_to_size (so to speak) cuts away the rightmost characters in the string until the resulting string has the given size. If the given string is smaller than the size limit already, nothing happens and the original string is returned.
In the case of multibyte characters, they should be discarded in their entirety, if one of their bytes exceeds the size limit.