summaryrefslogtreecommitdiff
path: root/ext/-test-/printf/printf.c
blob: fd60b0f5932f4616259f1dfcbad915bcad462a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <ruby.h>
#include <ruby/encoding.h>

static VALUE
printf_test_i(VALUE self, VALUE obj)
{
    char buf[256];
    snprintf(buf, sizeof(buf), "<%"PRIsVALUE">", obj);
    return rb_usascii_str_new2(buf);
}

static VALUE
printf_test_s(VALUE self, VALUE obj)
{
    return rb_enc_sprintf(rb_usascii_encoding(), "<%"PRIsVALUE">", obj);
}

static VALUE
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;
    char format[sizeof(int) * 6 + 8], *p = format, cnv;
    int n;

    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': break;
      default: rb_raise(rb_eArgError, "wrong conversion(%c)", cnv);
    }
    n = NUM2INT(num);
    *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';
    return rb_assoc_new(rb_enc_sprintf(rb_usascii_encoding(), format, n),
			rb_usascii_str_new_cstr(format));
}

void
Init_printf(void)
{
    VALUE m = rb_define_module_under(rb_define_module("Bug"), "Printf");
    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, "q", printf_test_q, 1);
    rb_define_singleton_method(m, "call", printf_test_call, -1);
}