summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-02-01 10:23:22 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-02-01 10:23:22 +0000
commitc2269d5b4f60829a523f3479492267ef99e1c5fb (patch)
tree1b0a0b035dbca831b168c86d833de524b5cf7848 /bignum.c
parentd9b49e39b2f3380cb6c4bb68a175a230c4702b58 (diff)
* intern.h: prototypes for new functions; rb_cstr_to_inum(),
rb_str_to_inum(), rb_cstr_to_dbl(), rb_str_to_dbl() * bignum.c (rb_cstr_to_inum): changed from rb_cstr2inum(), and added argument badcheck to be consistent with parser. [new] * bignum.c (rb_str_to_inum): ditto. * bignum.c (rb_cstr2inum): wapper of rb_cstr_to_inum() now. * bignum.c (rb_str2inum): ditto. * object.c (rb_cstr_to_dbl): float number parser. [new] * object.c (rb_str_to_dbl): ditto. * object.c (rb_Float): use rb_cstr_to_dbl() for strict check. * object.c (rb_Integer): use rb_str_to_inum() for strict check. * string.c (rb_str_to_f): use rb_str_to_dbl() with less check. * string.c (rb_str_to_i): use rb_str_to_inum() with less check. * string.c (rb_str_hex): ditto. * string.c (rb_str_oct): ditto. * sprintf.c (rb_f_sprintf): ditto. * time.c (obj2long): ditto. * parse.y (yylex): use rb_cstr_to_inum() for strict check. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c60
1 files changed, 47 insertions, 13 deletions
diff --git a/bignum.c b/bignum.c
index f4add5f5f4..173c8fb968 100644
--- a/bignum.c
+++ b/bignum.c
@@ -186,21 +186,26 @@ rb_int2inum(n)
}
VALUE
-rb_cstr2inum(str, base)
+rb_cstr_to_inum(str, base, badcheck)
const char *str;
int base;
+ int badcheck;
{
const char *s = str;
char *end;
- int badcheck = (base==0)?1:0;
- char sign = 1, c;
+ char sign = 1, c, nondigit = 0;
BDIGIT_DBL num;
long len, blen = 1;
long i;
VALUE z;
BDIGIT *zds;
- while (*str && ISSPACE(*str)) str++;
+ if (badcheck) {
+ while (ISSPACE(*str)) str++;
+ }
+ else {
+ while (ISSPACE(*str) || *str == '_') str++;
+ }
if (str[0] == '+') {
str++;
@@ -213,7 +218,7 @@ rb_cstr2inum(str, base)
if (badcheck) goto bad;
return INT2FIX(0);
}
- if (base == 0) {
+ if (base <= 0) {
if (str[0] == '0') {
if (str[1] == 'x' || str[1] == 'X') {
base = 16;
@@ -225,6 +230,9 @@ rb_cstr2inum(str, base)
base = 8;
}
}
+ else if (base < -1) {
+ base = -base;
+ }
else {
base = 10;
}
@@ -290,18 +298,27 @@ rb_cstr2inum(str, base)
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7':
c = c - '0';
+ nondigit = 0;
break;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
- if (base != 16) c = base;
- else c = c - 'a' + 10;
- break;
+ c -= 'a' - 'A';
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
- if (base != 16) c = base;
- else c = c - 'A' + 10;
+ if (base != 16) {
+ nondigit = c;
+ c = base;
+ }
+ else {
+ c = c - 'A' + 10;
+ nondigit = 0;
+ }
break;
case '_':
+ if (badcheck) {
+ if (nondigit) goto bad;
+ nondigit = c;
+ }
continue;
default:
c = base;
@@ -334,9 +351,10 @@ rb_cstr2inum(str, base)
}
VALUE
-rb_str2inum(str, base)
+rb_str_to_inum(str, base, badcheck)
VALUE str;
int base;
+ int badcheck;
{
char *s;
int len;
@@ -351,10 +369,26 @@ rb_str2inum(str, base)
p[len] = '\0';
s = p;
}
- if (base == 0 && len != strlen(s)) {
+ if (badcheck && len != strlen(s)) {
rb_raise(rb_eArgError, "string for Integer contains null byte");
}
- return rb_cstr2inum(s, base);
+ return rb_cstr_to_inum(s, base, badcheck);
+}
+
+VALUE
+rb_cstr2inum(str, base)
+ const char *str;
+ int base;
+{
+ return rb_cstr_to_inum(str, base, base==0);
+}
+
+VALUE
+rb_str2inum(str, base)
+ VALUE str;
+ int base;
+{
+ return rb_str_to_inum(str, base, base==0);
}
static char hexmap[] = "0123456789abcdef";