From: Eric Haszlakiewicz Date: Sun, 2 Aug 2020 02:23:01 +0000 (+0000) Subject: Take a hint from PR #464 and use json_object_new_string_len() to avoid a needless... X-Git-Tag: json-c-0.16-20220414~41 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bfec9c8685de475e109e6ff9a82bbf9f0ba87caf;p=thirdparty%2Fjson-c.git Take a hint from PR #464 and use json_object_new_string_len() to avoid a needless extra strlen() call. Also, create a _json_object_get_string_len() for internal use when we know for sure the object we're dealing with is a json_type_string. --- diff --git a/json_object.c b/json_object.c index 9198257c..a8ca7220 100644 --- a/json_object.c +++ b/json_object.c @@ -735,7 +735,7 @@ struct json_object *json_object_new_int(int32_t i) int32_t json_object_get_int(const struct json_object *jso) { - int64_t cint64=0; + int64_t cint64 = 0; double cdouble; enum json_type o_type; @@ -1305,18 +1305,20 @@ const char *json_object_get_string(struct json_object *jso) default: return json_object_to_json_string(jso); } } -int json_object_get_string_len(const struct json_object *jso) + +static inline ssize_t _json_object_get_string_len(const struct json_object_string *jso) { ssize_t len; + len = jso->len; + return (len < 0) ? -(ssize_t)len : len; +} +int json_object_get_string_len(const struct json_object *jso) +{ if (!jso) return 0; switch (jso->o_type) { - case json_type_string: - { - len = JC_STRING_C(jso)->len; - return (len < 0) ? -(ssize_t)len : len; - } + case json_type_string: return _json_object_get_string_len(JC_STRING_C(jso)); default: return 0; } } @@ -1605,9 +1607,10 @@ int json_object_equal(struct json_object *jso1, struct json_object *jso2) case json_type_string: { - return (json_object_get_string_len(jso1) == json_object_get_string_len(jso2) && + return (_json_object_get_string_len(JC_STRING(jso1)) == + _json_object_get_string_len(JC_STRING(jso2)) && memcmp(get_string_component(jso1), get_string_component(jso2), - json_object_get_string_len(jso1)) == 0); + _json_object_get_string_len(JC_STRING(jso1))) == 0); } case json_type_object: return json_object_all_values_equal(jso1, jso2); @@ -1672,7 +1675,10 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha } break; - case json_type_string: *dst = json_object_new_string(get_string_component(src)); break; + case json_type_string: + *dst = json_object_new_string_len(get_string_component(src), + _json_object_get_string_len(JC_STRING(src))); + break; case json_type_object: *dst = json_object_new_object(); break;