[apps] Calculation: input text stores in cache have to be checked again

when used as its length context dependent (because of 'ans' symbol)
This commit is contained in:
Émilie Feral
2018-01-31 15:37:02 +01:00
committed by EmilieNumworks
parent 53393524ef
commit a8fe8dc22e
3 changed files with 27 additions and 15 deletions

View File

@@ -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 *>(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 *>(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";
}

View File

@@ -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);

View File

@@ -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);