summaryrefslogtreecommitdiffhomepage
path: root/src/error.c
diff options
context:
space:
mode:
authorYukihiro Matz Matsumoto <[email protected]>2013-03-27 23:03:48 +0900
committerYukihiro Matz Matsumoto <[email protected]>2013-03-27 23:03:48 +0900
commit8ce842a5d978132c5733cacb7c15bb3d0f5ce66d (patch)
tree0875f3e9cabc75bd400cff36e0879e38f0f32ae1 /src/error.c
parent8f8c22ac6747319e82654177c971eacb67b2688c (diff)
downloadmruby-8ce842a5d978132c5733cacb7c15bb3d0f5ce66d.tar.gz
mruby-8ce842a5d978132c5733cacb7c15bb3d0f5ce66d.zip
implement mrb_format and mrb_vformat
Diffstat (limited to 'src/error.c')
-rw-r--r--src/error.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/error.c b/src/error.c
index ab5480e97..954140418 100644
--- a/src/error.c
+++ b/src/error.c
@@ -13,6 +13,7 @@
#include "error.h"
#include "mruby/variable.h"
#include "mruby/string.h"
+#include "mruby/array.h"
#include "mruby/class.h"
#include "mruby/proc.h"
#include "mruby/irep.h"
@@ -228,6 +229,54 @@ mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
mrb_exc_raise(mrb, mrb_exc_new3(mrb, c, mesg));
}
+mrb_value
+mrb_vformat(mrb_state *mrb, const char *format, va_list ap)
+{
+ const char *p = format;
+ const char *b = p;
+ ptrdiff_t size;
+ mrb_value ary = mrb_ary_new_capa(mrb, 4);
+
+ while (*p) {
+ const char c = *p++;
+
+ if (c == '%') {
+ if (*p == 'S') {
+ size = p - b - 1;
+ mrb_ary_push(mrb, ary, mrb_str_new(mrb, b, size));
+ mrb_ary_push(mrb, ary, va_arg(ap, mrb_value));
+ b = p + 1;
+ }
+ }
+ else if (c == '\\') {
+ if (!*p) break;
+ p++;
+ }
+ p++;
+ }
+ if (b == format) {
+ return mrb_str_new_cstr(mrb, format);
+ }
+ else {
+ size = p - b;
+ mrb_ary_push(mrb, ary, mrb_str_new(mrb, b, size));
+ return mrb_ary_join(mrb, ary, mrb_str_new(mrb,NULL,0));
+ }
+}
+
+mrb_value
+mrb_format(mrb_state *mrb, const char *format, ...)
+{
+ va_list ap;
+ mrb_value str;
+
+ va_start(ap, format);
+ str = mrb_vformat(mrb, format, ap);
+ va_end(ap);
+
+ return str;
+}
+
void
mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...)
{