@@ -45,6 +45,17 @@ public function getHelperSet()
45
45
* @return int The length of the string
46
46
*/
47
47
public static function strlen (?string $ string )
48
+ {
49
+ return self ::width ($ string );
50
+ }
51
+
52
+ /**
53
+ * Returns the width of a string, using mb_strwidth if it is available.
54
+ * The width is how many characters positions the string will use.
55
+ *
56
+ * @internal in Symfony 5.2
57
+ */
58
+ public static function width (?string $ string ): int
48
59
{
49
60
$ string ?? $ string = '' ;
50
61
@@ -59,6 +70,27 @@ public static function strlen(?string $string)
59
70
return mb_strwidth ($ string , $ encoding );
60
71
}
61
72
73
+ /**
74
+ * Returns the length of a string, using mb_strlen if it is available.
75
+ * The length is related to how many bytes the string will use.
76
+ *
77
+ * @internal in Symfony 5.2
78
+ */
79
+ public static function length (?string $ string ): int
80
+ {
81
+ $ string ?? $ string = '' ;
82
+
83
+ if (preg_match ('//u ' , $ string )) {
84
+ return (new UnicodeString ($ string ))->length ();
85
+ }
86
+
87
+ if (false === $ encoding = mb_detect_encoding ($ string , null , true )) {
88
+ return \strlen ($ string );
89
+ }
90
+
91
+ return mb_strlen ($ string , $ encoding );
92
+ }
93
+
62
94
/**
63
95
* Returns the subset of a string, using mb_substr if it is available.
64
96
*
@@ -123,13 +155,7 @@ public static function formatMemory(int $memory)
123
155
124
156
public static function strlenWithoutDecoration (OutputFormatterInterface $ formatter , ?string $ string )
125
157
{
126
- $ string = self ::removeDecoration ($ formatter , $ string );
127
-
128
- if (preg_match ('//u ' , $ string )) {
129
- return (new UnicodeString ($ string ))->width (true );
130
- }
131
-
132
- return self ::strlen ($ string );
158
+ return self ::width (self ::removeDecoration ($ formatter , $ string ));
133
159
}
134
160
135
161
public static function removeDecoration (OutputFormatterInterface $ formatter , ?string $ string )
0 commit comments