From 2d86b8889ff319bb23184b2e1472a9a4a7739919 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 23 Dec 2023 16:54:53 +0300 Subject: [PATCH] Use g_string_new_take(). Signed-off-by: Andrew Borodin --- lib/glibcompat.c | 35 +++++++++++++++++++++++++++++++++++ lib/glibcompat.h | 4 ++++ src/editor/editsearch.c | 3 ++- src/filemanager/layout.c | 3 +-- src/filemanager/panel.c | 6 ++---- src/usermenu.c | 6 ++++-- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/glibcompat.c b/lib/glibcompat.c index cb32f78e2..7e508b639 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -140,6 +140,41 @@ g_queue_clear_full (GQueue * queue, GDestroyNotify free_func) /* --------------------------------------------------------------------------------------------- */ +#if ! GLIB_CHECK_VERSION (2, 77, 0) +/** + * g_string_new_take: + * @init: (nullable): initial text used as the string. + * Ownership of the string is transferred to the #GString. + * Passing NULL creates an empty string. + * + * Creates a new #GString, initialized with the given string. + * + * After this call, @init belongs to the #GString and may no longer be + * modified by the caller. The memory of @data has to be dynamically + * allocated and will eventually be freed with g_free(). + * + * Returns: the new #GString + */ +GString * +g_string_new_take (char *init) +{ + GString *string; + + if (init == NULL) + return g_string_new (NULL); + + string = g_slice_new (GString); + + string->str = init; + string->len = strlen (string->str); + string->allocated_len = string->len + 1; + + return string; +} +#endif /* ! GLIB_CHECK_VERSION (2, 77, 0) */ + +/* --------------------------------------------------------------------------------------------- */ + /** * mc_g_string_copy: * @dest: (not nullable): the destination #GString. Its current contents are destroyed diff --git a/lib/glibcompat.h b/lib/glibcompat.h index 42abf1dff..4f0b4d634 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -25,6 +25,10 @@ void g_clear_list (GList ** list_ptr, GDestroyNotify destroy); void g_queue_clear_full (GQueue * queue, GDestroyNotify free_func); #endif /* ! GLIB_CHECK_VERSION (2, 60, 0) */ +#if ! GLIB_CHECK_VERSION (2, 77, 0) +GString *g_string_new_take (char *init); +#endif /* ! GLIB_CHECK_VERSION (2, 77, 0) */ + /* There is no such API in GLib2 */ GString *mc_g_string_copy (GString * dest, const GString * src); diff --git a/src/editor/editsearch.c b/src/editor/editsearch.c index 3ecc4587c..27cdc3ba5 100644 --- a/src/editor/editsearch.c +++ b/src/editor/editsearch.c @@ -873,7 +873,8 @@ edit_replace_cmd (WEdit * edit, gboolean again) edit->search = NULL; } - input2_str = g_string_new (input2); + input2_str = g_string_new_take (input2); + input2 = NULL; if (edit->search == NULL) { diff --git a/src/filemanager/layout.c b/src/filemanager/layout.c index c9d581fb3..11d4e1b1a 100644 --- a/src/filemanager/layout.c +++ b/src/filemanager/layout.c @@ -1015,8 +1015,7 @@ setup_cmdline (void) #ifdef ENABLE_SUBSHELL if (mc_global.tty.use_subshell) { - subshell_prompt = g_string_new (tmp_prompt); - g_free (tmp_prompt); + subshell_prompt = g_string_new_take (tmp_prompt); mc_prompt = subshell_prompt->str; } #endif diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index cccd70418..46631465c 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -2353,8 +2353,7 @@ goto_parent_dir (WPanel * panel) mc_build_filename (vfs_path_as_str (panel->panelized_descr->root_vpath), fname->str, (char *) NULL); - fname = g_string_new (fname2); - g_free (fname2); + fname = g_string_new_take (fname2); } bname = x_basename (fname->str); @@ -5243,8 +5242,7 @@ panel_panelize_cd (void) tmp_vpath = vfs_path_append_new (pdescr->root_vpath, plist->list[i].fname->str, (char *) NULL); - list->list[i].fname = g_string_new (vfs_path_as_str (tmp_vpath)); - vfs_path_free (tmp_vpath, TRUE); + list->list[i].fname = g_string_new_take (vfs_path_free (tmp_vpath, FALSE)); } list->list[i].f.link_to_dir = plist->list[i].f.link_to_dir; list->list[i].f.stale_link = plist->list[i].f.stale_link; diff --git a/src/usermenu.c b/src/usermenu.c index 91c7d5810..830f7e55e 100644 --- a/src/usermenu.c +++ b/src/usermenu.c @@ -939,11 +939,13 @@ expand_format (const Widget * edit_widget, char c, gboolean do_quote) if (tmp != NULL) { if (block == NULL) - block = g_string_new (tmp); + block = g_string_new_take (tmp); else + { g_string_append (block, tmp); + g_free (tmp); + } g_string_append_c (block, ' '); - g_free (tmp); } if (c_lc == 'u')