From: nobu.nakada@... Date: 2002-02-01T17:40:36+09:00 Subject: [ruby-dev:15834] Re: 文字列→整数変換 なかだです。 At Fri, 25 Jan 2002 17:35:02 +0900, Nobuyoshi-Nakada wrote: > * eval/parser, Integer(), Float() > 各数字間に高々一個の'_'が許される。符号の後、'.','e'の前後(浮 > 動少数点数の場合)には許されない。 > > * String#to_f > Float()と同じだが、例外を発生させるかわりに0.0を返す。 これのパッチです。 Index: intern.h =================================================================== RCS file: /cvs/ruby/src/ruby/intern.h,v retrieving revision 1.77 diff -u -2 -p -u -r1.77 intern.h --- intern.h 2002/01/11 09:18:50 1.77 +++ intern.h 2002/02/01 05:36:44 @@ -264,4 +264,6 @@ VALUE rb_Float _((VALUE)); VALUE rb_String _((VALUE)); VALUE rb_Array _((VALUE)); +double rb_cstr_to_dbl _((const char *, int)); +double rb_str_to_dbl _((VALUE, int)); /* parse.y */ EXTERN int ruby_sourceline; Index: object.c =================================================================== RCS file: /cvs/ruby/src/ruby/object.c,v retrieving revision 1.71 diff -u -2 -p -u -r1.71 object.c --- object.c 2002/02/01 06:02:59 1.71 +++ object.c 2002/02/01 08:01:58 @@ -964,4 +964,88 @@ rb_f_integer(obj, arg) } +double +rb_cstr_to_dbl(p, badcheck) + const char *p; + int badcheck; +{ + const char *q; + char *end; + double d; + + q = p; + if (badcheck) { + while (ISSPACE(*p)) p++; + } + else { + while (ISSPACE(*p) || *p == '_') p++; + } + d = strtod(p, &end); + if (p == end) { + if (badcheck) { + bad: + rb_invalid_str(q, "Float()"); + } + return d; + } + if (*end) { + char *buf = ALLOCA_N(char, strlen(p)); + char *n = buf; + + while (p < end) *n++ = *p++; + while (*p) { + if (*p == '_') { + /* remove underscores between digits */ + if (badcheck) { + if (n == buf || !ISDIGIT(n[-1])) goto bad; + ++p; + if (!ISDIGIT(*p)) goto bad; + } + else { + while (*++p == '_'); + continue; + } + } + *n++ = *p++; + } + *n = '\0'; + p = buf; + d = strtod(p, &end); + if (badcheck) { + if (p == end) goto bad; + while (*end && ISSPACE(*end)) end++; + if (*end) goto bad; + } + } + if (errno == ERANGE) { + errno = 0; + rb_raise(rb_eArgError, "Float %s out of range", q); + } + return d; +} + +double +rb_str_to_dbl(str, badcheck) + VALUE str; + int badcheck; +{ + char *s; + int len; + + StringValue(str); + s = RSTRING(str)->ptr; + len = RSTRING(str)->len; + if (s[len]) { /* no sentinel somehow */ + char *p = ALLOCA_N(char, len+1); + + MEMCPY(p, s, char, len); + p[len] = '\0'; + s = p; + } + if (badcheck && len != strlen(s)) { + rb_raise(rb_eArgError, "string for Float contains null byte"); + } + return rb_cstr_to_dbl(s, badcheck); +} + VALUE rb_Float(val) @@ -979,51 +1063,5 @@ rb_Float(val) case T_STRING: - { - char *q, *p, *end; - double d; - - q = p = StringValuePtr(val); - while (*p && ISSPACE(*p)) p++; - d = strtod(p, &end); - if (p == end) { - bad: - rb_invalid_str(q, "Float()"); - } - if (*end) { - if (*end == '_') { - char *buf = ALLOCA_N(char, strlen(p)); - char *n = buf, *last = p; - - while (p < end) *n++ = *p++; - while (*p) { - if (*p == '_' && (n > buf && ISDIGIT(n[-1]))) { - /* remove underscores between digits */ - last = ++p; - while (*p == '_') ++p; - if (!ISDIGIT(*p)) { - while (last < p) *n++ = *last++; - continue; - } - last = p; - } - *n++ = *p++; - } - while (*last && (*last == '_' || ISSPACE(*last))) - last++; - if (!*last) goto bad; - *n = '\0'; - p = buf; - d = strtod(p, &end); - if (p == end) goto bad; - } - while (*end && ISSPACE(*end)) end++; - if (*end) goto bad; - } - if (errno == ERANGE) { - errno = 0; - rb_raise(rb_eArgError, "Float %s out of range", p); - } - return rb_float_new(d); - } + return rb_float_new(rb_str_to_dbl(val, Qtrue)); case T_NIL: Index: string.c =================================================================== RCS file: /cvs/ruby/src/ruby/string.c,v retrieving revision 1.96 diff -u -2 -p -u -r1.96 string.c --- string.c 2002/01/28 08:44:37 1.96 +++ string.c 2002/02/01 07:48:11 @@ -1774,7 +1774,5 @@ rb_str_to_f(str) VALUE str; { - double f = strtod(RSTRING(str)->ptr, 0); - - return rb_float_new(f); + return rb_float_new(rb_str_to_dbl(str, Qfalse)); } -- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