-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Less temporary memory allocations & share AF data when possible #632
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,35 +16,37 @@ typedef struct { | |
int *pos; | ||
int has_off; | ||
int positions; | ||
char txt[20]; | ||
|
||
void *userdata; | ||
textslider_slide_cb slide; | ||
} textslider; | ||
|
||
static void textslider_render(component *c) { | ||
static void render_text(component *c) { | ||
Comment on lines
-24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this method to something that does not contain the word "render." textslider_format? textslider_updatestr? EDIT: probably better to remove the textslider changes from this PR in favor of PR 773. |
||
textslider *tb = widget_get_obj(c); | ||
str txt; | ||
str_from_format(&txt, "%s ", tb->text); | ||
int off = snprintf(tb->txt, sizeof(tb->txt), "%s ", tb->text); | ||
if(tb->has_off && *tb->pos == 0) { | ||
str_append_c(&txt, "OFF"); | ||
snprintf(tb->txt + off, sizeof(tb->txt) - off, "%s", "OFF"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dislike this -- the idea with str type is that we can later convert it to utf-8. This is taking steps backwards. How about we cache the str to textslider instead ? |
||
} else { | ||
for(int i = 0; i < tb->positions; i++) { | ||
if(i + 1 > *tb->pos) { | ||
str_append_c(&txt, "|"); | ||
off += snprintf(tb->txt + off, sizeof(tb->txt) - off, "%s", "|"); | ||
} else { | ||
str_append_c(&txt, "\x7f"); | ||
off += snprintf(tb->txt + off, sizeof(tb->txt) - off, "%s", "\x7f"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
static void textslider_render(component *c) { | ||
textslider *tb = widget_get_obj(c); | ||
text_mode mode = TEXT_UNSELECTED; | ||
if(component_is_selected(c)) { | ||
mode = TEXT_SELECTED; | ||
} else if(component_is_disabled(c)) { | ||
mode = TEXT_DISABLED; | ||
} | ||
text_render(&tb->tconf, mode, c->x, c->y, c->w, c->h, str_c(&txt)); | ||
str_free(&txt); | ||
text_render(&tb->tconf, mode, c->x, c->y, c->w, c->h, tb->txt); | ||
} | ||
|
||
static int textslider_action(component *c, int action) { | ||
|
@@ -60,6 +62,9 @@ static int textslider_action(component *c, int action) { | |
if(tb->slide) { | ||
tb->slide(c, tb->userdata, *tb->pos); | ||
} | ||
|
||
render_text(c); | ||
|
||
// reset ticks so text is bright | ||
tb->ticks = 0; | ||
tb->dir = 0; | ||
|
@@ -75,6 +80,9 @@ static int textslider_action(component *c, int action) { | |
if(tb->slide) { | ||
tb->slide(c, tb->userdata, *tb->pos); | ||
} | ||
|
||
render_text(c); | ||
|
||
// reset ticks so text is bright | ||
tb->ticks = 0; | ||
tb->dir = 0; | ||
|
@@ -122,6 +130,8 @@ component *textslider_create(const text_settings *tconf, const char *text, const | |
tb->slide = cb; | ||
widget_set_obj(c, tb); | ||
|
||
render_text(c); | ||
|
||
widget_set_render_cb(c, textslider_render); | ||
widget_set_action_cb(c, textslider_action); | ||
widget_set_tick_cb(c, textslider_tick); | ||
|
@@ -133,7 +143,10 @@ component *textslider_create_bind(const text_settings *tconf, const char *text, | |
unsigned int positions, int has_off, textslider_slide_cb cb, void *userdata, | ||
int *bind) { | ||
component *c = textslider_create(tconf, text, help, positions, has_off, cb, userdata); | ||
textslider *ts = widget_get_obj(c); | ||
ts->pos = (bind) ? bind : &ts->pos_; | ||
textslider *tb = widget_get_obj(c); | ||
tb->pos = (bind) ? bind : &tb->pos_; | ||
|
||
render_text(c); | ||
|
||
return c; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,13 +286,14 @@ bool str_to_long(const str *string, long *result) { | |
return (string->data != end); | ||
} | ||
|
||
bool str_to_int(const str *string, int *result) { | ||
long value; | ||
bool got = str_to_long(string, &value); | ||
if(got) { | ||
*result = clamp_long_to_int(value); | ||
bool str_to_int(const str *string, size_t pos, int *result) { | ||
char *end; | ||
*result = strtol(string->data + pos, &end, 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please bounds-check |
||
if(string->data + pos != end) { | ||
*result = clamp_long_to_int(*result); | ||
return true; | ||
} | ||
return got; | ||
return false; | ||
} | ||
|
||
const char *str_c(const str *string) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,7 +215,7 @@ bool str_to_long(const str *string, long *m); | |
* @return true If success | ||
* @return false If failure (result value is invalid) | ||
*/ | ||
bool str_to_int(const str *string, int *m); | ||
bool str_to_int(const str *string, size_t offset, int *m); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the new |
||
|
||
/** | ||
* @brief Returns a C string compatible represantation of a string object. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The strncmp call should compare all 3 characters like the str_equal_c did before, not just the first m.