pidgin/pidgin

206563c8b5c6
Parents 38d540a251b0
Children 7a4e48594a24
zephyr_tzc_escape_msg and zephyr_tzc_deescape_str refactoring

* Move declarations before implementations. Also slightly change `local_zephyr_normalize` signature
* Early return from functions
* Get `strlen(message)` once
* Convert `while` to `for`
* Simplify escaping
* Remove nulling of last char since `newmsg` already initialized to 0's

Testing Done:
Compile.

Reviewed at https://reviews.imfreedom.org/r/434/
--- a/libpurple/protocols/zephyr/zephyr.c Mon Jan 18 00:05:33 2021 -0600
+++ b/libpurple/protocols/zephyr/zephyr.c Mon Jan 18 18:00:51 2021 -0600
@@ -112,6 +112,10 @@
extern const char *username;
#endif
+static char *local_zephyr_normalize(const zephyr_account *zephyr, const char *);
+static void zephyr_chat_set_topic(PurpleConnection *gc, int id, const char *topic);
+static char *zephyr_tzc_deescape_str(const char *message);
+
static inline gboolean
use_tzc(const zephyr_account *zephyr)
{
@@ -166,10 +170,6 @@
return ZSubscribeTo(&sub, 1, 0);
}
-char *local_zephyr_normalize(zephyr_account* zephyr,const char *);
-static void zephyr_chat_set_topic(PurpleConnection * gc, int id, const char *topic);
-char* zephyr_tzc_deescape_str(const char *message);
-
static char *
zephyr_strip_local_realm(const zephyr_account *zephyr, const char *user)
{
@@ -1699,57 +1699,49 @@
return 1;
}
+
/* Munge the outgoing zephyr so that any quotes or backslashes are
escaped and do not confuse tzc: */
-
-static char* zephyr_tzc_escape_msg(const char *message)
+static char *
+zephyr_tzc_escape_msg(const char *message)
{
- gsize pos = 0, pos2 = 0;
+ gsize msglen;
char *newmsg;
- if (message && *message) {
- newmsg = g_new0(char,1+strlen(message)*2);
- while(pos < strlen(message)) {
- if (message[pos]=='\\') {
- newmsg[pos2]='\\';
- newmsg[pos2+1]='\\';
- pos2+=2;
- }
- else if (message[pos]=='"') {
- newmsg[pos2]='\\';
- newmsg[pos2+1]='"';
- pos2+=2;
- }
- else {
- newmsg[pos2] = message[pos];
- pos2++;
- }
+ if (!message || !*message) {
+ return g_strdup("");
+ }
+
+ msglen = strlen(message);
+ newmsg = g_new0(char, msglen*2 + 1);
+ for (gsize pos = 0, pos2 = 0; pos < msglen; pos++, pos2++) {
+ if (message[pos] == '\\' || message[pos] == '"') {
+ newmsg[pos2] = '\\';
+ pos2++;
+ }
+ newmsg[pos2] = message[pos];
+ }
+
+ return newmsg;
+}
+
+static char *
+zephyr_tzc_deescape_str(const char *message)
+{
+ gsize msglen;
+ char *newmsg;
+
+ if (!message || !*message) {
+ return g_strdup("");
+ }
+
+ msglen = strlen(message);
+ newmsg = g_new0(char, msglen + 1);
+ for (gsize pos = 0, pos2 = 0; pos < msglen; pos++, pos2++) {
+ if (message[pos] == '\\') {
pos++;
}
- } else {
- newmsg = g_strdup("");
- }
- /* fprintf(stderr,"newmsg %s message %s\n",newmsg,message); */
- return newmsg;
-}
-
-char* zephyr_tzc_deescape_str(const char *message)
-{
- gsize pos = 0, pos2 = 0;
- char *newmsg;
-
- if (message && *message) {
- newmsg = g_new0(char,strlen(message)+1);
- while(pos < strlen(message)) {
- if (message[pos]=='\\') {
- pos++;
- }
- newmsg[pos2] = message[pos];
- pos++;pos2++;
- }
- newmsg[pos2]='\0';
- } else {
- newmsg = g_strdup("");
+ newmsg[pos2] = message[pos];
}
return newmsg;
@@ -1825,10 +1817,10 @@
return TRUE;
}
-char *local_zephyr_normalize(zephyr_account *zephyr,const char *orig)
+/* Basically the inverse of zephyr_strip_local_realm */
+static char *
+local_zephyr_normalize(const zephyr_account *zephyr, const char *orig)
{
- /* Basically the inverse of zephyr_strip_local_realm */
-
if (*orig == '\0' || strchr(orig, '@')) {
return g_strdup(orig);
}