'Perform sctring escape - for regexp literals'

def _interpret_regexp(self, string, flags):

    self.index = 0
    self.length = len(string)
    self.source = string
    self.lineNumber = 0
    self.lineStart = 0
    octal = False
    st = u''
    inside_square = 0
    while (self.index < self.length):
        template = (u'[%s]' if (not inside_square) else u'%s')
        ch = self.source[self.index]
        self.index += 1
        if (ch == u'\\'):
            ch = self.source[self.index]
            self.index += 1
            if (not isLineTerminator(ch)):
                if (ch == u'u'):
                    digs = self.source[self.index:(self.index + 4)]
                    if ((len(digs) == 4) and all((isHexDigit(d) for d in digs))):
                        st += (template % unichr(int(digs, 16)))
                        self.index += 4
                    else:
                        st += u'u'
                elif (ch == u'x'):
                    digs = self.source[self.index:(self.index + 2)]
                    if ((len(digs) == 2) and all((isHexDigit(d) for d in digs))):
                        st += (template % unichr(int(digs, 16)))
                        self.index += 2
                    else:
                        st += u'x'
                elif (ch == u'0'):
                    st += u'\\0'
                elif (ch == u'n'):
                    st += u'\\n'
                elif (ch == u'r'):
                    st += u'\\r'
                elif (ch == u't'):
                    st += u'\\t'
                elif (ch == u'f'):
                    st += u'\\f'
                elif (ch == u'v'):
                    st += u'\\v'
                elif (ch in REGEXP_SPECIAL_SINGLE):
                    st += (u'\\' + ch)
                elif (ch == u'b'):
                    st += u'\\b'
                elif (ch == u'B'):
                    st += u'\\B'
                elif (ch == u'w'):
                    st += u'\\w'
                elif (ch == u'W'):
                    st += u'\\W'
                elif (ch == u'd'):
                    st += u'\\d'
                elif (ch == u'D'):
                    st += u'\\D'
                elif (ch == u's'):
                    st += (template % u'    \x0c\n\r DCTB \x0b\xa0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff')
                elif (ch == u'S'):
                    st += (template % u'\x00-\x08\x0e-\x1f!-\x9f\xa1-\u167f\u1681-\u180d\u180f-\u1fff\u200b-\u2027\u202a-\u202e\u2030-\u205e\u2060-\u2fff\u3001-\ufefe\uff00-\uffff')
                elif isDecimalDigit(ch):
                    num = ch
                    while ((self.index < self.length) and isDecimalDigit(self.source[self.index])):
                        num += self.source[self.index]
                        self.index += 1
                    st += (u'\\' + num)
                else:
                    st += ch
            else:
                self.lineNumber += 1
                if ((ch == u'\r') and (self.source[self.index] == u'\n')):
                    self.index += 1
                self.lineStart = self.index
        else:
            if (ch == u'['):
                inside_square = True
            elif (ch == u']'):
                inside_square = False
            st += ch
    return st
