diff --git a/ion/src/device/bench/command/crc.cpp b/ion/src/device/bench/command/crc.cpp index 117ebe135..7f549ff60 100644 --- a/ion/src/device/bench/command/crc.cpp +++ b/ion/src/device/bench/command/crc.cpp @@ -52,12 +52,11 @@ void CRC(const char * input) { Ion::Device::Cache::enable(); constexpr int bufferSize = 4+10+1; // crc is a uint32_t so 10 digits long. - char buffer[bufferSize] = {'C', 'R', 'C', '=', 0}; - int wantedLength = std::log10(crc*1.0)+1; - assert(wantedLength < bufferSize - 4); - Poincare::PrintInt::PadIntInBuffer(crc, buffer+4, wantedLength); - buffer[4+wantedLength] = 0; - + char buffer[bufferSize] = {'C', 'R', 'C', '='}; + for (int i = 4; i < bufferSize; i++) { + buffer[i] = 0; + } + Poincare::PrintInt::Left(crc, buffer+4, bufferSize - 4 - 1); reply(buffer); } diff --git a/ion/src/device/bench/command/external_flash_id.cpp b/ion/src/device/bench/command/external_flash_id.cpp index a0979ff99..30ee1baaa 100644 --- a/ion/src/device/bench/command/external_flash_id.cpp +++ b/ion/src/device/bench/command/external_flash_id.cpp @@ -23,9 +23,9 @@ void ExternalFlashId(const char * input) { uint8_t memoryType = 0; uint8_t capacityType = 0; Ion::Device::ExternalFlash::JDECid(&manufacturerID, &memoryType, &capacityType); - Poincare::PrintInt::PadIntInBuffer(manufacturerID, response + firstNumberIndex, numberLength); - Poincare::PrintInt::PadIntInBuffer(memoryType, response + firstNumberIndex + numberLength, numberLength); - Poincare::PrintInt::PadIntInBuffer(capacityType, response + firstNumberIndex + 2*numberLength, numberLength); + Poincare::PrintInt::Right(manufacturerID, response + firstNumberIndex, numberLength); + Poincare::PrintInt::Right(memoryType, response + firstNumberIndex + numberLength, numberLength); + Poincare::PrintInt::Right(capacityType, response + firstNumberIndex + 2*numberLength, numberLength); response[bufferSize -1] = 0; reply(response); } diff --git a/ion/src/device/bench/command/screen_id.cpp b/ion/src/device/bench/command/screen_id.cpp index 239ee6477..002edd277 100644 --- a/ion/src/device/bench/command/screen_id.cpp +++ b/ion/src/device/bench/command/screen_id.cpp @@ -15,12 +15,12 @@ void ScreenID(const char * input) { } uint32_t screenID = Ion::Device::Display::panelIdentifier(); - constexpr int bufferSize = 20; + constexpr int bufferSize = 10 + 1; // screenID is a uint32_t so 10 digits max char buffer[bufferSize]; for (int i = 0; i < bufferSize; i++) { buffer[i] = 0; } - Poincare::PrintInt::PadIntInBuffer(screenID, buffer, bufferSize - 1); + Poincare::PrintInt::Left(screenID, buffer, bufferSize - 1); reply(buffer); } diff --git a/ion/src/shared/events_benchmark.cpp b/ion/src/shared/events_benchmark.cpp index b6b8fc378..adad15e5c 100644 --- a/ion/src/shared/events_benchmark.cpp +++ b/ion/src/shared/events_benchmark.cpp @@ -80,14 +80,10 @@ Event getEvent(int * timeout) { for (int i = 0; i < numberOfScenari; i++) { constexpr int bufferLength = 50; char buffer[bufferLength]; - Poincare::PrintInt::PadIntInBuffer(timings[i], buffer, bufferLength); + Poincare::PrintInt::Left(timings[i], buffer, bufferLength); //buffer[50-1-3] = 0; // convert from ms to s without generating _udivmoddi4 (long long division) - char * bufferPointer = buffer; - while (*bufferPointer == '0') { - bufferPointer++; - } ctx->drawString(scenari[i].name(), KDPoint(0, line_y), font); - ctx->drawString(bufferPointer, KDPoint(200, line_y), font); + ctx->drawString(buffer, KDPoint(200, line_y), font); line_y += line_height; } while (1) { diff --git a/poincare/include/poincare/print_int.h b/poincare/include/poincare/print_int.h index 4debb9f1e..829e2dab2 100644 --- a/poincare/include/poincare/print_int.h +++ b/poincare/include/poincare/print_int.h @@ -7,7 +7,8 @@ namespace Poincare { namespace PrintInt { -bool PadIntInBuffer(uint32_t integer, char * buffer, int bufferLength); +bool Left(uint32_t integer, char * buffer, int bufferLength); +bool Right(uint32_t integer, char * buffer, int bufferLength); } diff --git a/poincare/src/print_int.cpp b/poincare/src/print_int.cpp index ecfd546d8..dedcb43b5 100644 --- a/poincare/src/print_int.cpp +++ b/poincare/src/print_int.cpp @@ -4,21 +4,47 @@ namespace Poincare { -bool PrintInt::PadIntInBuffer(uint32_t integer, char * buffer, int bufferLength) { +bool PrintIntInBuffer(uint32_t integer, char * buffer, int bufferLength, bool left) { assert(integer >= 0); + if (bufferLength == 0) { + return false; + } + + // Case integer is 0 + if (integer == 0) { + if (left) { + buffer[0] = '0'; + } else { + for (int i = 0; i < bufferLength; i++) { + buffer[i] = '0'; + } + } + return true; + } + int wantedLength = std::log10(integer*1.0)+1; if (wantedLength > bufferLength) { return false; } int modulo = 10; - for (int i = bufferLength - 1; i >= 0; i--) { + int startIndex = left ? wantedLength - 1 : bufferLength - 1; + for (int i = startIndex; i >= 0; i--) { int c = integer % modulo; buffer[i] = '0' + c; integer = (integer - c) / modulo; - /* After all digits are written on the right of the buffer, integer is equal - * to 0 and the buffer is padded with 0 on the left. */ + /* If the integer is written on the right, after all digits are written + * integer is equal to 0 and the buffer is padded with 0 on the left. */ } return true; } + +bool PrintInt::Left(uint32_t integer, char * buffer, int bufferLength) { + return PrintIntInBuffer(integer, buffer, bufferLength, true); +} + +bool PrintInt::Right(uint32_t integer, char * buffer, int bufferLength) { + return PrintIntInBuffer(integer, buffer, bufferLength, false); +} + } diff --git a/poincare/test/print_int.cpp b/poincare/test/print_int.cpp index 7fd0233e7..c4a9dea85 100644 --- a/poincare/test/print_int.cpp +++ b/poincare/test/print_int.cpp @@ -5,19 +5,37 @@ using namespace Poincare; -void assert_int_prints_as(int integer, const char * result) { +void assert_int_prints_as(int integer, const char * result, bool left) { constexpr int bufferSize = 5; char buffer[bufferSize]; - bool couldPrint = PrintInt::PadIntInBuffer(integer, buffer, bufferSize); - quiz_assert(couldPrint); for (int i = 0; i < bufferSize; i++) { + buffer[i] = 0; + } + bool couldPrint = left ? PrintInt::Left(integer, buffer, bufferSize) : PrintInt::Right(integer, buffer, bufferSize); + quiz_assert(couldPrint); + int i = 0; + while (result[i] != 0) { quiz_assert(result[i] == buffer[i]); + i++; + } + while (i < bufferSize) { + quiz_assert(buffer[i] == 0); + i++; } } -QUIZ_CASE(poincare_print_int) { - assert_int_prints_as(1, "00001"); - assert_int_prints_as(12, "00012"); - assert_int_prints_as(15678, "15678"); - assert_int_prints_as(0, "00000"); +QUIZ_CASE(poincare_print_int_left) { + assert_int_prints_as(1, "1", true); + assert_int_prints_as(12, "12", true); + assert_int_prints_as(15678, "15678", true); + assert_int_prints_as(99999, "99999", true); + assert_int_prints_as(0, "0", true); +} + +QUIZ_CASE(poincare_print_int_right) { + assert_int_prints_as(1, "00001", false); + assert_int_prints_as(12, "00012", false); + assert_int_prints_as(15678, "15678", false); + assert_int_prints_as(99999, "99999", true); + assert_int_prints_as(0, "00000", false); }