eval/decode: Omit calling convert_setup for each string

Uses the same trick eval/encode does.
This commit is contained in:
ZyX
2017-04-08 17:36:02 +03:00
parent b3587a456b
commit 6006cb74ef

View File

@@ -606,6 +606,17 @@ parse_json_number_ret:
} \ } \
} while (0) } while (0)
/// Last used p_enc value
///
/// Generic pointer: it is not used as a string, only pointer comparisons are
/// performed. Must not be freed.
static const void *last_p_enc = NULL;
/// Conversion setup for converting from UTF-8 to last_p_enc
static vimconv_T p_enc_conv = {
.vc_type = CONV_NONE,
};
/// Convert JSON string into VimL object /// Convert JSON string into VimL object
/// ///
/// @param[in] buf String to convert. UTF-8 encoding is assumed. /// @param[in] buf String to convert. UTF-8 encoding is assumed.
@@ -626,9 +637,12 @@ int json_decode_string(const char *const buf, const size_t buf_len,
EMSG(_("E474: Attempt to decode a blank string")); EMSG(_("E474: Attempt to decode a blank string"));
return FAIL; return FAIL;
} }
vimconv_T conv = { .vc_type = CONV_NONE }; if (last_p_enc != (const void *)p_enc) {
convert_setup(&conv, (char_u *) "utf-8", p_enc); p_enc_conv.vc_type = CONV_NONE;
conv.vc_fail = true; convert_setup(&p_enc_conv, (char_u *)"utf-8", p_enc);
p_enc_conv.vc_fail = true;
last_p_enc = p_enc;
}
int ret = OK; int ret = OK;
ValuesStack stack = KV_INITIAL_VALUE; ValuesStack stack = KV_INITIAL_VALUE;
ContainerStack container_stack = KV_INITIAL_VALUE; ContainerStack container_stack = KV_INITIAL_VALUE;
@@ -774,9 +788,9 @@ json_decode_string_cycle_start:
break; break;
} }
case '"': { case '"': {
if (parse_json_string(&conv, buf, buf_len, &p, &stack, &container_stack, if (parse_json_string(
&next_map_special, &didcomma, &didcolon) &p_enc_conv, buf, buf_len, &p, &stack, &container_stack,
== FAIL) { &next_map_special, &didcomma, &didcolon) == FAIL) {
// Error message was already given // Error message was already given
goto json_decode_string_fail; goto json_decode_string_fail;
} }
@@ -892,7 +906,6 @@ json_decode_string_fail:
tv_clear(&(kv_pop(stack).val)); tv_clear(&(kv_pop(stack).val));
} }
json_decode_string_ret: json_decode_string_ret:
convert_setup(&conv, NULL, NULL);
kv_destroy(stack); kv_destroy(stack);
kv_destroy(container_stack); kv_destroy(container_stack);
return ret; return ret;