summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-02-24 02:20:45 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-02-24 02:20:45 +0000
commit53ec85b5b4de333f564fe50dae970a4983daaa64 (patch)
tree66c9d6aaad2a7cf6da6851be5194eca82866bed1 /numeric.c
parent7c14876b2fbbd61bda95e42dea82c954fa9d0182 (diff)
numeric.c: micro optimizations
* numeric.c (flo_to_s, rb_fix2str): use rb_usascii_str_new instead of rb_usascii_str_new_cstr, when the length can be calculated. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53910 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/numeric.c b/numeric.c
index b88ce44674..607a72d756 100644
--- a/numeric.c
+++ b/numeric.c
@@ -733,8 +733,11 @@ flo_to_s(VALUE flt)
char *p, *e;
int sign, decpt, digs;
- if (isinf(value))
- return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
+ if (isinf(value)) {
+ static const char minf[] = "-Infinity";
+ const int pos = (value > 0); /* skip "-" */
+ return rb_usascii_str_new(minf+pos, strlen(minf)-pos);
+ }
else if (isnan(value))
return rb_usascii_str_new2("NaN");
@@ -2863,7 +2866,7 @@ fix_uminus(VALUE num)
VALUE
rb_fix2str(VALUE x, int base)
{
- char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf;
+ char buf[SIZEOF_VALUE*CHAR_BIT + 1], *const e = buf + sizeof buf, *b = e;
long val = FIX2LONG(x);
int neg = 0;
@@ -2877,7 +2880,6 @@ rb_fix2str(VALUE x, int base)
val = -val;
neg = 1;
}
- *--b = '\0';
do {
*--b = ruby_digitmap[(int)(val % base)];
} while (val /= base);
@@ -2885,7 +2887,7 @@ rb_fix2str(VALUE x, int base)
*--b = '-';
}
- return rb_usascii_str_new2(b);
+ return rb_usascii_str_new(b, e - b);
}
/*