File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ CHANGELOG
88 * made ` AbstractString::width() ` follow POSIX.1-2001
99 * added ` LazyString ` which provides memoizing stringable objects
1010 * The component is not marked as ` @experimental ` anymore.
11+ * Added the ` s() ` helper method to get either an ` UnicodeString ` or ` ByteString ` instance,
12+ depending of the input string UTF-8 compliancy.
1113
12145.0.0
1315-----
Original file line number Diff line number Diff line change @@ -20,3 +20,11 @@ function b(string $string = ''): ByteString
2020{
2121 return new ByteString ($ string );
2222}
23+
24+ /**
25+ * @return UnicodeString|ByteString
26+ */
27+ function s (string $ string ): AbstractString
28+ {
29+ return preg_match ('//u ' , $ string ) ? new UnicodeString ($ string ) : new ByteString ($ string );
30+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Symfony package.
5+ *
6+ * (c) Fabien Potencier <fabien@symfony.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Symfony \Component \String \Tests ;
13+
14+ use PHPUnit \Framework \TestCase ;
15+ use Symfony \Component \String \AbstractString ;
16+ use Symfony \Component \String \ByteString ;
17+ use function Symfony \Component \String \s ;
18+ use Symfony \Component \String \UnicodeString ;
19+
20+ final class FunctionsTest extends TestCase
21+ {
22+ /**
23+ * @dataProvider provideS
24+ */
25+ public function testS (AbstractString $ expected , string $ input )
26+ {
27+ $ this ->assertEquals ($ expected , s ($ input ));
28+ }
29+
30+ public function provideS ()
31+ {
32+ return [
33+ [new UnicodeString ('foo ' ), 'foo ' ],
34+ [new UnicodeString ('अनुच्छेद ' ), 'अनुच्छेद ' ],
35+ [new ByteString ("b \x80ar " ), "b \x80ar " ],
36+ [new ByteString ("\xfe\xff" ), "\xfe\xff" ],
37+ ];
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments