Created
January 27, 2011 20:45
-
-
Save pedromenezes/799227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment