@@ -280,22 +280,11 @@ int is_utf8(const char *text)
280
280
return 1 ;
281
281
}
282
282
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 )
284
284
{
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 );
299
288
}
300
289
301
290
static 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,
307
296
const char * eol = strchrnul (text , '\n' );
308
297
if (* eol == '\n' )
309
298
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 );
312
301
text = eol ;
313
302
indent = indent2 ;
314
303
}
@@ -335,16 +324,21 @@ static size_t display_mode_esc_sequence_len(const char *s)
335
324
* consumed (and no extra indent is necessary for the first line).
336
325
*/
337
326
int 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 )
339
328
{
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 ;
342
332
343
333
if (width <= 0 ) {
344
- strbuf_add_indented_text (buf , text , indent , indent2 );
334
+ strbuf_add_indented_text (buf , text , indent1 , indent2 );
345
335
return 1 ;
346
336
}
347
337
338
+ retry :
339
+ bol = text ;
340
+ w = indent = indent1 ;
341
+ space = NULL ;
348
342
if (indent < 0 ) {
349
343
w = - indent ;
350
344
space = text ;
@@ -366,8 +360,8 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
366
360
if (space )
367
361
start = space ;
368
362
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 );
371
365
if (!c )
372
366
return w ;
373
367
space = text ;
@@ -376,40 +370,41 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
376
370
else if (c == '\n' ) {
377
371
space ++ ;
378
372
if (* space == '\n' ) {
379
- strbuf_write (buf , "\n" , 1 );
373
+ strbuf_addch (buf , '\n' );
380
374
goto new_line ;
381
375
}
382
376
else if (!isalnum (* space ))
383
377
goto new_line ;
384
378
else
385
- strbuf_write (buf , " " , 1 );
379
+ strbuf_addch (buf , ' ' );
386
380
}
387
381
w ++ ;
388
382
text ++ ;
389
383
}
390
384
else {
391
385
new_line :
392
- strbuf_write (buf , "\n" , 1 );
386
+ strbuf_addch (buf , '\n' );
393
387
text = bol = space + isspace (* space );
394
388
space = NULL ;
395
389
w = indent = indent2 ;
396
390
}
397
391
continue ;
398
392
}
399
- if (assume_utf8 )
393
+ if (assume_utf8 ) {
400
394
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 {
402
402
w ++ ;
403
403
text ++ ;
404
404
}
405
405
}
406
406
}
407
407
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
-
413
408
int is_encoding_utf8 (const char * name )
414
409
{
415
410
if (!name )
0 commit comments