def first_bracketed_string(text, depth=0, lbrack="{", rbrack="}"): """If text is of the form {A}B, return {A},B. Otherwise, return "",text. """ thetext = text.strip() if not thetext: logger.error("empty string sent to first_bracketed_string()") return "" previouschar = "" # we need to keep track of the previous character becaause \{ does not # count as a bracket if depth == 0 and thetext[0] != lbrack: return "", thetext elif depth == 0: firstpart = lbrack depth = 1 thetext = thetext[1:] else: firstpart = "" # should be some number of brackets? while depth > 0 and thetext: currentchar = thetext[0] if currentchar == lbrack and previouschar != "\\": depth += 1 elif currentchar == rbrack and previouschar != "\\": depth -= 1 firstpart += currentchar if previouschar == "\\" and currentchar == "\\": previouschar = "\n" else: previouschar = currentchar thetext = thetext[1:] if depth == 0: return firstpart, thetext else: logger.error("no matching bracket %s in %s XX", lbrack, thetext) return "", firstpart[1:] # firstpart should be everything
def first_bracketed_string(text, depth=0, lbrack="{", rbrack="}"): """If text is of the form {A}B, return {A},B. Otherwise, return "",text. """ thetext = text.strip() if not thetext: logger.error("empty string sent to first_bracketed_string()") return "" previouschar = "" # we need to keep track of the previous character becaause \{ does not # count as a bracket if depth == 0 and thetext[0] != lbrack: return "",thetext elif depth == 0: firstpart = lbrack depth = 1 thetext = thetext[1:] else: firstpart = "" # should be some number of brackets? while depth > 0 and thetext: currentchar = thetext[0] if currentchar == lbrack and previouschar != "\\": depth += 1 elif currentchar == rbrack and previouschar != "\\": depth -= 1 firstpart += currentchar if previouschar == "\\" and currentchar == "\\": previouschar = "\n" else: previouschar = currentchar thetext = thetext[1:] if depth == 0: return firstpart, thetext else: logger.error("no matching bracket %s in %s XX", lbrack, thetext) return "",firstpart[1:] # firstpart should be everything