summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-27 08:46:12 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-27 08:46:12 +0000
commit4e057b91576ce7be07c9f76ac9ab51b6e0b1fb15 (patch)
tree2e7d498c2d55b039ef608f044febd827bde6267c /ext
parent1c8500b3d1a327ff4e56af3c9ed9164130becc74 (diff)
merge revision(s) 46098: [Backport #9861]
* vsnprintf.c (BSD_vfprintf): fix string width when precision is given. as the result of `memchr` is NULL or its offset from the start cannot exceed the size, the comparison was always false. [ruby-core:62737] [Bug #9861] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@46582 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r--ext/-test-/printf/printf.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/ext/-test-/printf/printf.c b/ext/-test-/printf/printf.c
index 775eb33dc7..dac2dbbffe 100644
--- a/ext/-test-/printf/printf.c
+++ b/ext/-test-/printf/printf.c
@@ -21,6 +21,83 @@ printf_test_v(VALUE self, VALUE obj)
return rb_enc_sprintf(rb_usascii_encoding(), "{%+"PRIsVALUE"}", obj);
}
+static VALUE
+printf_test_q(VALUE self, VALUE obj)
+{
+ return rb_enc_sprintf(rb_usascii_encoding(), "[% "PRIsVALUE"]", obj);
+}
+
+static char *
+utoa(char *p, char *e, unsigned int x)
+{
+ char *e0 = e;
+ if (e <= p) return p;
+ do {
+ *--e = x % 10 + '0';
+ } while ((x /= 10) != 0 && e > p);
+ memmove(p, e, e0 - e);
+ return p + (e0 - e);
+}
+
+static VALUE
+printf_test_call(int argc, VALUE *argv, VALUE self)
+{
+ VALUE opt, type, num, result;
+ char format[sizeof(int) * 6 + 8], *p = format, cnv;
+ int n;
+ const char *s;
+
+ rb_scan_args(argc, argv, "2:", &type, &num, &opt);
+ Check_Type(type, T_STRING);
+ 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;
+ case 's':
+ 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 = utoa(p, format + sizeof(format), NUM2UINT(v));
+ }
+ if (!NIL_P(v = rb_hash_aref(opt, ID2SYM(rb_intern("prec"))))) {
+ *p++ = '.';
+ if (FIXNUM_P(v))
+ p = utoa(p, format + sizeof(format), NUM2UINT(v));
+ }
+ }
+ *p++ = cnv;
+ *p++ = '\0';
+ if (cnv == 's') {
+ result = rb_enc_sprintf(rb_usascii_encoding(), format, s);
+ }
+ else {
+ result = rb_enc_sprintf(rb_usascii_encoding(), format, n);
+ }
+ return rb_assoc_new(result, rb_usascii_str_new_cstr(format));
+}
+
void
Init_printf(void)
{
@@ -28,4 +105,5 @@ Init_printf(void)
rb_define_singleton_method(m, "i", printf_test_i, 1);
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, "call", printf_test_call, -1);
}