Skip to content

Commit

Permalink
ISO C90
Browse files Browse the repository at this point in the history
  • Loading branch information
eagletmt committed Feb 15, 2015
1 parent 080adc1 commit ea4287e
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions ext/attribute_builder/attribute_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ static ID id_keys, id_sort_bang, id_merge_bang, id_temple, id_utils, id_escape_h
static void
concat_array_attribute(VALUE attributes, VALUE hash, VALUE key)
{
VALUE v;

Check_Type(hash, T_HASH);
VALUE v = rb_hash_delete(hash, key);
v = rb_hash_delete(hash, key);
if (!NIL_P(v)) {
VALUE ary;

v = rb_Array(v);
VALUE ary = rb_hash_aref(attributes, key);
ary = rb_hash_aref(attributes, key);
Check_Type(ary, T_ARRAY);
rb_ary_concat(ary, v);
}
Expand Down Expand Up @@ -77,8 +81,10 @@ normalize_data_i(VALUE key, VALUE value, VALUE normalized)
static VALUE
normalize_data(VALUE data)
{
VALUE normalized;

Check_Type(data, T_HASH);
VALUE normalized = rb_hash_new();
normalized = rb_hash_new();
rb_hash_foreach(data, normalize_data_i, normalized);
return normalized;
}
Expand All @@ -94,12 +100,14 @@ normalize(VALUE hash)
const char *key_cstr = StringValueCStr(key);
VALUE value = rb_hash_lookup(hash, key);
if (RB_TYPE_P(value, T_HASH) && strcmp(key_cstr, "data") == 0) {
VALUE data, data_keys;
long data_len, j;

rb_hash_delete(hash, key);
VALUE data = normalize_data(value);
VALUE data_keys = rb_funcall(data, id_keys, 0);
data = normalize_data(value);
data_keys = rb_funcall(data, id_keys, 0);
rb_funcall(data_keys, id_sort_bang, 0);
const long data_len = RARRAY_LEN(data_keys);
long j;
data_len = RARRAY_LEN(data_keys);
for (j = 0; j < data_len; j++) {
VALUE data_key = RARRAY_AREF(data_keys, j);
VALUE k = rb_str_new_cstr("data-");
Expand All @@ -118,8 +126,10 @@ merge(VALUE attributes, int argc, VALUE *argv)
int i;

for (i = 0; i < argc; i++) {
VALUE h;

Check_Type(argv[i], T_HASH);
VALUE h = stringify_keys(argv[i]);
h = stringify_keys(argv[i]);
concat_array_attribute(attributes, h, rb_str_new_cstr("class"));
concat_array_attribute(attributes, h, rb_str_new_cstr("id"));
normalize(h);
Expand All @@ -130,12 +140,13 @@ merge(VALUE attributes, int argc, VALUE *argv)
static VALUE
put_attribute(VALUE attr_quote, VALUE key, VALUE value)
{
value = rb_funcall(value, id_to_s, 0);
VALUE utils_class, str;

VALUE utils_class = rb_const_get(rb_const_get(rb_cObject, id_temple), id_utils);
value = rb_funcall(value, id_to_s, 0);
utils_class = rb_const_get(rb_const_get(rb_cObject, id_temple), id_utils);
value = rb_funcall(utils_class, id_escape_html, 1, value);

VALUE str = rb_str_new_cstr(" ");
str = rb_str_new_cstr(" ");
rb_str_concat(str, key);
rb_str_concat(str, rb_str_new_cstr("="));
rb_str_concat(str, attr_quote);
Expand All @@ -149,8 +160,10 @@ build_attribute(VALUE attr_quote, VALUE key, VALUE value)
{
const char *key_cstr = StringValueCStr(key);
if (strcmp(key_cstr, "class") == 0) {
long len;

Check_Type(value, T_ARRAY);
const long len = RARRAY_LEN(value);
len = RARRAY_LEN(value);
if (len == 0) {
return rb_str_new_cstr("");
} else {
Expand All @@ -164,8 +177,10 @@ build_attribute(VALUE attr_quote, VALUE key, VALUE value)
return put_attribute(attr_quote, key, rb_ary_join(ary, rb_str_new_cstr(" ")));
}
} else if (strcmp(key_cstr, "id") == 0) {
long len = RARRAY_LEN(value);

Check_Type(value, T_ARRAY);
const long len = RARRAY_LEN(value);
len = RARRAY_LEN(value);
if (len == 0) {
return rb_str_new_cstr("");
} else {
Expand All @@ -190,20 +205,22 @@ build_attribute(VALUE attr_quote, VALUE key, VALUE value)
static VALUE
m_build(int argc, VALUE *argv, VALUE self)
{
VALUE attr_quote, attributes, keys, buf;
long len, i;

if (argc == 0) {
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1+)");
}
VALUE attr_quote = argv[0];
VALUE attributes = rb_hash_new();
attr_quote = argv[0];
attributes = rb_hash_new();
rb_hash_aset(attributes, rb_str_new_cstr("id"), rb_ary_new());
rb_hash_aset(attributes, rb_str_new_cstr("class"), rb_ary_new());
merge(attributes, argc-1, argv+1);

VALUE keys = rb_funcall(attributes, id_keys, 0);
keys = rb_funcall(attributes, id_keys, 0);
rb_funcall(keys, id_sort_bang, 0);
long len = RARRAY_LEN(keys);
VALUE buf = rb_ary_new_capa(len);
long i;
len = RARRAY_LEN(keys);
buf = rb_ary_new_capa(len);
for (i = 0; i < len; i++) {
VALUE k = RARRAY_AREF(keys, i);
rb_ary_push(buf, build_attribute(attr_quote, k, rb_hash_aref(attributes, k)));
Expand Down

0 comments on commit ea4287e

Please sign in to comment.