summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-08-23 16:48:09 +0200
committerSébastien Helleu <flashcode@flashtux.org>2023-08-24 09:24:42 +0200
commitc2426e88c9583fab1d58d7d26e3bbe9d500ea570 (patch)
treef2dd4ea404ed258b7a511998583f000690d54931 /src
parent09eae3e2af7ea5c05d613b4056a6b90bf201e227 (diff)
downloadweechat-c2426e88c9583fab1d58d7d26e3bbe9d500ea570.zip
irc: add functions to convert strings to lower/upper case (following casemapping) (issue #194)
Diffstat (limited to 'src')
-rw-r--r--src/plugins/irc/irc-server.c66
-rw-r--r--src/plugins/irc/irc-server.h4
2 files changed, 70 insertions, 0 deletions
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
@@ -334,6 +334,72 @@ irc_server_strncasecmp (struct t_irc_server *server,
}
/*
+ * 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
* server name.
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,