Skip to content

Commit bc75c36

Browse files
committed
bug symfony#12864 [Console][Table] Fix cell padding with multi-byte (ttsuruoka)
This PR was merged into the 2.3 branch. Discussion ---------- [Console][Table] Fix cell padding with multi-byte | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A When the `TableHelper` dealing with East Asian text, it renders wrong widths. This fixes that problem. Commits ------- 11014c2 [Console][Table] Fix cell padding with multi-byte
2 parents 33bf087 + 11014c2 commit bc75c36

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Symfony/Component/Console/Helper/Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getHelperSet()
4141
}
4242

4343
/**
44-
* Returns the length of a string, using mb_strlen if it is available.
44+
* Returns the length of a string, using mb_strwidth if it is available.
4545
*
4646
* @param string $string The string to check its length
4747
*

src/Symfony/Component/Console/Helper/TableHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ private function renderCell(array $row, $column, $cellFormat)
386386
$width = $this->getColumnWidth($column);
387387

388388
// str_pad won't work properly with multi-byte strings, we need to fix the padding
389-
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($cell)) {
390-
$width += strlen($cell) - mb_strlen($cell, $encoding);
389+
if (function_exists('mb_strwidth') && false !== $encoding = mb_detect_encoding($cell)) {
390+
$width += strlen($cell) - mb_strwidth($cell, $encoding);
391391
}
392392

393393
$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);

src/Symfony/Component/Console/Tests/Helper/TableHelperTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function testRenderProvider()
235235

236236
public function testRenderMultiByte()
237237
{
238-
if (!function_exists('mb_strlen')) {
238+
if (!function_exists('mb_strwidth')) {
239239
$this->markTestSkipped('The "mbstring" extension is not available');
240240
}
241241

@@ -255,6 +255,33 @@ public function testRenderMultiByte()
255255
| 1234 |
256256
+------+
257257
258+
TABLE;
259+
260+
$this->assertEquals($expected, $this->getOutputContent($output));
261+
}
262+
263+
public function testRenderFullWidthCharacters()
264+
{
265+
if (!function_exists('mb_strwidth')) {
266+
$this->markTestSkipped('The "mbstring" extension is not available');
267+
}
268+
269+
$table = new TableHelper();
270+
$table
271+
->setHeaders(array('あいうえお'))
272+
->setRows(array(array(1234567890)))
273+
->setLayout(TableHelper::LAYOUT_DEFAULT)
274+
;
275+
$table->render($output = $this->getOutputStream());
276+
277+
$expected =
278+
<<<TABLE
279+
+------------+
280+
| あいうえお |
281+
+------------+
282+
| 1234567890 |
283+
+------------+
284+
258285
TABLE;
259286

260287
$this->assertEquals($expected, $this->getOutputContent($output));

0 commit comments

Comments
 (0)