Skip to content

Commit 9ff2c85

Browse files
committed
(menu_cbs_title) Remove stdstring functions and significantly
increase performance of sanitize_to_string function
1 parent d82bbaf commit 9ff2c85

File tree

1 file changed

+82
-113
lines changed

1 file changed

+82
-113
lines changed

menu/cbs/menu_cbs_title.c

Lines changed: 82 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,29 @@ static size_t safe_strbuf_append(char *s, size_t len, size_t _len,
6060

6161
static void sanitize_to_string(char *s, const char *lbl, size_t len)
6262
{
63-
char *pos;
64-
strlcpy(s, lbl, len);
65-
/* Replace underscores with spaces in a single pass */
66-
for (pos = s; *pos != '\0'; ++pos)
63+
const char *src = lbl;
64+
const char *end = s + len - 1;
65+
char *dst = s;
66+
67+
if (len == 0)
68+
return;
69+
70+
/* Combine copy and replace into a single pass */
71+
while (*src != '\0' && dst < end)
6772
{
68-
if (*pos == '_')
69-
*pos = ' ';
73+
*dst = (*src == '_') ? ' ' : *src;
74+
++src;
75+
++dst;
7076
}
77+
78+
*dst = '\0';
7179
}
7280

7381
#define DEFAULT_TITLE_MACRO(func_name, lbl) \
7482
static int (func_name)(const char *path, const char *label, unsigned menu_type, char *s, size_t len) \
7583
{ \
7684
const char *str = msg_hash_to_str(lbl); \
77-
if (s && !string_is_empty(str)) \
85+
if (s && str && (*str != '\0')) \
7886
sanitize_to_string(s, str, len); \
7987
return 1; \
8088
}
@@ -84,9 +92,9 @@ static void sanitize_to_string(char *s, const char *lbl, size_t len)
8492
{ \
8593
size_t _len = 0; \
8694
const char *title = msg_hash_to_str(lbl); \
87-
if (!string_is_empty(title)) \
95+
if (title && *title) \
8896
_len = strlcpy(s, title, len); \
89-
if (!string_is_empty(path)) \
97+
if (path && *path) \
9098
{ \
9199
_len = safe_strbuf_append(s, len, _len, ": ", STRLEN_CONST(": ")); \
92100
strlcpy(s + _len, path, len - _len); \
@@ -123,12 +131,11 @@ static void action_get_title_fill_path_search_filter_default(
123131
{
124132
size_t _len = 0;
125133
const char *title = msg_hash_to_str(lbl);
126-
if (!string_is_empty(title))
134+
if (title && *title != '\0')
127135
_len = strlcpy(s, title, len);
128136
_len = safe_strbuf_append(s, len, _len, " ", STRLEN_CONST(" "));
129-
if (!string_is_empty(path))
137+
if (path && *path != '\0')
130138
strlcpy(s + _len, path, len - _len);
131-
132139
menu_entries_search_append_terms_string(s, len);
133140
}
134141

@@ -163,13 +170,11 @@ static int action_get_title_icon_thumbnails(
163170
{
164171
enum msg_hash_enums label_value = MENU_ENUM_LABEL_VALUE_ICON_THUMBNAILS;
165172
const char *title = msg_hash_to_str(label_value);
166-
167-
if (s && !string_is_empty(title))
173+
if (s && title && *title != '\0')
168174
{
169175
sanitize_to_string(s, title, len);
170176
return 1;
171177
}
172-
173178
return 0;
174179
}
175180

@@ -274,113 +279,78 @@ static int action_get_title_dropdown_item(
274279
const char *path, const char *label, unsigned menu_type,
275280
char *s, size_t len)
276281
{
277-
/* Sanity check */
278-
if (!string_is_empty(path))
282+
if (!path || !*path)
283+
return 0;
284+
285+
if (!memcmp(path, "core_option_", STRLEN_CONST("core_option_")))
279286
{
280-
if (string_starts_with_size(path, "core_option_",
281-
STRLEN_CONST("core_option_")))
287+
core_option_manager_t *coreopts = NULL;
288+
const char *opt = strrchr(path, '_');
289+
if (opt && opt[1] != '\0')
282290
{
283-
/* This is a core options item */
284-
core_option_manager_t *coreopts = NULL;
285-
const char *opt = strrchr(path, '_');
286-
if (opt && opt[1] != '\0')
291+
retroarch_ctl(RARCH_CTL_CORE_OPTIONS_LIST_GET, &coreopts);
292+
293+
if (coreopts)
287294
{
288-
retroarch_ctl(RARCH_CTL_CORE_OPTIONS_LIST_GET, &coreopts);
295+
unsigned option_index = string_to_unsigned(opt + 1);
296+
const char *title = core_option_manager_get_desc(
297+
coreopts, option_index, true);
289298

290-
if (coreopts)
299+
if (s && title && *title)
291300
{
292-
unsigned option_index = string_to_unsigned(opt+1);
293-
const char *title = core_option_manager_get_desc(
294-
coreopts, option_index, true);
295-
296-
if (s && !string_is_empty(title))
297-
{
298-
strlcpy(s, title, len);
299-
return 1;
300-
}
301+
strlcpy(s, title, len);
302+
return 1;
301303
}
302304
}
303305
}
304-
else
306+
}
307+
else
308+
{
309+
enum msg_hash_enums enum_idx = (enum msg_hash_enums)
310+
(string_to_unsigned(path) + 2);
311+
312+
if ((enum_idx > MSG_UNKNOWN) && (enum_idx < MSG_LAST))
305313
{
306-
/* This is a 'normal' drop down list */
307-
308-
/* In msg_hash.h, msg_hash_enums are generated via
309-
* the following macro:
310-
* #define MENU_LABEL(STR) \
311-
* MENU_ENUM_LABEL_##STR, \
312-
* MENU_ENUM_SUBLABEL_##STR, \
313-
* MENU_ENUM_LABEL_VALUE_##STR
314-
* to get 'MENU_ENUM_LABEL_VALUE_' from a
315-
* 'MENU_ENUM_LABEL_', we therefore add 2... */
316-
enum msg_hash_enums enum_idx = (enum msg_hash_enums)
317-
(string_to_unsigned(path) + 2);
318-
319-
/* Check if enum index is valid
320-
* Note: This is a very crude check, but better than nothing */
321-
if ((enum_idx > MSG_UNKNOWN) && (enum_idx < MSG_LAST))
314+
switch (enum_idx)
322315
{
323-
/* An annoyance: MENU_ENUM_LABEL_THUMBNAILS and
324-
* MENU_ENUM_LABEL_LEFT_THUMBNAILS require special
325-
* treatment, since their titles depend upon the
326-
* current menu driver... */
327-
switch (enum_idx)
328-
{
329-
case MENU_ENUM_LABEL_VALUE_THUMBNAILS:
330-
return action_get_title_thumbnails(
331-
path, label, menu_type, s, len);
332-
case MENU_ENUM_LABEL_VALUE_LEFT_THUMBNAILS:
333-
return action_get_title_left_thumbnails(
334-
path, label, menu_type, s, len);
335-
default:
316+
case MENU_ENUM_LABEL_VALUE_THUMBNAILS:
317+
return action_get_title_thumbnails(
318+
path, label, menu_type, s, len);
319+
case MENU_ENUM_LABEL_VALUE_LEFT_THUMBNAILS:
320+
return action_get_title_left_thumbnails(
321+
path, label, menu_type, s, len);
322+
default:
323+
if ((enum_idx >= MENU_ENUM_LABEL_INPUT_LIBRETRO_DEVICE) &&
324+
(enum_idx <= MENU_ENUM_LABEL_INPUT_LIBRETRO_DEVICE_LAST))
325+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_TYPE;
326+
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_PLAYER_ANALOG_DPAD_MODE) &&
327+
(enum_idx <= MENU_ENUM_LABEL_INPUT_PLAYER_ANALOG_DPAD_MODE_LAST))
328+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_ADC_TYPE;
329+
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_DEVICE_INDEX) &&
330+
(enum_idx <= MENU_ENUM_LABEL_INPUT_DEVICE_INDEX_LAST))
331+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_INDEX;
332+
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVATION_TYPE) &&
333+
(enum_idx <= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVATION_TYPE_LAST))
334+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_RESERVATION_TYPE;
335+
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVED_DEVICE_NAME) &&
336+
(enum_idx <= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVED_DEVICE_NAME_LAST))
337+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_RESERVED_DEVICE_NAME;
338+
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_MOUSE_INDEX) &&
339+
(enum_idx <= MENU_ENUM_LABEL_INPUT_MOUSE_INDEX_LAST))
340+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_MOUSE_INDEX;
341+
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_REMAP_PORT) &&
342+
(enum_idx <= MENU_ENUM_LABEL_INPUT_REMAP_PORT_LAST))
343+
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_REMAP_PORT;
344+
345+
{
346+
const char *title = msg_hash_to_str(enum_idx);
347+
if (s && title && *title)
336348
{
337-
/* Submenu label exceptions */
338-
/* Device Type */
339-
if ((enum_idx >= MENU_ENUM_LABEL_INPUT_LIBRETRO_DEVICE) &&
340-
(enum_idx <= MENU_ENUM_LABEL_INPUT_LIBRETRO_DEVICE_LAST))
341-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_TYPE;
342-
343-
/* Analog to Digital Type */
344-
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_PLAYER_ANALOG_DPAD_MODE) &&
345-
(enum_idx <= MENU_ENUM_LABEL_INPUT_PLAYER_ANALOG_DPAD_MODE_LAST))
346-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_ADC_TYPE;
347-
348-
/* Device Index */
349-
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_DEVICE_INDEX) &&
350-
(enum_idx <= MENU_ENUM_LABEL_INPUT_DEVICE_INDEX_LAST))
351-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_INDEX;
352-
353-
/* Device Reservation Type */
354-
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVATION_TYPE) &&
355-
(enum_idx <= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVATION_TYPE_LAST))
356-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_RESERVATION_TYPE;
357-
358-
/* Reserved Device Name */
359-
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVED_DEVICE_NAME) &&
360-
(enum_idx <= MENU_ENUM_LABEL_INPUT_DEVICE_RESERVED_DEVICE_NAME_LAST))
361-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_DEVICE_RESERVED_DEVICE_NAME;
362-
363-
/* Mouse Index */
364-
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_MOUSE_INDEX) &&
365-
(enum_idx <= MENU_ENUM_LABEL_INPUT_MOUSE_INDEX_LAST))
366-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_MOUSE_INDEX;
367-
368-
/* Mapped Port (virtual -> 'physical' port mapping) */
369-
else if ((enum_idx >= MENU_ENUM_LABEL_INPUT_REMAP_PORT) &&
370-
(enum_idx <= MENU_ENUM_LABEL_INPUT_REMAP_PORT_LAST))
371-
enum_idx = MENU_ENUM_LABEL_VALUE_INPUT_REMAP_PORT;
372-
373-
{
374-
const char *title = msg_hash_to_str(enum_idx);
375-
if (s && !string_is_empty(title))
376-
{
377-
sanitize_to_string(s, title, len);
378-
return 1;
379-
}
380-
}
349+
sanitize_to_string(s, title, len);
350+
return 1;
381351
}
382-
break;
383-
}
352+
}
353+
break;
384354
}
385355
}
386356
}
@@ -824,12 +794,11 @@ static int action_get_title_deferred_database_manager_list(
824794
const char *path, const char *label,
825795
unsigned menu_type, char *s, size_t len)
826796
{
827-
if (!string_is_empty(path))
797+
if (path && *path != '\0')
828798
{
829799
fill_pathname(s, path_basename(path), "", len);
830800
return 0;
831-
}
832-
801+
}
833802
strlcpy(s, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE), len);
834803
return 0;
835804
}

0 commit comments

Comments
 (0)