[ion] PrintInt::Left and PrintInt::Right

Right pads the left of the integer with zeroes.
This commit is contained in:
Léa Saviot
2019-05-31 15:33:26 +02:00
parent 0af1bc2117
commit b14475738e
7 changed files with 70 additions and 30 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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