eel: use g_string_replace over eel functionality

Use g_string_replace over eel_str_replace_substring and
eel_str_double_underscores.

Remove the now unused eel* functions.

Bump glib version to 2.76 for g_string_free_and_steal().
This commit is contained in:
Peter Eisenmann 2023-05-11 00:57:45 +02:00 committed by António Fernandes
parent b5dcb9e124
commit 2d28cc235b
6 changed files with 36 additions and 160 deletions

View file

@ -32,57 +32,6 @@
#include "eel-lib-self-check-functions.h"
#endif
/**
* eel_str_double_underscores:
* @string: input string
*
* This is used if you want to preserve underscore characters
* when creating a label with gtk_label_new_with_mnemonic().
*
* Returns: a newly allocated copy of @string,
* with a doubled number of underscores.
* If @string doesn't contain underscores, returns a copy of it.
* If @string is %NULL, returns %NULL.
*/
char *
eel_str_double_underscores (const char *string)
{
int underscores;
const char *p;
char *q;
char *escaped;
if (string == NULL)
{
return NULL;
}
underscores = 0;
for (p = string; *p != '\0'; p++)
{
underscores += (*p == '_');
}
if (underscores == 0)
{
return g_strdup (string);
}
escaped = g_new (char, strlen (string) + underscores + 1);
for (p = string, q = escaped; *p != '\0'; p++, q++)
{
/* Add an extra underscore. */
if (*p == '_')
{
*q++ = '_';
}
*q = *p;
}
*q = '\0';
return escaped;
}
/**
* eel_str_capitalize:
* @string: input string
@ -171,70 +120,6 @@ eel_str_middle_truncate (const gchar *string,
return g_strconcat (left_substring, ellipsis, right_substring, NULL);
}
/**
* eel_str_replace_substring:
* @string: input string
* @substring: (not nullable): string to be replaced
* @replacement: string used as replacement
*
* Returns: (transfer full): a copy of @string with all occurences of @substring
* replaced with @replacement.
*/
char *
eel_str_replace_substring (const char *string,
const char *substring,
const char *replacement)
{
int substring_length, replacement_length, result_length, remaining_length;
const char *p, *substring_position;
char *result, *result_position;
g_return_val_if_fail (substring != NULL, g_strdup (string));
g_return_val_if_fail (substring[0] != '\0', g_strdup (string));
if (string == NULL)
{
return NULL;
}
substring_length = substring ? strlen (substring) : 0;
replacement_length = replacement ? strlen (replacement) : 0;
result_length = strlen (string);
for (p = string;; p = substring_position + substring_length)
{
substring_position = strstr (p, substring);
if (substring_position == NULL)
{
break;
}
result_length += replacement_length - substring_length;
}
result = g_malloc (result_length + 1);
result_position = result;
for (p = string;; p = substring_position + substring_length)
{
substring_position = strstr (p, substring);
if (substring_position == NULL)
{
remaining_length = strlen (p);
memcpy (result_position, p, remaining_length);
result_position += remaining_length;
break;
}
memcpy (result_position, p, substring_position - p);
result_position += substring_position - p;
memcpy (result_position, replacement, replacement_length);
result_position += replacement_length;
}
g_assert (result_position - result == result_length);
result_position[0] = '\0';
return result;
}
/**
* get_common_prefix_length:
* @str_a: first string
@ -384,15 +269,6 @@ enum
void
eel_self_check_string (void)
{
EEL_CHECK_STRING_RESULT (eel_str_double_underscores (NULL), NULL);
EEL_CHECK_STRING_RESULT (eel_str_double_underscores (""), "");
EEL_CHECK_STRING_RESULT (eel_str_double_underscores ("_"), "__");
EEL_CHECK_STRING_RESULT (eel_str_double_underscores ("foo"), "foo");
EEL_CHECK_STRING_RESULT (eel_str_double_underscores ("foo_bar"), "foo__bar");
EEL_CHECK_STRING_RESULT (eel_str_double_underscores ("foo_bar_2"), "foo__bar__2");
EEL_CHECK_STRING_RESULT (eel_str_double_underscores ("_foo"), "__foo");
EEL_CHECK_STRING_RESULT (eel_str_double_underscores ("foo_"), "foo__");
EEL_CHECK_STRING_RESULT (eel_str_capitalize (NULL), NULL);
EEL_CHECK_STRING_RESULT (eel_str_capitalize (""), "");
EEL_CHECK_STRING_RESULT (eel_str_capitalize ("foo"), "Foo");
@ -431,17 +307,6 @@ eel_self_check_string (void)
EEL_CHECK_STRING_RESULT (eel_str_middle_truncate ("something_odd", 13), "something_odd");
EEL_CHECK_STRING_RESULT (eel_str_middle_truncate ("ääääääääää", 5), "ää…ää");
EEL_CHECK_STRING_RESULT (eel_str_middle_truncate ("あぃいぅうぇえぉ", 7), "あぃい…ぇえぉ");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring (NULL, "foo", NULL), NULL);
EEL_CHECK_STRING_RESULT (eel_str_replace_substring (NULL, "foo", "bar"), NULL);
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("bar", "foo", NULL), "bar");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("", "foo", ""), "");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("", "foo", "bar"), "");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("bar", "foo", ""), "bar");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("xxx", "x", "foo"), "foofoofoo");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("fff", "f", "foo"), "foofoofoo");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("foofoofoo", "foo", "f"), "fff");
EEL_CHECK_STRING_RESULT (eel_str_replace_substring ("foofoofoo", "f", ""), "oooooo");
}
#endif /* !EEL_OMIT_SELF_CHECK */

