Commit a3968276 authored by deuce's avatar deuce
Browse files

Don't use strlen() every time you append to an "infinite line length

paragraph"... track the length instead.

Speeds up word_wrap() with huge messages that don't have many paragraphs.
parent 777a3191
......@@ -272,6 +272,7 @@ struct paragraph {
struct prefix prefix;
char *text;
size_t alloc_size;
size_t len;
};
/*
......@@ -296,18 +297,18 @@ static void free_paragraphs(struct paragraph *paragraph, int count)
*/
static BOOL paragraph_append(struct paragraph *paragraph, const char *bytes, size_t count)
{
size_t len = strlen(paragraph->text);
char *new_text;
while (len + count + 1 > paragraph->alloc_size) {
while (paragraph->len + count + 1 > paragraph->alloc_size) {
new_text = realloc(paragraph->text, paragraph->alloc_size * 2);
if (new_text == NULL)
return FALSE;
paragraph->text = new_text;
paragraph->alloc_size *= 2;
}
memcpy(paragraph->text + len, bytes, count);
paragraph->text[len+count] = 0;
memcpy(paragraph->text + paragraph->len, bytes, count);
paragraph->text[paragraph->len+count] = 0;
paragraph->len += count;
return TRUE;
}
......@@ -342,6 +343,7 @@ static struct paragraph *word_unwrap(char *inbuf, int oldlen, BOOL handle_quotes
}
ret = newret;
ret[paragraph].text = (char *)malloc(oldlen+1);
ret[paragraph].len = 0;
ret[paragraph].prefix.bytes = NULL;
if (ret[paragraph].text == NULL) {
free_paragraphs(ret, paragraph+1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment