ui-util: integrate string capitalization function

Integrate `eel_str_capitalize` as `nautilus_capitalize_str` into ui utilities
and turn the self-check functionality into a unittest.
This commit is contained in:
Peter Eisenmann 2023-10-24 00:22:29 +02:00
parent 43b6c59148
commit f5633d1ea7
8 changed files with 84 additions and 50 deletions

View file

@ -32,43 +32,6 @@
#include "eel-lib-self-check-functions.h"
#endif
/**
* eel_str_capitalize:
* @string: input string
*
* Returns: a newly allocated copy of @string,
* with the first letter capitalized.
* If @string is %NULL, returns %NULL.
*/
char *
eel_str_capitalize (const char *string)
{
char *capitalized = NULL;
if (string == NULL)
{
return NULL;
}
if (g_utf8_validate (string, -1, NULL))
{
g_autofree gunichar *ucs4 = NULL;
ucs4 = g_utf8_to_ucs4 (string, -1, NULL, NULL, NULL);
if (ucs4 != NULL)
{
ucs4[0] = g_unichar_toupper (ucs4[0]);
capitalized = g_ucs4_to_utf8 (ucs4, -1, NULL, NULL, NULL);
}
}
if (capitalized == NULL)
{
return g_strdup (string);
}
return capitalized;
}
/**
* get_common_prefix_length:
* @str_a: first string
@ -192,10 +155,6 @@ eel_str_get_common_prefix (GList *strs,
void
eel_self_check_string (void)
{
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");
EEL_CHECK_STRING_RESULT (eel_str_capitalize ("Foo"), "Foo");
}
#endif /* !EEL_OMIT_SELF_CHECK */

View file

@ -34,9 +34,6 @@
/* NULL is allowed for all the str parameters to these functions. */
/* Capitalize a string */
char * eel_str_capitalize (const char *str);
/**
* eel_str_get_common_prefix:
* @str: set of strings

View file

@ -26,7 +26,6 @@
#endif
#include <libnautilus-extension/nautilus-extension-private.h>
#include <eel/eel-string.h>
#include <gdesktop-enums.h>
#include <gio/gio.h>
#include <glib.h>
@ -5899,11 +5898,11 @@ get_real_name (const char *name,
if (!g_utf8_validate (name, -1, NULL))
{
g_autofree gchar *login_name = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
capitalized_login_name = eel_str_capitalize (login_name);
capitalized_login_name = nautilus_capitalize_str (login_name);
}
else
{
capitalized_login_name = eel_str_capitalize (name);
capitalized_login_name = nautilus_capitalize_str (name);
}
if (capitalized_login_name == NULL)

View file

@ -24,7 +24,6 @@
#include <adwaita.h>
#include <cairo.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
@ -2448,7 +2447,7 @@ setup_volume_information (NautilusPropertiesWindow *self)
{
/* Translators: %s will be filled with a filesystem type, such as 'ext4' or 'msdos'. */
g_autofree gchar *fs_label = g_strdup_printf (_("%s Filesystem"), fs_type);
gchar *cap_label = eel_str_capitalize (fs_label);
gchar *cap_label = nautilus_capitalize_str (fs_label);
if (cap_label != NULL)
{
g_free (fs_label);

View file

@ -30,6 +30,36 @@
#include <string.h>
#include <glib/gi18n.h>
char *
nautilus_capitalize_str (const char *string)
{
char *capitalized = NULL;
if (string == NULL)
{
return NULL;
}
if (g_utf8_validate (string, -1, NULL))
{
g_autofree gunichar *ucs4 = NULL;
ucs4 = g_utf8_to_ucs4 (string, -1, NULL, NULL, NULL);
if (ucs4 != NULL)
{
ucs4[0] = g_unichar_toupper (ucs4[0]);
capitalized = g_ucs4_to_utf8 (ucs4, -1, NULL, NULL, NULL);
}
}
if (capitalized == NULL)
{
return g_strdup (string);
}
return capitalized;
}
/**
* nautilus_gmenu_set_from_model:
* @target_menu: the #GMenu to be filled

View file

@ -25,6 +25,16 @@
#include <adwaita.h>
#include <gtk/gtk.h>
/**
* nautilus_capitalize_str:
* @string: input string
*
* Returns: a newly allocated copy of @string, with the first letter capitalized.
* If @string is %NULL, returns %NULL.
*/
char * nautilus_capitalize_str (const char *string);
void nautilus_gmenu_set_from_model (GMenu *target_menu,
GMenuModel *source_model);
gint nautilus_g_menu_model_find_by_string (GMenuModel *model,

View file

@ -44,7 +44,10 @@ tests = [
]],
['test-file-operations-trash-or-delete', [
'test-file-operations-trash-or-delete.c'
]]
]],
['test-ui-utilities', [
'test-ui-utilities.c'
]],
]
tracker_tests = [

View file

@ -0,0 +1,37 @@
#include <glib.h>
#include <nautilus-ui-utilities.h>
static void
test_string_capitalization (void)
{
char *capitalized;
g_assert_null (nautilus_capitalize_str (NULL));
capitalized = nautilus_capitalize_str ("");
g_assert_cmpstr (capitalized, ==, "");
g_free (capitalized);
capitalized = nautilus_capitalize_str ("foo");
g_assert_cmpstr (capitalized, ==, "Foo");
g_free (capitalized);
capitalized = nautilus_capitalize_str ("Foo");
g_assert_cmpstr (capitalized, ==, "Foo");
g_free (capitalized);
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_set_nonfatal_assertions ();
g_test_add_func ("/string-capitalization",
test_string_capitalization);
return g_test_run ();
}