[gmime] Optimized g_strstrip (g_strdup (str))



commit 4953832734bb1ffb68e4c56f75dae6d12ebcde89
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Sun Feb 19 11:49:14 2012 -0500

    Optimized g_strstrip (g_strdup (str))
    
    2012-02-19  Jeffrey Stedfast  <fejj gnome org>
    
    	* gmime/gmime-common.c (g_mime_strdup_trim): New function that
    	optimizes g_strstrip combined with g_strdup in both speed and
    	memory usage (g_strstrip won't shrink the size of the dup'd
    	memory).

 ChangeLog             |    7 +++++++
 gmime/gmime-common.c  |   37 +++++++++++++++++++++++++++++++++++++
 gmime/gmime-common.h  |    2 ++
 gmime/gmime-message.c |    7 ++++---
 gmime/gmime-part.c    |   12 ++++++------
 5 files changed, 56 insertions(+), 9 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 64891ec..e7cf059 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2012-02-19  Jeffrey Stedfast  <fejj gnome org>
+
+	* gmime/gmime-common.c (g_mime_strdup_trim): New function that
+	optimizes g_strstrip combined with g_strdup in both speed and
+	memory usage (g_strstrip won't shrink the size of the dup'd
+	memory).
+
 2012-02-18  Jeffrey Stedfast  <fejj gnome org>
 
 	* configure.ac: Add a --enable-smime flag which defaults to "no".
diff --git a/gmime/gmime-common.c b/gmime/gmime-common.c
index 03506f9..a604090 100644
--- a/gmime/gmime-common.c
+++ b/gmime/gmime-common.c
@@ -25,6 +25,7 @@
 
 #include <string.h>
 
+#include "gmime-table-private.h"
 #include "gmime-common.h"
 
 #ifndef g_tolower
@@ -51,3 +52,39 @@ g_mime_strcase_hash (gconstpointer key)
 	
 	return h;
 }
+
+
+/**
+ * g_mime_strdup_trim:
+ * @str: The string to duplicate and trim
+ *
+ * Duplicates the given input string while also trimming leading and
+ * trailing whitespace.
+ *
+ * Returns a duplicate string, minus any leading and trailing
+ * whitespace that the original string may have contained.
+ **/
+char *
+g_mime_strdup_trim (const char *str)
+{
+	register const char *inptr = str;
+	const char *start, *end;
+	
+	while (is_lwsp (*inptr))
+		inptr++;
+	
+	start = inptr;
+	end = inptr;
+	
+	while (*inptr) {
+		while (*inptr && !is_lwsp (*inptr))
+			inptr++;
+		
+		end = inptr;
+		
+		while (is_lwsp (*inptr))
+			inptr++;
+	}
+	
+	return g_strndup (start, (size_t) (end - start));
+}
diff --git a/gmime/gmime-common.h b/gmime/gmime-common.h
index 09b9569..afbf7c4 100644
--- a/gmime/gmime-common.h
+++ b/gmime/gmime-common.h
@@ -30,6 +30,8 @@ G_GNUC_INTERNAL int g_mime_strcase_equal (gconstpointer v, gconstpointer v2);
 
 G_GNUC_INTERNAL guint g_mime_strcase_hash (gconstpointer key);
 
+G_GNUC_INTERNAL char *g_mime_strdup_trim (const char *str);
+
 G_END_DECLS
 
 #endif /* __GMIME_COMMON_H__ */
diff --git a/gmime/gmime-message.c b/gmime/gmime-message.c
index b6ae30a..33f64d7 100644
--- a/gmime/gmime-message.c
+++ b/gmime/gmime-message.c
@@ -33,6 +33,7 @@
 #include "gmime-multipart-encrypted.h"
 #include "gmime-part.h"
 #include "gmime-utils.h"
+#include "gmime-common.h"
 #include "gmime-stream-mem.h"
 #include "gmime-table-private.h"
 #include "gmime-parse-utils.h"
@@ -1114,7 +1115,7 @@ g_mime_message_set_reply_to (GMimeMessage *message, const char *reply_to)
 	g_return_if_fail (reply_to != NULL);
 	
 	g_free (message->reply_to);
-	message->reply_to = g_strstrip (g_strdup (reply_to));
+	message->reply_to = g_mime_strdup_trim (reply_to);
 	
 	g_mime_header_list_set (GMIME_OBJECT (message)->headers, "Reply-To", message->reply_to);
 }
@@ -1270,7 +1271,7 @@ g_mime_message_set_subject (GMimeMessage *message, const char *subject)
 	g_return_if_fail (subject != NULL);
 	
 	g_free (message->subject);
-	message->subject = g_strstrip (g_strdup (subject));
+	message->subject = g_mime_strdup_trim (subject);
 	
 	encoded = g_mime_utils_header_encode_text (message->subject);
 	g_mime_object_set_header (GMIME_OBJECT (message), "Subject", encoded);
@@ -1401,7 +1402,7 @@ g_mime_message_set_message_id (GMimeMessage *message, const char *message_id)
 	g_return_if_fail (message_id != NULL);
 	
 	g_free (message->message_id);
-	message->message_id = g_strstrip (g_strdup (message_id));
+	message->message_id = g_mime_strdup_trim (message_id);
 	
 	msgid = g_strdup_printf ("<%s>", message_id);
 	g_mime_object_set_header (GMIME_OBJECT (message), "Message-Id", msgid);
diff --git a/gmime/gmime-part.c b/gmime/gmime-part.c
index 1c2d7df..4bcc8f9 100644
--- a/gmime/gmime-part.c
+++ b/gmime/gmime-part.c
@@ -29,6 +29,7 @@
 
 #include "gmime-part.h"
 #include "gmime-utils.h"
+#include "gmime-common.h"
 #include "gmime-stream-mem.h"
 #include "gmime-stream-null.h"
 #include "gmime-stream-filter.h"
@@ -175,23 +176,22 @@ process_header (GMimeObject *object, const char *header, const char *value)
 	
 	switch (i) {
 	case HEADER_CONTENT_TRANSFER_ENCODING:
-		text = g_alloca (strlen (value) + 1);
-		strcpy (text, value);
-		g_strstrip (text);
+		text = g_mime_strdup_trim (value);
 		mime_part->encoding = g_mime_content_encoding_from_string (text);
+		g_free (text);
 		break;
 	case HEADER_CONTENT_DESCRIPTION:
 		/* FIXME: we should decode this */
 		g_free (mime_part->content_description);
-		mime_part->content_description = g_strstrip (g_strdup (value));
+		mime_part->content_description = g_mime_strdup_trim (value);
 		break;
 	case HEADER_CONTENT_LOCATION:
 		g_free (mime_part->content_location);
-		mime_part->content_location = g_strstrip (g_strdup (value));
+		mime_part->content_location = g_mime_strdup_trim (value);
 		break;
 	case HEADER_CONTENT_MD5:
 		g_free (mime_part->content_md5);
-		mime_part->content_md5 = g_strstrip (g_strdup (value));
+		mime_part->content_md5 = g_mime_strdup_trim (value);
 		break;
 	default:
 		return FALSE;



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