From 21453dc7381d09e6926c02c0143812680c1cfa8a Mon Sep 17 00:00:00 2001 From: tadf Date: Tue, 20 Nov 2012 12:10:08 +0000 Subject: * complex.c: some improvements. * rational.c: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- rational.c | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'rational.c') diff --git a/rational.c b/rational.c index 24d7ff1afe..16b7e4b354 100644 --- a/rational.c +++ b/rational.c @@ -1958,40 +1958,53 @@ float_rationalize(int argc, VALUE *argv, VALUE self) #include +inline static int +issign(int c) +{ + return (c == '-' || c == '+'); +} + static int read_sign(const char **s) { int sign = '?'; - if (**s == '-' || **s == '+') { + if (issign(**s)) { sign = **s; (*s)++; } return sign; } +inline static int +isdecimal(int c) +{ + return isdigit((unsigned char)c); +} + static int read_digits(const char **s, int strict, VALUE *num, int *count) { - int us = 1; + int us = 1, ret = 1; + const char *b = *s; - if (!isdigit((unsigned char)**s)) + if (!isdecimal(**s)) { + *num = ZERO; return 0; + } - *num = ZERO; - - while (isdigit((unsigned char)**s) || **s == '_') { + while (isdecimal(**s) || **s == '_') { if (**s == '_') { if (strict) { - if (us) - return 0; + if (us) { + ret = 0; + goto conv; + } } us = 1; } else { - *num = f_mul(*num, INT2FIX(10)); - *num = f_add(*num, INT2FIX(**s - '0')); if (count) (*count)++; us = 0; @@ -2002,7 +2015,15 @@ read_digits(const char **s, int strict, do { (*s)--; } while (**s == '_'); - return 1; + conv: + *num = rb_cstr_to_inum(b, 10, 0); + return ret; +} + +inline static int +islettere(int c) +{ + return (c == 'e' || c == 'E'); } static int @@ -2034,7 +2055,7 @@ read_num(const char **s, int numsign, int strict, } } - if (**s == 'e' || **s == 'E') { + if (islettere(**s)) { int expsign; (*s)++; @@ -2054,7 +2075,7 @@ read_num(const char **s, int numsign, int strict, return 1; } -static int +inline static int read_den(const char **s, int strict, VALUE *num) { @@ -2093,7 +2114,7 @@ read_rat(const char **s, int strict, return 1; } -static void +inline static void skip_ws(const char **s) { while (isspace((unsigned char)**s)) -- cgit v1.2.3