Unverified Commit c883f4e3 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4609 from shuujii/change-the-mrb_vformat-specifier-%d-for-int

Change the `mrb_vformat` specifier `%d` for `int`
parents 3bacc302 d1817e77
......@@ -6,14 +6,28 @@
#define NATIVE_TYPES \
char c; \
int d; \
mrb_float f; \
mrb_int i; \
mrb_sym n; \
char *s; \
struct RClass *C
#define NATIVE_DEFINE_TYPE_FUNC(t) \
static mrb_value \
native_s_##t(mrb_state *mrb, mrb_value klass) \
{ \
mrb_value obj, type = mrb_fixnum_value(ARG_##t); \
mrb_get_args(mrb, "o", &obj); \
return mrb_funcall(mrb, klass, "new", 2, type, obj); \
}
#define NATIVE_DEFINE_TYPE_METHOD(t) \
mrb_define_class_method(mrb, n, #t, native_s_##t, MRB_ARGS_REQ(1))
typedef enum {
ARG_c,
ARG_d,
ARG_f,
ARG_i,
ARG_n,
......@@ -51,56 +65,37 @@ static mrb_value
native_initialize(mrb_state *mrb, mrb_value self)
{
VFNative data, *datap;
mrb_int type;
mrb_value obj;
mrb_get_args(mrb, "o", &obj);
switch (mrb_type(obj)) {
case MRB_TT_FLOAT:
data.f = mrb_float(obj);
data.type = ARG_f;
break;
case MRB_TT_FIXNUM:
data.i = mrb_fixnum(obj);
data.type = ARG_i;
break;
case MRB_TT_SYMBOL:
data.n = mrb_symbol(obj);
data.type = ARG_n;
break;
case MRB_TT_CLASS: case MRB_TT_SCLASS: case MRB_TT_MODULE:
data.C = mrb_class_ptr(obj);
data.type = ARG_C;
break;
case MRB_TT_STRING: {
mrb_int len = RSTRING_LEN(obj);
const char *s = RSTRING_PTR(obj);
if (len == 1) {
/* one byte string is considered char */
data.c = s[0];
data.type = ARG_c;
}
else {
data.s = (char*)mrb_malloc(mrb, len + 1);
memcpy(data.s, s, len);
data.s[len] = '\0';
data.type = ARG_s;
}
break;
}
default: {
mrb_value msg = mrb_str_new_cstr(mrb, "native type for ");
mrb_str_cat_cstr(mrb, msg, mrb_class_name(mrb, mrb_class(mrb, obj)));
mrb_str_cat_cstr(mrb, msg, " is unknown");
mrb_raise(mrb, E_ARGUMENT_ERROR, RSTRING_PTR(msg));
}
mrb_get_args(mrb, "io", &type, &obj);
data.type = (VFArgumentType)type;
switch (data.type) {
case ARG_c: data.c = RSTRING_PTR(obj)[0]; break;
case ARG_d: data.d = (int)mrb_fixnum(obj); break;
case ARG_f: data.f = mrb_float(obj); break;
case ARG_i: data.i = mrb_fixnum(obj); break;
case ARG_n: data.n = mrb_symbol(obj); break;
case ARG_s: data.s = (char*)mrb_malloc(mrb, RSTRING_LEN(obj) + 1);
memcpy(data.s, RSTRING_PTR(obj), RSTRING_LEN(obj));
data.s[RSTRING_LEN(obj)] = '\0'; break;
case ARG_C: data.C = mrb_class_ptr(obj); break;
default: mrb_raise(mrb, E_ARGUMENT_ERROR, "unknown type");
}
datap = (VFNative*)mrb_malloc(mrb, sizeof(VFNative));
*datap = data;
mrb_data_init(self, datap, &native_data_type);
return self;
}
NATIVE_DEFINE_TYPE_FUNC(c)
NATIVE_DEFINE_TYPE_FUNC(d)
NATIVE_DEFINE_TYPE_FUNC(f)
NATIVE_DEFINE_TYPE_FUNC(i)
NATIVE_DEFINE_TYPE_FUNC(n)
NATIVE_DEFINE_TYPE_FUNC(s)
NATIVE_DEFINE_TYPE_FUNC(C)
static VFArgument*
arg_from_obj(mrb_state *mrb, mrb_value obj, struct RClass *native_class,
VFArgument *vf_arg)
......@@ -130,6 +125,7 @@ arg_from_obj(mrb_state *mrb, mrb_value obj, struct RClass *native_class,
#define VF_FORMAT2(fmt, args) ( \
VF_ARG(args, 0), VF_ARG(args, 1), \
VF_FORMAT2_COND_EXPR(fmt, vf_args, vf_args+1, c) : \
VF_FORMAT2_COND_EXPR(fmt, vf_args, vf_args+1, d) : \
VF_FORMAT2_COND_EXPR(fmt, vf_args, vf_args+1, f) : \
VF_FORMAT2_COND_EXPR(fmt, vf_args, vf_args+1, i) : \
VF_FORMAT2_COND_EXPR(fmt, vf_args, vf_args+1, n) : \
......@@ -142,6 +138,7 @@ arg_from_obj(mrb_state *mrb, mrb_value obj, struct RClass *native_class,
a1->type == ARG_##t ? VF_FORMAT_TYPED(fmt, 2, a2, (a1)->t)
#define VF_FORMAT_TYPED(fmt, n_arg, type_a, v1) \
VF_FORMAT_TYPED_COND_EXPR(fmt, n_arg, type_a, v1, c) : \
VF_FORMAT_TYPED_COND_EXPR(fmt, n_arg, type_a, v1, d) : \
VF_FORMAT_TYPED_COND_EXPR(fmt, n_arg, type_a, v1, f) : \
VF_FORMAT_TYPED_COND_EXPR(fmt, n_arg, type_a, v1, i) : \
VF_FORMAT_TYPED_COND_EXPR(fmt, n_arg, type_a, v1, n) : \
......@@ -180,7 +177,12 @@ mrb_init_test_vformat(mrb_state *mrb)
n = mrb_define_class_under(mrb, vf, "Native", mrb->object_class);
MRB_SET_INSTANCE_TT(n, MRB_TT_DATA);
mrb_define_method(mrb, n, "initialize", native_initialize, MRB_ARGS_REQ(1));
mrb_singleton_class(mrb, mrb_obj_value(n));
mrb_define_alias(mrb, n->c, "[]", "new");
NATIVE_DEFINE_TYPE_METHOD(c);
NATIVE_DEFINE_TYPE_METHOD(d);
NATIVE_DEFINE_TYPE_METHOD(f);
NATIVE_DEFINE_TYPE_METHOD(i);
NATIVE_DEFINE_TYPE_METHOD(n);
NATIVE_DEFINE_TYPE_METHOD(s);
NATIVE_DEFINE_TYPE_METHOD(C);
mrb_define_method(mrb, n, "initialize", native_initialize, MRB_ARGS_REQ(2));
}
......@@ -282,8 +282,9 @@ mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
* Specifier | Argument Type | Note
* ----------+----------------+--------------------------------------------
* c | char |
* d,i | mrb_int |
* d | int |
* f | mrb_float |
* i | mrb_int |
* l | char*, mrb_int | Arguments are string and length.
* n | mrb_sym |
* s | char* | Argument is NUL terminated string.
......@@ -303,7 +304,7 @@ mrb_vformat(mrb_state *mrb, const char *format, va_list ap)
const char *chars, *p = format, *b = format, *e;
char ch;
struct RClass *cls;
mrb_int len;
mrb_int len, i;
mrb_bool inspect = FALSE;
mrb_value result = mrb_str_new_capa(mrb, 128), obj, str;
int ai = mrb_gc_arena_save(mrb);
......@@ -324,7 +325,8 @@ mrb_vformat(mrb_state *mrb, const char *format, va_list ap)
len = 1;
goto L_cat;
case 'd': case 'i':
obj = mrb_fixnum_value(va_arg(ap, mrb_int));
i = *p == 'd' ? (mrb_int)va_arg(ap, int) : va_arg(ap, mrb_int);
obj = mrb_fixnum_value(i);
goto L_cat_obj;
case 'f':
obj = mrb_float_value(mrb, va_arg(ap, mrb_float));
......
......@@ -30,24 +30,26 @@ assert('mrb_vformat') do
n = TestVFormat::Native
assert_format '', ['']
assert_format 'No specifier!', ['No specifier!']
assert_format '`c`: C', ['`c`: %c', n[?C]]
assert_format '`d`: 123', ['`d`: %d', n[123]]
assert_format '`i`: -83', ['`i`: %i', n[-83]]
assert_format '`f`: 0.0125', ['`f`: %f', n[0.0125]]
assert_format '`c`: C', ['`c`: %c', n.c(?C)]
assert_format '`d`: 123', ['`d`: %d', n.d(123)]
assert_format '`d`: -79', ['`d`: %d', n.d(-79)]
assert_format '`i`: 514', ['`i`: %i', n.i(514)]
assert_format '`i`: -83', ['`i`: %i', n.i(-83)]
assert_format '`f`: 0.0125', ['`f`: %f', n.f(0.0125)]
assert_format '`t`: NilClass', ['`t`: %t', nil]
assert_format '`t`: FalseClass', ['`t`: %t', false]
assert_format '`t`: TrueClass', ['`t`: %t', true]
assert_format '`t`: Fixnum', ['`t`: %t', 0]
assert_format '`t`: Hash', ['`t`: %t', k: "value"]
assert_format_pattern '#<Class:#<Class:#<Hash:0x*>>>', ['%t', sclass({})]
assert_format '-Infinity', ['%f', n[-Float::INFINITY]]
assert_format 'NaN: Not a Number', ['%f: Not a Number', n[Float::NAN]]
assert_format 'string and length', ['string %l length', n['andante'], n[3]]
assert_format '`n`: sym', ['`n`: %n', n[:sym]]
assert_format '%C文字列%', ['%s', n['%C文字列%']]
assert_format '`C`: Kernel module', ['`C`: %C module', n[Kernel]]
assert_format '`C`: NilClass', ['`C`: %C', n[nil.class]]
assert_format_pattern '#<Class:#<String:0x*>>', ['%C', n[sclass("")]]
assert_format '-Infinity', ['%f', n.f(-Float::INFINITY)]
assert_format 'NaN: Not a Number', ['%f: Not a Number', n.f(Float::NAN)]
assert_format 'string and length', ['string %l length', n.s('andante'), n.i(3)]
assert_format '`n`: sym', ['`n`: %n', n.n(:sym)]
assert_format '%C文字列%', ['%s', n.s('%C文字列%')]
assert_format '`C`: Kernel module', ['`C`: %C module', n.C(Kernel)]
assert_format '`C`: NilClass', ['`C`: %C', n.C(nil.class)]
assert_format_pattern '#<Class:#<String:0x*>>', ['%C', n.C(sclass(""))]
assert_format '`T`: NilClass', ['`T`: %T', nil]
assert_format '`T`: FalseClass', ['`T`: %T', false]
assert_format '`T`: TrueClass', ['`T`: %T', true]
......@@ -64,18 +66,18 @@ assert('mrb_vformat') do
assert_format '`v`: 1...3', ['`v`: %v', 1...3]
assert_format '`S`: {:a=>1, "b"=>"c"}', ['`S`: %S', a: 1, "b" => ?c]
assert_format 'percent: %', ['percent: %%']
assert_format '"I": inspect char', ['%!c: inspect char', n[?I]]
assert_format '709: inspect mrb_int', ['%!d: inspect mrb_int', n[709]]
assert_format '"a\x00b\xff"', ['%!l', n["a\000b\xFFc\000d"], n[4]]
assert_format ':"&.": inspect symbol', ['%!n: inspect symbol', n['&.'.to_sym]]
assert_format '"I": inspect char', ['%!c: inspect char', n.c(?I)]
assert_format '709: inspect mrb_int', ['%!d: inspect mrb_int', n.i(709)]
assert_format '"a\x00b\xff"', ['%!l', n.s("a\000b\xFFc\000d"), n.i(4)]
assert_format ':"&.": inspect symbol', ['%!n: inspect symbol', n.n(:'&.')]
assert_format 'inspect "String"', ['inspect %!v', 'String']
assert_format 'inspect Array: [1, :x, {}]', ['inspect Array: %!v', [1,:x,{}]]
assert_format_pattern '`!C`: #<Class:0x*>', ['`!C`: %!C', n[Class.new]]
assert_format 'to_s -> to_s: ab,cd', ['to_s -> to_s: %n,%v', n[:ab], 'cd']
assert_format_pattern '`!C`: #<Class:0x*>', ['`!C`: %!C', n.C(Class.new)]
assert_format 'to_s -> to_s: ab,cd', ['to_s -> to_s: %n,%v', n.n(:ab), 'cd']
assert_format 'to_s -> inspect: x:y', ['to_s -> inspect: %v%!v', 'x', :y]
assert_format 'inspect -> to_s: "a"b', ['inspect -> to_s: %!v%n', 'a', n[:b]]
assert_format 'inspect -> to_s: "a"b', ['inspect -> to_s: %!v%n', 'a', n.n(:b)]
assert_format 'Y -> to_s: nile', ['Y -> to_s: %Y%v', nil, "e"]
assert_format '"abc":Z', ['%!s%!n', n['abc'], n[:Z]]
assert_format '"abc":Z', ['%!s%!n', n.s('abc'), n.n('Z'.to_sym)]
assert_format 'escape: \\%a,b,c,d', ['escape: \\\\\%a,b,\c%v', ',d']
assert_implementation_dependent 'unknown specifier: %^',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment