diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2000-12-25 06:29:27 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2000-12-25 06:29:27 +0000 |
commit | 99020d6e50702eb371111d73280eb80b4b29ba5b (patch) | |
tree | f2ec985d66f5d99a203277cf0961d960228d441e /object.c | |
parent | 8f6673c2d4a7e6ff470b584141c590e4b066f5e2 (diff) |
001225
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1075 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'object.c')
-rw-r--r-- | object.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -15,6 +15,7 @@ #include "ruby.h" #include "st.h" #include <stdio.h> +#include <errno.h> VALUE rb_mKernel; VALUE rb_cObject; @@ -946,6 +947,48 @@ rb_Float(val) case T_BIGNUM: return rb_float_new(rb_big2dbl(val)); + case T_STRING: + { + char *q, *p, *end; + double d; + + q = p = STR2CSTR(val); + while (*p && ISSPACE(*p)) p++; + again: + d = strtod(p, &end); + if (p == end) { + bad: + rb_raise(rb_eArgError, "invalid value for Float: \"%s\"", q); + } + if (*end) { + if (*end == '_') { + char *buf = ALLOCA_N(char, strlen(p)); + char *n = buf, *last; + + while (*p) { + if (*p == '_') { + last = ++p; + continue; + } + *n++ = *p++; + } + while (*last && (*last == '_' || ISSPACE(*last))) + last++; + if (!*last) goto bad; + *n = '\0'; + p = buf; + goto again; + } + 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); + } + case T_NIL: return rb_float_new(0.0); |