1
1
#include "git-compat-util.h"
2
+ #include "strbuf.h"
2
3
#include "utf8.h"
3
4
4
5
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
@@ -279,14 +280,22 @@ int is_utf8(const char *text)
279
280
return 1 ;
280
281
}
281
282
282
- static void print_spaces (int count )
283
+ static inline void strbuf_write (struct strbuf * sb , const char * buf , int len )
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 )
283
292
{
284
293
static const char s [] = " " ;
285
294
while (count >= sizeof (s )) {
286
- fwrite ( s , sizeof (s ) - 1 , 1 , stdout );
295
+ strbuf_write ( buf , s , sizeof (s ) - 1 );
287
296
count -= sizeof (s ) - 1 ;
288
297
}
289
- fwrite ( s , count , 1 , stdout );
298
+ strbuf_write ( buf , s , count );
290
299
}
291
300
292
301
/*
@@ -295,7 +304,8 @@ static void print_spaces(int count)
295
304
* If indent is negative, assume that already -indent columns have been
296
305
* consumed (and no extra indent is necessary for the first line).
297
306
*/
298
- int print_wrapped_text (const char * text , int indent , int indent2 , int width )
307
+ int strbuf_add_wrapped_text (struct strbuf * buf ,
308
+ const char * text , int indent , int indent2 , int width )
299
309
{
300
310
int w = indent , assume_utf8 = is_utf8 (text );
301
311
const char * bol = text , * space = NULL ;
@@ -315,8 +325,8 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
315
325
if (space )
316
326
start = space ;
317
327
else
318
- print_spaces (indent );
319
- fwrite ( start , text - start , 1 , stdout );
328
+ print_spaces (buf , indent );
329
+ strbuf_write ( buf , start , text - start );
320
330
if (!c )
321
331
return w ;
322
332
space = text ;
@@ -325,20 +335,20 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
325
335
else if (c == '\n' ) {
326
336
space ++ ;
327
337
if (* space == '\n' ) {
328
- putchar ( '\n' );
338
+ strbuf_write ( buf , "\n" , 1 );
329
339
goto new_line ;
330
340
}
331
341
else if (!isalnum (* space ))
332
342
goto new_line ;
333
343
else
334
- putchar ( ' ' );
344
+ strbuf_write ( buf , " " , 1 );
335
345
}
336
346
w ++ ;
337
347
text ++ ;
338
348
}
339
349
else {
340
350
new_line :
341
- putchar ( '\n' );
351
+ strbuf_write ( buf , "\n" , 1 );
342
352
text = bol = space + isspace (* space );
343
353
space = NULL ;
344
354
w = indent = indent2 ;
@@ -354,6 +364,11 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
354
364
}
355
365
}
356
366
367
+ int print_wrapped_text (const char * text , int indent , int indent2 , int width )
368
+ {
369
+ return strbuf_add_wrapped_text (NULL , text , indent , indent2 , width );
370
+ }
371
+
357
372
int is_encoding_utf8 (const char * name )
358
373
{
359
374
if (!name )
0 commit comments