@utils.MemoizeFunction()
def _LiteralPrefix(regex):
  start = list(regex)
  result = []

  while True:
    if not start:
      return "".join(result)
    if start[0] == "\\":
      # A bar \ is a mystery, we do nothing with it.
      if len(start) == 1:
        return "".join(result)
      # A \ followed by certain characters is a special and we don't handle
      # specials.
      if start[1] in "0123456789AbBdDsSwWZ":
        return "".join(result)
      # These are unlikely to appear in a regex, but just in case:
      if start[1] == "a":
        result += "\a"
        start = start[2:]
        continue
      if start[1] == "b":
        result += "\b"
        start = start[2:]
        continue
      if start[1] == "f":
        result += "\f"
        start = start[2:]
        continue
      if start[1] == "n":
        result += "\n"
        start = start[2:]
        continue
      if start[1] == "r":
        result += "\r"
        start = start[2:]
        continue
      if start[1] == "t":
        result += "\t"
        start = start[2:]
        continue
      if start[1] == "v":
        result += "\v"
        start = start[2:]
        continue
      # A \ followed by another character, e.g. '.' is a literal of that
      # character.
      result += start[1]
      start = start[2:]
      continue
    if start[0] in ".^$*+?{}[]|()":
      return "".join(result)
    result += start[0]
    start = start[1:]
Returns longest prefix of regex which consists of literal characters.
dataset/ETHPy150Open google/grr/grr/lib/data_stores/common.py/_LiteralPrefix