From 46aab39ee6eb8c9fa0b774ce42b3f4bd6e63ab3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 31 May 2019 14:15:28 +0300 Subject: [PATCH] Remove empty literals when concatenating regexes --- regex.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/regex.py b/regex.py index 3baf5f5..f0d4156 100644 --- a/regex.py +++ b/regex.py @@ -81,7 +81,11 @@ def concat(*elements): combined = [] for element in flattened: - if len(combined) > 0 and type(combined[-1]) == Literal and type(element) == Literal: + if type(element) == Literal and element.text == '': + # Drop empty literals + continue + + elif len(combined) > 0 and type(combined[-1]) == Literal and type(element) == Literal: # Combine two literals next to each other # into one literal previous = combined.pop() @@ -90,7 +94,11 @@ def concat(*elements): else: combined.append(element) - if len(combined) == 1: + if len(combined) == 0: + # Empty regex, represent with empty literal + return lit('') + + elif len(combined) == 1: element, = combined return element else: