code: def _cleanly_slice_encoded_string(encoded_string, length_limit):
          
          sliced, remaining = encoded_string[:length_limit], encoded_string[length_limit:]
          try:
              sliced.decode('utf-8')
          except UnicodeDecodeError as e:
              sliced, remaining = sliced[:e.start], sliced[e.start:] + remaining
  
          return sliced, remaining
docs: Takes a byte string (a UTF-8 encoded string) and splits it into two pieces such that the first slice is no
          longer than argument `length_limit`, then returns a tuple containing the first slice and remainder of the
          byte string, respectively. The first slice may actually be shorter than `length_limit`, because this ensures
          that the string does not get split in the middle of a multi-byte character.
  
          This works because the first byte in a multi-byte unicode character encodes how many bytes compose that
          character, so we can determine empirically if we are splitting in the middle of the character and correct for
          that.
  
          You can read more about how this works here: https://en.wikipedia.org/wiki/UTF-8#Description
  
          :param encoded_string: The encoded string to split in two
          :param length_limit: The maximum length allowed for the first slice of the string
          :return: A tuple of (slice, remaining)