Merge branch '5.2' into 5.x

* 5.2:
  Add test.
  [Console] Add Helper::strwidth() and Helper::strlength()
  Update README.md
This commit is contained in:
Fabien Potencier 2021-04-08 08:54:07 +02:00
commit 8fc4c1bf3b
5 changed files with 52 additions and 9 deletions

View File

@ -20,6 +20,7 @@ Documentation
* Read the [Getting Started guide][7] if you are new to Symfony.
* Try the [Symfony Demo application][23] to learn Symfony in practice.
* Discover Symfony ecosystem in detail with [Symfony The Fast Track][26].
* Master Symfony with the [Guides and Tutorials][8], the [Components docs][9]
and the [Best Practices][10] reference.
@ -74,3 +75,4 @@ Symfony development is sponsored by [SensioLabs][21], led by the
[23]: https://github.com/symfony/symfony-demo
[24]: https://symfony.com/coc
[25]: https://symfony.com/doc/current/contributing/code_of_conduct/care_team.html
[26]: https://symfony.com/book

View File

@ -45,6 +45,17 @@ abstract class Helper implements HelperInterface
* @return int The length of the string
*/
public static function strlen(?string $string)
{
return self::width($string);
}
/**
* Returns the width of a string, using mb_strwidth if it is available.
* The width is how many characters positions the string will use.
*
* @internal in Symfony 5.2
*/
public static function width(?string $string): int
{
$string ?? $string = '';
@ -59,6 +70,27 @@ abstract class Helper implements HelperInterface
return mb_strwidth($string, $encoding);
}
/**
* Returns the length of a string, using mb_strlen if it is available.
* The length is related to how many bytes the string will use.
*
* @internal in Symfony 5.2
*/
public static function length(?string $string): int
{
$string ?? $string = '';
if (preg_match('//u', $string)) {
return (new UnicodeString($string))->length();
}
if (false === $encoding = mb_detect_encoding($string, null, true)) {
return \strlen($string);
}
return mb_strlen($string, $encoding);
}
/**
* Returns the subset of a string, using mb_substr if it is available.
*
@ -123,13 +155,7 @@ abstract class Helper implements HelperInterface
public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string)
{
$string = self::removeDecoration($formatter, $string);
if (preg_match('//u', $string)) {
return (new UnicodeString($string))->width(true);
}
return self::strlen($string);
return self::width(self::removeDecoration($formatter, $string));
}
public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string)

View File

@ -523,7 +523,7 @@ final class ProgressBar
$completeBars = $bar->getBarOffset();
$display = str_repeat($bar->getBarCharacter(), $completeBars);
if ($completeBars < $bar->getBarWidth()) {
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter());
$emptyBars = $bar->getBarWidth() - $completeBars - Helper::length(Helper::removeDecoration($output->getFormatter(), $bar->getProgressCharacter()));
$display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars);
}

View File

@ -511,7 +511,7 @@ class Table
return sprintf($style->getBorderFormat(), str_repeat($style->getBorderChars()[2], $width));
}
$width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
$width += Helper::length($cell) - Helper::length(Helper::removeDecoration($this->output->getFormatter(), $cell));
$content = sprintf($style->getCellRowContentFormat(), $cell);
$padType = $style->getPadType();

View File

@ -899,6 +899,21 @@ And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whi
);
}
public function testUnicode()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10, 0);
ProgressBar::setFormatDefinition('test', '%current%/%max% [%bar%] %percent:3s%% %message% Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.');
$bar->setFormat('test');
$bar->setProgressCharacter('💧');
$bar->start();
rewind($output->getStream());
$this->assertStringContainsString(
' 0/10 [💧] 0%',
stream_get_contents($output->getStream())
);
$bar->finish();
}
/**
* @dataProvider provideFormat
*/