From 6ebf350194a4580bf5182b6bbee0db594c021c78 Mon Sep 17 00:00:00 2001 From: corvid Date: Fri, 5 Jun 2009 23:16:57 +0200 Subject: faster expand tabs --- src/misc.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/misc.c b/src/misc.c index c28c359c..90d85983 100644 --- a/src/misc.c +++ b/src/misc.c @@ -54,30 +54,35 @@ char *a_Misc_escape_chars(const char *str, const char *esc_set) */ 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(""); + + 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++; + } - 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; } -- cgit v1.2.3