From a8fe8dc22e3e1a3701615b3203d794b6af18aad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 31 Jan 2018 15:37:02 +0100 Subject: [PATCH] [apps] Calculation: input text stores in cache have to be checked again when used as its length context dependent (because of 'ans' symbol) --- apps/calculation/app.cpp | 36 +++++++++++-------- apps/calculation/app.h | 1 + .../edit_expression_controller.cpp | 5 +++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/apps/calculation/app.cpp b/apps/calculation/app.cpp index a81b23d25..ef09162ed 100644 --- a/apps/calculation/app.cpp +++ b/apps/calculation/app.cpp @@ -53,25 +53,11 @@ bool App::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event e if ((event == Ion::Events::Var || event == Ion::Events::XNT) && TextFieldDelegateApp::textFieldDidReceiveEvent(textField, event)) { return true; } - /* Here, we check that the expression entered by the user can be printed with - * less than k_printedExpressionLength characters. Otherwise, we prevent the - * user from adding this expression to the calculation store. */ if (textField->isEditing() && textField->textFieldShouldFinishEditing(event)) { if (textField->text()[0] == 0) { return true; } - Expression * exp = Expression::parse(textField->text()); - if (exp == nullptr) { - textField->app()->displayWarning(I18n::Message::SyntaxError); - return true; - } - Expression::ReplaceSymbolWithExpression(&exp, Symbol::SpecialSymbols::Ans, static_cast(snapshot())->calculationStore()->ansExpression(localContext())); - char buffer[Calculation::k_printedExpressionSize]; - int length = exp->writeTextInBuffer(buffer, sizeof(buffer)); - delete exp; - /* if the buffer is totally full, it is VERY likely that writeTextInBuffer - * escaped before printing utterly the expression. */ - if (length >= Calculation::k_printedExpressionSize-1) { + if (!textInputIsCorrect(textField->text())) { displayWarning(I18n::Message::SyntaxError); return true; } @@ -79,6 +65,26 @@ bool App::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event e return false; } +bool App::textInputIsCorrect(const char * text) { + /* Here, we check that the expression entered by the user can be printed with + * less than k_printedExpressionLength characters. Otherwise, we prevent the + * user from adding this expression to the calculation store. */ + Expression * exp = Expression::parse(text); + if (exp == nullptr) { + return false; + } + Expression::ReplaceSymbolWithExpression(&exp, Symbol::SpecialSymbols::Ans, static_cast(snapshot())->calculationStore()->ansExpression(localContext())); + char buffer[Calculation::k_printedExpressionSize]; + int length = exp->writeTextInBuffer(buffer, sizeof(buffer)); + delete exp; + /* if the buffer is totally full, it is VERY likely that writeTextInBuffer + * escaped before printing utterly the expression. */ + if (length >= Calculation::k_printedExpressionSize-1) { + return false; + } + return true; +} + const char * App::XNT() { return "x"; } diff --git a/apps/calculation/app.h b/apps/calculation/app.h index fdda07e99..e8dd96f7b 100644 --- a/apps/calculation/app.h +++ b/apps/calculation/app.h @@ -28,6 +28,7 @@ public: CalculationStore m_calculationStore; }; bool textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event event) override; + bool textInputIsCorrect(const char * text); const char * XNT() override; private: App(Container * container, Snapshot * snapshot); diff --git a/apps/calculation/edit_expression_controller.cpp b/apps/calculation/edit_expression_controller.cpp index 5062d8d7a..c3476d3ca 100644 --- a/apps/calculation/edit_expression_controller.cpp +++ b/apps/calculation/edit_expression_controller.cpp @@ -87,6 +87,11 @@ void EditExpressionController::didBecomeFirstResponder() { bool EditExpressionController::textFieldDidReceiveEvent(::TextField * textField, Ion::Events::Event event) { if (textField->isEditing() && textField->textFieldShouldFinishEditing(event) && textField->draftTextLength() == 0 && m_cacheBuffer[0] != 0) { App * calculationApp = (App *)app(); + /* The input text store in m_cacheBuffer might have beed correct the first + * time but then be too long when replacing ans in another context */ + if (!calculationApp->textInputIsCorrect(m_cacheBuffer)) { + return true; + } m_calculationStore->push(m_cacheBuffer, calculationApp->localContext()); m_historyController->reload(); ((ContentView *)view())->mainView()->scrollToCell(0, m_historyController->numberOfRows()-1);