Skip to content

Commit 9a0a30a

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b8b2e4 commit 9a0a30a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

strbuf.c

+33
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,36 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
464464
{
465465
strbuf_add_urlencode(sb, s, strlen(s), reserved);
466466
}
467+
468+
void strbuf_addf_ln(struct strbuf *sb, const char *fmt, ...)
469+
{
470+
va_list ap;
471+
va_start(ap, fmt);
472+
strbuf_vaddf(sb, fmt, ap);
473+
va_end(ap);
474+
strbuf_addch(sb, '\n');
475+
}
476+
477+
int printf_ln(const char *fmt, ...)
478+
{
479+
int ret;
480+
va_list ap;
481+
va_start(ap, fmt);
482+
ret = vprintf(fmt, ap);
483+
va_end(ap);
484+
if (ret < 0 || putchar('\n') == EOF)
485+
return -1;
486+
return ret + 1;
487+
}
488+
489+
int fprintf_ln(FILE *fp, const char *fmt, ...)
490+
{
491+
int ret;
492+
va_list ap;
493+
va_start(ap, fmt);
494+
ret = vfprintf(fp, fmt, ap);
495+
va_end(ap);
496+
if (ret < 0 || putc('\n', fp) == EOF)
497+
return -1;
498+
return ret + 1;
499+
}

strbuf.h

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ __attribute__((format (printf,2,3)))
9999
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
100100
__attribute__((format (printf,2,0)))
101101
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
102+
__attribute__((format (printf,2,3)))
103+
extern void strbuf_addf_ln(struct strbuf *sb, const char *fmt, ...);
102104

103105
extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
104106

@@ -129,4 +131,9 @@ extern void strbuf_add_urlencode(struct strbuf *, const char *, size_t,
129131
extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
130132
int reserved);
131133

134+
__attribute__((format (printf,1,2)))
135+
extern int printf_ln(const char *fmt, ...);
136+
__attribute__((format (printf,2,3)))
137+
extern int fprintf_ln(FILE *fp, const char *fmt, ...);
138+
132139
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)