mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Have jsonfindptrs factor out escape_needed
On a mid-range x86_64 laptop, processing the 181 MiB citylots.json file from github.com/zemirco/sf-city-lots-json: $ time gen/bin/example-jsonfindptrs < citylots.json > /dev/null Before this commit: real 0m7.550s After: real 0m7.333s Ratio: 1.03x
This commit is contained in:
1 parent
b6c01b3464
commit
9d69939f2c
1 file changed
+17
-11
@@ -313,19 +313,21 @@ class JsonThing {
|
|||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
std::string //
|
bool //
|
||||||
escape(std::string s) {
|
escape_needed(const std::string& s) {
|
||||||
for (char& c : s) {
|
for (const char& c : s) {
|
||||||
if ((c == '~') || (c == '/') || (c == '\n') || (c == '\r')) {
|
if ((c == '~') || (c == '/') || (c == '\n') || (c == '\r')) {
|
||||||
goto escape_needed;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
escape_needed:
|
std::string //
|
||||||
|
escape(const std::string& s) {
|
||||||
std::string e;
|
std::string e;
|
||||||
e.reserve(8 + s.length());
|
e.reserve(8 + s.length());
|
||||||
for (char& c : s) {
|
for (const char& c : s) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '~':
|
case '~':
|
||||||
e += "~0";
|
e += "~0";
|
||||||
@@ -377,11 +379,15 @@ print_json_pointers(JsonThing& jt, uint32_t depth) {
|
|||||||
case JsonThing::Kind::Object:
|
case JsonThing::Kind::Object:
|
||||||
g_dst += "/";
|
g_dst += "/";
|
||||||
for (auto& kv : jt.value.o) {
|
for (auto& kv : jt.value.o) {
|
||||||
std::string e = escape(kv.first);
|
if (!escape_needed(kv.first)) {
|
||||||
if (e.empty() && !kv.first.empty()) {
|
g_dst += kv.first;
|
||||||
return "main: unsupported \"\\u000A\" or \"\\u000D\" in object key";
|
} else {
|
||||||
|
std::string e = escape(kv.first);
|
||||||
|
if (e.empty()) {
|
||||||
|
return "main: unsupported \"\\u000A\" or \"\\u000D\" in object key";
|
||||||
|
}
|
||||||
|
g_dst += e;
|
||||||
}
|
}
|
||||||
g_dst += e;
|
|
||||||
TRY(print_json_pointers(kv.second, depth));
|
TRY(print_json_pointers(kv.second, depth));
|
||||||
g_dst.resize(n + 1);
|
g_dst.resize(n + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user