summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-12-25 06:29:27 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-12-25 06:29:27 +0000
commit99020d6e50702eb371111d73280eb80b4b29ba5b (patch)
treef2ec985d66f5d99a203277cf0961d960228d441e /object.c
parent8f6673c2d4a7e6ff470b584141c590e4b066f5e2 (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.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/object.c b/object.c
index e7f202a..4e3d5dd 100644
--- a/object.c
+++ b/object.c
@@ -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);