From f9241bd7e6bc6cf7007e587bb9ac14f11dded0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 31 Jan 2018 13:32:14 +0100 Subject: [PATCH] [poincare] Decimal: writeTextInBuffer has to handle cases when the mantissa is too big to be serialize --- poincare/src/decimal.cpp | 14 +++++++++++--- poincare/test/convert_expression_to_text.cpp | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/poincare/src/decimal.cpp b/poincare/src/decimal.cpp index c81e7c199..0970c2550 100644 --- a/poincare/src/decimal.cpp +++ b/poincare/src/decimal.cpp @@ -125,17 +125,23 @@ int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignif buffer[currentChar] = 0; return currentChar; } + char tempBuffer[200]; + int mantissaLength = m_mantissa.writeTextInBuffer(tempBuffer, 200); + if (strcmp(tempBuffer, "undef") == 0) { + strlcpy(buffer, tempBuffer, bufferSize); + return mantissaLength; + } int nbOfDigitsInMantissaWithoutSign = numberOfDigitsInMantissaWithoutSign(); int numberOfRequiredDigits = nbOfDigitsInMantissaWithoutSign > m_exponent ? nbOfDigitsInMantissaWithoutSign : m_exponent; numberOfRequiredDigits = m_exponent < 0 ? 1+nbOfDigitsInMantissaWithoutSign-m_exponent : numberOfRequiredDigits; /* Case 0: the number would be too long if we print it as a natural decimal */ if (numberOfRequiredDigits > k_maxLength) { if (nbOfDigitsInMantissaWithoutSign == 1) { - currentChar +=m_mantissa.writeTextInBuffer(buffer, bufferSize); + currentChar += strlcpy(buffer, tempBuffer, bufferSize); } else { currentChar++; if (currentChar >= bufferSize-1) { return bufferSize-1; } - currentChar += m_mantissa.writeTextInBuffer(buffer+currentChar, bufferSize-currentChar); + currentChar += strlcpy(buffer+currentChar, tempBuffer, bufferSize-currentChar); int decimalMarkerPosition = 1; if (buffer[1] == '-') { decimalMarkerPosition++; @@ -153,6 +159,8 @@ int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignif return currentChar; } /* Case 2: Print a natural decimal number */ + int deltaCharMantissa = m_exponent < 0 ? -m_exponent+1 : 0; + strlcpy(buffer+deltaCharMantissa, tempBuffer, bufferSize-deltaCharMantissa); if (m_mantissa.isNegative()) { buffer[currentChar++] = '-'; } @@ -176,7 +184,7 @@ int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignif tempChar = buffer[currentChar]; tempCharPosition = currentChar; } - currentChar += m_mantissa.writeTextInBuffer(buffer+currentChar, bufferSize-currentChar); + currentChar += mantissaLength; if (m_mantissa.isNegative()) { // replace the temp char back buffer[tempCharPosition] = tempChar; } diff --git a/poincare/test/convert_expression_to_text.cpp b/poincare/test/convert_expression_to_text.cpp index 74553c432..ab4e483d1 100644 --- a/poincare/test/convert_expression_to_text.cpp +++ b/poincare/test/convert_expression_to_text.cpp @@ -164,6 +164,8 @@ QUIZ_CASE(poincare_decimal_to_text) { assert_expression_prints_to(&e14, "9.99999999999995E-7"); Decimal e15(0.0000009999999999999995); assert_expression_prints_to(&e15, "0.000001"); + Decimal e16(0.0000009999999999901200121020102010201201201021099995); + assert_expression_prints_to(&e16, "9.9999999999012E-7"); } QUIZ_CASE(poincare_complex_to_text) {