diff options
author | corvid <corvid@lavabit.com> | 2009-06-05 23:16:57 +0200 |
---|---|---|
committer | corvid <corvid@lavabit.com> | 2009-06-05 23:16:57 +0200 |
commit | 6ebf350194a4580bf5182b6bbee0db594c021c78 (patch) | |
tree | 8943ef33ae323bd635123b33b6d5c341736bf89c /src/misc.c | |
parent | cbe9d121c63d49e559f678fa5cd1a3b87965bdbb (diff) |
faster expand tabs
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 37 |
1 files changed, 21 insertions, 16 deletions
@@ -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; } |