From 787aec6f959fdcb54dbe7d2ca779bdb2cdcf1ec2 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 22 Jul 2021 14:32:14 +0100 Subject: [PATCH] Remove in-memory duplication of pebbles' code text --- src/calc.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/calc.c b/src/calc.c index 8b1ed7b..ca0b080 100644 --- a/src/calc.c +++ b/src/calc.c @@ -24,11 +24,9 @@ int current_script_num; // PebbLisp environment static struct Environment env; -// Live code text -char current_code_text[SMAX_LENGTH] = ""; - -// The actual displayed code text +// The displayed/processed code text char displayed_code[SMAX_LENGTH] = ""; +int excess_chars = 1; // The result of execution #define RESULT_PREFIX "R:" @@ -54,9 +52,7 @@ const char *tokens[] = { static int8_t selected_token = 1; const char *func_tokens[] = { - "spent ", "window ", - "max ", "min ", - "sq ", "cube ", "exp ", + "window ", "sc ", "cw ", "pw ", "rw ", "atl ", "utl ", "sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub " }; @@ -73,8 +69,8 @@ const uint32_t outbox_size = 1024; static inline int8_t tokenCount() { return using_func_tokens? - sizeof(func_tokens) / sizeof(func_tokens[0]) : - sizeof(tokens) / sizeof(tokens[0]); + sizeof(func_tokens) / sizeof(func_tokens[0]) : + sizeof(tokens) / sizeof(tokens[0]); } // Get current token from tokens[] or func_tokens[], as appropriate @@ -84,19 +80,28 @@ static inline const char* getToken(int8_t n) return using_func_tokens? func_tokens[t] : tokens[t]; } +static void trim(size_t len) +{ + size_t end = strlen(displayed_code) - 1; + for (size_t i = 0; i < len; i++) { + displayed_code[end - i] = '\0'; + } +} + // Update the current code text with the contents of `current_code_text`, // and add the current selected_token to the end static void updateText() { + trim(excess_chars); const char *token = getToken(selected_token); - strcpy(displayed_code, current_code_text); - - strcat(displayed_code, + const char* add = token[0] == ' ' ? "_": // Display space as underscore token[0] == '\n'? "\\n": // Display newline as \n - token // Display others literally - ); + token; // Display others literally + + strcat(displayed_code, add); + excess_chars = strlen(add); text_layer_set_text(s_input_text_layer, displayed_code); } @@ -174,10 +179,11 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context) } if(!using_func_tokens) { - if(current_code_text[0] == '\0') { + if(displayed_code[0] == '\0') { window_stack_remove(window_stack_get_top_window(), true); } else { - current_code_text[strlen(current_code_text) - 1] = '\0'; + trim(excess_chars + 1); + excess_chars = 0; updateText(); } } else { @@ -190,7 +196,7 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context) // Adds the current token string to the main code string static void add_token() { - strcat(current_code_text, getToken(selected_token)); + strcat(displayed_code, getToken(selected_token)); selected_token = 0; if(using_func_tokens) { using_func_tokens = false; @@ -205,9 +211,13 @@ static void add_token() // Calculate result, display it and reset static void calculate() { - Object obj = parseEval(current_code_text, &env); char temp[RESULT_LENGTH + 2] = ""; + trim(excess_chars); + excess_chars = 0; + Object obj = parseEval(displayed_code, &env); + updateText(); + stringNObj(temp, &obj, RESULT_LENGTH); if(obj.type == TYPE_ERROR) { text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD)); @@ -307,12 +317,12 @@ static void code_window_load(Window *window) // If possible, load the previous code text #ifdef FORCE_TEXT - strncpy(current_code_text, FORCE_TEXT, SMAX_LENGTH); + strncpy(displayed_code, FORCE_TEXT, SMAX_LENGTH); updateText(); adjustFont(); #else if(persist_exists(current_script_num)) { - persist_read_string(current_script_num, current_code_text, SMAX_LENGTH); + persist_read_string(current_script_num, displayed_code, SMAX_LENGTH); updateText(); adjustFont(); } @@ -469,7 +479,6 @@ static void inbox_received_callback(DictionaryIterator *iter, void *context) { } char *script_text = script_tuple->value->cstring; snprintf(displayed_code, sizeof(displayed_code), "%s", script_text); - snprintf(current_code_text, sizeof(current_code_text), "%s", script_text); updateText(); } }