int main(void) { kstr s1 = kstring_new(1); ASSERT_TRUE(kstring_length(s1) == 0); ASSERT_TRUE(kstring_capacity(s1) == 1); kstr s2 = kstring_newfrom("this is a string"); kstr s3 = kstring_newfromvf("this is a %s", "string"); ASSERT_STR_EQ(kstring_content(s2), kstring_content(s3)); kstring_free(s3); kstring_free(s2); kstring_free(s1); s1 = kstring_newfrom("test"); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); kstring_append(s1, "ing"); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); ASSERT_STR_EQ(kstring_content(s1), "testing"); kstring_assimilate(s1, kstring_newfrom(" 1 2 3")); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); ASSERT_STR_EQ(kstring_content(s1), "testing 1 2 3"); kstring_free(s1); s1 = kstring_newfrom("testing"); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); kstring_appendvf(s1, " %d %d %d", 1, 2, 3); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); ASSERT_STR_EQ(kstring_content(s1), "testing 1 2 3"); size_t cap = kstring_capacity(s1); kstring_realloc(s1, cap*2); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); ASSERT_TRUE(kstring_capacity(s1) == cap*2); ASSERT_STR_EQ(kstring_content(s1), "testing 1 2 3"); kstring_free(s1); s1 = kstring_newfrom(" test "); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); kstring_stripl(s1); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); ASSERT_STR_EQ(kstring_content(s1), "test "); kstring_stripr(s1); ASSERT_TRUE(kstring_content(s1)[kstring_length(s1)] == '\0'); ASSERT_STR_EQ(kstring_content(s1), "test"); kstring_free(s1); fprintf(stderr, "Passed %d asserts\n", asserts); return 0; }
static void string_no_len_version() { struct kstring *s; s = kstring_new("hello"); assert_string_equal(kstring_cstr(s), "hello"); kstring_append(s, "foo"); assert_string_equal(kstring_cstr(s), "hellofoo"); kstring_insert(s, 2, "bar"); assert_string_equal(kstring_cstr(s), "hebarllofoo"); kstring_free(s); }
char* genlua_emit(emit_desc* emit, fnum when) { char* result; kstr s = kstring_newfrom("Vis.emit("); kstring_append(s, "Vis.flist, "); kstring_appendvf(s, "%d, Vis.frames2msec(%d), ", emit->n, when); kstring_appendvf(s, "%g, %g, %g, %g, ", emit->x, emit->y, emit->ux, emit->uy); kstring_appendvf(s, "%g, %g, ", emit->rad, emit->urad); kstring_appendvf(s, "%g, %g, ", emit->ds, emit->uds); kstring_appendvf(s, "%g, %g, ", emit->theta, emit->utheta); kstring_appendvf(s, "Vis.frames2msec(%d), Vis.frames2msec(%d), ", emit->life, emit->ulife); kstring_appendvf(s, "%g, %g, %g, ", (double)emit->r, (double)emit->g, (double)emit->b); kstring_appendvf(s, "%g, %g, %g, ", (double)emit->ur, (double)emit->ug, (double)emit->ub); kstring_appendvf(s, "%s, ", genlua_force(emit->force)); kstring_appendvf(s, "%s, ", genlua_limit(emit->limit)); kstring_appendvf(s, "%s)", genlua_blender(emit->blender)); result = dupstr(kstring_content(s)); kstring_free(s); return result; }