fixed error in json float dump (where a pointer was checked against nullptr instead of '\0')

This commit is contained in:
The-EDev 2022-06-21 02:25:42 +03:00
parent 6f832f82fa
commit ab2a132bff
No known key found for this signature in database
GPG Key ID: 51C45DC0C413DCD9

View File

@ -1836,12 +1836,12 @@ namespace crow
enum enum
{ {
start, start,
decp, decp, // Decimal point
zero zero
} f_state; } f_state;
char outbuf[128]; char outbuf[128];
MSC_COMPATIBLE_SPRINTF(outbuf, "%f", v.num.d); MSC_COMPATIBLE_SPRINTF(outbuf, "%f", v.num.d);
char *p = &outbuf[0], *o = nullptr; char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0
f_state = start; f_state = start;
while (*p != '\0') while (*p != '\0')
{ {
@ -1849,15 +1849,17 @@ namespace crow
char ch = *p; char ch = *p;
switch (f_state) switch (f_state)
{ {
case start: case start: // Loop and lookahead until a decimal point is found
if (ch == '.') if (ch == '.')
{ {
if (p + 1 && *(p + 1) == '0') p++; char fch = *(p + 1);
// if the first character is 0, leave it be (this is so that "1.00000" becomes "1.0" and not "1.")
if ( fch != '\0' && fch == '0') p++;
f_state = decp; f_state = decp;
} }
p++; p++;
break; break;
case decp: case decp: // Loop until a 0 is found, if found, record its position
if (ch == '0') if (ch == '0')
{ {
f_state = zero; f_state = zero;
@ -1865,7 +1867,7 @@ namespace crow
} }
p++; p++;
break; break;
case zero: case zero: // if a non 0 is found (e.g. 1.00004) remove the earlier recorded 0 position and look for more trailing 0s
if (ch != '0') if (ch != '0')
{ {
o = nullptr; o = nullptr;
@ -1875,7 +1877,7 @@ namespace crow
break; break;
} }
} }
if (o != nullptr) if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin
*o = '\0'; *o = '\0';
out += outbuf; out += outbuf;
#undef MSC_COMPATIBLE_SPRINTF #undef MSC_COMPATIBLE_SPRINTF