@@ -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,27 @@ 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
+ {
288
+ $ result = '' ;
289
+
290
+ if (\function_exists ('mb_detect_encoding ' ) && false !== $ encoding = mb_detect_encoding ($ string , null , true )) {
291
+ for ($ i = 0 ; $ i < mb_strlen ($ string , $ encoding ); $ i += $ charsPerLine ) {
292
+ if (' ' === mb_substr ($ string , $ i , 1 , $ encoding )) {
293
+ ++$ i ;
294
+ }
295
+ $ result .= mb_substr ($ string , $ i , $ charsPerLine , $ encoding )."\n" ;
296
+ }
297
+ } else {
298
+ for ($ i = 0 ; $ i < \strlen ($ string ); $ i += $ charsPerLine ) {
299
+ if (' ' === substr ($ string , $ i , 1 )) {
300
+ ++$ i ;
301
+ }
302
+ $ result .= substr ($ string , $ i , $ charsPerLine )."\n" ;
303
+ }
304
+ }
305
+
306
+ return $ result ;
307
+ }
285
308
}
0 commit comments