Skip to content

Commit a2bca4d

Browse files
committed
Add capital hex support
1 parent d866e04 commit a2bca4d

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## v2.3.0 (2017-07-15) ##
44

5-
*
5+
* Add `float.export` option that forces float encoder to use `var_export` for
6+
encoding floating point numbers
7+
* Float encoder now delegates integer encoding to the integer encoder
68

79
## v2.2.0 (2017-07-08) ##
810

src/Encoder/IntegerEncoder.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function __construct()
3333
'decimal' => function ($value, $options) {
3434
return $this->encodeDecimal($value, $options);
3535
},
36-
'hexadecimal' => function ($value) {
37-
return $this->encodeHexadecimal($value);
36+
'hexadecimal' => function ($value, $options) {
37+
return $this->encodeHexadecimal($value, $options);
3838
},
3939
];
4040
}
@@ -67,7 +67,7 @@ public function encode($value, $depth, array $options, callable $encode)
6767
*/
6868
public function encodeBinary($integer)
6969
{
70-
return sprintf('%s0b%s', $this->sign($integer), decbin(abs($integer)));
70+
return sprintf('%s0b%b', $this->sign($integer), abs($integer));
7171
}
7272

7373
/**
@@ -77,7 +77,7 @@ public function encodeBinary($integer)
7777
*/
7878
public function encodeOctal($integer)
7979
{
80-
return sprintf('%s0%s', $this->sign($integer), decoct(abs($integer)));
80+
return sprintf('%s0%o', $this->sign($integer), abs($integer));
8181
}
8282

8383
/**
@@ -89,7 +89,7 @@ public function encodeOctal($integer)
8989
public function encodeDecimal($integer, $options)
9090
{
9191
if ($integer === 1 << (PHP_INT_SIZE * 8 - 1)) {
92-
return sprintf('(int)%s%s', $options['whitespace'] ? ' ' : '', $integer);
92+
return sprintf('(int)%s%d', $options['whitespace'] ? ' ' : '', $integer);
9393
}
9494

9595
return var_export($integer, true);
@@ -98,11 +98,16 @@ public function encodeDecimal($integer, $options)
9898
/**
9999
* Encodes an integer into hexadecimal representation.
100100
* @param int $integer The integer to encode
101+
* @param array $options The integer encoding options
101102
* @return string The PHP code representation for the integer
102103
*/
103-
public function encodeHexadecimal($integer)
104+
public function encodeHexadecimal($integer, $options)
104105
{
105-
return sprintf('%s0x%s', $this->sign($integer), dechex(abs($integer)));
106+
if ($options['hex.capitalize']) {
107+
return sprintf('%s0x%X', $this->sign($integer), abs($integer));
108+
}
109+
110+
return sprintf('%s0x%x', $this->sign($integer), abs($integer));
106111
}
107112

108113
/**

src/Encoder/StringEncoder.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private function getSingleQuotedString($string)
9393
/**
9494
* Returns the string wrapped in double quotes and all but print characters escaped.
9595
* @param string $string String to wrap and escape
96-
* @param array $options String encoding options
96+
* @param array $options The string encoding options
9797
* @return string The string wrapped in double quotes and escape correctly
9898
*/
9999
private function getDoubleQuotedString($string, $options)
@@ -108,13 +108,15 @@ private function getDoubleQuotedString($string, $options)
108108
]);
109109

110110
if ($options['string.utf8']) {
111-
$string = $this->encodeUtf8($string);
111+
$string = $this->encodeUtf8($string, $options);
112112
}
113113

