From e9be3269540696c9300684636ba4db942116651a Mon Sep 17 00:00:00 2001 From: Julian Bouzas Date: Thu, 24 Mar 2022 04:34:53 -0400 Subject: [PATCH] spa-json: add _new_from_stringn() API --- lib/wp/spa-json.c | 17 ++++++++++++++++- lib/wp/spa-json.h | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/wp/spa-json.c b/lib/wp/spa-json.c index cc76d960..193d1051 100644 --- a/lib/wp/spa-json.c +++ b/lib/wp/spa-json.c @@ -170,11 +170,26 @@ wp_spa_json_new (const gchar *data, size_t size) */ WpSpaJson * wp_spa_json_new_from_string (const gchar *json_str) +{ + return wp_spa_json_new_from_stringn(json_str, strlen (json_str)); +} + +/*! + * \brief Constructs a new WpSpaJson from a JSON string with specific length. + * + * \ingroup wpspajson + * \param json_str a JSON string + * \param len the specific length of the string + * \returns a new WpSpaJson that references the data in \a json_str. \a json_str + * is not copied, so it needs to stay alive. + */ +WpSpaJson * +wp_spa_json_new_from_stringn (const gchar *json_str, size_t len) { WpSpaJson *self = g_slice_new0 (WpSpaJson); g_ref_count_init (&self->ref); self->flags = FLAG_NO_OWNERSHIP; - spa_json_init (&self->json_data, json_str, strlen (json_str)); + spa_json_init (&self->json_data, json_str, len); self->builder = NULL; self->data = (gchar *)self->json_data.cur; self->size = self->json_data.end - self->json_data.cur; diff --git a/lib/wp/spa-json.h b/lib/wp/spa-json.h index 085ce5e6..1e51b220 100644 --- a/lib/wp/spa-json.h +++ b/lib/wp/spa-json.h @@ -37,6 +37,9 @@ void wp_spa_json_unref (WpSpaJson *self); WP_API WpSpaJson * wp_spa_json_new_from_string (const gchar *json_str); +WP_API +WpSpaJson * wp_spa_json_new_from_stringn (const gchar *json_str, size_t len); + WP_API WpSpaJson * wp_spa_json_new_wrap (struct spa_json *json);