From aedd02b0d181bf00b83ddab38bd061e2bdd96731 Mon Sep 17 00:00:00 2001 From: Ash Logan Date: Sun, 4 Aug 2024 18:15:16 +1000 Subject: [PATCH] chore(config): Switch to string_view for translated strings Elides about 100 strlen calls by allowing the length to be calculated at compile time ofc the WUPS backend doesn't USE this info but, y'know. --- src/Notification.cpp | 4 ++-- src/Notification.h | 4 +--- src/config.cpp | 20 ++++++++++++++------ src/config.h | 16 +++++++++------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Notification.cpp b/src/Notification.cpp index 43f08df..cc6177e 100644 --- a/src/Notification.cpp +++ b/src/Notification.cpp @@ -18,7 +18,7 @@ #include #include -void ShowNotification(std::string_view notification) { +void ShowNotification(const char* notification) { auto err1 = NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO, NOTIFICATION_MODULE_DEFAULT_OPTION_KEEP_UNTIL_SHOWN, true); auto err2 = NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_INFO, @@ -27,5 +27,5 @@ void ShowNotification(std::string_view notification) { if (err1 != NOTIFICATION_MODULE_RESULT_SUCCESS || err2 != NOTIFICATION_MODULE_RESULT_SUCCESS) return; - NotificationModule_AddInfoNotification(notification.data()); + NotificationModule_AddInfoNotification(notification); } diff --git a/src/Notification.h b/src/Notification.h index 879f729..ebdc7a3 100644 --- a/src/Notification.h +++ b/src/Notification.h @@ -1,5 +1,3 @@ #pragma once -#include - -void ShowNotification(std::string_view notification); \ No newline at end of file +void ShowNotification(const char* notification); diff --git a/src/config.cpp b/src/config.cpp index 88a1633..da01d1b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -37,7 +37,7 @@ bool Config::need_relaunch = false; bool Config::unregister_task_item_pressed = false; bool Config::is_wiiu_menu = false; -config_strings strings; +static config_strings strings; constexpr config_strings get_config_strings(nn::swkbd::LanguageType language) { switch (language) { @@ -232,10 +232,18 @@ static void unregister_task_item_on_input_cb(void *context, WUPSConfigSimplePadD } static int32_t unregister_task_item_get_display_value(void *context, char *out_buf, int32_t out_size) { - if (!Config::is_wiiu_menu) - strncpy(out_buf, strings.need_menu_action, out_size); - else - strncpy(out_buf, Config::unregister_task_item_pressed ? strings.restart_to_apply_action : strings.press_a_action, out_size); + auto string = strings.need_menu_action; + if (Config::is_wiiu_menu) { + if (Config::unregister_task_item_pressed) { + string = strings.restart_to_apply_action; + } else { + string = strings.press_a_action; + } + } + + if ((int)string.length() > out_size - 1) return -1; + string.copy(out_buf, string.length()); + out_buf[string.length()] = '\0'; return 0; } @@ -283,7 +291,7 @@ static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHa }; WUPSConfigAPIItemOptionsV2 unregisterTasksItemOptions = { - .displayName = strings.reset_wwp_setting, + .displayName = strings.reset_wwp_setting.data(), .context = nullptr, .callbacks = unregisterTasksItemCallbacks, }; diff --git a/src/config.h b/src/config.h index a83e178..ae11129 100644 --- a/src/config.h +++ b/src/config.h @@ -5,6 +5,8 @@ #ifndef INKAY_CONFIG_H #define INKAY_CONFIG_H +#include + class Config { public: static void Init(); @@ -23,13 +25,13 @@ class Config { struct config_strings { const char *plugin_name; - const char *network_category; - const char *connect_to_network_setting; - const char *other_category; - const char *reset_wwp_setting; - const char *press_a_action; - const char *restart_to_apply_action; - const char *need_menu_action; + std::string_view network_category; + std::string_view connect_to_network_setting; + std::string_view other_category; + std::string_view reset_wwp_setting; + std::string_view press_a_action; + std::string_view restart_to_apply_action; + std::string_view need_menu_action; }; #endif //INKAY_CONFIG_H