From 347c3f321466af6a7e057701693e6547b7d47d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Tue, 3 Jan 2023 20:50:58 +0100 Subject: [PATCH] core: move function hook_get_priority_and_name to wee-string.c (issue #1872) --- src/core/hook/wee-hook-command-run.c | 3 +- src/core/hook/wee-hook-command.c | 3 +- src/core/hook/wee-hook-completion.c | 4 +- src/core/hook/wee-hook-config.c | 3 +- src/core/hook/wee-hook-focus.c | 3 +- src/core/hook/wee-hook-hdata.c | 4 +- src/core/hook/wee-hook-hsignal.c | 3 +- src/core/hook/wee-hook-info-hashtable.c | 3 +- src/core/hook/wee-hook-info.c | 3 +- src/core/hook/wee-hook-infolist.c | 3 +- src/core/hook/wee-hook-line.c | 3 +- src/core/hook/wee-hook-modifier.c | 3 +- src/core/hook/wee-hook-signal.c | 3 +- src/core/wee-hook.c | 45 ------------------ src/core/wee-hook.h | 2 - src/core/wee-string.c | 46 +++++++++++++++++++ src/core/wee-string.h | 4 ++ tests/unit/core/test-core-string.cpp | 61 +++++++++++++++++++++++++ 18 files changed, 139 insertions(+), 60 deletions(-) diff --git a/src/core/hook/wee-hook-command-run.c b/src/core/hook/wee-hook-command-run.c index 2bec7d3b9..235ad8f1b 100644 --- a/src/core/hook/wee-hook-command-run.c +++ b/src/core/hook/wee-hook-command-run.c @@ -78,7 +78,8 @@ hook_command_run (struct t_weechat_plugin *plugin, return NULL; } - hook_get_priority_and_name (command, &priority, &ptr_command); + string_get_priority_and_name (command, &priority, &ptr_command, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_COMMAND_RUN, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-command.c b/src/core/hook/wee-hook-command.c index 4b7b0048a..5b1120ba5 100644 --- a/src/core/hook/wee-hook-command.c +++ b/src/core/hook/wee-hook-command.c @@ -308,7 +308,8 @@ hook_command (struct t_weechat_plugin *plugin, const char *command, return NULL; } - hook_get_priority_and_name (command, &priority, &ptr_command); + string_get_priority_and_name (command, &priority, &ptr_command, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_COMMAND, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-completion.c b/src/core/hook/wee-hook-completion.c index 800a44722..ae5d285d0 100644 --- a/src/core/hook/wee-hook-completion.c +++ b/src/core/hook/wee-hook-completion.c @@ -78,7 +78,9 @@ hook_completion (struct t_weechat_plugin *plugin, const char *completion_item, return NULL; } - hook_get_priority_and_name (completion_item, &priority, &ptr_completion_item); + string_get_priority_and_name (completion_item, + &priority, &ptr_completion_item, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_COMPLETION, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-config.c b/src/core/hook/wee-hook-config.c index 8f510c792..cacf8f0d5 100644 --- a/src/core/hook/wee-hook-config.c +++ b/src/core/hook/wee-hook-config.c @@ -75,7 +75,8 @@ hook_config (struct t_weechat_plugin *plugin, const char *option, return NULL; } - hook_get_priority_and_name (option, &priority, &ptr_option); + string_get_priority_and_name (option, &priority, &ptr_option, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_CONFIG, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-focus.c b/src/core/hook/wee-hook-focus.c index ca1e6fe8d..744382e44 100644 --- a/src/core/hook/wee-hook-focus.c +++ b/src/core/hook/wee-hook-focus.c @@ -78,7 +78,8 @@ hook_focus (struct t_weechat_plugin *plugin, return NULL; } - hook_get_priority_and_name (area, &priority, &ptr_area); + string_get_priority_and_name (area, &priority, &ptr_area, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_FOCUS, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-hdata.c b/src/core/hook/wee-hook-hdata.c index 118d5edb3..6fefbcd54 100644 --- a/src/core/hook/wee-hook-hdata.c +++ b/src/core/hook/wee-hook-hdata.c @@ -32,6 +32,7 @@ #include "../wee-hook.h" #include "../wee-infolist.h" #include "../wee-log.h" +#include "../wee-string.h" /* @@ -77,7 +78,8 @@ hook_hdata (struct t_weechat_plugin *plugin, const char *hdata_name, return NULL; } - hook_get_priority_and_name (hdata_name, &priority, &ptr_hdata_name); + string_get_priority_and_name (hdata_name, &priority, &ptr_hdata_name, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_HDATA, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-hsignal.c b/src/core/hook/wee-hook-hsignal.c index de6e36289..1ed01f8b5 100644 --- a/src/core/hook/wee-hook-hsignal.c +++ b/src/core/hook/wee-hook-hsignal.c @@ -77,7 +77,8 @@ hook_hsignal (struct t_weechat_plugin *plugin, const char *signal, return NULL; } - hook_get_priority_and_name (signal, &priority, &ptr_signal); + string_get_priority_and_name (signal, &priority, &ptr_signal, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_HSIGNAL, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-info-hashtable.c b/src/core/hook/wee-hook-info-hashtable.c index a55e6aa61..3665276a7 100644 --- a/src/core/hook/wee-hook-info-hashtable.c +++ b/src/core/hook/wee-hook-info-hashtable.c @@ -77,7 +77,8 @@ hook_info_hashtable (struct t_weechat_plugin *plugin, const char *info_name, return NULL; } - hook_get_priority_and_name (info_name, &priority, &ptr_info_name); + string_get_priority_and_name (info_name, &priority, &ptr_info_name, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_INFO_HASHTABLE, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-info.c b/src/core/hook/wee-hook-info.c index a05514197..53d3525ea 100644 --- a/src/core/hook/wee-hook-info.c +++ b/src/core/hook/wee-hook-info.c @@ -76,7 +76,8 @@ hook_info (struct t_weechat_plugin *plugin, const char *info_name, return NULL; } - hook_get_priority_and_name (info_name, &priority, &ptr_info_name); + string_get_priority_and_name (info_name, &priority, &ptr_info_name, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_INFO, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-infolist.c b/src/core/hook/wee-hook-infolist.c index 4c2e36fda..3c0162482 100644 --- a/src/core/hook/wee-hook-infolist.c +++ b/src/core/hook/wee-hook-infolist.c @@ -77,7 +77,8 @@ hook_infolist (struct t_weechat_plugin *plugin, const char *infolist_name, return NULL; } - hook_get_priority_and_name (infolist_name, &priority, &ptr_infolist_name); + string_get_priority_and_name (infolist_name, &priority, &ptr_infolist_name, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_INFOLIST, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-line.c b/src/core/hook/wee-hook-line.c index 4e55f8c3c..6b3305fc4 100644 --- a/src/core/hook/wee-hook-line.c +++ b/src/core/hook/wee-hook-line.c @@ -87,7 +87,8 @@ hook_line (struct t_weechat_plugin *plugin, const char *buffer_type, return NULL; } - hook_get_priority_and_name (buffer_type, &priority, &ptr_buffer_type); + string_get_priority_and_name (buffer_type, &priority, &ptr_buffer_type, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_LINE, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-modifier.c b/src/core/hook/wee-hook-modifier.c index 9211f0475..ad5e372c6 100644 --- a/src/core/hook/wee-hook-modifier.c +++ b/src/core/hook/wee-hook-modifier.c @@ -75,7 +75,8 @@ hook_modifier (struct t_weechat_plugin *plugin, const char *modifier, return NULL; } - hook_get_priority_and_name (modifier, &priority, &ptr_modifier); + string_get_priority_and_name (modifier, &priority, &ptr_modifier, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_MODIFIER, priority, callback_pointer, callback_data); diff --git a/src/core/hook/wee-hook-signal.c b/src/core/hook/wee-hook-signal.c index a18c9cbc7..c2d8c15c5 100644 --- a/src/core/hook/wee-hook-signal.c +++ b/src/core/hook/wee-hook-signal.c @@ -77,7 +77,8 @@ hook_signal (struct t_weechat_plugin *plugin, const char *signal, return NULL; } - hook_get_priority_and_name (signal, &priority, &ptr_signal); + string_get_priority_and_name (signal, &priority, &ptr_signal, + HOOK_PRIORITY_DEFAULT); hook_init_data (new_hook, plugin, HOOK_TYPE_SIGNAL, priority, callback_pointer, callback_data); diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index ea9efa21c..74e2553d7 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -340,51 +340,6 @@ hook_remove_deleted () } } -/* - * Extracts priority and name from a string. - * - * String can be: - * - a simple name like "test": - * => priority = 1000 (default), name = "test" - * - a priority + "|" + name, like "500|test": - * => priority = 500, name = "test" - */ - -void -hook_get_priority_and_name (const char *string, - int *priority, const char **name) -{ - char *pos, *str_priority, *error; - long number; - - if (priority) - *priority = HOOK_PRIORITY_DEFAULT; - if (name) - *name = string; - - if (!string) - return; - - pos = strchr (string, '|'); - if (pos) - { - str_priority = string_strndup (string, pos - string); - if (str_priority) - { - error = NULL; - number = strtol (str_priority, &error, 10); - if (error && !error[0]) - { - if (priority) - *priority = number; - if (name) - *name = pos + 1; - } - free (str_priority); - } - } -} - /* * Initializes a new hook with default values. */ diff --git a/src/core/wee-hook.h b/src/core/wee-hook.h index 328827451..7d6cf8b05 100644 --- a/src/core/wee-hook.h +++ b/src/core/wee-hook.h @@ -123,8 +123,6 @@ extern int hook_socketpair_ok; extern void hook_init (); extern void hook_add_to_list (struct t_hook *new_hook); -extern void hook_get_priority_and_name (const char *string, int *priority, - const char **name); extern void hook_init_data (struct t_hook *hook, struct t_weechat_plugin *plugin, int type, int priority, diff --git a/src/core/wee-string.c b/src/core/wee-string.c index 2c41fbc0d..346cfc265 100644 --- a/src/core/wee-string.c +++ b/src/core/wee-string.c @@ -4129,6 +4129,52 @@ string_replace_with_callback (const char *string, return result; } +/* + * Extracts priority and name from a string. + * + * String can be: + * - a simple name like "test": + * => priority = default_priority, name = "test" + * - a priority + "|" + name, like "500|test": + * => priority = 500, name = "test" + */ + +void +string_get_priority_and_name (const char *string, + int *priority, const char **name, + int default_priority) +{ + char *pos, *str_priority, *error; + long number; + + if (priority) + *priority = default_priority; + if (name) + *name = string; + + if (!string) + return; + + pos = strchr (string, '|'); + if (pos) + { + str_priority = string_strndup (string, pos - string); + if (str_priority) + { + error = NULL; + number = strtol (str_priority, &error, 10); + if (error && !error[0]) + { + if (priority) + *priority = number; + if (name) + *name = pos + 1; + } + free (str_priority); + } + } +} + /* * Hashes a shared string. * The string starts after the reference count, which is skipped. diff --git a/src/core/wee-string.h b/src/core/wee-string.h index adeb41c6a..785e41bac 100644 --- a/src/core/wee-string.h +++ b/src/core/wee-string.h @@ -141,6 +141,10 @@ extern char *string_replace_with_callback (const char *string, char *(*callback)(void *data, const char *text), void *callback_data, int *errors); +extern void string_get_priority_and_name (const char *string, + int *priority, + const char **name, + int default_priority); extern const char *string_shared_get (const char *string); extern void string_shared_free (const char *string); extern char **string_dyn_alloc (int size_alloc); diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp index c9baf2462..7c14345d9 100644 --- a/tests/unit/core/test-core-string.cpp +++ b/tests/unit/core/test-core-string.cpp @@ -2540,6 +2540,67 @@ TEST(CoreString, InputForBuffer) config_file_option_reset (config_look_command_chars, 1); } +/* + * Tests functions: + * string_get_priority_and_name + */ + +TEST(CoreString, GetPriorityAndName) +{ + const char *empty = ""; + const char *delimiter = "|"; + const char *name = "test"; + const char *name_prio_empty = "|test"; + const char *name_prio = "1234|test"; + const char *ptr_name; + int priority; + + string_get_priority_and_name (NULL, NULL, NULL, 0); + string_get_priority_and_name ("test", NULL, NULL, 0); + + /* NULL => (default_priority, NULL) */ + priority = -1; + ptr_name = NULL; + string_get_priority_and_name (NULL, &priority, &ptr_name, 500); + LONGS_EQUAL(500, priority); + POINTERS_EQUAL(NULL, ptr_name); + + /* "" => (default_priority, "") */ + priority = -1; + ptr_name = NULL; + string_get_priority_and_name (empty, &priority, &ptr_name, 500); + LONGS_EQUAL(500, priority); + STRCMP_EQUAL("", ptr_name); + + /* "|" => (0, "") */ + priority = -1; + ptr_name = NULL; + string_get_priority_and_name (delimiter, &priority, &ptr_name, 500); + LONGS_EQUAL(0, priority); + STRCMP_EQUAL("", ptr_name); + + /* "test" => (default_priority, "test") */ + priority = -1; + ptr_name = NULL; + string_get_priority_and_name (name, &priority, &ptr_name, 500); + LONGS_EQUAL(500, priority); + STRCMP_EQUAL("test", ptr_name); + + /* "|test" => (0, "test") */ + priority = -1; + ptr_name = NULL; + string_get_priority_and_name (name_prio_empty, &priority, &ptr_name, 500); + LONGS_EQUAL(0, priority); + STRCMP_EQUAL("test", ptr_name); + + /* "1234|test" => (1234, "test") */ + priority = -1; + ptr_name = NULL; + string_get_priority_and_name (name_prio, &priority, &ptr_name, 500); + LONGS_EQUAL(1234, priority); + STRCMP_EQUAL("test", ptr_name); +} + /* * Tests functions: * string_shared_get