* complex.c: some improvements.

* rational.c: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tadf 2012-11-20 12:10:08 +00:00
parent 3b3d2006c2
commit 21453dc738
3 changed files with 69 additions and 25 deletions

View File

@ -1,3 +1,8 @@
Tue Nov 20 21:06:41 2012 Tadayoshi Funaba <tadf@dotrb.org>
* complex.c: some improvements.
* rational.c: ditto.
Tue Nov 20 21:01:16 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> Tue Nov 20 21:01:16 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (incs): BSD make cannot deal with non-prefixed dependency * common.mk (incs): BSD make cannot deal with non-prefixed dependency

View File

@ -1498,13 +1498,19 @@ numeric_to_c(VALUE self)
#include <ctype.h> #include <ctype.h>
inline static int
issign(int c)
{
return (c == '-' || c == '+');
}
static int static int
read_sign(const char **s, read_sign(const char **s,
char **b) char **b)
{ {
int sign = '?'; int sign = '?';
if (**s == '-' || **s == '+') { if (issign(**s)) {
sign = **b = **s; sign = **b = **s;
(*s)++; (*s)++;
(*b)++; (*b)++;
@ -1512,16 +1518,22 @@ read_sign(const char **s,
return sign; return sign;
} }
inline static int
isdecimal(int c)
{
return isdigit((unsigned char)c);
}
static int static int
read_digits(const char **s, int strict, read_digits(const char **s, int strict,
char **b) char **b)
{ {
int us = 1; int us = 1;
if (!isdigit((unsigned char)**s)) if (!isdecimal(**s))
return 0; return 0;
while (isdigit((unsigned char)**s) || **s == '_') { while (isdecimal(**s) || **s == '_') {
if (**s == '_') { if (**s == '_') {
if (strict) { if (strict) {
if (us) if (us)
@ -1543,6 +1555,12 @@ read_digits(const char **s, int strict,
return 1; return 1;
} }
inline static int
islettere(int c)
{
return (c == 'e' || c == 'E');
}
static int static int
read_num(const char **s, int strict, read_num(const char **s, int strict,
char **b) char **b)
@ -1562,7 +1580,7 @@ read_num(const char **s, int strict,
} }
} }
if (**s == 'e' || **s == 'E') { if (islettere(**s)) {
**b = **s; **b = **s;
(*s)++; (*s)++;
(*b)++; (*b)++;
@ -1575,7 +1593,7 @@ read_num(const char **s, int strict,
return 1; return 1;
} }
static int inline static int
read_den(const char **s, int strict, read_den(const char **s, int strict,
char **b) char **b)
{ {
@ -1612,7 +1630,7 @@ read_rat(const char **s, int strict,
return 1; return 1;
} }
static int inline static int
isimagunit(int c) isimagunit(int c)
{ {
return (c == 'i' || c == 'I' || return (c == 'i' || c == 'I' ||
@ -1654,7 +1672,7 @@ read_comp(const char **s, int strict,
**b = '\0'; **b = '\0';
num = str2num(bb); num = str2num(bb);
*ret = rb_complex_new2(num, ZERO); *ret = rb_complex_new2(num, ZERO);
return 0; /* e.g. "1/" */ return 0; /* e.g. "-" */
} }
**b = '\0'; **b = '\0';
num = str2num(bb); num = str2num(bb);
@ -1673,9 +1691,9 @@ read_comp(const char **s, int strict,
st = read_rat(s, strict, b); st = read_rat(s, strict, b);
**b = '\0'; **b = '\0';
if (strlen(bb) < 1 || if (strlen(bb) < 1 ||
!isdigit((unsigned char)*(bb + strlen(bb) - 1))) { !isdecimal(*(bb + strlen(bb) - 1))) {
*ret = rb_complex_new2(num, ZERO); *ret = rb_complex_new2(num, ZERO);
return 0; /* e.g. "1@x" */ return 0; /* e.g. "1@-" */
} }
num2 = str2num(bb); num2 = str2num(bb);
*ret = rb_complex_polar(num, num2); *ret = rb_complex_polar(num, num2);
@ -1685,7 +1703,7 @@ read_comp(const char **s, int strict,
return 1; /* e.g. "1@2" */ return 1; /* e.g. "1@2" */
} }
if (**s == '-' || **s == '+') { if (issign(**s)) {
bb = *b; bb = *b;
sign = read_sign(s, b); sign = read_sign(s, b);
if (isimagunit(**s)) if (isimagunit(**s))
@ -1713,7 +1731,7 @@ read_comp(const char **s, int strict,
} }
} }
static void inline static void
skip_ws(const char **s) skip_ws(const char **s)
{ {
while (isspace((unsigned char)**s)) while (isspace((unsigned char)**s))

View File

@ -1958,40 +1958,53 @@ float_rationalize(int argc, VALUE *argv, VALUE self)
#include <ctype.h> #include <ctype.h>
inline static int
issign(int c)
{
return (c == '-' || c == '+');
}
static int static int
read_sign(const char **s) read_sign(const char **s)
{ {
int sign = '?'; int sign = '?';
if (**s == '-' || **s == '+') { if (issign(**s)) {
sign = **s; sign = **s;
(*s)++; (*s)++;
} }
return sign; return sign;
} }
inline static int
isdecimal(int c)
{
return isdigit((unsigned char)c);
}
static int static int
read_digits(const char **s, int strict, read_digits(const char **s, int strict,
VALUE *num, int *count) 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; return 0;
}
*num = ZERO; while (isdecimal(**s) || **s == '_') {
while (isdigit((unsigned char)**s) || **s == '_') {
if (**s == '_') { if (**s == '_') {
if (strict) { if (strict) {
if (us) if (us) {
return 0; ret = 0;
goto conv;
}
} }
us = 1; us = 1;
} }
else { else {
*num = f_mul(*num, INT2FIX(10));
*num = f_add(*num, INT2FIX(**s - '0'));
if (count) if (count)
(*count)++; (*count)++;
us = 0; us = 0;
@ -2002,7 +2015,15 @@ read_digits(const char **s, int strict,
do { do {
(*s)--; (*s)--;
} while (**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 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; int expsign;
(*s)++; (*s)++;
@ -2054,7 +2075,7 @@ read_num(const char **s, int numsign, int strict,
return 1; return 1;
} }
static int inline static int
read_den(const char **s, int strict, read_den(const char **s, int strict,
VALUE *num) VALUE *num)
{ {
@ -2093,7 +2114,7 @@ read_rat(const char **s, int strict,
return 1; return 1;
} }
static void inline static void
skip_ws(const char **s) skip_ws(const char **s)
{ {
while (isspace((unsigned char)**s)) while (isspace((unsigned char)**s))