strbuffer: Optimize string termination

Do not zero all of the memory, just the first unused byte to make the
string null terminated.
This commit is contained in:
Petri Lehtinen 2009-06-30 14:39:55 +03:00
parent b724c2b122
commit 55af26f05a
2 changed files with 5 additions and 6 deletions

View File

@ -16,7 +16,8 @@ int strbuffer_init(strbuffer_t *strbuff)
if(!strbuff->value) if(!strbuff->value)
return -1; return -1;
memset(strbuff->value, 0, strbuff->size); /* initialize to empty */
strbuff->value[0] = '\0';
return 0; return 0;
} }
@ -55,13 +56,11 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size)
strbuff->value = realloc(strbuff->value, strbuff->size); strbuff->value = realloc(strbuff->value, strbuff->size);
if(!strbuff->value) if(!strbuff->value)
return -1; return -1;
memset(strbuff->value + strbuff->length + size, 0,
strbuff->size - strbuff->length - size);
} }
memcpy(strbuff->value + strbuff->length, data, size); memcpy(strbuff->value + strbuff->length, data, size);
strbuff->length += size; strbuff->length += size;
strbuff->value[strbuff->length] = '\0';
return 0; return 0;
} }

View File

@ -3,8 +3,8 @@
typedef struct { typedef struct {
char *value; char *value;
int length; int length; /* bytes used */
int size; int size; /* bytes allocated */
} strbuffer_t; } strbuffer_t;
int strbuffer_init(strbuffer_t *strbuff); int strbuffer_init(strbuffer_t *strbuff);