[balsa: 1/2] address: Simplify vcard_strsplit()




commit 3bc1ac9fca95079f9c43ecd4abecc3043c0b2115
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Thu Dec 31 17:21:27 2020 -0500

    address: Simplify vcard_strsplit()
    
    by using a GPtrArray to make a GStrv array, instead of first collecting
    strings in a GSlist.
    
    modified:   libbalsa/address.c

 libbalsa/address.c | 65 ++++++++++++++++++++----------------------------------
 1 file changed, 24 insertions(+), 41 deletions(-)
---
diff --git a/libbalsa/address.c b/libbalsa/address.c
index 59bbb5319..aa8e533ba 100644
--- a/libbalsa/address.c
+++ b/libbalsa/address.c
@@ -260,67 +260,50 @@ vcard_charset_to_utf8(gchar * str, const gchar * charset)
 /* mainly copied from g_strsplit, but (a) with the fixed delimiter ';'
  * (b) ignoring '\;' sequences (c) always returning as many elements as
  * possible and (d) unescape '\;' sequences in the resulting array */
-static gchar **
-vcard_strsplit(const gchar * string)
+static char **
+vcard_strsplit(const char *string)
 {
-    GSList *string_list = NULL, *slist;
-    gchar **str_array, *s;
-    guint n = 0;
-    const gchar *remainder;
-
-    g_return_val_if_fail(string != NULL, NULL);
+    GPtrArray *string_list;
+    char *s;
+    const char *remainder;
+    char **str;
 
+    string_list = g_ptr_array_new();
     remainder = string;
     s = strchr(remainder, ';');
-    while (s && s > remainder && s[-1] == '\\')
+    while (s != NULL && s > remainder && s[-1] == '\\')
        s = strchr(s + 1, ';');
 
-    while (s) {
+    while (s != NULL) {
        gsize len;
 
        len = s - remainder;
         /* skip empty fields: */
-        if (len > 0) {
-            string_list =
-                g_slist_prepend(string_list, g_strndup(remainder, len));
-            n++;
-        }
+        if (len > 0)
+            g_ptr_array_add(string_list, g_strndup(remainder, len));
        remainder = s + 1;
        s = strchr(remainder, ';');
-       while (s && s > remainder && s[-1] == '\\')
+        while (s != NULL && s > remainder && s[-1] == '\\')
            s = strchr(s + 1, ';');
     }
 
-    if (*string) {
-       n++;
-       string_list = g_slist_prepend(string_list, g_strdup(remainder));
-    }
-
-    if (n < 5U) {
-       str_array = g_new0(gchar*, 5U);
-    } else {
-       str_array = g_new0(gchar*, n + 1);
-    }
+    if (*string != '\0')
+        g_ptr_array_add(string_list, g_strdup(remainder));
 
-    for (slist = string_list; slist; slist = slist->next) {
-       gchar * str = (gchar *) slist->data;
-       gchar * p;
+    /* NULL-terminate, and NULL-pad to 5 entries if necessary: */
+    do {
+        g_ptr_array_add(string_list, NULL);
+    } while (string_list->len < 5);
 
-       while ((p = strstr(str, "\\;"))) {
-           gchar * newstr = g_malloc(strlen(str));
+    for (str = (char **) string_list->pdata; *str != NULL; str++) {
+        char *p;
 
-           strncpy(newstr, str, p - str);
-           strcpy(newstr + (p - str), p + 1);
-           g_free(str);
-           str = newstr;
-       }
-       str_array[--n] = str;
+        /* Unescape any escaped ';': */
+        while ((p = strstr(*str, "\\;")) != NULL)
+            memmove(p, p + 1, strlen(p + 1) + 1);
     }
-    g_assert(n == 0);
-
-    g_slist_free(string_list);
 
-    return str_array;
+    return (char **) g_ptr_array_free(string_list, FALSE);
 }
 
 /*


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]