Use string_starts_with_size where possible - avoids the strlen

This commit is contained in:
twinaphex 2020-06-25 12:51:04 +02:00
parent 35527c24c3
commit 601a73fa8b
20 changed files with 70 additions and 57 deletions

View file

@ -521,7 +521,8 @@ static void rcheevos_async_task_callback(
case CHEEVOS_ASYNC_AWARD_ACHIEVEMENT:
/* ignore already unlocked */
if (string_starts_with(buffer, "User already has "))
if (string_starts_with_size(buffer, "User already has ",
STRLEN_CONST("User already has ")))
break;
/* fallthrough to default */

View file

@ -2533,7 +2533,7 @@ static bool check_menu_driver_compatibility(settings_t *settings)
return true;
/* TODO/FIXME - maintenance hazard */
if (string_starts_with(video_driver, "d3d"))
if (string_starts_with_size(video_driver, "d3d", STRLEN_CONST("d3d")))
if (
string_is_equal(video_driver, "d3d9") ||
string_is_equal(video_driver, "d3d10") ||
@ -2541,7 +2541,7 @@ static bool check_menu_driver_compatibility(settings_t *settings)
string_is_equal(video_driver, "d3d12")
)
return true;
if (string_starts_with(video_driver, "gl"))
if (string_starts_with_size(video_driver, "gl", STRLEN_CONST("gl")))
if (
string_is_equal(video_driver, "gl") ||
string_is_equal(video_driver, "gl1") ||

View file

@ -172,11 +172,11 @@ SlangProcess::SlangProcess()
const char *value_str = strtok(0, delims);
int value = (int)strtoul(value_str, nullptr, 0);
if (string_starts_with(token, "Max"))
if (string_starts_with_size(token, "Max", STRLEN_CONST("Max")))
{
if (string_starts_with(token, "MaxCompute"))
if (string_starts_with_size(token, "MaxCompute", STRLEN_CONST("MaxCompute")))
{
if (string_starts_with(token, "MaxComputeWork"))
if (string_starts_with_size(token, "MaxComputeWork", STRLEN_CONST("MaxComputeWork")))
{
if (string_is_equal(token, "MaxComputeWorkGroupCountX"))
Resources.maxComputeWorkGroupCountX = value;
@ -202,7 +202,7 @@ SlangProcess::SlangProcess()
else if (string_is_equal(token, "MaxComputeAtomicCounterBuffers"))
Resources.maxComputeAtomicCounterBuffers = value;
}
else if (string_starts_with(token, "MaxVertex"))
else if (string_starts_with_size(token, "MaxVertex", STRLEN_CONST("MaxVertex")))
{
if (string_is_equal(token, "MaxVertexAttribs"))
Resources.maxVertexAttribs = value;
@ -223,9 +223,9 @@ SlangProcess::SlangProcess()
else if (string_is_equal(token, "MaxVertexAtomicCounterBuffers"))
Resources.maxVertexAtomicCounterBuffers = value;
}
else if (string_starts_with(token, "MaxTess"))
else if (string_starts_with_size(token, "MaxTess", STRLEN_CONST("MaxTess")))
{
if (string_starts_with(token, "MaxTessEvaluation"))
if (string_starts_with_size(token, "MaxTessEvaluation", STRLEN_CONST("MaxTessEvaluation")))
{
if (string_is_equal(token, "MaxTessEvaluationInputComponents"))
Resources.maxTessEvaluationInputComponents = value;
@ -242,7 +242,7 @@ SlangProcess::SlangProcess()
else if (string_is_equal(token, "MaxTessEvaluationImageUniforms"))
Resources.maxTessEvaluationImageUniforms = value;
}
else if (string_starts_with(token, "MaxTessControl"))
else if (string_starts_with_size(token, "MaxTessControl", STRLEN_CONST("MaxTessControl")))
{
if (string_is_equal(token, "MaxTessControlInputComponents"))
Resources.maxTessControlInputComponents = value;
@ -266,7 +266,7 @@ SlangProcess::SlangProcess()
else if (string_is_equal(token, "MaxTessGenLevel"))
Resources.maxTessGenLevel = value;
}
else if (string_starts_with(token, "MaxFragment"))
else if (string_starts_with_size(token, "MaxFragment", STRLEN_CONST("MaxFragment")))
{
if (string_is_equal(token, "MaxFragmentUniformComponents"))
Resources.maxFragmentUniformComponents = value;
@ -360,7 +360,7 @@ SlangProcess::SlangProcess()
else if (string_is_equal(token, "MaxSamples"))
Resources.maxSamples = value;
}
else if (string_starts_with(token, "general"))
else if (string_starts_with_size(token, "general", STRLEN_CONST("general")))
{
if (string_is_equal(token, "generalUniformIndexing"))
Resources.limits.generalUniformIndexing = (value != 0);

View file

@ -1062,7 +1062,7 @@ static void handle_hotplug(android_input_t *android,
*/
else if(
(
string_starts_with(device_model, "R800") ||
string_starts_with_size(device_model, "R800", STRLEN_CONST("R800")) ||
strstr(device_model, "Xperia Play") ||
strstr(device_model, "Play") ||
strstr(device_model, "SO-01D")
@ -1114,7 +1114,7 @@ static void handle_hotplug(android_input_t *android,
/* Amazon Fire TV & Fire stick */
else if (
string_starts_with(device_model, "AFT") &&
string_starts_with_size(device_model, "AFT", STRLEN_CONST("AFT")) &&
(
strstr(device_model, "AFTB") ||
strstr(device_model, "AFTT") ||

View file

@ -1342,7 +1342,8 @@ struct string_list* cdrom_get_available_drives(void)
for (i = 0; i < (int)dir_list->size; i++)
{
if (string_starts_with(dir_list->elems[i].data, "/dev/sg"))
if (string_starts_with_size(dir_list->elems[i].data, "/dev/sg",
STRLEN_CONST("/dev/sg")))
{
libretro_vfs_implementation_file *stream;
char drive_model[32] = {0};

View file

@ -381,7 +381,8 @@ static bool parse_line(config_file_t *conf,
/* Starting a line with an 'include' directive
* appends a sub-config file
* > All other comments are ignored */
if (!string_starts_with(comment, "include "))
if (!string_starts_with_size(comment, "include ",
STRLEN_CONST("include ")))
return false;
include_line = comment + STRLEN_CONST("include ");

View file

@ -635,9 +635,9 @@ bool path_is_absolute(const char *path)
#if defined(_WIN32)
/* Many roads lead to Rome...
* Note: Drive letter can only be 1 character long */
if (string_starts_with(path, "\\\\") ||
string_starts_with(path + 1, ":/") ||
string_starts_with(path + 1, ":\\"))
if (string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\")) ||
string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/")) ||
string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\")))
return true;
#elif defined(__wiiu__) || defined(VITA)
{

View file

@ -45,6 +45,12 @@ static INLINE bool string_is_equal(const char *a, const char *b)
return (a && b) ? !strcmp(a, b) : false;
}
static INLINE bool string_starts_with_size(const char *str, const char *prefix,
size_t size)
{
return (str && prefix) ? !strncmp(prefix, str, size) : false;
}
static INLINE bool string_starts_with(const char *str, const char *prefix)
{
return (str && prefix) ? !strncmp(prefix, str, strlen(prefix)) : false;

View file

@ -436,7 +436,7 @@ static void cheat_manager_load_cb_second_pass(char *key, char *value)
cheat_st->cheats[cheat_idx].handler = (unsigned)strtoul(value, NULL, 0);
else if (string_is_equal(key, "memory_search_size"))
cheat_st->cheats[cheat_idx].memory_search_size = (unsigned)strtoul(value, NULL, 0);
else if (string_starts_with(key, "repeat_"))
else if (string_starts_with_size(key, "repeat_", STRLEN_CONST("repeat_")))
{
if (string_is_equal(key, "repeat_add_to_address"))
cheat_st->cheats[cheat_idx].repeat_add_to_address = (unsigned)strtoul(value, NULL, 0);
@ -445,7 +445,7 @@ static void cheat_manager_load_cb_second_pass(char *key, char *value)
else if (string_is_equal(key, "repeat_count"))
cheat_st->cheats[cheat_idx].repeat_count = (unsigned)strtoul(value, NULL, 0);
}
else if (string_starts_with(key, "rumble"))
else if (string_starts_with_size(key, "rumble", STRLEN_CONST("rumble")))
{
if (string_is_equal(key, "rumble_port"))
cheat_st->cheats[cheat_idx].rumble_port = (unsigned)strtoul(value, NULL, 0);

View file

@ -1901,7 +1901,7 @@ int menu_cbs_init_bind_get_string_representation(menu_file_list_cbs_t *cbs,
if (!cbs)
return -1;
if ( string_starts_with(label, "input_player") &&
if ( string_starts_with_size(label, "input_player", STRLEN_CONST("input_player")) &&
string_ends_with(label, "joypad_index")
)
{

View file

@ -842,7 +842,7 @@ static int menu_cbs_init_bind_left_compare_label(menu_file_list_cbs_t *cbs,
}
}
if ( string_starts_with(label, "input_player") &&
if ( string_starts_with_size(label, "input_player", STRLEN_CONST("input_player")) &&
string_ends_with(label, "_joypad_index"))
{
unsigned i;
@ -868,7 +868,7 @@ static int menu_cbs_init_bind_left_compare_label(menu_file_list_cbs_t *cbs,
return 0;
}
if (strstr(label, "rdb_entry") || string_starts_with(label, "content_info"))
if (strstr(label, "rdb_entry") || string_starts_with_size(label, "content_info", STRLEN_CONST("content_info")))
{
BIND_ACTION_LEFT(cbs, action_left_scroll);
}

View file

@ -959,7 +959,7 @@ static int menu_cbs_init_bind_right_compare_label(menu_file_list_cbs_t *cbs,
}
}
if ( string_starts_with(label, "input_player") &&
if ( string_starts_with_size(label, "input_player", STRLEN_CONST("input_player")) &&
string_ends_with(label, "_joypad_index"))
{
unsigned i;
@ -984,7 +984,8 @@ static int menu_cbs_init_bind_right_compare_label(menu_file_list_cbs_t *cbs,
return 0;
}
if (strstr(label, "rdb_entry") || string_starts_with(label, "content_info"))
if ( strstr(label, "rdb_entry") ||
string_starts_with_size(label, "content_info", STRLEN_CONST("content_info")))
{
BIND_ACTION_RIGHT(cbs, action_right_scroll);
}

View file

@ -169,7 +169,7 @@ static int action_get_title_dropdown_item(
if (string_is_empty(path))
return 0;
if (string_starts_with(path, "core_option_"))
if (string_starts_with_size(path, "core_option_", STRLEN_CONST("core_option_")))
{
/* This is a core options item */
struct string_list *tmp_str_list = string_split(path, "_");

View file

@ -103,7 +103,7 @@ static void ozone_draw_entry_value(
{
if (string_is_equal(entry->value, "..."))
return;
if (string_starts_with(entry->value, "(") &&
if (string_starts_with_size(entry->value, "(", STRLEN_CONST("(")) &&
string_ends_with (entry->value, ")")
)
{

View file

@ -3091,7 +3091,7 @@ static int xmb_draw_item(
if (string_is_equal(entry.value, "..."))
found = true;
else if (string_starts_with(entry.value, "(") &&
else if (string_starts_with_size(entry.value, "(", STRLEN_CONST("(")) &&
string_ends_with (entry.value, ")")
)
{

View file

@ -10948,7 +10948,8 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
{
menu_entries_ctl(MENU_ENTRIES_CTL_CLEAR, info->list);
if (string_starts_with(info->path, "core_option_"))
if (string_starts_with_size(info->path, "core_option_",
STRLEN_CONST("core_option_")))
{
struct string_list *tmp_str_list = string_split(info->path, "_");
@ -11366,7 +11367,8 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type,
case DISPLAYLIST_DROPDOWN_LIST_SPECIAL:
menu_entries_ctl(MENU_ENTRIES_CTL_CLEAR, info->list);
if (string_starts_with(info->path, "core_option_"))
if (string_starts_with_size(info->path, "core_option_",
STRLEN_CONST("core_option_")))
{
struct string_list *tmp_str_list = string_split(info->path, "_");

View file

@ -2034,7 +2034,7 @@ static JSON_Parser_HandlerResult JSONObjectMemberHandler(JSON_Parser parser, cha
pCtx->current_entry_val = &pCtx->current_entry->crc32;
else if (string_is_equal(pValue, "db_name"))
pCtx->current_entry_val = &pCtx->current_entry->db_name;
else if (string_starts_with(pValue, "subsystem_"))
else if (string_starts_with_size(pValue, "subsystem_", STRLEN_CONST("subsystem_")))
{
if (string_is_equal(pValue, "subsystem_ident"))
pCtx->current_entry_val = &pCtx->current_entry->subsystem_ident;
@ -2043,7 +2043,7 @@ static JSON_Parser_HandlerResult JSONObjectMemberHandler(JSON_Parser parser, cha
else if (string_is_equal(pValue, "subsystem_roms"))
pCtx->current_entry_string_list_val = &pCtx->current_entry->subsystem_roms;
}
else if (string_starts_with(pValue, "runtime_"))
else if (string_starts_with_size(pValue, "runtime_", STRLEN_CONST("runtime_")))
{
if (string_is_equal(pValue, "runtime_hours"))
pCtx->current_entry_uint_val = &pCtx->current_entry->runtime_hours;
@ -2052,7 +2052,7 @@ static JSON_Parser_HandlerResult JSONObjectMemberHandler(JSON_Parser parser, cha
else if (string_is_equal(pValue, "runtime_seconds"))
pCtx->current_entry_uint_val = &pCtx->current_entry->runtime_seconds;
}
else if (string_starts_with(pValue, "last_played_"))
else if (string_starts_with_size(pValue, "last_played_", STRLEN_CONST("last_played_")))
{
if (string_is_equal(pValue, "last_played_year"))
pCtx->current_entry_uint_val = &pCtx->current_entry->last_played_year;

View file

@ -3764,7 +3764,7 @@ static enum action_iterate_type action_iterate_type(const char *label)
{
if (string_is_equal(label, "info_screen"))
return ITERATE_TYPE_INFO;
if (string_starts_with(label, "help"))
if (string_starts_with_size(label, "help", STRLEN_CONST("help")))
if (
string_is_equal(label, "help") ||
string_is_equal(label, "help_controls") ||
@ -3778,7 +3778,7 @@ static enum action_iterate_type action_iterate_type(const char *label)
return ITERATE_TYPE_HELP;
if (string_is_equal(label, "cheevos_description"))
return ITERATE_TYPE_HELP;
if (string_starts_with(label, "custom_bind"))
if (string_starts_with_size(label, "custom_bind", STRLEN_CONST("custom_bind")))
if (
string_is_equal(label, "custom_bind") ||
string_is_equal(label, "custom_bind_all") ||
@ -11042,12 +11042,12 @@ enum rarch_content_type path_is_media_type(const char *path)
string_to_lower(ext_lower);
/* hack, to detect livestreams so the ffmpeg core can be started */
if (string_starts_with(path, "udp://") ||
string_starts_with(path, "http://") ||
string_starts_with(path, "https://") ||
string_starts_with(path, "tcp://") ||
string_starts_with(path, "rtmp://") ||
string_starts_with(path, "rtp://"))
if (string_starts_with_size(path, "udp://", STRLEN_CONST("udp://")) ||
string_starts_with_size(path, "http://", STRLEN_CONST("http://")) ||
string_starts_with_size(path, "https://", STRLEN_CONST("https://")) ||
string_starts_with_size(path, "tcp://", STRLEN_CONST("tcp://")) ||
string_starts_with_size(path, "rtmp://", STRLEN_CONST("rtmp://")) ||
string_starts_with_size(path, "rtp://", STRLEN_CONST("rtp://")))
return RARCH_CONTENT_MOVIE;
switch (msg_hash_to_file_type(msg_hash_calculate(ext_lower)))
@ -24249,7 +24249,7 @@ static void menu_input_pointer_close_messagebox(struct rarch_state *p_rarch)
pop_stack = true;
/* > Help box */
if (string_starts_with(label, "help"))
if (string_starts_with_size(label, "help", STRLEN_CONST("help")))
if (
string_is_equal(label, msg_hash_to_str(MENU_ENUM_LABEL_HELP))
|| string_is_equal(label, msg_hash_to_str(MENU_ENUM_LABEL_HELP_CONTROLS))
@ -32355,7 +32355,7 @@ enum gfx_ctx_api video_context_driver_get_api(void)
const char *video_ident = (p_rarch->current_video)
? p_rarch->current_video->ident
: NULL;
if (string_starts_with(video_ident, "d3d"))
if (string_starts_with_size(video_ident, "d3d", STRLEN_CONST("d3d")))
{
if (string_is_equal(video_ident, "d3d9"))
return GFX_CTX_DIRECT3D9_API;
@ -32366,7 +32366,7 @@ enum gfx_ctx_api video_context_driver_get_api(void)
else if (string_is_equal(video_ident, "d3d12"))
return GFX_CTX_DIRECT3D12_API;
}
if (string_starts_with(video_ident, "gl"))
if (string_starts_with_size(video_ident, "gl", STRLEN_CONST("gl")))
{
if (string_is_equal(video_ident, "gl"))
return GFX_CTX_OPENGL_API;

View file

@ -250,7 +250,7 @@ int detect_psp_game(intfstream_t *fd, char *game_id)
bool found = false;
game_id[5] = '\0';
if (string_starts_with(game_id, "UL"))
if (string_starts_with_size(game_id, "UL", STRLEN_CONST("UL")))
{
if (
(string_is_equal(game_id, "ULES-"))
@ -263,7 +263,7 @@ int detect_psp_game(intfstream_t *fd, char *game_id)
)
found = true;
}
if (!found && string_starts_with(game_id, "UC"))
if (!found && string_starts_with_size(game_id, "UC", STRLEN_CONST("UC")))
{
if (
(string_is_equal(game_id, "UCES-"))
@ -276,7 +276,7 @@ int detect_psp_game(intfstream_t *fd, char *game_id)
found = true;
}
if (!found && string_starts_with(game_id, "NP"))
if (!found && string_starts_with_size(game_id, "NP", STRLEN_CONST("NP")))
{
if (
(string_is_equal(game_id, "NPEH-"))

View file

@ -2178,16 +2178,17 @@ void MainWindow::setCoreActions()
for (i = 0; i < m_launchWithComboBox->count(); i++)
{
QByteArray CorePathArray;
const char *core_path_data = NULL;
QVariantMap map = m_launchWithComboBox->itemData(i, Qt::UserRole).toMap();
QVariantMap map =
m_launchWithComboBox->itemData(
i, Qt::UserRole).toMap();
QByteArray CorePathArray =
map.value("core_path").toString().toUtf8();
const char *core_path_data = CorePathArray.constData();
CorePathArray = map.value("core_path").toString().toUtf8();
core_path_data = CorePathArray.constData();
if (string_starts_with(path_basename(core_path_data), info->core_file_id.str) ||
map.value("core_name").toString() == info->core_name ||
map.value("core_name").toString() == info->display_name)
if (string_starts_with(path_basename(core_path_data),
info->core_file_id.str) ||
map.value("core_name").toString() == info->core_name ||
map.value("core_name").toString() == info->display_name)
{
found_existing = true;
break;