Optimize byte array escaping performance python

  python, python-3.x, re, regex

I need to perform custom escaping over a byte array in python. However, during escaping python converts bytes to integers, making performance optimization very difficult. How can I speed up my escaping function?

ESCAPE_DICT={
    0x00: [0x5C,0x7A], # null        -> z 0x5c 0x7a
    0x22: [0x5C,0x71], # "           -> q 0x5c 0x71
    0x3B: [0x5C,0x73], # ;           -> s 0x5c 0x73
    0x5C: [0x5C,0x5C], #            ->  0x5c 0x5c
    0x0A: [0x5C,0x6E], # line-feed   -> n 0x5c 0x6e
    0x0C: [0x5C,0x66], # form-feed   -> f 0x5c 0x66
    0x0D: [0x5C,0x63], # carr-return -> c 0x5c 0x63
}

def escape(string: bytes):
    str_len=string.__len__()
    escaped_list=[]
    for i in range(0,str_len):
        curr_byte=string[i]
        escape = ESCAPE_DICT.get(curr_byte)
        if escape is None:
            # Don't escape current byte
            escaped_list.append(curr_byte)
        else:
            # Escape current byte
            escaped_list.extend(escape)
    return bytes(escaped_array)

Source: Python-3x Questions

LEAVE A COMMENT