mirror of
https://github.com/json-c/json-c.git
synced 2026-04-05 13:29:06 +08:00
Restore sprintbuf(), add macro for string literals
Hawciz pointed out that the previous commit modifies the public interface of printbuf. Per his suggestion, sprintbuf() was restored and a new pair of macros was added that wraps printbuf_memappend(). Using a wrapper macro instead of modifying sprintbuf() also reduces function call overhead, bringing total performance gains to approximately 400%.
This commit is contained in:
@@ -17,7 +17,7 @@ static void test_basic_printbuf_memset()
|
||||
|
||||
printf("%s: starting test\n", __func__);
|
||||
pb = printbuf_new();
|
||||
sprintbuf(pb, "blue:1");
|
||||
sprintbuf(pb, "blue:%d", 1);
|
||||
printbuf_memset(pb, -1, 'x', 52);
|
||||
printf("Buffer contents:%.*s\n", printbuf_length(pb), pb->buf);
|
||||
printbuf_free(pb);
|
||||
@@ -106,6 +106,49 @@ static void test_printbuf_memappend(int *before_resize)
|
||||
printf("Append to just after resize: %d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
|
||||
free(data);
|
||||
printbuf_free(pb);
|
||||
|
||||
#define SA_TEST_STR "XXXXXXXXXXXXXXXX"
|
||||
pb = printbuf_new();
|
||||
printbuf_strappend(pb, SA_TEST_STR);
|
||||
printf("Buffer size after printbuf_strappend(): %d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
printbuf_free(pb);
|
||||
#undef SA_TEST_STR
|
||||
|
||||
printf("%s: end test\n", __func__);
|
||||
}
|
||||
|
||||
static void test_sprintbuf(int before_resize);
|
||||
static void test_sprintbuf(int before_resize)
|
||||
{
|
||||
struct printbuf *pb;
|
||||
|
||||
printf("%s: starting test\n", __func__);
|
||||
pb = printbuf_new();
|
||||
printf("Buffer length: %d\n", printbuf_length(pb));
|
||||
|
||||
char *data = malloc(before_resize + 1 + 1);
|
||||
memset(data, 'X', before_resize + 1 + 1);
|
||||
data[before_resize + 1] = '\0';
|
||||
sprintbuf(pb, "%s", data);
|
||||
free(data);
|
||||
printf("sprintbuf to just after resize(%d+1): %d, [%s], strlen(buf)=%d\n", before_resize, printbuf_length(pb), pb->buf, (int)strlen(pb->buf));
|
||||
|
||||
printbuf_reset(pb);
|
||||
sprintbuf(pb, "plain");
|
||||
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
|
||||
sprintbuf(pb, "%d", 1);
|
||||
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
|
||||
sprintbuf(pb, "%d", INT_MAX);
|
||||
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
|
||||
sprintbuf(pb, "%d", INT_MIN);
|
||||
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
|
||||
sprintbuf(pb, "%s", "%s");
|
||||
printf("%d, [%s]\n", printbuf_length(pb), pb->buf);
|
||||
|
||||
printbuf_free(pb);
|
||||
printf("%s: end test\n", __func__);
|
||||
@@ -123,6 +166,8 @@ int main(int argc, char **argv)
|
||||
printf("========================================\n");
|
||||
test_printbuf_memappend(&before_resize);
|
||||
printf("========================================\n");
|
||||
test_sprintbuf(before_resize);
|
||||
printf("========================================\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user