changeset 1160:ea163ab97e87

faster expand tabs
author corvid <corvid@lavabit.com>
date Fri, 05 Jun 2009 23:16:57 +0200
parents fb041759363c
children cbf3c80c6a15
files src/misc.c
diffstat 1 files changed, 21 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/misc.c	Fri Jun 05 01:22:16 2009 +0000
+++ b/src/misc.c	Fri Jun 05 23:16:57 2009 +0200
@@ -54,30 +54,35 @@
  */
 char *a_Misc_expand_tabs(const char *str, int len)
 {
-   Dstr *New = dStr_new("");
    int i = 0, j, pos = 0, old_pos, char_len;
    uint_t code;
    char *val;
 
-   while (i < len) {
-      code = a_Utf8_decode(&str[i], str + len, &char_len);
+   if (memchr(str, '\t', len) == NULL) {
+      val = dStrndup(str, len);
+   } else {
+      Dstr *New = dStr_new("");
 
-      if (code == '\t') {
-         /* Fill with whitespaces until the next tab. */
-         old_pos = pos;
-         pos += TAB_SIZE - (pos % TAB_SIZE);
-         for (j = old_pos; j < pos; j++)
-            dStr_append_c(New, ' ');
-      } else {
-         dStr_append_l(New, &str[i], char_len);
-         pos++;
+      while (i < len) {
+         code = a_Utf8_decode(&str[i], str + len, &char_len);
+
+         if (code == '\t') {
+            /* Fill with whitespaces until the next tab. */
+            old_pos = pos;
+            pos += TAB_SIZE - (pos % TAB_SIZE);
+            for (j = old_pos; j < pos; j++)
+               dStr_append_c(New, ' ');
+         } else {
+            dStr_append_l(New, &str[i], char_len);
+            pos++;
+         }
+
+         i += char_len;
       }
 
-      i += char_len;
+      val = New->str;
+      dStr_free(New, FALSE);
    }
-
-   val = New->str;
-   dStr_free(New, FALSE);
    return val;
 }