Reduced code size

This commit is contained in:
Benoit Blanchon
2017-05-27 15:08:11 +02:00
parent f76017a015
commit 7415f206ea
2 changed files with 44 additions and 23 deletions

View File

@ -38,18 +38,34 @@ class JsonWriter {
// Returns the number of bytes sent to the Print implementation. // Returns the number of bytes sent to the Print implementation.
// This is very handy for implementations of printTo() that must return the // This is very handy for implementations of printTo() that must return the
// number of bytes written. // number of bytes written.
size_t bytesWritten() const { return _length; } size_t bytesWritten() const {
return _length;
}
void beginArray() { writeRaw('['); } void beginArray() {
void endArray() { writeRaw(']'); } writeRaw('[');
}
void endArray() {
writeRaw(']');
}
void beginObject() { writeRaw('{'); } void beginObject() {
void endObject() { writeRaw('}'); } writeRaw('{');
}
void endObject() {
writeRaw('}');
}
void writeColon() { writeRaw(':'); } void writeColon() {
void writeComma() { writeRaw(','); } writeRaw(':');
}
void writeComma() {
writeRaw(',');
}
void writeBoolean(bool value) { writeRaw(value ? "true" : "false"); } void writeBoolean(bool value) {
writeRaw(value ? "true" : "false");
}
void writeString(const char *value) { void writeString(const char *value) {
if (!value) { if (!value) {
@ -106,7 +122,7 @@ class JsonWriter {
*ptr = 0; *ptr = 0;
do { do {
*--ptr = static_cast<char>(value % 10 + '0'); *--ptr = char(value % 10 + '0');
value = UInt(value / 10); value = UInt(value / 10);
} while (value); } while (value);
@ -137,8 +153,12 @@ class JsonWriter {
writeRaw(ptr); writeRaw(ptr);
} }
void writeRaw(const char *s) { _length += _sink.print(s); } void writeRaw(const char *s) {
void writeRaw(char c) { _length += _sink.print(c); } _length += _sink.print(s);
}
void writeRaw(char c) {
_length += _sink.print(c);
}
protected: protected:
Print &_sink; Print &_sink;
@ -154,19 +174,19 @@ class JsonWriter {
integralPart = uint32_t(value); integralPart = uint32_t(value);
JsonFloat remainder = value - JsonFloat(integralPart); JsonFloat remainder = value - JsonFloat(integralPart);
decimalPart = uint32_t(remainder * maxDecimalPart); remainder *= maxDecimalPart;
remainder = remainder * maxDecimalPart - JsonFloat(decimalPart); decimalPart = uint32_t(remainder);
remainder = remainder - JsonFloat(decimalPart);
// rounding // rounding:
if (remainder > 0.5) { // increment by 1 if remainder >= 0.5
decimalPart++; decimalPart += uint32_t(remainder * 2);
if (decimalPart >= maxDecimalPart) { if (decimalPart >= maxDecimalPart) {
decimalPart -= maxDecimalPart; decimalPart = 0;
integralPart++; integralPart++;
if (powersOf10 && integralPart >= 10) { if (powersOf10 && integralPart >= 10) {
powersOf10++; powersOf10++;
integralPart /= 10; integralPart = 1;
}
} }
} }
} }

View File

@ -84,6 +84,7 @@ TEST_CASE("JsonWriter::writeFloat()") {
SECTION("Rounding when too many decimals") { SECTION("Rounding when too many decimals") {
check(0.000099999999999, "0.0001"); check(0.000099999999999, "0.0001");
check(0.0000099999999999, "1e-5"); check(0.0000099999999999, "1e-5");
check(0.9999999996, "1");
} }
SECTION("9 decimal places") { SECTION("9 decimal places") {