summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-04-14 06:54:27 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-04-14 06:54:27 +0000
commit3f4472433d09fbeedaa9c41c7d76eb1299e31638 (patch)
treea4ad2e83d82088c8cd076830e3b25031d6ed061b /numeric.c
parentd39d90f86ae5b61530dd76689921f31505290272 (diff)
downloadruby-3f4472433d09fbeedaa9c41c7d76eb1299e31638.tar.gz
* bignum.c (rb_cstr_to_inum, rb_big2str): allow 2-36 as radix.
* numeric.c (rb_fix2str): ditto. * string.c (rb_str_to_i): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3677 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/numeric.c b/numeric.c
index 3e804bc56f..bd8be06d3a 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1178,21 +1178,30 @@ rb_fix2str(x, base)
VALUE x;
int base;
{
- char fmt[4], buf[22], *b = buf;
+ extern const char ruby_digitmap[];
+ char buf[SIZEOF_LONG*CHAR_BIT/2 + 2], *b = buf + sizeof buf;
long val = FIX2LONG(x);
+ int neg = 0;
- fmt[0] = '%'; fmt[1] = 'l'; fmt[3] = '\0';
- if (base == 10) fmt[2] = 'd';
- else if (base == 16) fmt[2] = 'x';
- else if (base == 8) fmt[2] = 'o';
- else rb_raise(rb_eArgError, "illegal radix %d", base);
+ if (base < 2 || 36 < base) {
+ rb_raise(rb_eArgError, "illegal radix %d", base);
+ }
+ if (val == 0) {
+ return rb_str_new2("0");
+ }
if (val < 0) {
val = -val;
- *b++ = '-';
+ neg = 1;
+ }
+ *--b = '\0';
+ do {
+ *--b = ruby_digitmap[(int)(val % base)];
+ } while (val /= base);
+ if (neg) {
+ *--b = '-';
}
- sprintf(b, fmt, val);
- return rb_str_new2(buf);
+ return rb_str_new2(b);
}
static VALUE