@@ -258,7 +258,7 @@ private function applyCurrentStyle(string $text, string $current, int $width, in
258
258
}
259
259
260
260
preg_match ('~( \\n)$~ ' , $ text , $ matches );
261
- $ text = $ prefix .preg_replace ( ' ~([^ \\ n]{ ' . $ width . ' }) \\ *~ ' , "\$ 1 \n" , $ text );
261
+ $ text = $ prefix .$ this -> addLineBreaks ( $ text , $ width );
262
262
$ text = rtrim ($ text , "\n" ).($ matches [1 ] ?? '' );
263
263
264
264
if (!$ currentLineLength && '' !== $ current && "\n" !== substr ($ current , -1 )) {
@@ -282,4 +282,26 @@ private function applyCurrentStyle(string $text, string $current, int $width, in
282
282
283
283
return implode ("\n" , $ lines );
284
284
}
285
+
286
+ private function addLineBreaks ($ string , $ charsPerLine ) {
287
+ $ result = '' ;
288
+
289
+ if (\function_exists ('mb_detect_encoding ' ) && false !== $ encoding = mb_detect_encoding ($ string , null , true )) {
290
+ for ($ i = 0 ; $ i < mb_strlen ($ string , $ encoding ); $ i += $ charsPerLine ) {
291
+ if (' ' === mb_substr ($ string , $ i , 1 , $ encoding )) {
292
+ ++$ i ;
293
+ }
294
+ $ result .= mb_substr ($ string , $ i , $ charsPerLine , $ encoding )."\n" ;
295
+ }
296
+ } else {
297
+ for ($ i = 0 ; $ i < \strlen ($ string ); $ i += $ charsPerLine ) {
298
+ if (' ' === substr ($ string , $ i , 1 )) {
299
+ ++$ i ;
300
+ }
301
+ $ result .= substr ($ string , $ i , $ charsPerLine )."\n" ;
302
+ }
303
+ }
304
+
305
+ return $ result ;
306
+ }
285
307
}
0 commit comments