diff --git a/include/tim.h b/include/tim.h index 60ddba0..104a834 100644 --- a/include/tim.h +++ b/include/tim.h @@ -301,14 +301,10 @@ bool tim_radiobutton(cstr txt, i32* state, i32 v, i32 x, i32 y, i32 w, TimStyle TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style); /// @param e uninitialized state -/// @param capacity in bytes +/// @param buffer an array +/// @param capacity buffer size in bytes /// @param initial_content may be NULL -void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content); - -static inline void TimEditState_destroy(TimEditState* e) { - if(!e) return; - free(e->s); -} +void TimEditState_construct(TimEditState* e, char* buffer, i32 capacity, cstr initial_content); void TimEditState_insert(TimEditState* e, cstr s); void TimEditState_delete(TimEditState* e); diff --git a/src/edit.c b/src/edit.c index 555ec35..e80e303 100755 --- a/src/edit.c +++ b/src/edit.c @@ -1,13 +1,16 @@ #include "tim.h" -void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content){ +void TimEditState_construct(TimEditState* e, char* buf, i32 capacity, cstr initial_content){ e->masked = false; e->length = initial_content ? tim_utf8_len(initial_content) : 0; e->cursor = e->length; e->capacity = capacity; - e->s = (char*)malloc(capacity + 1); - i32 byte_len = strlen(initial_content); - memcpy(e->s, initial_content, byte_len); + e->s = buf; + i32 byte_len = 0; + if(e->length > 0){ + byte_len = strlen(initial_content); + memcpy(e->s, initial_content, byte_len); + } e->s[byte_len] = 0; } diff --git a/test/test.c b/test/test.c index d6a8887..0737c71 100644 --- a/test/test.c +++ b/test/test.c @@ -126,8 +126,10 @@ static inline void test_screen(TimEvent* e) { } i32 main(void) { - TimEditState_construct(&ed1, 32, "Edit 1"); - TimEditState_construct(&ed2, 32, ""); + char ed1_buf[32]; + char ed2_buf[32]; + TimEditState_construct(&ed1, ed1_buf, ARRAY_SIZE(ed1_buf), "Edit 1"); + TimEditState_construct(&ed2, ed2_buf, ARRAY_SIZE(ed2_buf), NULL); while (tim_run(1.5)) { test_screen(&tim->event); @@ -135,9 +137,6 @@ i32 main(void) { break; } } - - TimEditState_destroy(&ed1); - TimEditState_destroy(&ed2); return 0; }