chd114 发表于 2014-7-4 16:27
不明觉厉···
VALUE rb_fix2str(VALUE x, int base) { extern const char ruby_digitmap[]; char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf; long val = FIX2LONG(x); int neg = 0; if (base < 2 || 36 < base) { rb_raise(rb_eArgError, "invalid radix %d", base); } if (val == 0) { return rb_usascii_str_new2("0"); } if (val < 0) { val = -val; neg = 1; } *--b = '\0'; do { *--b = ruby_digitmap[(int)(val % base)]; } while (val /= base); if (neg) { *--b = '-'; } return rb_usascii_str_new2(b); }
VALUE
rb_fix2str(VALUE x, int base)
{
extern const char ruby_digitmap[];
char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf;
long val = FIX2LONG(x);
int neg = 0;
if (base < 2 || 36 < base) {
rb_raise(rb_eArgError, "invalid radix %d", base);
}
if (val == 0) {
return rb_usascii_str_new2("0");
}
if (val < 0) {
val = -val;
neg = 1;
}
*--b = '\0';
do {
*--b = ruby_digitmap[(int)(val % base)];
} while (val /= base);
if (neg) {
*--b = '-';
}
return rb_usascii_str_new2(b);
}
源码的主要部分在此,自己欣赏吧 |