ext/oj/compat.c in oj-3.11.8 vs ext/oj/compat.c in oj-3.12.0

- old
+ new

@@ -21,18 +21,30 @@ *pi->options.create_id == *key && (int)pi->options.create_id_len == klen && 0 == strncmp(pi->options.create_id, key, klen)) { parent->classname = oj_strndup(str, len); parent->clen = len; } else { - volatile VALUE rstr = rb_str_new(str, len); + volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str); if (Qundef == rkey) { - rkey = rb_str_new(key, klen); - rstr = oj_encode(rstr); - rkey = oj_encode(rkey); + VALUE *slot; + if (Yes == pi->options.sym_key) { - rkey = rb_str_intern(rkey); + if (Qnil == (rkey = oj_sym_hash_get(key, klen, &slot))) { + rkey = rb_str_new(key, klen); + rkey = oj_encode(rkey); + rkey = rb_str_intern(rkey); + *slot = rkey; + rb_gc_register_address(slot); + } + } else { + if (Qnil == (rkey = oj_str_hash_get(key, klen, &slot))) { + rkey = rb_str_new(key, klen); + rkey = oj_encode(rkey); + *slot = rkey; + rb_gc_register_address(slot); + } } } if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) { VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len); @@ -91,27 +103,13 @@ if (Yes == pi->options.trace) { oj_trace_parse_hash_end(pi, __FILE__, __LINE__); } } -static VALUE calc_hash_key(ParseInfo pi, Val parent) { - volatile VALUE rkey = parent->key_val; - - if (Qundef == rkey) { - rkey = rb_str_new(parent->key, parent->klen); - } - rkey = oj_encode(rkey); - if (Yes == pi->options.sym_key) { - rkey = rb_str_intern(rkey); - } - return rkey; -} - static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) { - volatile VALUE rstr = rb_str_new(str, len); + volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str); - rstr = oj_encode(rstr); if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) { VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len); if (Qnil != clas) { pi->stack.head->val = rb_funcall(clas, oj_json_create_id, 1, rstr); @@ -140,14 +138,14 @@ // values. Even using the store method to set the values will fail // the unit tests. rb_funcall(stack_peek(&pi->stack)->val, rb_intern("[]="), 2, - calc_hash_key(pi, parent), + oj_calc_hash_key(pi, parent), rval); } else { - rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rval); + rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rval); } if (Yes == pi->options.trace) { oj_trace_parse_call("set_number", pi, __FILE__, __LINE__, rval); } } @@ -159,14 +157,14 @@ // values. Even using the store method to set the values will fail // the unit tests. rb_funcall(stack_peek(&pi->stack)->val, rb_intern("[]="), 2, - calc_hash_key(pi, parent), + oj_calc_hash_key(pi, parent), value); } else { - rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), value); + rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value); } if (Yes == pi->options.trace) { oj_trace_parse_call("set_value", pi, __FILE__, __LINE__, value); } } @@ -197,12 +195,11 @@ oj_trace_parse_call("append_number", pi, __FILE__, __LINE__, rval); } } static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) { - volatile VALUE rstr = rb_str_new(str, len); + volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str); - rstr = oj_encode(rstr); if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) { VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len); if (Qnil != clas) { rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));