提交 9a0a30aa 编写于 作者: N Nguyễn Thái Ngọc Duy 提交者: Junio C Hamano

strbuf: convenience format functions with \n automatically appended

These functions are helpful when we do not want to expose \n to
translators. For example

    printf("hello world\n");

can be converted to

    printf_ln(_("hello world"));
Signed-off-by: NNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 1b8b2e4d
......@@ -464,3 +464,36 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
{
strbuf_add_urlencode(sb, s, strlen(s), reserved);
}
void strbuf_addf_ln(struct strbuf *sb, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
strbuf_vaddf(sb, fmt, ap);
va_end(ap);
strbuf_addch(sb, '\n');
}
int printf_ln(const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vprintf(fmt, ap);
va_end(ap);
if (ret < 0 || putchar('\n') == EOF)
return -1;
return ret + 1;
}
int fprintf_ln(FILE *fp, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf(fp, fmt, ap);
va_end(ap);
if (ret < 0 || putc('\n', fp) == EOF)
return -1;
return ret + 1;
}
......@@ -99,6 +99,8 @@ __attribute__((format (printf,2,3)))
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
__attribute__((format (printf,2,0)))
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
__attribute__((format (printf,2,3)))
extern void strbuf_addf_ln(struct strbuf *sb, const char *fmt, ...);
extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
......@@ -129,4 +131,9 @@ extern void strbuf_add_urlencode(struct strbuf *, const char *, size_t,
extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
int reserved);
__attribute__((format (printf,1,2)))
extern int printf_ln(const char *fmt, ...);
__attribute__((format (printf,2,3)))
extern int fprintf_ln(FILE *fp, const char *fmt, ...);
#endif /* STRBUF_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册