From d80e2c1624ec4c1a1ae2bc0073ae7352d5c0ce09 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <jan.travnicek@.fit.cvut.cz>
Date: Tue, 2 Apr 2019 15:56:40 +0200
Subject: [PATCH] fix V2 optimisation of regexps

---
 .../regexp/simplify/RegExpOptimizeUnboundedPart.hpp  | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.hpp b/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.hpp
index 00fb3cef1a..ffac4f3072 100644
--- a/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.hpp
+++ b/alib2algo/src/regexp/simplify/RegExpOptimizeUnboundedPart.hpp
@@ -471,17 +471,19 @@ bool RegExpOptimize::Unbounded < SymbolType >::V2( UnboundedRegExpAlternation <
 	 */
 
 	ext::vector < const UnboundedRegExpElement < SymbolType > * > iterElements;
-	// cache iter elements because of operator invalidation after erase
+	// cache iter elements because of operator invalidation after erase only cache nodes that are not iterations (the nodes omitted may only come from situation like x** where the double iteration wil get optimized out elsewhere).
 	for( const UnboundedRegExpElement < SymbolType > & n : node.getElements ( ) ) {
 		const UnboundedRegExpIteration < SymbolType > * iter = dynamic_cast < const UnboundedRegExpIteration < SymbolType > * > ( & n );
 		if( iter ) {
 			const UnboundedRegExpAlternation < SymbolType > * inner = dynamic_cast < const UnboundedRegExpAlternation < SymbolType > * > ( & iter->getChild ( ) );
 			if ( inner )
-				for ( const UnboundedRegExpElement < SymbolType > & innerElement : inner->getElements ( ) )
-					iterElements.push_back ( & innerElement );
+				for ( const UnboundedRegExpElement < SymbolType > & innerElement : inner->getElements ( ) ) {
+					if ( ! dynamic_cast < const UnboundedRegExpIteration < SymbolType > * > ( & innerElement ) )
+						iterElements.push_back ( & innerElement );
+				}
 			else
-				iterElements.push_back ( & iter->getChild ( ) );
-
+				if ( ! dynamic_cast < const UnboundedRegExpIteration < SymbolType > * > ( & iter->getChild ( ) ) )
+					iterElements.push_back ( & iter->getChild ( ) );
 		}
 	}
 
-- 
GitLab