bug #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
This commit is contained in:
Fabien Potencier 2015-02-05 09:10:27 +01:00
commit bc75c36e03
3 changed files with 31 additions and 4 deletions

View File

@ -41,7 +41,7 @@ abstract class Helper implements HelperInterface
}
/**
* Returns the length of a string, using mb_strlen if it is available.
* Returns the length of a string, using mb_strwidth if it is available.
*
* @param string $string The string to check its length
*

View File

@ -386,8 +386,8 @@ class TableHelper extends Helper
$width = $this->getColumnWidth($column);
// str_pad won't work properly with multi-byte strings, we need to fix the padding
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($cell)) {
$width += strlen($cell) - mb_strlen($cell, $encoding);
if (function_exists('mb_strwidth') && false !== $encoding = mb_detect_encoding($cell)) {
$width += strlen($cell) - mb_strwidth($cell, $encoding);
}
$width += $this->strlen($cell) - $this->computeLengthWithoutDecoration($cell);

View File

@ -235,7 +235,7 @@ TABLE
public function testRenderMultiByte()
{
if (!function_exists('mb_strlen')) {
if (!function_exists('mb_strwidth')) {
$this->markTestSkipped('The "mbstring" extension is not available');
}
@ -255,6 +255,33 @@ TABLE
| 1234 |
+------+
TABLE;
$this->assertEquals($expected, $this->getOutputContent($output));
}
public function testRenderFullWidthCharacters()
{
if (!function_exists('mb_strwidth')) {
$this->markTestSkipped('The "mbstring" extension is not available');
}
$table = new TableHelper();
$table
->setHeaders(array('あいうえお'))
->setRows(array(array(1234567890)))
->setLayout(TableHelper::LAYOUT_DEFAULT)
;
$table->render($output = $this->getOutputStream());
$expected =
<<<TABLE
+------------+
| あいうえお |
+------------+
| 1234567890 |
+------------+
TABLE;
$this->assertEquals($expected, $this->getOutputContent($output));