[libgda] Optimized gda_data_model_dump() speed when truncating lines



commit 8e1756319d7aa5e454fbf2b399a7a95d5a198982
Author: Vivien Malerba <malerba gnome-db org>
Date:   Tue Feb 22 21:14:38 2011 +0100

    Optimized gda_data_model_dump() speed when truncating lines

 libgda/gda-data-model.c |   39 ++++++++++++++++++++++++++-------------
 1 files changed, 26 insertions(+), 13 deletions(-)
---
diff --git a/libgda/gda-data-model.c b/libgda/gda-data-model.c
index 9c5ee8c..2f7c4b2 100644
--- a/libgda/gda-data-model.c
+++ b/libgda/gda-data-model.c
@@ -2491,27 +2491,40 @@ real_gda_data_model_dump_as_string (GdaDataModel *model, gboolean dump_attribute
 		g_string_free (string, FALSE);
 		if (max_width > 0) {
 			/* truncate all lines */
-			gchar *ptr;
+			GString *ns;
+			ns = g_string_sized_new (strlen (str));
+
+			gchar *ptr, *sptr;
 			gint len; /* number of characters since start of line */
-			for (ptr = str, len=0;
+			for (ptr = str, sptr = ptr, len=0;
 			     *ptr;
 			     ptr = g_utf8_next_char (ptr), len++) {
-				if (*ptr == '\n')
+				if (*ptr == '\n') {
+					*ptr = 0;
 					len = 0;
-				else if (len >= max_width) {
-					gchar *sptr;
+					g_string_append (ns, sptr);
+					g_string_append_c (ns, '\n');
 					*ptr = '\n';
-					ptr++;
-					sptr = ptr;
-					for (; *ptr && (*ptr != '\n'); ptr++) ;
-					if (*ptr == '\n')
-						ptr++;
-					if (ptr > sptr)
-						g_memmove (sptr, ptr, strlen (ptr) + 1);
+					sptr = ptr+1;
+				}
+				else if (len >= max_width) {
+					gchar c;
+					c = *ptr;
+					*ptr = 0;
 					len = 0;
-					ptr = sptr;
+					g_string_append (ns, sptr);
+					g_string_append_c (ns, '\n');
+					*ptr = c;
+					for (; *ptr && (*ptr != '\n');
+					     ptr = g_utf8_next_char (ptr));
+					if (!*ptr)
+						break;
+					sptr = ptr+1;
 				}
 			}
+			g_free (str);
+			str = ns->str;
+			g_string_free (ns, FALSE);
 		}
 	}
 	else {



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