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
|
# File 'ext/split_pgdump/native_compute_name.c', line 55
static VALUE
spgd_compute_name(VALUE self, VALUE split_rule, VALUE values)
{
VALUE res = 0;
int encoding = -1;
char *result = (char*) xmalloc(INITIAL_CAPA);
int pos = 0, capa = INITIAL_CAPA;
long i, rule_len = RARRAY_LEN(split_rule);
if (!result) {
rb_memerror();
}
for (i = 0; i < rule_len; i++) {
VALUE rule = rb_ary_entry(split_rule, i);
if (rb_class_of(rule) == rb_cArray) {
long fieldnum = NUM2LONG(rb_ary_entry(rule, 0));
VALUE actions = rb_ary_entry(rule, 1);
rule = rb_ary_entry(values, fieldnum);
encoding = ENCODING_GET(rule);
if (RTEST(actions) && RARRAY_LEN(actions)) {
rule = apply_actions(rule, actions);
}
}
if (rb_class_of(rule) == rb_cString) {
long size = RSTRING_LEN(rule);
if (capa < pos + size + 1) {
char *tmp;
capa = pos + size + 1;
if (i + 1 != rule_len) capa = (capa * 3) >> 1;
tmp = (char*) xrealloc(result, capa);
if (!tmp) {
xfree(result);
rb_memerror();
}
result = tmp;
}
if (encoding == -1) encoding = ENCODING_GET(rule);
strncpy(result + pos, RSTRING_PTR(rule), size + 1);
pos += size;
}
}
res = rb_str_new(result, pos);
ENCODING_SET(res, encoding);
ENC_CODERANGE_CLEAR(res);
xfree(result);
return res;
}
|