From 11af7c23554dbe0660e4a73277b219aec3de1286 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Mon, 7 Aug 2023 17:41:16 +0200 Subject: [PATCH] Sprintf regex refactoring --- src/Filters/SprintfToPH.php | 8 +++++--- src/MateCatFilter.php | 4 ++-- src/MyMemoryFilter.php | 6 ++---- tests/MateCatSubFilteringTest.php | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Filters/SprintfToPH.php b/src/Filters/SprintfToPH.php index 5883570..d755881 100644 --- a/src/Filters/SprintfToPH.php +++ b/src/Filters/SprintfToPH.php @@ -41,12 +41,14 @@ public function transform( $segment ) { $sprintfLocker = new SprintfLocker( $this->pipeline->getSource(), $this->pipeline->getTarget() ); - //placeholding + // placeholding $segment = $sprintfLocker->lock( $segment ); // Octal parsing is disabled due to Hungarian percentages 20%-os - // preg_match_all( '/(?:\x25\x25)|(\x25(?:(?:[1-9]\d*)\$|\((?:[^\)]+)\))?(?:\+)?(?:0|\'[^$])?(?:-)?(?:\d+)?(?:\.(?:\d+))?(?:[b-fiosuxX]))/', $segment, $vars, PREG_SET_ORDER ); - preg_match_all( '/(?:\x25\x25)|(\x25(?:(?:[1-9]\d*)\$|\((?:[^\)]+)\))?(?:\+)?(?:-)?(?:0|\'[^$])?(?:\d+)?(?:\.(?:\d+))?(?:[b-fiosuxX]))/', $segment, $vars, PREG_SET_ORDER ); + $regex = '/(?:\x25\x25)|(\x25(?:(?:[1-9]\d*)\$|\((?:[^\)]+)\))?(?:\+)?(?:0|[+-]?\'[^$])?(?:-)?(?:\d+)?(?:\.(?:\d+))?((?:[hjlqtzL]{0,2}([ac-gopsuxAC-GOSUX]{1})|(\-[a-z]{1,3}))(?![\d\w])|(?:#@[\w]+@)|(?:@)))/'; + + + preg_match_all( $regex, $segment, $vars, PREG_SET_ORDER ); foreach ( $vars as $pos => $variable ) { //replace subsequent elements excluding already encoded diff --git a/src/MateCatFilter.php b/src/MateCatFilter.php index f42713c..c938160 100644 --- a/src/MateCatFilter.php +++ b/src/MateCatFilter.php @@ -40,6 +40,7 @@ use Matecat\SubFiltering\Filters\StandardPHToMateCatCustomPH; use Matecat\SubFiltering\Filters\SubFilteredPhToHtml; use Matecat\SubFiltering\Filters\TwigToPh; +use Matecat\SubFiltering\Filters\Variables; /** * Class Filter @@ -160,12 +161,11 @@ public function fromLayer0ToLayer1( $segment ) { $channel->addLast( new LtGtDecode() ); $channel->addLast( new HtmlToPh() ); $channel->addLast( new TwigToPh() ); + $channel->addLast( new Variables() ); $channel->addLast( new RubyOnRailsI18n() ); $channel->addLast( new Snails() ); $channel->addLast( new DoubleSquareBrackets() ); - //$channel->addLast( new DoubleUnderscore() ); $channel->addLast( new DollarCurlyBrackets() ); - $channel->addLast( new PercentSnail() ); $channel->addLast( new PercentNumberSnail() ); $channel->addLast( new Percentages() ); $channel->addLast( new SquareSprintf() ); diff --git a/src/MyMemoryFilter.php b/src/MyMemoryFilter.php index 70008d3..8103f0a 100644 --- a/src/MyMemoryFilter.php +++ b/src/MyMemoryFilter.php @@ -64,19 +64,17 @@ public function fromLayer0ToLayer1( $segment, $cid = null ) { $channel->addLast( new PlaceHoldXliffTags() ); $channel->addLast( new LtGtDecode() ); $channel->addLast( new HtmlToPh() ); + $channel->addLast( new TwigToPh() ); + $channel->addLast( new Variables() ); if ( $cid == 'airbnb' ) { - $channel->addLast( new Variables() ); $channel->addLast( new SmartCounts() ); } - $channel->addLast( new TwigToPh() ); $channel->addLast( new RubyOnRailsI18n() ); $channel->addLast( new Snails() ); $channel->addLast( new DoubleSquareBrackets() ); - //$channel->addLast( new DoubleUnderscore() ); $channel->addLast( new DollarCurlyBrackets() ); - $channel->addLast( new PercentSnail() ); $channel->addLast( new PercentNumberSnail() ); $channel->addLast( new Percentages() ); diff --git a/tests/MateCatSubFilteringTest.php b/tests/MateCatSubFilteringTest.php index d7cab42..6cc5b81 100644 --- a/tests/MateCatSubFilteringTest.php +++ b/tests/MateCatSubFilteringTest.php @@ -871,7 +871,7 @@ public function testPercentSnailSyntax() { $filter = $this->getFilterInstance(); $db_segment = 'This string: %@ is a IOS placeholder %@.'; - $segment_from_UI = 'This string: is a IOS placeholder .'; + $segment_from_UI = 'This string: is a IOS placeholder .'; $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) ); @@ -891,7 +891,7 @@ public function testWithMixedPercentTags() { $filter = $this->getFilterInstance(); $db_segment = 'This string contains all these tags: %-4d %@ %12$@ ​%{{|discount|}} {% if count < 3 %} but not this %placeholder%'; - $segment_from_UI = 'This string contains all these tags: ​% but not this %placeholder%'; + $segment_from_UI = 'This string contains all these tags: ​% but not this %placeholder%'; $this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) ); $this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );