aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcorvid <corvid@lavabit.com>2009-06-05 23:16:57 +0200
committercorvid <corvid@lavabit.com>2009-06-05 23:16:57 +0200
commit6ebf350194a4580bf5182b6bbee0db594c021c78 (patch)
tree8943ef33ae323bd635123b33b6d5c341736bf89c /src
parentcbe9d121c63d49e559f678fa5cd1a3b87965bdbb (diff)
faster expand tabs
Diffstat (limited to 'src')
-rw-r--r--src/misc.c37
1 files 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;
}