Skip to content

Commit f79eb44

Browse files
bazsialltilla
authored andcommitted
filterx: use stack declared FilterXString instances where it makes sense
Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
1 parent 874b34c commit f79eb44

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

lib/filterx/object-json-object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static gboolean
200200
_iter_inner(FilterXJsonObject *self, const gchar *obj_key, struct json_object *jso,
201201
FilterXDictIterFunc func, gpointer user_data)
202202
{
203-
FilterXObject *key = filterx_string_new(obj_key, -1);
203+
FILTERX_STRING_DECLARE_ON_STACK(key, obj_key, strlen(obj_key));
204204
FilterXObject *value = filterx_json_convert_json_to_object_cached(&self->super.super, &self->root_container,
205205
jso);
206206

modules/xml/filterx-parse-windows-eventlog-xml.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static gboolean
7272
_convert_to_dict(GMarkupParseContext *context, XmlElemContext *elem_context, GError **error)
7373
{
7474
const gchar *parent_elem_name = (const gchar *) g_markup_parse_context_get_element_stack(context)->next->data;
75-
FilterXObject *key = filterx_string_new(parent_elem_name, -1);
75+
FILTERX_STRING_DECLARE_ON_STACK(key, parent_elem_name, -1);
7676

7777
FilterXObject *dict_obj = filterx_object_create_dict(elem_context->parent_obj);
7878
if (!dict_obj)
@@ -106,7 +106,7 @@ _prepare_elem(const gchar *new_elem_name, XmlElemContext *last_elem_context, Xml
106106
{
107107
xml_elem_context_init(new_elem_context, last_elem_context->current_obj, NULL);
108108

109-
FilterXObject *new_elem_key = filterx_string_new(new_elem_name, -1);
109+
FILTERX_STRING_DECLARE_ON_STACK(new_elem_key, new_elem_name, -1);
110110
FilterXObject *existing_obj = NULL;
111111

112112
if (!filterx_object_is_key_set(new_elem_context->parent_obj, new_elem_key))
@@ -330,8 +330,8 @@ _text(FilterXGeneratorFunctionParseXml *s,
330330
return;
331331
}
332332

333-
FilterXObject *key = filterx_string_new(state->last_data_name->str, state->last_data_name->len);
334-
FilterXObject *text_obj = filterx_string_new(text, text_len);
333+
FILTERX_STRING_DECLARE_ON_STACK(key, state->last_data_name->str, state->last_data_name->len);
334+
FILTERX_STRING_DECLARE_ON_STACK(text_obj, text, text_len);
335335

336336
if (!filterx_object_set_subscript(elem_context->current_obj, key, &text_obj))
337337
{

modules/xml/filterx-parse-xml.c

+16-22
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ _prepare_elem(const gchar *new_elem_name, XmlElemContext *last_elem_context, gbo
249249
FilterXObject *new_elem_obj = _create_object_for_new_elem(last_elem_context->current_obj, has_attrs, &new_elem_repr);
250250
xml_elem_context_init(new_elem_context, last_elem_context->current_obj, new_elem_obj);
251251

252-
FilterXObject *new_elem_key = filterx_string_new(new_elem_name, -1);
252+
FILTERX_STRING_DECLARE_ON_STACK(new_elem_key, new_elem_name, -1);
253253
FilterXObject *existing_obj = NULL;
254254

255255
if (!filterx_object_is_key_set(new_elem_context->parent_obj, new_elem_key))
@@ -314,8 +314,8 @@ _collect_attrs(const gchar *element_name, XmlElemContext *elem_context,
314314

315315
const gchar *attr_value = attribute_values[i];
316316

317-
FilterXObject *key = filterx_string_new(attr_key->str, attr_key->len);
318-
FilterXObject *value = filterx_string_new(attr_value, -1);
317+
FILTERX_STRING_DECLARE_ON_STACK(key, attr_key->str, attr_key->len);
318+
FILTERX_STRING_DECLARE_ON_STACK(value, attr_value, -1);
319319

320320
gboolean success = filterx_object_set_subscript(elem_context->current_obj, key, &value);
321321

@@ -336,7 +336,7 @@ static gboolean
336336
_convert_to_dict(GMarkupParseContext *context, XmlElemContext *elem_context, GError **error)
337337
{
338338
const gchar *parent_elem_name = (const gchar *) g_markup_parse_context_get_element_stack(context)->next->data;
339-
FilterXObject *key = filterx_string_new(parent_elem_name, -1);
339+
FILTERX_STRING_DECLARE_ON_STACK(key, parent_elem_name, -1);
340340

341341
FilterXObject *dict_obj = filterx_object_create_dict(elem_context->parent_obj);
342342
if (!dict_obj)
@@ -349,7 +349,7 @@ _convert_to_dict(GMarkupParseContext *context, XmlElemContext *elem_context, GEr
349349

350350
if (existing_value_len > 0)
351351
{
352-
FilterXObject *existing_value_key = filterx_string_new("#text", 5);
352+
FILTERX_STRING_DECLARE_ON_STACK(existing_value_key, "#text", 5);
353353
gboolean success = filterx_object_set_subscript(dict_obj, existing_value_key, &elem_context->current_obj);
354354
filterx_object_unref(existing_value_key);
355355

@@ -447,12 +447,12 @@ static void
447447
_replace_string_text(XmlElemContext *elem_context, const gchar *element_name, const gchar *text, gsize text_len,
448448
GError **error)
449449
{
450-
FilterXObject *text_obj = filterx_string_new(text, text_len);
450+
FILTERX_STRING_DECLARE_ON_STACK(text_obj, text, text_len);
451451

452452
FilterXObject *parent_obj = filterx_ref_unwrap_rw(elem_context->parent_obj);
453453
if (filterx_object_is_type(parent_obj, &FILTERX_TYPE_NAME(dict)))
454454
{
455-
FilterXObject *key = filterx_string_new(element_name, -1);
455+
FILTERX_STRING_DECLARE_ON_STACK(key, element_name, -1);
456456
gboolean result = filterx_object_set_subscript(parent_obj, key, &text_obj);
457457
filterx_object_unref(key);
458458

@@ -482,10 +482,10 @@ _replace_string_text(XmlElemContext *elem_context, const gchar *element_name, co
482482
filterx_object_unref(text_obj);
483483
}
484484

485-
static FilterXObject *
486-
_create_text_obj(FilterXObject *dict, FilterXObject *existing_text_key, const gchar *text, gsize text_len)
485+
static GString *
486+
_concatenate_text_value(FilterXObject *dict, FilterXObject *existing_text_key, const gchar *text, gsize text_len)
487487
{
488-
FilterXObject *text_obj = NULL;
488+
GString *buffer = NULL;
489489

490490
FilterXObject *existing_obj = filterx_object_get_subscript(dict, existing_text_key);
491491
if (existing_obj)
@@ -499,32 +499,26 @@ _create_text_obj(FilterXObject *dict, FilterXObject *existing_text_key, const gc
499499
}
500500
else if (existing_value_len)
501501
{
502-
ScratchBuffersMarker marker;
503-
GString *buffer = scratch_buffers_alloc_and_mark(&marker);
502+
buffer = scratch_buffers_alloc();
504503
g_string_append_len(buffer, existing_value, existing_value_len);
505504
g_string_append_len(buffer, text, text_len);
506-
text_obj = filterx_string_new(buffer->str, buffer->len);
507-
scratch_buffers_reclaim_marked(marker);
508505
}
509506
filterx_object_unref(existing_obj);
510507
}
511508

512-
if (!text_obj)
513-
text_obj = filterx_string_new(text, text_len);
514-
515-
return text_obj;
509+
return buffer;
516510
}
517511

518512
static void
519513
_add_text_to_dict(XmlElemContext *elem_context, const gchar *text, gsize text_len, GError **error)
520514
{
521-
FilterXObject *key = filterx_string_new("#text", 5);
522-
FilterXObject *text_obj = _create_text_obj(elem_context->current_obj, key, text, text_len);
515+
FILTERX_STRING_DECLARE_ON_STACK(key, "#text", 5);
516+
GString *new_text = _concatenate_text_value(elem_context->current_obj, key, text, text_len);
517+
FILTERX_STRING_DECLARE_ON_STACK(text_obj, new_text ? new_text->str : text, new_text ? new_text->len : text_len);
523518

524519
if (!filterx_object_set_subscript(elem_context->current_obj, key, &text_obj))
525520
{
526-
const gchar *new_text = filterx_string_get_value_ref(text_obj, NULL);
527-
_set_error(error, "failed to add text to dict: \"#text\"=\"%s\"", new_text);
521+
_set_error(error, "failed to add text to dict: \"#text\"=\"%s\"", new_text ? new_text->str : text);
528522
goto fail;
529523
}
530524

0 commit comments

Comments
 (0)