|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Matecat\SubFiltering\Filters; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Matecat\SubFiltering\Commons\AbstractHandler; |
|
8
|
|
|
use Matecat\SubFiltering\Enum\CTypeEnum; |
|
9
|
|
|
use Matecat\SubFiltering\Filters\Sprintf\SprintfLocker; |
|
10
|
|
|
|
|
11
|
|
|
class SprintfToPH extends AbstractHandler { |
|
12
|
|
|
|
|
13
|
91 |
|
public function __construct() { |
|
14
|
91 |
|
parent::__construct(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TestSet: |
|
19
|
|
|
* <code> |
|
20
|
|
|
* |%-4d|%-4d| |
|
21
|
|
|
* |%':4d| |
|
22
|
|
|
* |%-':4d| |
|
23
|
|
|
* |%-'04d| |
|
24
|
|
|
* %02.2f |
|
25
|
|
|
* %02d |
|
26
|
|
|
* %1$s! |
|
27
|
|
|
* %08b |
|
28
|
|
|
* 20%-os - ignored |
|
29
|
|
|
* 20%-dir - ignored |
|
30
|
|
|
* 20%-zar - ignored |
|
31
|
|
|
*</code> |
|
32
|
|
|
* |
|
33
|
|
|
* @param $segment |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
* @see |
|
37
|
|
|
* - https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1 |
|
38
|
|
|
* - https://en.cppreference.com/w/c/io/fprintf |
|
39
|
|
|
* - https://www.php.net/manual/en/function.sprintf.php |
|
40
|
|
|
* - https://www.w3resource.com/c-programming/stdio/c_library_method_sprintf.php |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
81 |
|
public function transform( string $segment ): string { |
|
44
|
|
|
|
|
45
|
|
|
// disabled for now, we want to check if this is really needed |
|
46
|
|
|
// $sprintfLocker = new SprintfLocker( $this->pipeline->getSource(), $this->pipeline->getTarget() ); |
|
47
|
|
|
|
|
48
|
|
|
// placeholding |
|
49
|
|
|
// $segment = $sprintfLocker->lock( $segment ); |
|
50
|
|
|
|
|
51
|
|
|
// Octal parsing is disabled due to Hungarian percentages 20%-os |
|
52
|
81 |
|
$regex = '/(?:\x25\x25)|(\x25(?:(?:[1-9]\d*)\$|\((?:[^)]+)\))?(?:\+)?(?:0|[+-]?\'[^$])?(?:-)?(?:\d+)?(?:\.(?:\d+))?((?:[hjlqtzL]{0,2}[a-iopsuxAC-HOSUX])))/'; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
81 |
|
preg_match_all( $regex, $segment, $vars, PREG_SET_ORDER ); |
|
56
|
81 |
|
foreach ( $vars as $variable ) { |
|
57
|
|
|
|
|
58
|
|
|
//replace subsequent elements excluding already encoded |
|
59
|
4 |
|
$segment = preg_replace( |
|
60
|
4 |
|
'/' . preg_quote( $variable[ 0 ], '/' ) . '/', |
|
61
|
4 |
|
'<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::SPRINTF . '" equiv-text="base64:' . base64_encode( $variable[ 0 ] ) . '"/>', |
|
62
|
4 |
|
$segment, |
|
63
|
4 |
|
1 |
|
64
|
4 |
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
//revert placeholding |
|
68
|
|
|
// return $sprintfLocker->unlock( $segment ); |
|
69
|
81 |
|
return $segment; |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |