Skip to content

Commit

Permalink
Merge pull request #36 from matecat/new-sprintf-regex
Browse files Browse the repository at this point in the history
Sprintf regex refactoring
  • Loading branch information
mauretto78 authored Aug 10, 2023
2 parents 0d451eb + bd46e92 commit 903773a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 49 deletions.
10 changes: 0 additions & 10 deletions src/Filters/Sprintf/language/de-AT/not_allowed.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Filters/Sprintf/language/de-CH/not_allowed.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Filters/Sprintf/language/de-DE/not_allowed.php

This file was deleted.

14 changes: 11 additions & 3 deletions src/Filters/SprintfToPH.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function __construct() {
* 20%-zar - ignored
*</code>
*
* @see
* - https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
* - https://en.cppreference.com/w/c/io/fprintf
* - https://www.php.net/manual/en/function.sprintf.php
* - https://www.w3resource.com/c-programming/stdio/c_library_method_sprintf.php
*
* @param $segment
*
* @return string
Expand All @@ -41,12 +47,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-giopsuxAC-GOSUX]{1})(?![\d\w])|(?:#@[\w]+@)|(?:@)))/';


preg_match_all( $regex, $segment, $vars, PREG_SET_ORDER );
foreach ( $vars as $pos => $variable ) {

//replace subsequent elements excluding already encoded
Expand Down
4 changes: 2 additions & 2 deletions src/MateCatFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() );
Expand Down
6 changes: 2 additions & 4 deletions src/MyMemoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
4 changes: 2 additions & 2 deletions tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public function testPercentSnailSyntax() {
$filter = $this->getFilterInstance();

$db_segment = 'This string: %@ is a IOS placeholder %@.';
$segment_from_UI = 'This string: <ph id="mtc_1" ctype="' . CTypeEnum::PERCENT_SNAILS . '" equiv-text="base64:JUA="/> is a IOS placeholder <ph id="mtc_2" ctype="' . CTypeEnum::PERCENT_SNAILS . '" equiv-text="base64:JUA="/>.';
$segment_from_UI = 'This string: <ph id="mtc_1" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:JUA="/> is a IOS placeholder <ph id="mtc_2" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:JUA="/>.';

$this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) );
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );
Expand All @@ -891,7 +891,7 @@ public function testWithMixedPercentTags() {
$filter = $this->getFilterInstance();

$db_segment = 'This string contains all these tags: %-4d %@ %12$@ ​%{{|discount|}} {% if count &lt; 3 %} but not this %placeholder%';
$segment_from_UI = 'This string contains all these tags: <ph id="mtc_1" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:JS00ZA=="/> <ph id="mtc_2" ctype="' . CTypeEnum::PERCENT_SNAILS . '" equiv-text="base64:JUA="/> <ph id="mtc_3" ctype="' . CTypeEnum::PERCENT_NUMBER_SNAILS . '" equiv-text="base64:JTEyJEA="/> ​%<ph id="mtc_4" ctype="' . CTypeEnum::TWIG . '" equiv-text="base64:e3t8ZGlzY291bnR8fX0="/> <ph id="mtc_5" ctype="' . CTypeEnum::TWIG . '" equiv-text="base64:eyUgaWYgY291bnQgJmx0OyAzICV9"/> but not this %placeholder%';
$segment_from_UI = 'This string contains all these tags: <ph id="mtc_1" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:JS00ZA=="/> <ph id="mtc_2" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:JUA="/> <ph id="mtc_3" ctype="' . CTypeEnum::PERCENT_NUMBER_SNAILS . '" equiv-text="base64:JTEyJEA="/> ​%<ph id="mtc_4" ctype="' . CTypeEnum::TWIG . '" equiv-text="base64:e3t8ZGlzY291bnR8fX0="/> <ph id="mtc_5" ctype="' . CTypeEnum::TWIG . '" equiv-text="base64:eyUgaWYgY291bnQgJmx0OyAzICV9"/> but not this %placeholder%';

$this->assertEquals( $db_segment, $filter->fromLayer1ToLayer0( $segment_from_UI ) );
$this->assertEquals( $segment_from_UI, $filter->fromLayer0ToLayer1( $db_segment ) );
Expand Down
8 changes: 0 additions & 8 deletions tests/SprintfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ private function runTests($notAllowed, $languageToTest)

$this->assertEquals($segment, $segmentL1);
$this->assertEquals($segment, $segmentL2);

$filter = MateCatFilter::getInstance( new FeatureSet(), 'en-US', 'it-IT' );

$segmentL1 = $filter->fromLayer0ToLayer1( $segment );
$segmentL2 = $filter->fromLayer0ToLayer2( $segment );

$this->assertNotEquals($segment, $segmentL1);
$this->assertNotEquals($segment, $segmentL2);
}
}
}

0 comments on commit 903773a

Please sign in to comment.