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>
This commit is contained in:
Johannes Schindelin 2008-11-10 18:47:00 +01:00 committed by Junio C Hamano
parent ae0b270230
commit a94410c813
2 changed files with 26 additions and 9 deletions

33
utf8.c
View File

@ -1,4 +1,5 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "strbuf.h"
#include "utf8.h" #include "utf8.h"
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */ /* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
@ -279,14 +280,22 @@ int is_utf8(const char *text)
return 1; return 1;
} }
static void print_spaces(int count) static inline void strbuf_write(struct strbuf *sb, const char *buf, int len)
{
if (sb)
strbuf_insert(sb, sb->len, buf, len);
else
fwrite(buf, len, 1, stdout);
}
static void print_spaces(struct strbuf *buf, int count)
{ {
static const char s[] = " "; static const char s[] = " ";
while (count >= sizeof(s)) { while (count >= sizeof(s)) {
fwrite(s, sizeof(s) - 1, 1, stdout); strbuf_write(buf, s, sizeof(s) - 1);
count -= sizeof(s) - 1; count -= sizeof(s) - 1;
} }
fwrite(s, count, 1, stdout); strbuf_write(buf, s, count);
} }
/* /*
@ -295,7 +304,8 @@ static void print_spaces(int count)
* If indent is negative, assume that already -indent columns have been * If indent is negative, assume that already -indent columns have been
* consumed (and no extra indent is necessary for the first line). * consumed (and no extra indent is necessary for the first line).
*/ */
int print_wrapped_text(const char *text, int indent, int indent2, int width) int strbuf_add_wrapped_text(struct strbuf *buf,
const char *text, int indent, int indent2, int width)
{ {
int w = indent, assume_utf8 = is_utf8(text); int w = indent, assume_utf8 = is_utf8(text);
const char *bol = text, *space = NULL; const char *bol = text, *space = NULL;
@ -315,8 +325,8 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
if (space) if (space)
start = space; start = space;
else else
print_spaces(indent); print_spaces(buf, indent);
fwrite(start, text - start, 1, stdout); strbuf_write(buf, start, text - start);
if (!c) if (!c)
return w; return w;
space = text; space = text;
@ -325,20 +335,20 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
else if (c == '\n') { else if (c == '\n') {
space++; space++;
if (*space == '\n') { if (*space == '\n') {
putchar('\n'); strbuf_write(buf, "\n", 1);
goto new_line; goto new_line;
} }
else if (!isalnum(*space)) else if (!isalnum(*space))
goto new_line; goto new_line;
else else
putchar(' '); strbuf_write(buf, " ", 1);
} }
w++; w++;
text++; text++;
} }
else { else {
new_line: new_line:
putchar('\n'); strbuf_write(buf, "\n", 1);
text = bol = space + isspace(*space); text = bol = space + isspace(*space);
space = NULL; space = NULL;
w = indent = indent2; w = indent = indent2;
@ -354,6 +364,11 @@ new_line:
} }
} }
int print_wrapped_text(const char *text, int indent, int indent2, int width)
{
return strbuf_add_wrapped_text(NULL, text, indent, indent2, width);
}
int is_encoding_utf8(const char *name) int is_encoding_utf8(const char *name)
{ {
if (!name) if (!name)

2
utf8.h
View File

@ -10,6 +10,8 @@ int is_utf8(const char *text);
int is_encoding_utf8(const char *name); int is_encoding_utf8(const char *name);
int print_wrapped_text(const char *text, int indent, int indent2, int len); int print_wrapped_text(const char *text, int indent, int indent2, int len);
int strbuf_add_wrapped_text(struct strbuf *buf,
const char *text, int indent, int indent2, int width);
#ifndef NO_ICONV #ifndef NO_ICONV
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding); char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);