mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* rational.c (nurat_{to_s,inspect}): performance improvement.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20547 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2d3f41d19f
commit
d5d39dd3fe
2 changed files with 27 additions and 9 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Fri Dec 5 21:45:45 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* rational.c (nurat_{to_s,inspect}): performance improvement.
|
||||||
|
|
||||||
Fri Dec 5 21:42:44 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
Fri Dec 5 21:42:44 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
* complex.c: inpsect should not depend on to_s.
|
* complex.c: inpsect should not depend on to_s.
|
||||||
|
|
32
rational.c
32
rational.c
|
@ -27,8 +27,8 @@
|
||||||
VALUE rb_cRational;
|
VALUE rb_cRational;
|
||||||
|
|
||||||
static ID id_abs, id_cmp, id_convert, id_equal_p, id_expt, id_floor,
|
static ID id_abs, id_cmp, id_convert, id_equal_p, id_expt, id_floor,
|
||||||
id_format, id_hash, id_idiv, id_inspect, id_integer_p, id_negate,
|
id_hash, id_idiv, id_inspect, id_integer_p, id_negate, id_to_f,
|
||||||
id_to_f, id_to_i, id_to_s, id_truncate;
|
id_to_i, id_to_s, id_truncate;
|
||||||
|
|
||||||
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
|
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
|
||||||
|
|
||||||
|
@ -1110,20 +1110,35 @@ nurat_hash(VALUE self)
|
||||||
return f_xor(f_hash(dat->num), f_hash(dat->den));
|
return f_xor(f_hash(dat->num), f_hash(dat->den));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
nurat_format(VALUE self, VALUE (*func)(VALUE))
|
||||||
|
{
|
||||||
|
VALUE s;
|
||||||
|
get_dat1(self);
|
||||||
|
|
||||||
|
s = (*func)(dat->num);
|
||||||
|
rb_str_cat2(s, "/");
|
||||||
|
rb_str_concat(s, (*func)(dat->den));
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
nurat_to_s(VALUE self)
|
nurat_to_s(VALUE self)
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
return nurat_format(self, f_to_s);
|
||||||
return rb_funcall(rb_mKernel, id_format, 3,
|
|
||||||
rb_str_new2("%d/%d"), dat->num, dat->den);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
nurat_inspect(VALUE self)
|
nurat_inspect(VALUE self)
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
VALUE s;
|
||||||
return rb_funcall(rb_mKernel, id_format, 3,
|
|
||||||
rb_str_new2("(%d/%d)"), dat->num, dat->den);
|
s = rb_str_new2("(");
|
||||||
|
rb_str_concat(s, nurat_format(self, f_inspect));
|
||||||
|
rb_str_cat2(s, ")");
|
||||||
|
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -1484,7 +1499,6 @@ Init_Rational(void)
|
||||||
id_equal_p = rb_intern("==");
|
id_equal_p = rb_intern("==");
|
||||||
id_expt = rb_intern("**");
|
id_expt = rb_intern("**");
|
||||||
id_floor = rb_intern("floor");
|
id_floor = rb_intern("floor");
|
||||||
id_format = rb_intern("format");
|
|
||||||
id_hash = rb_intern("hash");
|
id_hash = rb_intern("hash");
|
||||||
id_idiv = rb_intern("div");
|
id_idiv = rb_intern("div");
|
||||||
id_inspect = rb_intern("inspect");
|
id_inspect = rb_intern("inspect");
|
||||||
|
|
Loading…
Add table
Reference in a new issue