def remove_surrogates(s):
    """
    The following is a helper function, used to convert two-character
    surrogate sequences into single characters.  This is needed
    because some systems create surrogates but others don't.
    """
    pieces = re.split('(&#\d+;)', s)
    for i in range(3, len(pieces)-1, 2):
        if pieces[i-1] != '': continue
        high,low = int(pieces[i-2][2:-1]), int(pieces[i][2:-1])
        if 0xd800 <= high <= 0xdbff and 0xdc00 <= low <= 0xdfff:
            pieces[i-2] = '&#%d;' % (((high&0x3ff)<<10) +
                                     (low&0x3ff) + 0x10000)
            pieces[i] = ''
    return ''.join(pieces)
dataset/ETHPy150Open CollabQ/CollabQ/vendor/epydoc/test/util.py/remove_surrogates/original