def escape(ident):
    """Escape identifier."""
    string = []
    length = len(ident)
    start_dash = length > 0 and ident[0] == '-'
    if length == 1 and start_dash:
        string.append('\\{}'.format(ident))
    else:
        for index, c in enumerate(ident):
            codepoint = ord(c)
            if codepoint == 0:
                string.append('�')
            elif 1 <= codepoint <= 31 or codepoint == 127:
                string.append('\\{:x} '.format(codepoint))
            elif (index == 0 or start_dash and index == 1
                ) and 48 <= codepoint <= 57:
                string.append('\\{:x} '.format(codepoint))
            elif codepoint in (45, 95
                ) or codepoint >= 128 or 48 <= codepoint <= 57 or 48 <= codepoint <= 57 or 65 <= codepoint <= 90 or 97 <= codepoint <= 122:
                string.append(c)
            else:
                string.append('\\{}'.format(c))
    return ''.join(string)
