aboutsummaryrefslogtreecommitdiffstats
path: root/rational.c
diff options
context:
space:
mode:
Diffstat (limited to 'rational.c')
-rw-r--r--rational.c40
1 files changed, 8 insertions, 32 deletions
diff --git a/rational.c b/rational.c
index 1a0f4cbabc..e0fe2a20cd 100644
--- a/rational.c
+++ b/rational.c
@@ -227,8 +227,6 @@ k_rational_p(VALUE x)
inline static long
i_gcd(long x, long y)
{
- long b;
-
if (x < 0)
x = -x;
if (y < 0)
@@ -239,32 +237,12 @@ i_gcd(long x, long y)
if (y == 0)
return x;
- b = 0;
- while ((x & 1) == 0 && (y & 1) == 0) {
- b += 1;
- x >>= 1;
- y >>= 1;
- }
-
- while ((x & 1) == 0)
- x >>= 1;
-
- while ((y & 1) == 0)
- y >>= 1;
-
- while (x != y) {
- if (y > x) {
- long t;
- t = x;
- x = y;
- y = t;
- }
- x -= y;
- while ((x & 1) == 0)
- x >>= 1;
+ while (x > 0) {
+ long t = x;
+ x = y % x;
+ y = t;
}
-
- return x << b;
+ return y;
}
inline static VALUE
@@ -1229,10 +1207,11 @@ float_to_r(VALUE self)
static VALUE rat_pat, an_e_pat, a_dot_pat, underscores_pat, an_underscore;
+#define WS "\\s*"
#define DIGITS "(?:\\d(?:_\\d|\\d)*)"
#define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?"
#define DENOMINATOR DIGITS
-#define PATTERN "\\A([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?"
+#define PATTERN "\\A" WS "([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?" WS
static void
make_patterns(void)
@@ -1261,9 +1240,6 @@ make_patterns(void)
rb_global_variable(&an_underscore);
}
-#define id_strip rb_intern("strip")
-#define f_strip(x) rb_funcall(x, id_strip, 0)
-
#define id_match rb_intern("match")
#define f_match(x,y) rb_funcall(x, id_match, 1, y)
@@ -1283,7 +1259,7 @@ string_to_r_internal(VALUE self)
{
VALUE s, m;
- s = f_strip(self);
+ s = self;
if (RSTRING_LEN(s) == 0)
return rb_assoc_new(Qnil, self);