]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
optimize textbox reflowing
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 16 Oct 2014 11:42:11 +0000 (13:42 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 16 Oct 2014 11:48:28 +0000 (13:48 +0200)
textbox.c

index 8bbd409d76bf0ec1919ebc40f6b9c49c613f0f5a..b6fedd6bfbf809c423cd9243fc71d305702e5be1 100644 (file)
--- a/textbox.c
+++ b/textbox.c
@@ -189,7 +189,7 @@ static void doReflow(const char * text, char ** resultPtr, int width,
            result = malloc(strlen(text) + (strlen(text) / (width - 1)) + 2);
        } else
            result = malloc(strlen(text) * 2 + 2);
-       *result = '\0';
+       *resultPtr = result;
     }
        
     memset(&ps,0,sizeof(mbstate_t));
@@ -204,8 +204,9 @@ static void doReflow(const char * text, char ** resultPtr, int width,
            len = wstrlen(text, end - text);
            if (len <= width) {
                if (result) {
-                   strncat(result, text, end - text);
-                   strcat(result, "\n");
+                   memcpy(result, text, end - text);
+                   result += end - text;
+                   *result++ = '\n';
                    height++;
                }
 
@@ -247,8 +248,9 @@ static void doReflow(const char * text, char ** resultPtr, int width,
 #endif                                 
                if (spcptr) chptr = spcptr;
                if (result) {
-                   strncat(result, text, chptr - text );
-                   strcat(result, "\n");
+                   memcpy(result, text, chptr - text);
+                   result += chptr - text;
+                   *result++ = '\n';
                    height++;
                }
 
@@ -263,8 +265,10 @@ static void doReflow(const char * text, char ** resultPtr, int width,
        }
     }
 
+    if (result)
+       *result = '\0';
+
     if (badness) *badness = howbad;
-    if (resultPtr) *resultPtr = result;
     if (heightPtr) *heightPtr = height;
 #ifdef DEBUG_WRAP
     fprintf(stderr, "width %d, badness %d, height %d\n",width, howbad, height);