diff options
Diffstat (limited to 'ext/-test-/printf/printf.c')
| -rw-r--r-- | ext/-test-/printf/printf.c | 86 |
1 files changed, 54 insertions, 32 deletions
diff --git a/ext/-test-/printf/printf.c b/ext/-test-/printf/printf.c index e793bb7a48..88625dc148 100644 --- a/ext/-test-/printf/printf.c +++ b/ext/-test-/printf/printf.c @@ -19,13 +19,19 @@ printf_test_q(VALUE self, VALUE obj) return rb_enc_sprintf(rb_usascii_encoding(), "[% "PRIsVALUE"]", obj); } +static VALUE +printf_test_value(VALUE self, VALUE obj) +{ + return rb_enc_sprintf(rb_usascii_encoding(), "%"PRIsVALUE, obj); +} + static char * uint_to_str(char *p, char *e, unsigned int x) { char *e0 = e; if (e <= p) return p; do { - *--e = x % 10 + '0'; + *--e = x % 10 + '0'; } while ((x /= 10) != 0 && e > p); memmove(p, e, e0 - e); return p + (e0 - e); @@ -44,53 +50,66 @@ printf_test_call(int argc, VALUE *argv, VALUE self) if (RSTRING_LEN(type) != 1) rb_raise(rb_eArgError, "wrong length(%ld)", RSTRING_LEN(type)); switch (cnv = RSTRING_PTR(type)[0]) { case 'd': case 'x': case 'o': case 'X': - n = NUM2INT(num); - break; + n = NUM2INT(num); + break; case 's': - s = StringValueCStr(num); - break; + s = StringValueCStr(num); + break; default: rb_raise(rb_eArgError, "wrong conversion(%c)", cnv); } *p++ = '%'; if (!NIL_P(opt)) { - VALUE v; - Check_Type(opt, T_HASH); - if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("space"))))) { - *p++ = ' '; - } - if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("hash"))))) { - *p++ = '#'; - } - if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("plus"))))) { - *p++ = '+'; - } - if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("minus"))))) { - *p++ = '-'; - } - if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("zero"))))) { - *p++ = '0'; - } - if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("width"))))) { - p = uint_to_str(p, format + sizeof(format), NUM2UINT(v)); - } - if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("prec"))))) { - *p++ = '.'; - if (FIXNUM_P(v)) - p = uint_to_str(p, format + sizeof(format), NUM2UINT(v)); - } + VALUE v; + Check_Type(opt, T_HASH); + if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("space"))))) { + *p++ = ' '; + } + if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("hash"))))) { + *p++ = '#'; + } + if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("plus"))))) { + *p++ = '+'; + } + if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("minus"))))) { + *p++ = '-'; + } + if (RTEST(rb_hash_aref(opt, ID2SYM(rb_intern("zero"))))) { + *p++ = '0'; + } + if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("width"))))) { + p = uint_to_str(p, format + sizeof(format), NUM2UINT(v)); + } + if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("prec"))))) { + *p++ = '.'; + if (FIXNUM_P(v)) + p = uint_to_str(p, format + sizeof(format), NUM2UINT(v)); + } } *p++ = cnv; *p++ = '\0'; if (cnv == 's') { - result = rb_enc_sprintf(rb_usascii_encoding(), format, s); + result = rb_enc_sprintf(rb_usascii_encoding(), format, s); } else { - result = rb_enc_sprintf(rb_usascii_encoding(), format, n); + result = rb_enc_sprintf(rb_usascii_encoding(), format, n); } return rb_assoc_new(result, rb_usascii_str_new_cstr(format)); } static VALUE +printf_test_enc_sprintf(VALUE self, VALUE enc) +{ + return rb_enc_sprintf(rb_to_encoding(enc), "%s", "x"); +} + +static VALUE +printf_test_catf(VALUE self, VALUE str) +{ + StringValue(str); + return rb_str_catf(str, "%s", "x"); +} + +static VALUE snprintf_count(VALUE self, VALUE str) { int n = ruby_snprintf(NULL, 0, "%s", StringValueCStr(str)); @@ -104,6 +123,9 @@ Init_printf(void) rb_define_singleton_method(m, "s", printf_test_s, 1); rb_define_singleton_method(m, "v", printf_test_v, 1); rb_define_singleton_method(m, "q", printf_test_q, 1); + rb_define_singleton_method(m, "value", printf_test_value, 1); rb_define_singleton_method(m, "call", printf_test_call, -1); + rb_define_singleton_method(m, "enc_sprintf", printf_test_enc_sprintf, 1); + rb_define_singleton_method(m, "catf", printf_test_catf, 1); rb_define_singleton_method(m, "sncount", snprintf_count, 1); } |
