2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/JsonBuilder.h"
|
2018-08-31 16:21:24 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
JsonBuilderObject JsonBuilder::makeMessage(const char* name, const char* description) {
|
2018-09-09 06:44:48 +08:00
|
|
|
JsonBuilderObject out;
|
2018-08-31 16:21:24 +08:00
|
|
|
out["name"] = name;
|
|
|
|
out["description"] = description;
|
|
|
|
return out;
|
|
|
|
}
|
2018-09-10 18:07:11 +08:00
|
|
|
|
|
|
|
// dst must have at least len + 3 bytes available (".e" becomes "0.0e0")
|
|
|
|
// Returns bytes written, or 0 on failure.
|
2021-03-11 02:06:03 +08:00
|
|
|
int JsonBuilder::coerceAsciiNumberToJSON(const char* s, int len, char* dst) {
|
|
|
|
if (len == 0) {
|
2018-09-10 18:07:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
const char* send = s + len;
|
|
|
|
char* wptr = dst;
|
2018-09-10 18:07:11 +08:00
|
|
|
bool dot = false;
|
|
|
|
|
|
|
|
// Allow one optional sign
|
2021-03-11 02:06:03 +08:00
|
|
|
if (*s == '-') {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// Output not yet valid so return failure
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 04:28:31 +08:00
|
|
|
// 'inf' becomes 1e99
|
2021-03-11 02:06:03 +08:00
|
|
|
if (*s == 'i') {
|
|
|
|
if (len >= 3 && (strncmp(s, "inf", 3) == 0)) {
|
2018-09-11 04:28:31 +08:00
|
|
|
strcpy(wptr, "1e99");
|
|
|
|
return 4 + wptr - dst;
|
|
|
|
}
|
|
|
|
// Anything else starting with 'i' is a failure
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:07:11 +08:00
|
|
|
// Skip leading zeroes
|
2021-03-11 02:06:03 +08:00
|
|
|
while (*s == '0') {
|
2018-09-10 18:07:11 +08:00
|
|
|
++s;
|
|
|
|
|
|
|
|
// If found end, number is valid and zero
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a dot is found, write a zero before it
|
2021-03-11 02:06:03 +08:00
|
|
|
if (*s == '.') {
|
2018-09-10 18:07:11 +08:00
|
|
|
dot = true;
|
|
|
|
*wptr++ = '0';
|
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// If found end, add a zero and return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no digit after the dot, write a zero
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!isdigit(*s)) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all digits found
|
2021-03-11 02:06:03 +08:00
|
|
|
while (isdigit(*s)) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// If found end, number is valid so return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If there is a dot, return unless its the first
|
2021-03-11 02:06:03 +08:00
|
|
|
if (*s == '.') {
|
|
|
|
if (dot) {
|
2018-09-10 18:07:11 +08:00
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// If found end, add a zero and return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are more digits write them, else write a 0
|
2021-03-11 02:06:03 +08:00
|
|
|
if (isdigit(*s)) {
|
2018-09-10 18:07:11 +08:00
|
|
|
do {
|
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// If found end, number is valid so return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
} while (isdigit(*s));
|
|
|
|
} else {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now we can have an e or E, else stop
|
2021-03-11 02:06:03 +08:00
|
|
|
if (*s == 'e' || *s == 'E') {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// If found end, add a zero and return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow one optional sign
|
2021-03-11 02:06:03 +08:00
|
|
|
if (*s == '-' || *s == '+') {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = *s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If found end, add a zero and return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are more digits write then, else write a 0
|
2021-03-11 02:06:03 +08:00
|
|
|
if (isdigit(*s)) {
|
2018-09-10 18:07:11 +08:00
|
|
|
do {
|
|
|
|
*wptr++ = *s++;
|
|
|
|
|
|
|
|
// If found end, number is valid so return
|
2021-03-11 02:06:03 +08:00
|
|
|
if (s == send) {
|
2018-09-10 18:07:11 +08:00
|
|
|
return wptr - dst;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
} while (isdigit(*s));
|
|
|
|
} else {
|
2018-09-10 18:07:11 +08:00
|
|
|
*wptr++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wptr - dst;
|
|
|
|
}
|