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 <johannes.schindelin@gmx.de>
Johannes Schindelin committed
Nov 10, 2008 at 18:47 UTC
a94410c8134581f2f11a7db838da4d8725911a3c
2 files changed
+26
-9
utf8.c
+24
-9
@@ -1,4 +1,5 @@
1
#include "git-compat-util.h"
2
+#include "strbuf.h"
3
#include "utf8.h"
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)
280
return 1;
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)
292
{
293
static const char s[] = " ";
294
while (count >= sizeof(s)) {
286
- fwrite(s, sizeof(s) - 1, 1, stdout);
295
+ strbuf_write(buf, s, sizeof(s) - 1);
296
count -= sizeof(s) - 1;
297
}
289
- fwrite(s, count, 1, stdout);
298
+ strbuf_write(buf, s, count);
299
}
300
301
/*
@@ -295,7 +304,8 @@ static void print_spaces(int count)
304
* If indent is negative, assume that already -indent columns have been
305
* consumed (and no extra indent is necessary for the first line).
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)
309
{
310
int w = indent, assume_utf8 = is_utf8(text);
311
const char *bol = text, *space = NULL;
@@ -315,8 +325,8 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
325
if (space)
326
start = space;
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);
330
if (!c)
331
return w;
332
space = text;
@@ -325,20 +335,20 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
335
else if (c == '\n') {
336
space++;
337
if (*space == '\n') {
328
- putchar('\n');
338
+ strbuf_write(buf, "\n", 1);
339
goto new_line;
340
}
341
else if (!isalnum(*space))
342
goto new_line;
343
else
334
- putchar(' ');
344
+ strbuf_write(buf, " ", 1);
345
}
346
w++;
347
text++;
348
}
349
else {
350
new_line:
341
- putchar('\n');
351
+ strbuf_write(buf, "\n", 1);
352
text = bol = space + isspace(*space);
353
space = NULL;
354
w = indent = indent2;
@@ -354,6 +364,11 @@ new_line:
364
}
365
}
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
+
372
int is_encoding_utf8(const char *name)
373
{
374
if (!name)
utf8.h
+2
@@ -10,6 +10,8 @@ int is_utf8(const char *text);
10
int is_encoding_utf8(const char *name);
11
12
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);
15
16
#ifndef NO_ICONV
17
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);