diff --git a/lib/glibcompat.c b/lib/glibcompat.c index a18a6a8a3..d32ec7d0d 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -186,3 +186,25 @@ mc_g_string_copy (GString * dest, const GString * src) } /* --------------------------------------------------------------------------------------------- */ + +/** + * mc_g_string_dup: + * @s: (nullable): the source #GString + * @return: @copy of @s + * + * Copies the bytes from one #GString to another. + * + * There is no such API in GLib2. + */ +GString * +mc_g_string_dup (const GString * s) +{ + GString *ret = NULL; + + if (s != NULL) + ret = g_string_new_len (s->str, s->len); + + return ret; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/glibcompat.h b/lib/glibcompat.h index af6049926..c83bdc096 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -27,6 +27,9 @@ void g_queue_clear_full (GQueue * queue, GDestroyNotify free_func); /* There is no such API in GLib2 */ GString *mc_g_string_copy (GString * dest, const GString * src); +/* There is no such API in GLib2 */ +GString *mc_g_string_dup (const GString * s); + /*** inline functions ****************************************************************************/ #endif /* MC_GLIBCOMPAT_H */ diff --git a/lib/search/hex.c b/lib/search/hex.c index fe048372f..e74e9ae5d 100644 --- a/lib/search/hex.c +++ b/lib/search/hex.c @@ -222,7 +222,8 @@ GString * mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str) { (void) lc_mc_search; - return g_string_new_len (replace_str->str, replace_str->len); + + return mc_g_string_dup (replace_str); } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/search/normal.c b/lib/search/normal.c index d9658cd1a..7fda2feb7 100644 --- a/lib/search/normal.c +++ b/lib/search/normal.c @@ -109,5 +109,6 @@ GString * mc_search_normal_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str) { (void) lc_mc_search; - return g_string_new_len (replace_str->str, replace_str->len); + + return mc_g_string_dup (replace_str); } diff --git a/lib/search/regex.c b/lib/search/regex.c index f92a73955..b8721cb2e 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -1006,7 +1006,7 @@ mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * repla mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len); if (lc_mc_search->num_results < 0) - return g_string_new_len (replace_str->str, replace_str->len); + return mc_g_string_dup (replace_str); if (num_replace_tokens > lc_mc_search->num_results - 1 || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS) diff --git a/lib/search/search.c b/lib/search/search.c index 0e8148640..d0a64d8d9 100644 --- a/lib/search/search.c +++ b/lib/search/search.c @@ -363,7 +363,7 @@ mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str return g_string_new (""); if (lc_mc_search == NULL) - return g_string_new_len (replace_str->str, replace_str->len); + return mc_g_string_dup (replace_str); switch (lc_mc_search->search_type) { @@ -380,7 +380,7 @@ mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str); break; default: - ret = g_string_new_len (replace_str->str, replace_str->len); + ret = mc_g_string_dup (replace_str); break; } return ret; diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index a50cd5204..ffc6b656b 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -725,7 +725,7 @@ dir_list_reload (dir_list * list, const vfs_path_t * vpath, GCompareFunc sort, fentry = &list->list[i]; dfentry = &dir_copy.list[i]; - dfentry->fname = g_string_new_len (fentry->fname->str, fentry->fname->len); + dfentry->fname = mc_g_string_dup (fentry->fname); dfentry->f.marked = fentry->f.marked; dfentry->f.dir_size_computed = fentry->f.dir_size_computed; dfentry->f.link_to_dir = fentry->f.link_to_dir; diff --git a/src/filemanager/find.c b/src/filemanager/find.c index b272cba59..b365bb871 100644 --- a/src/filemanager/find.c +++ b/src/filemanager/find.c @@ -1857,8 +1857,7 @@ do_find (WPanel * panel, const char *start_dir, ssize_t start_dir_len, const cha continue; } - list->list[list->len].fname = g_string_new_len (list->list[list->len].fname->str, - list->list[list->len].fname->len); + list->list[list->len].fname = mc_g_string_dup (list->list[list->len].fname); list->list[list->len].f.marked = 0; list->list[list->len].f.link_to_dir = link_to_dir ? 1 : 0; list->list[list->len].f.stale_link = stale_link ? 1 : 0; diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index 913cf9076..7e134093b 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -2273,7 +2273,7 @@ goto_parent_dir (WPanel * panel) vfs_path_t *dname_vpath; if (g_path_is_absolute (fname->str)) - fname = g_string_new_len (fname->str, fname->len); + fname = mc_g_string_dup (fname); else { char *fname2; diff --git a/src/filemanager/panelize.c b/src/filemanager/panelize.c index bacd94720..5fa675f2b 100644 --- a/src/filemanager/panelize.c +++ b/src/filemanager/panelize.c @@ -461,8 +461,7 @@ do_panelize_cd (WPanel * panel) for (i = 0; i < panelized_panel.list.len; i++) { if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname->str)) - list->list[i].fname = g_string_new_len (panelized_panel.list.list[i].fname->str, - panelized_panel.list.list[i].fname->len); + list->list[i].fname = mc_g_string_dup (panelized_panel.list.list[i].fname); else { vfs_path_t *tmp_vpath; @@ -523,8 +522,7 @@ panelize_save_panel (WPanel * panel) for (i = 0; i < panel->dir.len; i++) { - panelized_panel.list.list[i].fname = - g_string_new_len (list->list[i].fname->str, list->list[i].fname->len); + panelized_panel.list.list[i].fname = mc_g_string_dup (list->list[i].fname); panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir; panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link; panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed; diff --git a/tests/src/editor/editcmd__edit_complete_word_cmd.c b/tests/src/editor/editcmd__edit_complete_word_cmd.c index b5caa6b41..77e4aed3b 100644 --- a/tests/src/editor/editcmd__edit_complete_word_cmd.c +++ b/tests/src/editor/editcmd__edit_complete_word_cmd.c @@ -115,7 +115,7 @@ editcmd_dialog_completion_show (const WEdit * edit, GQueue * compl, int max_widt GString *s = (GString *) i->data; g_queue_push_tail (editcmd_dialog_completion_show__compl, - g_string_new_len (s->str, s->len)); + mc_g_string_dup (s)); } }