114+
$format = $options['hex.capitalize'] ? '\x%02X' : '\x%02x';
115+
114116
return sprintf('"%s"', preg_replace_callback(
115117
'/[^\x20-\x7E]/',
116-
function ($matches) {
117-
return sprintf('\x%02x', ord($matches[0]));
118+
function ($matches) use ($format) {
119+
return sprintf($format, ord($matches[0]));
118120
},
119121
$string
120122
));
@@ -123,10 +125,12 @@ function ($matches) {
123125
/**
124126
* Encodes all multibyte UTF-8 characters into PHP7 string encoding.
125127
* @param string $string The string to encoder
128+
* @param array $options The string encoding options
126129
* @return string The string with all the multibyte characters encoded
127130
*/
128-
private function encodeUtf8($string)
131+
private function encodeUtf8($string, $options)
129132
{
133+
$format = $options['hex.capitalize'] ? '\u{%X}' : '\u{%x}';
130134
$pattern =
131135
'/ [\xC2-\xDF][\x80-\xBF]
132136
| \xE0[\xA0-\xBF][\x80-\xBF]
@@ -136,8 +140,8 @@ private function encodeUtf8($string)
136140
| [\xF1-\xF3][\x80-\xBF]{3}
137141
| \xF4[\x80-\x8F][\x80-\xBF]{2}/x';
138142

139-
return preg_replace_callback($pattern, function ($match) {
140-
return sprintf('\u{%s}', dechex($this->getCodePoint($match[0])));
143+
return preg_replace_callback($pattern, function ($match) use ($format) {
144+
return sprintf($format, $this->getCodePoint($match[0]));
141145
}, $string);
142146
}
143147

src/PHPEncoder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class PHPEncoder
2828
'recursion.detect' => true,
2929
'recursion.ignore' => false,
3030
'recursion.max' => false,
31+
'hex.capitalize' => false,
3132
];
3233

3334
/**

tests/tests/EncodingTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function testIntegerTypes()
8181
$this->assertEncode('-13371337', -13371337, ['integer.type' => 'decimal']);
8282
$this->assertEncode('0xcc07c9', 13371337, ['integer.type' => 'hexadecimal']);
8383
$this->assertEncode('-0xcc07c9', -13371337, ['integer.type' => 'hexadecimal']);
84+
$this->assertEncode('0xCC07C9', 13371337, ['integer.type' => 'hexadecimal', 'hex.capitalize' => true]);
85+
$this->assertEncode('-0xCC07C9', -13371337, ['integer.type' => 'hexadecimal', 'hex.capitalize' => true]);
8486
}
8587

8688
public function testInvalidIntegerType()
@@ -156,6 +158,8 @@ public function testFloatIntegers()
156158
$this->assertEncode('1337', 1337, $encoder, 1337.0);
157159
$this->assertEncode('-1337', -1337, $encoder, -1337.0);
158160
$this->assertEncode('2000000000', 2000000000, $encoder, 2000000000.0);
161+
162+
$this->assertEncode('0xf', 15, ['float.integers' => true, 'integer.type' => 'hexadecimal'], 15.0);
159163
}
160164

161165
public function testMaximumFloatIntegers()
@@ -223,6 +227,8 @@ public function testStringEncoding()
223227
$this->assertEncode('"\t\$foo"', "\t\$foo");
224228
$this->assertEncode('"\t{\$foo}"', "\t{\$foo}");
225229
$this->assertEncode('"\x00"', "\x00");
230+
$this->assertEncode('"\xff"', "\xFF");
231+
$this->assertEncode('"\xFF"', "\xFF", ['hex.capitalize' => true]);
226232
$this->assertEncode("'\r'", "\r", ['string.escape' => false]);
227233
}
228234

@@ -240,7 +246,7 @@ public function testUtf8String()
240246
$encoder = new PHPEncoder(['string.utf8' => true]);
241247

242248
$this->assertEncode('"\nA"', "\nA", $encoder);
243-
$this->assertEncode('"\nA\u{c4}\x00"', "\n\x00", $encoder);
249+
$this->assertSame('"\nA\u{c4}\x00"', $encoder->encode("\n\x00"));
244250

245251
if (version_compare(PHP_VERSION, '7', '<')) {
246252
$this->assertSame('"\u{a2}"', $encoder->encode("\xC2\xA2"));
@@ -253,6 +259,9 @@ public function testUtf8String()
253259
$this->assertEncode('"\u{10348}"', "\u{10348}", $encoder);
254260
$this->assertEncode('"\u{e5}\u{e4}\u{f6}\u{c5}\u{c4}\u{d6}"', 'åäöÅÄÖ', $encoder);
255261
}
262+
263+
$encoder->setOption('hex.capitalize', true);
264+
$this->assertSame('"\nA\u{C4}\x00"', $encoder->encode("\n\x00"));
256265
}
257266

258267
public function testGMPEncoding()

0 commit comments

Comments
 (0)