From a94ef9433d5caa5b1b334bcfc34166613285c9c7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 26 Aug 2014 13:30:08 +0900 Subject: [PATCH] change interface of format functions --- error.c | 4 ++-- include/picrin/string.h | 10 +++++++--- string.c | 44 ++++++++++++++++++++++++++++++++++++----- write.c | 2 +- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/error.c b/error.c index f4d46f5e..71d24c71 100644 --- a/error.c +++ b/error.c @@ -27,7 +27,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...) pic_value err_line; va_start(ap, fmt); - err_line = pic_vformat(pic, fmt, ap); + err_line = pic_xvformat(pic, fmt, ap); va_end(ap); fprintf(stderr, "warn: %s\n", pic_str_cstr(pic_str_ptr(pic_car(pic, err_line)))); @@ -130,7 +130,7 @@ pic_errorf(pic_state *pic, const char *fmt, ...) const char *msg; va_start(ap, fmt); - err_line = pic_vformat(pic, fmt, ap); + err_line = pic_xvformat(pic, fmt, ap); va_end(ap); msg = pic_str_cstr(pic_str_ptr(pic_car(pic, err_line))); diff --git a/include/picrin/string.h b/include/picrin/string.h index c2564ffe..3df116cf 100644 --- a/include/picrin/string.h +++ b/include/picrin/string.h @@ -31,9 +31,13 @@ int pic_strcmp(pic_str *, pic_str *); const char *pic_str_cstr(pic_str *); -pic_value pic_format(pic_state *, const char *, ...); -pic_value pic_vformat(pic_state *, const char *, va_list); -pic_value pic_vfformat(pic_state *, xFILE *, const char *, va_list); +pic_str *pic_format(pic_state *, const char *, ...); +pic_str *pic_vformat(pic_state *, const char *, va_list); +void pic_vfformat(pic_state *, xFILE *, const char *, va_list); + +pic_value pic_xformat(pic_state *, const char *, ...); +pic_value pic_xvformat(pic_state *, const char *, va_list); +pic_value pic_xvfformat(pic_state *, xFILE *, const char *, va_list); #if defined(__cplusplus) } diff --git a/string.c b/string.c index 6533b45f..92a3c569 100644 --- a/string.c +++ b/string.c @@ -137,7 +137,7 @@ pic_str_cstr(pic_str *str) } pic_value -pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) +pic_xvfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) { char c; pic_value irrs = pic_nil_value(); @@ -205,14 +205,14 @@ pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) } pic_value -pic_vformat(pic_state *pic, const char *fmt, va_list ap) +pic_xvformat(pic_state *pic, const char *fmt, va_list ap) { struct pic_port *port; pic_value irrs; port = pic_open_output_string(pic); - irrs = pic_vfformat(pic, port->file, fmt, ap); + irrs = pic_xvfformat(pic, port->file, fmt, ap); irrs = pic_cons(pic, pic_obj_value(pic_get_output_string(pic, port)), irrs); pic_close_port(pic, port); @@ -220,18 +220,52 @@ pic_vformat(pic_state *pic, const char *fmt, va_list ap) } pic_value -pic_format(pic_state *pic, const char *fmt, ...) +pic_xformat(pic_state *pic, const char *fmt, ...) { va_list ap; pic_value objs; va_start(ap, fmt); - objs = pic_vformat(pic, fmt, ap); + objs = pic_xvformat(pic, fmt, ap); va_end(ap); return objs; } +void +pic_vfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) +{ + pic_xvfformat(pic, file, fmt, ap); +} + +pic_str * +pic_vformat(pic_state *pic, const char *fmt, va_list ap) +{ + struct pic_port *port; + pic_str *str; + + port = pic_open_output_string(pic); + + pic_vfformat(pic, port->file, fmt, ap); + str = pic_get_output_string(pic, port); + + pic_close_port(pic, port); + return str; +} + +pic_str * +pic_format(pic_state *pic, const char *fmt, ...) +{ + va_list ap; + pic_str *str; + + va_start(ap, fmt); + str = pic_vformat(pic, fmt, ap); + va_end(ap); + + return str; +} + static pic_value pic_str_string_p(pic_state *pic) { diff --git a/write.c b/write.c index 70a547b9..1ae61195 100644 --- a/write.c +++ b/write.c @@ -442,7 +442,7 @@ pic_printf(pic_state *pic, const char *fmt, ...) va_start(ap, fmt); - str = pic_str_ptr(pic_car(pic, pic_vformat(pic, fmt, ap))); + str = pic_str_ptr(pic_car(pic, pic_xvformat(pic, fmt, ap))); va_end(ap);