diff --git a/apps/shared/toolbox_helpers.cpp b/apps/shared/toolbox_helpers.cpp index 33c7b4461..ee39e138d 100644 --- a/apps/shared/toolbox_helpers.cpp +++ b/apps/shared/toolbox_helpers.cpp @@ -19,15 +19,16 @@ int CursorIndexInCommandText(const char * text) { return strlen(text); } -void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize) { - TextToInsertForCommandText(I18n::translate(message), buffer, bufferSize); +void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize, bool replaceArgsWithEmptyChar) { + TextToInsertForCommandText(I18n::translate(message), buffer, bufferSize, replaceArgsWithEmptyChar); } -void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize) { +void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize, bool replaceArgsWithEmptyChar) { int currentNewTextIndex = 0; int numberOfOpenParentheses = 0; int numberOfOpenBrackets = 0; bool insideQuote = false; + bool argumentAlreadyReplaced = false; size_t commandLength = strlen(command); for (size_t i = 0; i < commandLength; i++) { if (command[i] == ')') { @@ -41,7 +42,15 @@ void TextToInsertForCommandText(const char * command, char * buffer, int bufferS || (numberOfOpenBrackets > 0 && (command[i] == ',' || command[i] == '[' || command[i] == ']'))) && (!insideQuote || command[i] == '\'')) { assert(currentNewTextIndex < bufferSize); + if (argumentAlreadyReplaced) { + argumentAlreadyReplaced = false; + } buffer[currentNewTextIndex++] = command[i]; + } else { + if (replaceArgsWithEmptyChar && !argumentAlreadyReplaced) { + buffer[currentNewTextIndex++] = Ion::Charset::Empty; + argumentAlreadyReplaced = true; + } } if (command[i] == '(') { numberOfOpenParentheses++; @@ -61,18 +70,7 @@ void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer } void TextToParseIntoLayoutForCommandText(const char * command, char * buffer, int bufferSize) { - TextToInsertForCommandText(command, buffer, bufferSize); - size_t bufferLength = strlen(buffer); - for (size_t i = 0; i < bufferLength; i++) { - if (buffer[i] == '(' || buffer[i] == ',' || (i < bufferLength - 1 && buffer[i] == '[' && buffer[i+1] == ']')) { - // Shift the buffer to make room for the new char. Use memmove to avoid - // overwritting. - memmove(&buffer[i+2], &buffer[i+1], bufferLength - (i+1) + 1); - bufferLength++; - i++; - buffer[i] = Ion::Charset::Empty; - } - } + TextToInsertForCommandText(command, buffer, bufferSize, true); } } diff --git a/apps/shared/toolbox_helpers.h b/apps/shared/toolbox_helpers.h index 0065993a2..ae8226644 100644 --- a/apps/shared/toolbox_helpers.h +++ b/apps/shared/toolbox_helpers.h @@ -13,8 +13,8 @@ int CursorIndexInCommandText(const char * text); * - The end of the text */ -void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize); -void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize); +void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize, bool replaceArgsWithEmptyChar = false); +void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize, bool replaceArgsWithEmptyChar = false); /* Removes the arguments from a command: * - Removes text between parentheses or brackets, except commas */ void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer, int bufferSize); diff --git a/poincare/src/layout_engine.cpp b/poincare/src/layout_engine.cpp index 9b2e43ddb..48f21fd80 100644 --- a/poincare/src/layout_engine.cpp +++ b/poincare/src/layout_engine.cpp @@ -37,13 +37,15 @@ ExpressionLayout * LayoutEngine::createPrefixLayout(const Expression * expressio result->addOrMergeChildAtIndex(createStringLayout(operatorName, strlen(operatorName)), 0, true); // Create the layout of arguments separated by commas. - HorizontalLayout * args = new HorizontalLayout(); + ExpressionLayout * args = nullptr; int numberOfOperands = expression->numberOfOperands(); if (numberOfOperands > 0) { - args->addOrMergeChildAtIndex(expression->operand(0)->createLayout(floatDisplayMode, complexFormat), 0, true); + args = new HorizontalLayout(); + HorizontalLayout * horizontalArgs = static_cast(args); + horizontalArgs->addOrMergeChildAtIndex(expression->operand(0)->createLayout(floatDisplayMode, complexFormat), 0, true); for (int i = 1; i < numberOfOperands; i++) { - args->addChildAtIndex(new CharLayout(','), args->numberOfChildren()); - args->addOrMergeChildAtIndex(expression->operand(i)->createLayout(floatDisplayMode, complexFormat), args->numberOfChildren(), true); + horizontalArgs->addChildAtIndex(new CharLayout(','), args->numberOfChildren()); + horizontalArgs->addOrMergeChildAtIndex(expression->operand(i)->createLayout(floatDisplayMode, complexFormat), horizontalArgs->numberOfChildren(), true); } } // Add the parenthesed arguments.