From 05d894d2a6120b47a2afe6ea1ed6c749fdcef8e9 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Wed, 29 Mar 2023 01:33:00 -0500 Subject: [PATCH] Small perf optimization for `expr | ""` mapping to `Optional(expr)` --- pyparsing/core.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyparsing/core.py b/pyparsing/core.py index ae7dcb61..03371ab1 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -1552,6 +1552,9 @@ def __or__(self, other) -> "ParserElement": return _PendingSkip(self, must_skip=True) if isinstance(other, str_type): + # `expr | ""` is equivalent to `Opt(expr)` + if other == "": + return Opt(self) other = self._literalStringClass(other) if not isinstance(other, ParserElement): return NotImplemented