View file

@ -34,9 +34,6 @@
/* NULL is allowed for all the str parameters to these functions. */
/* Escape function for '_' character. */
char * eel_str_double_underscores (const char *str);
/* Capitalize a string */
char * eel_str_capitalize (const char *str);
@ -58,12 +55,6 @@ char * eel_str_capitalize (const char *str);
gchar *eel_str_middle_truncate (const gchar *string,
guint truncate_length);
/* Replace all occurrences of substring with replacement. */
char * eel_str_replace_substring (const char *str,
const char *substring,
const char *replacement);
/**
* eel_str_get_common_prefix:
* @str: set of strings

View file

@ -88,7 +88,7 @@ pkgconfig = import('pkgconfig')
################
# Dependencies #
################
glib_ver = '>= 2.74.0'
glib_ver = '>= 2.77.0'
libm = cc.find_library('m')

View file

@ -22,7 +22,6 @@
*/
#include "config.h"
#include <eel/eel-string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "nautilus-enum-types.h"
@ -563,9 +562,12 @@ _nautilus_gtk_bookmarks_manager_set_bookmark_label (NautilusGtkBookmarksManager
if (link)
{
GtkBookmark *bookmark = link->data;
GString *inlined_label = g_string_new (label);
g_string_replace (inlined_label, "\n", " ", 0);
g_free (bookmark->label);
bookmark->label = eel_str_replace_substring (label, "\n", " ");
bookmark->label = g_string_free_and_steal (inlined_label);
}
else
{

View file

@ -5342,7 +5342,7 @@ nautilus_file_get_date_as_string (NautilusFile *file,
gint days_ago;
const gchar *format;
gchar *result;
gchar *result_with_ratio;
GString *time_label;
if (!nautilus_file_get_date (file, date_type, &file_time_raw))
{
@ -5486,10 +5486,10 @@ nautilus_file_get_date_as_string (NautilusFile *file,
/* Replace ":" with ratio. Replacement is done afterward because g_date_time_format
* may fail with utf8 chars in some locales */
result_with_ratio = eel_str_replace_substring (result, ":", "");
g_free (result);
time_label = g_string_new_take (g_steal_pointer (&result));
g_string_replace (time_label, ":", "", 0);
return result_with_ratio;
return g_string_free_and_steal (time_label);
}
static void
@ -6051,9 +6051,9 @@ get_real_name (const char *name,
}
else
{
real_name = eel_str_replace_substring
(part_before_comma, "&", capitalized_login_name);
g_free (part_before_comma);
GString *real_name_str = g_string_new_take (g_steal_pointer (&part_before_comma));
g_string_replace (real_name_str, "&", capitalized_login_name, 0);
real_name = g_string_free_and_steal (real_name_str);
}

View file

@ -582,6 +582,27 @@ set_floating_bar_status (NautilusFilesView *view,
floating_bar_set_status_data_free);
}
/**
* escape_underscores:
* @to_escape: input string
*
* This is used to preserve underscore characters in strings, when they would
* otherwise be used for mnemonics.
*
* Returns: A copy of @to_escape, with underscore characters duplicated.
* If @to_escape doesn't contain underscores, returns a copy of it.
* If @to_escape is %NULL, returns an empty string.
*/
static char *
escape_underscores (const char *to_escape)
{
GString *string = g_string_new (to_escape);
g_string_replace (string, "_", "__", 0);
return g_string_free_and_steal (string);
}
static char *
real_get_backing_uri (NautilusFilesView *view)
{
@ -5676,7 +5697,7 @@ add_template_to_templates_menus (NautilusFilesView *view,
g_action_map_add_action (G_ACTION_MAP (priv->view_action_group), action);
detailed_action_name = g_strconcat ("view.", action_name, NULL);
label = eel_str_double_underscores (name);
label = escape_underscores (name);
menu_item = g_menu_item_new (label, detailed_action_name);
mimetype_icon = get_menu_icon_for_file (file, GTK_WIDGET (view));
@ -5852,7 +5873,7 @@ update_directory_in_templates_menu (NautilusFilesView *view,
g_autofree char *label = NULL;
display_name = nautilus_file_get_display_name (file);
label = eel_str_double_underscores (display_name);
label = escape_underscores (display_name);
menu_item = g_menu_item_new_submenu (label, children_menu);
g_menu_append_item (menu, menu_item);
any_templates = TRUE;
@ -8052,9 +8073,7 @@ update_selection_menu (NautilusFilesView *view,
if (app != NULL)
{
char *escaped_app;
escaped_app = eel_str_double_underscores (g_app_info_get_name (app));
g_autofree char *escaped_app = escape_underscores (g_app_info_get_name (app));
item_label = g_strdup_printf (_("Open With %s"), escaped_app);
app_icon = g_app_info_get_icon (app);
@ -8062,7 +8081,6 @@ update_selection_menu (NautilusFilesView *view,
{
g_object_ref (app_icon);
}
g_free (escaped_app);
g_object_unref (app);
}
else if (show_run)