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: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
1b8b2e4dc8
commit
9a0a30aa4b
33
strbuf.c
33
strbuf.c
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user