(menu_cbs_title) SANITIZE_TO_STRING rewrite:

* Turn macro into function
* Don't use strchr (O(n^2), switch to single pass loop instead
This commit is contained in:
libretroadmin 2025-07-14 19:13:11 +02:00
parent 6e5e63b6d4
commit 4099b49483
1 changed files with 22 additions and 17 deletions

View File

@ -43,22 +43,29 @@
return action_get_title_generic(s, len, path, msg_hash_to_str(lbl)); \ return action_get_title_generic(s, len, path, msg_hash_to_str(lbl)); \
} \ } \
#define SANITIZE_TO_STRING(s, label, len) \ static void sanitize_to_string(char *s, const char *lbl, size_t len)
{ \ {
char *pos = NULL; \ size_t _len = strlcpy(s, lbl, len);
strlcpy(s, label, len); \ if (_len >= len)
while ((pos = strchr(s, '_'))) \ s[len - 1] = '\0';
*pos = ' '; \ else
{
char *pos;
/* Replace underscores with spaces in a single pass */
for (pos = s; *pos != '\0'; ++pos)
{
if (*pos == '_')
*pos = ' ';
} }
}
}
#define DEFAULT_TITLE_MACRO(func_name, lbl) \ #define DEFAULT_TITLE_MACRO(func_name, lbl) \
static int (func_name)(const char *path, const char *label, unsigned menu_type, char *s, size_t len) \ static int (func_name)(const char *path, const char *label, unsigned menu_type, char *s, size_t len) \
{ \ { \
const char *str = msg_hash_to_str(lbl); \ const char *str = msg_hash_to_str(lbl); \
if (s && !string_is_empty(str)) \ if (s && !string_is_empty(str)) \
{ \ sanitize_to_string(s, str, len); \
SANITIZE_TO_STRING(s, str, len); \
} \
return 1; \ return 1; \
} }
@ -130,9 +137,7 @@ static int action_get_title_action_generic(
unsigned menu_type, char *s, size_t len) unsigned menu_type, char *s, size_t len)
{ {
if (s && !string_is_empty(label)) if (s && !string_is_empty(label))
{ sanitize_to_string(s, label, len);
SANITIZE_TO_STRING(s, label, len);
}
return 1; return 1;
} }
@ -158,7 +163,7 @@ static int action_get_title_icon_thumbnails(
if (s && !string_is_empty(title)) if (s && !string_is_empty(title))
{ {
SANITIZE_TO_STRING(s, title, len); sanitize_to_string(s, title, len);
return 1; return 1;
} }
@ -186,7 +191,7 @@ static int action_get_title_thumbnails(
title = msg_hash_to_str(label_value); title = msg_hash_to_str(label_value);
if (s && !string_is_empty(title)) if (s && !string_is_empty(title))
{ {
SANITIZE_TO_STRING(s, title, len); sanitize_to_string(s, title, len);
return 1; return 1;
} }
return 0; return 0;
@ -219,7 +224,7 @@ static int action_get_title_left_thumbnails(
if (s && !string_is_empty(title)) if (s && !string_is_empty(title))
{ {
SANITIZE_TO_STRING(s, title, len); sanitize_to_string(s, title, len);
return 1; return 1;
} }
@ -363,7 +368,7 @@ static int action_get_title_dropdown_item(
const char *title = msg_hash_to_str(enum_idx); const char *title = msg_hash_to_str(enum_idx);
if (s && !string_is_empty(title)) if (s && !string_is_empty(title))
{ {
SANITIZE_TO_STRING(s, title, len); sanitize_to_string(s, title, len);
return 1; return 1;
} }
} }