def _partial_match(buffer, string, safe_size):
    """
    Helper to handle the possible edge case of a string partially
    occurring at the end of buffer currently under analysis.

    @param buffer buffer to inspect
    @param string string to find
    @param safe_size size of buffer suffix where the partial match is to be
            attempted.

    @retval A partition of the given buffer into two parts (send_buf, buf)
            where:
               send_buf: buffer that can be immediately sent out to receiver;
                         can be '' meaning that more data is required to
                         continue parsing for timestamp related tags.
               buf: continue analyzing this buffer.
    """

    # search string starting from this position:
    start_pos = max(0, len(buffer) - safe_size)
    pos = buffer.find(string, start_pos)
    if pos >= 0:
        # found completely:
        retval = (buffer[0: pos], buffer[pos:])
    else:
        # try partial match:
        prefix = string
        while not buffer.startswith(prefix, len(buffer) - len(prefix)):
            prefix = prefix[:len(prefix) - 1]

        if prefix:
            # there is a partial match:
            retval = (buffer[0: -len(prefix)], buffer[-len(prefix):])
        else:
            retval = (buffer, '')

    (send_buf, buf) = retval
    assert buffer == send_buf + buf, "output must be partition of input"
    return retval