mirror of
https://github.com/mapbox/tippecanoe.git
synced 2025-02-23 18:40:17 +00:00
Use more idiomatic C++ to quote JSON strings
This commit is contained in:
parent
5960a15fcd
commit
e3823c966c
27
mbtiles.cpp
27
mbtiles.cpp
@ -86,26 +86,21 @@ void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void quote(std::string *buf, const char *s) {
|
static void quote(std::string &buf, std::string const &s) {
|
||||||
char tmp[strlen(s) * 8 + 1];
|
for (size_t i = 0; i < s.size(); i++) {
|
||||||
char *out = tmp;
|
unsigned char ch = s[i];
|
||||||
|
|
||||||
for (; *s != '\0'; s++) {
|
|
||||||
unsigned char ch = (unsigned char) *s;
|
|
||||||
|
|
||||||
if (ch == '\\' || ch == '\"') {
|
if (ch == '\\' || ch == '\"') {
|
||||||
*out++ = '\\';
|
buf.push_back('\\');
|
||||||
*out++ = ch;
|
buf.push_back(ch);
|
||||||
} else if (ch < ' ') {
|
} else if (ch < ' ') {
|
||||||
sprintf(out, "\\u%04x", ch);
|
char tmp[7];
|
||||||
out = out + strlen(out);
|
sprintf(tmp, "\\u%04x", ch);
|
||||||
|
buf.append(std::string(tmp));
|
||||||
} else {
|
} else {
|
||||||
*out++ = ch;
|
buf.push_back(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = '\0';
|
|
||||||
buf->append(tmp, strlen(tmp));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void aprintf(std::string *buf, const char *format, ...) {
|
void aprintf(std::string *buf, const char *format, ...) {
|
||||||
@ -243,7 +238,7 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, int minzoom, int
|
|||||||
|
|
||||||
auto fk = layermap.find(lnames[i]);
|
auto fk = layermap.find(lnames[i]);
|
||||||
aprintf(&buf, "{ \"id\": \"");
|
aprintf(&buf, "{ \"id\": \"");
|
||||||
quote(&buf, lnames[i].c_str());
|
quote(buf, lnames[i]);
|
||||||
aprintf(&buf, "\", \"description\": \"\", \"minzoom\": %d, \"maxzoom\": %d, \"fields\": {", fk->second.minzoom, fk->second.maxzoom);
|
aprintf(&buf, "\", \"description\": \"\", \"minzoom\": %d, \"maxzoom\": %d, \"fields\": {", fk->second.minzoom, fk->second.maxzoom);
|
||||||
|
|
||||||
std::set<type_and_string>::iterator j;
|
std::set<type_and_string>::iterator j;
|
||||||
@ -256,7 +251,7 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *fname, int minzoom, int
|
|||||||
}
|
}
|
||||||
|
|
||||||
aprintf(&buf, "\"");
|
aprintf(&buf, "\"");
|
||||||
quote(&buf, j->string.c_str());
|
quote(buf, j->string);
|
||||||
|
|
||||||
if (j->type == VT_NUMBER) {
|
if (j->type == VT_NUMBER) {
|
||||||
aprintf(&buf, "\": \"Number\"");
|
aprintf(&buf, "\": \"Number\"");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user