Reorganize a String in such a way that surrogates are printable
    and lonely surrogates are escaped.

    :param s: input string
    :return: string with escaped lonely surrogates and 32bit surrogates

def patch_string(s):
    """
    Reorganize a String in such a way that surrogates are printable
    and lonely surrogates are escaped.

    :param s: input string
    :return: string with escaped lonely surrogates and 32bit surrogates
    """
    res = ''
    it = PeekIterator(s)
    for c in it:
        if (ord(c) >> 10) == 0b110110:
            # High surrogate
            # Check for the next
            n = it.peek()
            if n and (ord(n) >> 10) == 0b110111:
                # Next is a low surrogate! Merge them together
                res += chr(((ord(c) & 0x3ff) << 10 | (ord(n) & 0x3ff)) + 0x10000)
                # Skip next char, as we already consumed it
                next(it)
            else:
                # Lonely high surrogate
                res += "\\u{:04x}".format(ord(c))
        elif (ord(c) >> 10) == 0b110111:
            # Lonely low surrogate
            res += "\\u{:04x}".format(ord(c))
        else:
            # Looks like a normal char...
            res += c
    return res