diff --git a/apps/sequence/sequence.cpp b/apps/sequence/sequence.cpp index 02701e9fd..0f2a359d7 100644 --- a/apps/sequence/sequence.cpp +++ b/apps/sequence/sequence.cpp @@ -2,6 +2,7 @@ #include "cache_context.h" #include "sequence_store.h" #include +#include #include #include #include @@ -122,11 +123,11 @@ T Sequence::approximateToNextRank(int n, SequenceContext * sqctx) const { if (n < initialRank() || n < 0) { return NAN; } - constexpr int bufferSize = 5; + + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char unknownN[bufferSize]; - int codePointSize = UTF8Decoder::CodePointToChars(UCodePointUnknownN, unknownN, bufferSize); - assert(codePointSize <= bufferSize - 1); - unknownN[codePointSize] = 0; + Poincare::SerializationHelper::CodePoint(unknownN, bufferSize, UCodePointUnknownN); + CacheContext ctx = CacheContext(sqctx); T un = sqctx->valueOfSequenceAtPreviousRank(0, 0); T unm1 = sqctx->valueOfSequenceAtPreviousRank(0, 1); diff --git a/apps/shared/cartesian_function.cpp b/apps/shared/cartesian_function.cpp index 7faa5492e..b6d304e61 100644 --- a/apps/shared/cartesian_function.cpp +++ b/apps/shared/cartesian_function.cpp @@ -3,6 +3,7 @@ #include "poincare_helpers.h" #include #include +#include #include #include #include @@ -104,34 +105,30 @@ double CartesianFunction::sumBetweenBounds(double start, double end, Poincare::C } Expression::Coordinate2D CartesianFunction::nextMinimumFrom(double start, double step, double max, Context * context) const { - constexpr int bufferSize = 3; + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char unknownX[bufferSize]; - int codePointSize = UTF8Decoder::CodePointToChars(UCodePointUnknownX, unknownX, bufferSize); - assert(codePointSize <= bufferSize); + SerializationHelper::CodePoint(unknownX, bufferSize, UCodePointUnknownX); return PoincareHelpers::NextMinimum(expressionReduced(context), unknownX, start, step, max, *context); } Expression::Coordinate2D CartesianFunction::nextMaximumFrom(double start, double step, double max, Context * context) const { - constexpr int bufferSize = 3; + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char unknownX[bufferSize]; - int codePointSize = UTF8Decoder::CodePointToChars(UCodePointUnknownX, unknownX, bufferSize); - assert(codePointSize <= bufferSize); + SerializationHelper::CodePoint(unknownX, bufferSize, UCodePointUnknownX); return PoincareHelpers::NextMaximum(expressionReduced(context), unknownX, start, step, max, *context); } double CartesianFunction::nextRootFrom(double start, double step, double max, Context * context) const { - constexpr int bufferSize = 3; + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char unknownX[bufferSize]; - int codePointSize = UTF8Decoder::CodePointToChars(UCodePointUnknownX, unknownX, bufferSize); - assert(codePointSize <= bufferSize); + SerializationHelper::CodePoint(unknownX, bufferSize, UCodePointUnknownX); return PoincareHelpers::NextRoot(expressionReduced(context), unknownX, start, step, max, *context); } Expression::Coordinate2D CartesianFunction::nextIntersectionFrom(double start, double step, double max, Poincare::Context * context, Expression e) const { - constexpr int bufferSize = 3; + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char unknownX[bufferSize]; - int codePointSize = UTF8Decoder::CodePointToChars(UCodePointUnknownX, unknownX, bufferSize); - assert(codePointSize <= bufferSize); + SerializationHelper::CodePoint(unknownX, bufferSize, UCodePointUnknownX); return PoincareHelpers::NextIntersection(expressionReduced(context), unknownX, start, step, max, *context, e); } diff --git a/poincare/src/symbol.cpp b/poincare/src/symbol.cpp index a0711dc46..1bb2f97d9 100644 --- a/poincare/src/symbol.cpp +++ b/poincare/src/symbol.cpp @@ -157,7 +157,7 @@ bool SymbolNode::isUnknown(CodePoint unknownSymbol) const { } Symbol Symbol::Builder(CodePoint name) { - constexpr int bufferSize = 5; + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char buffer[bufferSize]; int codePointSize = UTF8Decoder::CodePointToChars(name, buffer, bufferSize); assert(codePointSize <= bufferSize); diff --git a/poincare/src/trigonometry.cpp b/poincare/src/trigonometry.cpp index 2c7403a5a..84b694c5c 100644 --- a/poincare/src/trigonometry.cpp +++ b/poincare/src/trigonometry.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -22,11 +23,11 @@ namespace Poincare { float Trigonometry::characteristicXRange(const Expression & e, Context & context, Preferences::AngleUnit angleUnit) { assert(e.numberOfChildren() == 1); - constexpr int size = 5; - char x[size]; - int zeroIndex = UTF8Decoder::CodePointToChars(UCodePointUnknownX, x, size); - assert(zeroIndex <= size - 1); - x[zeroIndex] = 0; + + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; + char x[bufferSize]; + SerializationHelper::CodePoint(x, bufferSize, UCodePointUnknownX); + int d = e.childAtIndex(0).polynomialDegree(context, x); if (d < 0 || d > 1) { // child(0) is not linear so we cannot easily find an interesting range diff --git a/poincare/test/user_variable.cpp b/poincare/test/user_variable.cpp index 562aeae43..f22fdd477 100644 --- a/poincare/test/user_variable.cpp +++ b/poincare/test/user_variable.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "helper.h" using namespace Poincare; @@ -156,11 +157,11 @@ QUIZ_CASE(poincare_user_variable_functions_with_context) { // f : x→ x^2 assert_simplify("x^2→f(x)"); // Approximate f(?-2) with ? = 5 - constexpr int bufferSize = 5; + + constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char x[bufferSize]; - int codePointSize = UTF8Decoder::CodePointToChars(UCodePointUnknownX, x, bufferSize); - quiz_assert(codePointSize <= bufferSize - 1); - x[codePointSize] = 0; + Poincare::SerializationHelper::CodePoint(x, bufferSize, UCodePointUnknownX); + assert_parsed_expression_approximates_with_value_for_symbol(Function::Builder("f", 1, Subtraction::Builder(Symbol::Builder(UCodePointUnknownX), Rational::Builder(2))), x, 5.0, 9.0); // Approximate f(?-1)+f(?+1) with ? = 3 assert_parsed_expression_approximates_with_value_for_symbol(Addition::Builder(Function::Builder("f", 1, Subtraction::Builder(Symbol::Builder(UCodePointUnknownX), Rational::Builder(1))), Function::Builder("f", 1, Addition::Builder(Symbol::Builder(UCodePointUnknownX), Rational::Builder(1)))), x, 3.0, 20.0);