diff --git a/poincare/include/poincare/absolute_value_layout.h b/poincare/include/poincare/absolute_value_layout.h index f23b56737..0aa42cb75 100644 --- a/poincare/include/poincare/absolute_value_layout.h +++ b/poincare/include/poincare/absolute_value_layout.h @@ -16,7 +16,7 @@ public: // SerializationHelperInterface int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, AbsoluteValue::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, AbsoluteValue::s_functionHelper.name(), true); } // TreeNode diff --git a/poincare/include/poincare/ceiling_layout.h b/poincare/include/poincare/ceiling_layout.h index fe3377f5d..a0f553aeb 100644 --- a/poincare/include/poincare/ceiling_layout.h +++ b/poincare/include/poincare/ceiling_layout.h @@ -16,7 +16,7 @@ public: Type type() const override { return Type::CeilingLayout; } int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Ceiling::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Ceiling::s_functionHelper.name(), true); } // TreeNode diff --git a/poincare/include/poincare/condensed_sum_layout.h b/poincare/include/poincare/condensed_sum_layout.h index 688b78bc4..54cb6e636 100644 --- a/poincare/include/poincare/condensed_sum_layout.h +++ b/poincare/include/poincare/condensed_sum_layout.h @@ -23,7 +23,7 @@ public: void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override { assert(false); } void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override { assert(false); } int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Sum::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Sum::s_functionHelper.name(), true); } LayoutNode * layoutToPointWhenInserting(Expression * correspondingExpression) override { diff --git a/poincare/include/poincare/floor_layout.h b/poincare/include/poincare/floor_layout.h index 61e07f26f..f3f0cab60 100644 --- a/poincare/include/poincare/floor_layout.h +++ b/poincare/include/poincare/floor_layout.h @@ -16,7 +16,7 @@ public: Type type() const override { return Type::FloorLayout; } int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override { - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Floor::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Floor::s_functionHelper.name(), true); } // TreeNode diff --git a/poincare/include/poincare/serialization_helper.h b/poincare/include/poincare/serialization_helper.h index 29d47aa91..4ee86219b 100644 --- a/poincare/include/poincare/serialization_helper.h +++ b/poincare/include/poincare/serialization_helper.h @@ -24,6 +24,11 @@ namespace SerializationHelper { int firstChildIndex = 0, int lastChildIndex = -1); + /* needsSystemParentheses add System parentheses wrapping the layout children. + * It is used when serializing special layouts to avoid creating parsable + * string from misformed layout. For instance, we don't want to parse: + * |2)(3|, so we serialize it in "abs({2)(3})" where {} are System parentheses + * instead of "abs(2)(3)". */ int Prefix( const TreeNode * node, char * buffer, @@ -31,6 +36,7 @@ namespace SerializationHelper { Preferences::PrintFloatMode floatDisplayMode, int numberOfDigits, const char * operatorName, + bool needsSystemParentheses = false, int lastChildIndex = -1); int SerializeChild( diff --git a/poincare/src/binomial_coefficient_layout.cpp b/poincare/src/binomial_coefficient_layout.cpp index c04b5c675..839d2cd55 100644 --- a/poincare/src/binomial_coefficient_layout.cpp +++ b/poincare/src/binomial_coefficient_layout.cpp @@ -74,7 +74,7 @@ void BinomialCoefficientLayoutNode::moveCursorDown(LayoutCursor * cursor, bool * } int BinomialCoefficientLayoutNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, BinomialCoefficient::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, BinomialCoefficient::s_functionHelper.name(), true); } KDSize BinomialCoefficientLayoutNode::computeSize() { diff --git a/poincare/src/conjugate_layout.cpp b/poincare/src/conjugate_layout.cpp index ac636dcad..63e0b2432 100644 --- a/poincare/src/conjugate_layout.cpp +++ b/poincare/src/conjugate_layout.cpp @@ -52,7 +52,7 @@ void ConjugateLayoutNode::moveCursorRight(LayoutCursor * cursor, bool * shouldRe } int ConjugateLayoutNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Conjugate::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, Conjugate::s_functionHelper.name(), true); } KDSize ConjugateLayoutNode::computeSize() { diff --git a/poincare/src/nth_root_layout.cpp b/poincare/src/nth_root_layout.cpp index 7eb8b2e04..52a32be93 100644 --- a/poincare/src/nth_root_layout.cpp +++ b/poincare/src/nth_root_layout.cpp @@ -152,13 +152,13 @@ int NthRootLayoutNode::serialize(char * buffer, int bufferSize, Preferences::Pri assert((const_cast(this))->indexLayout()); if ((const_cast(this))->indexLayout()->isEmpty()) { // Case: root(x,empty): Write "'SquareRootSymbol'('radicandLayout')" - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name(), 0); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name(), true, 0); } // Case: root(x,n) - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, NthRoot::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, NthRoot::s_functionHelper.name(), true); } // Case: squareRoot(x) - return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name()); + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, SquareRoot::s_functionHelper.name(), true); } KDSize NthRootLayoutNode::computeSize() { diff --git a/poincare/src/serialization_helper.cpp b/poincare/src/serialization_helper.cpp index 84896271e..4991226e4 100644 --- a/poincare/src/serialization_helper.cpp +++ b/poincare/src/serialization_helper.cpp @@ -68,6 +68,7 @@ int InfixPrefix( Preferences::PrintFloatMode floatDisplayMode, int numberOfDigits, const char * operatorName, + bool needsSystemParentheses, int firstChildIndex, int lastChildIndex) { @@ -95,10 +96,13 @@ int InfixPrefix( if (numberOfChar >= bufferSize-1) { return bufferSize-1; } - // Add the opening system parenthesis - numberOfChar += UTF8Decoder::CodePointToChars(UCodePointLeftSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar); - if (numberOfChar >= bufferSize-1) { - return bufferSize-1; + + if (needsSystemParentheses) { + // Add the opening system parenthesis + numberOfChar += UTF8Decoder::CodePointToChars(UCodePointLeftSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar); + if (numberOfChar >= bufferSize-1) { + return bufferSize-1; + } } } @@ -133,11 +137,14 @@ int InfixPrefix( } if (prefix) { - // Add the closing system parenthesis - numberOfChar += UTF8Decoder::CodePointToChars(UCodePointRightSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar); - if (numberOfChar >= bufferSize-1) { - return bufferSize-1; + if (needsSystemParentheses) { + // Add the closing system parenthesis + numberOfChar += UTF8Decoder::CodePointToChars(UCodePointRightSystemParenthesis, buffer+numberOfChar, bufferSize - numberOfChar); + if (numberOfChar >= bufferSize-1) { + return bufferSize-1; + } } + // Add the closing parenthesis numberOfChar += UTF8Decoder::CodePointToChars(')', buffer+numberOfChar, bufferSize - numberOfChar); if (numberOfChar >= bufferSize-1) { @@ -159,7 +166,7 @@ int SerializationHelper::Infix( int firstChildIndex, int lastChildIndex) { - return InfixPrefix(false, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, firstChildIndex, lastChildIndex); + return InfixPrefix(false, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, false, firstChildIndex, lastChildIndex); } int SerializationHelper::Prefix( @@ -169,9 +176,10 @@ int SerializationHelper::Prefix( Preferences::PrintFloatMode floatDisplayMode, int numberOfDigits, const char * operatorName, + bool needsSystemParentheses, int lastChildIndex) { - return InfixPrefix(true, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, 0, lastChildIndex); + return InfixPrefix(true, node, buffer, bufferSize, floatDisplayMode, numberOfDigits, operatorName, needsSystemParentheses, 0, lastChildIndex); } int SerializationHelper::CodePoint(char * buffer, int bufferSize, class CodePoint c) {