diff options
author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2009-05-25 18:42:24 +0200 |
---|---|---|
committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2009-05-25 18:42:24 +0200 |
commit | 50260728b2e2d2c9e61a13b54b6b973bdc48fae0 (patch) | |
tree | 63b462a3d4cbbf445665714812331b2f7e968204 /src/misc.c | |
parent | 6d62e8cf2ed9fe4eda942a59ba140b151b82b228 (diff) |
make tab expansion for plain text utf8 aware
In discussion with corvid <corvid@lavabit.com>.
Diffstat (limited to 'src/misc.c')
-rw-r--r-- | src/misc.c | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -16,6 +16,7 @@ #include <string.h> #include <ctype.h> +#include "utf8.hh" #include "msg.h" #include "misc.h" @@ -47,7 +48,6 @@ char *a_Misc_escape_chars(const char *str, const char *esc_set) return p; } - #define TAB_SIZE 8 /* * Takes a string and converts any tabs to spaces. @@ -55,23 +55,27 @@ 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, j, pos, old_pos; + int i = 0, j, pos = 0, old_pos, char_len; + uint_t code; char *val; - if (len) { - for (pos = 0, i = 0; i < len; i++) { - if (str[i] == '\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_c(New, str[i]); - 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; } + val = New->str; dStr_free(New, FALSE); return val; |