summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ae0b270)
raw | patch | inline | side by side (parent: ae0b270)
author | Johannes Schindelin <johannes.schindelin@gmx.de> | |
Mon, 10 Nov 2008 17:47:00 +0000 (18:47 +0100) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Mon, 19 Oct 2009 07:57:29 +0000 (00:57 -0700) |
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>
indent, other-indent and text width.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
utf8.c | patch | blob | history | |
utf8.h | patch | blob | history |
index 2aace1d42cd224e578e667290515daadf006e71c..da996695ccf06315f10f9c837d62784f1c34ed05 100644 (file)
--- a/utf8.c
+++ b/utf8.c
#include "git-compat-util.h"
+#include "strbuf.h"
#include "utf8.h"
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
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[] = " ";
while (count >= sizeof(s)) {
- fwrite(s, sizeof(s) - 1, 1, stdout);
+ strbuf_write(buf, s, sizeof(s) - 1);
count -= sizeof(s) - 1;
}
- fwrite(s, count, 1, stdout);
+ strbuf_write(buf, s, count);
}
/*
* If indent is negative, assume that already -indent columns have been
* 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);
const char *bol = text, *space = NULL;
if (space)
start = space;
else
- print_spaces(indent);
- fwrite(start, text - start, 1, stdout);
+ print_spaces(buf, indent);
+ strbuf_write(buf, start, text - start);
if (!c)
return w;
space = text;
else if (c == '\n') {
space++;
if (*space == '\n') {
- putchar('\n');
+ strbuf_write(buf, "\n", 1);
goto new_line;
}
else if (!isalnum(*space))
goto new_line;
else
- putchar(' ');
+ strbuf_write(buf, " ", 1);
}
w++;
text++;
}
else {
new_line:
- putchar('\n');
+ strbuf_write(buf, "\n", 1);
text = bol = space + isspace(*space);
space = NULL;
w = indent = indent2;
}
}
+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)
{
if (!name)
index 2f1b14ff49ef3c73bee6f298ba396b96120c34b7..ae30ae4c6e501e4766db93c94253fd404cd29357 100644 (file)
--- a/utf8.h
+++ b/utf8.h
int is_encoding_utf8(const char *name);
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
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);