summaryrefslogtreecommitdiffhomepage
path: root/src/error.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.c')
-rw-r--r--src/error.c61
1 files changed, 53 insertions, 8 deletions
diff --git a/src/error.c b/src/error.c
index ab5480e97..52e4958a1 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,20 +229,64 @@ 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, ...)
{
va_list args;
- char buf[256];
- int n;
+ mrb_value mesg;
va_start(args, fmt);
- n = vsnprintf(buf, sizeof(buf), fmt, args);
+ mesg = mrb_vformat(mrb, fmt, args);
va_end(args);
- if (n < 0) {
- n = 0;
- }
- mrb_exc_raise(mrb, mrb_exc_new(mrb, c, buf, n));
+ mrb_exc_raise(mrb, mrb_exc_new3(mrb, c, mesg));
}
void
@@ -344,7 +389,7 @@ exception_call:
break;
default:
- mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%d for 0..3)", argc);
+ mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 0..3)", mrb_fixnum_value(argc));
break;
}
if (argc > 0) {