Skip to content

Commit

Permalink
Improve memory management and string handling in ItemInput class (#256)
Browse files Browse the repository at this point in the history
- **Bug Fixes**
- Improved memory management and string handling in the input
functionality.
- Enhanced efficiency of string operations when typing and clearing
input values.
  • Loading branch information
forntoh authored Oct 27, 2024
2 parents 59c4568 + 894f51c commit 5155935
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/ItemInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,18 @@ class ItemInput : public MenuItem {
*/
void typeChar(MenuRenderer* renderer, const unsigned char character) {
uint8_t length = strlen(value);
char* buf = new char[length + 2];
if (cursor < length) {
char start[length];
char end[length];
char* joined = new char[length + 2];
substring(value, 0, cursor, start);
substring(value, cursor, length - cursor, end);
concat(start, character, end, joined);
value = joined;
concat(start, character, end, buf);
} else {
char* buf = new char[length + 2];
concat(value, character, buf);
value = buf;
}
delete[] value;
value = buf;
cursor++;
uint8_t viewSize = getViewSize(renderer);
if (cursor > (view + viewSize - 1)) {
Expand All @@ -293,7 +292,7 @@ class ItemInput : public MenuItem {
* @brief Clear the value of the input field
*/
void clear(MenuRenderer* renderer) {
value = (char*)"";
value[0] = '\0';
draw(renderer);
renderer->drawBlinker();
// Log
Expand Down

0 comments on commit 5155935

Please sign in to comment.