Skip to content

Commit a94410c

Browse files
dschogitster
authored andcommitted
Add strbuf_add_wrapped_text() to utf8.[ch]
The newly added function can rewrap text according to a given first-line indent, other-indent and text width. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ae0b270 commit a94410c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

utf8.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "git-compat-util.h"
2+
#include "strbuf.h"
23
#include "utf8.h"
34

45
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
@@ -279,14 +280,22 @@ int is_utf8(const char *text)
279280
return 1;
280281
}
281282

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)
283292
{
284293
static const char s[] = " ";
285294
while (count >= sizeof(s)) {
286-
fwrite(s, sizeof(s) - 1, 1, stdout);
295+
strbuf_write(buf, s, sizeof(s) - 1);
287296
count -= sizeof(s) - 1;
288297
}
289-
fwrite(s, count, 1, stdout);
298+
strbuf_write(buf, s, count);
290299
}
291300

292301
/*
@@ -295,7 +304,8 @@ static void print_spaces(int count)
295304
* If indent is negative, assume that already -indent columns have been
296305
* consumed (and no extra indent is necessary for the first line).
297306
*/
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)
299309
{
300310
int w = indent, assume_utf8 = is_utf8(text);
301311
const char *bol = text, *space = NULL;
@@ -315,8 +325,8 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
315325
if (space)
316326
start = space;
317327
else
318-
print_spaces(indent);
319-
fwrite(start, text - start, 1, stdout);
328+
print_spaces(buf, indent);
329+
strbuf_write(buf, start, text - start);
320330
if (!c)
321331
return w;
322332
space = text;
@@ -325,20 +335,20 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
325335
else if (c == '\n') {
326336
space++;
327337
if (*space == '\n') {
328-
putchar('\n');
338+
strbuf_write(buf, "\n", 1);
329339
goto new_line;
330340
}
331341
else if (!isalnum(*space))
332342
goto new_line;
333343
else
334-
putchar(' ');
344+
strbuf_write(buf, " ", 1);
335345
}
336346
w++;
337347
text++;
338348
}
339349
else {
340350
new_line:
341-
putchar('\n');
351+
strbuf_write(buf, "\n", 1);
342352
text = bol = space + isspace(*space);
343353
space = NULL;
344354
w = indent = indent2;
@@ -354,6 +364,11 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
354364
}
355365
}
356366

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+
357372
int is_encoding_utf8(const char *name)
358373
{
359374
if (!name)

utf8.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ int is_utf8(const char *text);
1010
int is_encoding_utf8(const char *name);
1111

1212
int print_wrapped_text(const char *text, int indent, int indent2, int len);
13+
int strbuf_add_wrapped_text(struct strbuf *buf,
14+
const char *text, int indent, int indent2, int width);
1315

1416
#ifndef NO_ICONV
1517
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);

0 commit comments

Comments
 (0)