From c2426e88c9583fab1d58d7d26e3bbe9d500ea570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Wed, 23 Aug 2023 16:48:09 +0200 Subject: irc: add functions to convert strings to lower/upper case (following casemapping) (issue #194) --- src/plugins/irc/irc-server.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/plugins/irc/irc-server.h | 4 +++ 2 files changed, 70 insertions(+) (limited to 'src') diff --git a/src/plugins/irc/irc-server.c b/src/plugins/irc/irc-server.c index 2cd37d1ff..1c4106796 100644 --- a/src/plugins/irc/irc-server.c +++ b/src/plugins/irc/irc-server.c @@ -333,6 +333,72 @@ irc_server_strncasecmp (struct t_irc_server *server, return weechat_strncasecmp_range (string1, string2, max, range); } +/* + * Converts string to lower case, following server casemapping. + * + * Note: result must be freed after use. + */ + +char * +irc_server_string_tolower (struct t_irc_server *server, const char *string) +{ + char *result, *ptr_result; + int casemapping, range; + + if (!string) + return NULL; + + casemapping = (server) ? server->casemapping : -1; + if ((casemapping < 0) || (casemapping >= IRC_SERVER_NUM_CASEMAPPING)) + casemapping = IRC_SERVER_CASEMAPPING_RFC1459; + + range = irc_server_casemapping_range[casemapping]; + + result = strdup (string); + ptr_result = result; + while (ptr_result && ptr_result[0]) + { + if ((ptr_result[0] >= 'A') && (ptr_result[0] < 'A' + range)) + ptr_result[0] += ('a' - 'A'); + ptr_result = (char *)weechat_utf8_next_char (ptr_result); + } + + return result; +} + +/* + * Converts string to upper case, following server casemapping. + * + * Note: result must be freed after use. + */ + +char * +irc_server_string_toupper (struct t_irc_server *server, const char *string) +{ + char *result, *ptr_result; + int casemapping, range; + + if (!string) + return NULL; + + casemapping = (server) ? server->casemapping : -1; + if ((casemapping < 0) || (casemapping >= IRC_SERVER_NUM_CASEMAPPING)) + casemapping = IRC_SERVER_CASEMAPPING_RFC1459; + + range = irc_server_casemapping_range[casemapping]; + + result = strdup (string); + ptr_result = result; + while (ptr_result && ptr_result[0]) + { + if ((ptr_result[0] >= 'a') && (ptr_result[0] < 'a' + range)) + ptr_result[0] -= ('a' - 'A'); + ptr_result = (char *)weechat_utf8_next_char (ptr_result); + } + + return result; +} + /* * Evaluates a string using the server as context: * ${irc_server.xxx} and ${server} are replaced by a server option and the diff --git a/src/plugins/irc/irc-server.h b/src/plugins/irc/irc-server.h index d34cb31d0..4ae647ed2 100644 --- a/src/plugins/irc/irc-server.h +++ b/src/plugins/irc/irc-server.h @@ -348,6 +348,10 @@ extern int irc_server_strcasecmp (struct t_irc_server *server, extern int irc_server_strncasecmp (struct t_irc_server *server, const char *string1, const char *string2, int max); +extern char *irc_server_string_tolower (struct t_irc_server *server, + const char *string); +extern char *irc_server_string_toupper (struct t_irc_server *server, + const char *string); extern char *irc_server_eval_expression (struct t_irc_server *server, const char *string); extern void irc_server_sasl_get_creds (struct t_irc_server *server, -- cgit v1.2.3