| 1 |
#include <ruby.h> |
| 2 |
#include <ruby/encoding.h> |
| 3 |
|
| 4 |
static VALUE |
| 5 |
printf_test_s(VALUE self, VALUE obj) |
| 6 |
{ |
| 7 |
return rb_enc_sprintf(rb_usascii_encoding(), "<%"PRIsVALUE">", obj); |
| 8 |
} |
| 9 |
|
| 10 |
static VALUE |
| 11 |
printf_test_v(VALUE self, VALUE obj) |
| 12 |
{ |
| 13 |
return rb_enc_sprintf(rb_usascii_encoding(), "{%+"PRIsVALUE"}", obj); |
| 14 |
} |
| 15 |
|
| 16 |
static VALUE |
| 17 |
printf_test_q(VALUE self, VALUE obj) |
| 18 |
{ |
| 19 |
return rb_enc_sprintf(rb_usascii_encoding(), "[% "PRIsVALUE"]", obj); |
| 20 |
} |
| 21 |
|
| 22 |
static char * |
| 23 |
uint_to_str(char *p, char *e, unsigned int x) |
| 24 |
{ |
| 25 |
char *e0 = e; |
| 26 |
if (e <= p) return p; |
| 27 |
do { |
| 28 |
*--e = x % 10 + '0'; |
| 29 |
} while ((x /= 10) != 0 && e > p); |
| 30 |
memmove(p, e, e0 - e); |
| 31 |
return p + (e0 - e); |
| 32 |
} |
| 33 |
|
| 34 |
static VALUE |
| 35 |
printf_test_call(int argc, VALUE *argv, VALUE self) |
| 36 |
{ |
| 37 |
VALUE opt, type, num, result; |
| 38 |
char format[sizeof(int) * 6 + 8], *p = format, cnv; |
| 39 |
int n = 0; |
| 40 |
const char *s = 0; |
| 41 |
|
| 42 |
rb_scan_args(argc, argv, "2:", &type, &num, &opt); |
| 43 |
Check_Type(type, T_STRING); |
| 44 |
if (RSTRING_LEN(type) != 1) rb_raise(rb_eArgError, "wrong length(%ld)", RSTRING_LEN(type)); |
| 45 |
switch (cnv = RSTRING_PTR(type)[0]) { |
| 46 |
case 'd': case 'x': case 'o': case 'X': |
| 47 |
n = NUM2INT(num); |
| 48 |
break; |
| 49 |
case 's': |
| 50 |
s = StringValueCStr(num); |
| 51 |
break; |
| 52 |
default: rb_raise(rb_eArgError, "wrong conversion(%c)", cnv); |
| 53 |
} |
| 54 |
*p++ = '%'; |
| 55 |
if (!NIL_P(opt)) { |
| 56 |
VALUE v; |
| 57 |
Check_Type(opt, T_HASH); |
| 58 |
if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("space"))))) { |
| 59 |
*p++ = ' '; |
| 60 |
} |
| 61 |
if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("hash"))))) { |
| 62 |
*p++ = '#'; |
| 63 |
} |
| 64 |
if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("plus"))))) { |
| 65 |
*p++ = '+'; |
| 66 |
} |
| 67 |
if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("minus"))))) { |
| 68 |
*p++ = '-'; |
| 69 |
} |
| 70 |
if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("zero"))))) { |
| 71 |
*p++ = '0'; |
| 72 |
} |
| 73 |
if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("width"))))) { |
| 74 |
p = uint_to_str(p, format + sizeof(format), NUM2UINT(v)); |
| 75 |
} |
| 76 |
if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("prec"))))) { |
| 77 |
*p++ = '.'; |
| 78 |
if (FIXNUM_P(v)) |
| 79 |
p = uint_to_str(p, format + sizeof(format), NUM2UINT(v)); |
| 80 |
} |
| 81 |
} |
| 82 |
*p++ = cnv; |
| 83 |
*p++ = '\0'; |
| 84 |
if (cnv == 's') { |
| 85 |
result = rb_enc_sprintf(rb_usascii_encoding(), format, s); |
| 86 |
} |
| 87 |
else { |
| 88 |
result = rb_enc_sprintf(rb_usascii_encoding(), format, n); |
| 89 |
} |
| 90 |
return rb_assoc_new(result, rb_usascii_str_new_cstr(format)); |
| 91 |
} |
| 92 |
|
| 93 |
static VALUE |
| 94 |
snprintf_count(VALUE self, VALUE str) |
| 95 |
{ |
| 96 |
int n = ruby_snprintf(NULL, 0, "%s", StringValueCStr(str)); |
| 97 |
return INT2FIX(n); |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
Init_printf(void) |
| 102 |
{ |
| 103 |
VALUE m = rb_define_module_under(rb_define_module("Bug"), "Printf"); |
| 104 |
rb_define_singleton_method(m, "s", printf_test_s, 1); |
| 105 |
rb_define_singleton_method(m, "v", printf_test_v, 1); |
| 106 |
rb_define_singleton_method(m, "q", printf_test_q, 1); |
| 107 |
rb_define_singleton_method(m, "call", printf_test_call, -1); |
| 108 |
rb_define_singleton_method(m, "sncount", snprintf_count, 1); |
| 109 |
} |