@@ -280,22 +280,11 @@ int is_utf8(const char *text)
280280 return 1 ;
281281}
282282
283- static inline void strbuf_write (struct strbuf * sb , const char * buf , int len )
283+ static void strbuf_addchars (struct strbuf * sb , int c , size_t n )
284284{
285- if (sb )
286- strbuf_insert (sb , sb -> len , buf , len );
287- else
288- fwrite (buf , len , 1 , stdout );
289- }
290-
291- static void print_spaces (struct strbuf * buf , int count )
292- {
293- static const char s [] = " " ;
294- while (count >= sizeof (s )) {
295- strbuf_write (buf , s , sizeof (s ) - 1 );
296- count -= sizeof (s ) - 1 ;
297- }
298- strbuf_write (buf , s , count );
285+ strbuf_grow (sb , n );
286+ memset (sb -> buf + sb -> len , c , n );
287+ strbuf_setlen (sb , sb -> len + n );
299288}
300289
301290static void strbuf_add_indented_text (struct strbuf * buf , const char * text ,
@@ -307,8 +296,8 @@ static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
307296 const char * eol = strchrnul (text , '\n' );
308297 if (* eol == '\n' )
309298 eol ++ ;
310- print_spaces (buf , indent );
311- strbuf_write (buf , text , eol - text );
299+ strbuf_addchars (buf , ' ' , indent );
300+ strbuf_add (buf , text , eol - text );
312301 text = eol ;
313302 indent = indent2 ;
314303 }
@@ -335,16 +324,21 @@ static size_t display_mode_esc_sequence_len(const char *s)
335324 * consumed (and no extra indent is necessary for the first line).
336325 */
337326int strbuf_add_wrapped_text (struct strbuf * buf ,
338- const char * text , int indent , int indent2 , int width )
327+ const char * text , int indent1 , int indent2 , int width )
339328{
340- int w = indent , assume_utf8 = is_utf8 (text );
341- const char * bol = text , * space = NULL ;
329+ int indent , w , assume_utf8 = 1 ;
330+ const char * bol , * space , * start = text ;
331+ size_t orig_len = buf -> len ;
342332
343333 if (width <= 0 ) {
344- strbuf_add_indented_text (buf , text , indent , indent2 );
334+ strbuf_add_indented_text (buf , text , indent1 , indent2 );
345335 return 1 ;
346336 }
347337
338+ retry :
339+ bol = text ;
340+ w = indent = indent1 ;
341+ space = NULL ;
348342 if (indent < 0 ) {
349343 w = - indent ;
350344 space = text ;
@@ -366,8 +360,8 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
366360 if (space )
367361 start = space ;
368362 else
369- print_spaces (buf , indent );
370- strbuf_write (buf , start , text - start );
363+ strbuf_addchars (buf , ' ' , indent );
364+ strbuf_add (buf , start , text - start );
371365 if (!c )
372366 return w ;
373367 space = text ;
@@ -376,40 +370,41 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
376370 else if (c == '\n' ) {
377371 space ++ ;
378372 if (* space == '\n' ) {
379- strbuf_write (buf , "\n" , 1 );
373+ strbuf_addch (buf , '\n' );
380374 goto new_line ;
381375 }
382376 else if (!isalnum (* space ))
383377 goto new_line ;
384378 else
385- strbuf_write (buf , " " , 1 );
379+ strbuf_addch (buf , ' ' );
386380 }
387381 w ++ ;
388382 text ++ ;
389383 }
390384 else {
391385new_line :
392- strbuf_write (buf , "\n" , 1 );
386+ strbuf_addch (buf , '\n' );
393387 text = bol = space + isspace (* space );
394388 space = NULL ;
395389 w = indent = indent2 ;
396390 }
397391 continue ;
398392 }
399- if (assume_utf8 )
393+ if (assume_utf8 ) {
400394 w += utf8_width (& text , NULL );
401- else {
395+ if (!text ) {
396+ assume_utf8 = 0 ;
397+ text = start ;
398+ strbuf_setlen (buf , orig_len );
399+ goto retry ;
400+ }
401+ } else {
402402 w ++ ;
403403 text ++ ;
404404 }
405405 }
406406}
407407
408- int print_wrapped_text (const char * text , int indent , int indent2 , int width )
409- {
410- return strbuf_add_wrapped_text (NULL , text , indent , indent2 , width );
411- }
412-
413408int is_encoding_utf8 (const char * name )
414409{
415410 if (!name )
0 commit comments