Ejemplo n.º 1
0
    def Normalise(inStr):

        # Note: We lowercase here since Python doesn't handle case insensitive strings...
        inStr = inStr.lower()

        stack = Stack()
        pos = -1

        while True:

            # Find next special character...
            pos += 1
            pos = findChars(inStr, specialChars, pos)
            if pos < 0:
                break

            # Ignore if preceeded by '\'...
            if pos and inStr[pos - 1] == '\\':
                continue

            # Handle cases...
            c = inStr[pos]
            if c in '([':
                stack.push(pos)
                continue
            if c == '|':  # |...
                continue
            else:  # []()...
                if c == ')': shouldBe = '('
                else: shouldBe = '['
                if stack and inStr[stack.TOS()] != shouldBe:
                    tPos = stack.TOS()
                    inStr = inStr[0:tPos + 1] + shouldBe + inStr[tPos + 1:]
                    pos += 1
                elif stack:
                    tPos = stack.pop()
                else:
                    inStr = shouldBe + inStr
                    pos += 1

        # Put missing cases on end
        endCases = {"(": ")", "[": "]"}
        while stack:
            tPos = stack.pop()
            inStr = inStr + endCases[inStr[tPos]]

        return inStr