From fb65a3208f535736c683e740f62a16e54f0ec388 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 02:35:42 +0000 Subject: [PATCH] Write tag strings without an escaped copy when nothing needs escaping escape_cstr and escape_xml build a new string, reserved at twice the input length, for every tag key, tag value, user name and role written to JSON or XML output, even though most of these strings contain nothing to escape. Add Json_Escaped and Xml_Escaped stream helpers. They scan the string once and, if no character would be escaped, write it directly; otherwise they fall back to escape_cstr/escape_xml. Output is identical, because a string with nothing to escape escapes to itself. Use them at the eight call sites that stream escaped strings in output_json.cc, output_xml.cc and output_custom.cc. Checked against escape_cstr and escape_xml for 2.1 million strings (every one- and two-byte string, random strings rich in special characters, and 1,200 real tag keys and values). No mismatches. Nova Scotia extract, CPU time, 15 alternating runs: [out:json]; way[building](44.60,-63.66,44.70,-63.52); out tags; 454 ms -> 442 ms [out:xml]; way[building](44.60,-63.66,44.70,-63.52); out tags; 471 ms -> 443 ms Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT --- src/expat/escape_json.cc | 13 +++++++++++++ src/expat/escape_json.h | 13 +++++++++++++ src/expat/escape_xml.cc | 13 +++++++++++++ src/expat/escape_xml.h | 13 +++++++++++++ src/overpass_api/output_formats/output_custom.cc | 2 +- src/overpass_api/output_formats/output_json.cc | 8 ++++---- src/overpass_api/output_formats/output_xml.cc | 6 +++--- 7 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/expat/escape_json.cc b/src/expat/escape_json.cc index 27a183b71..9d3201bca 100644 --- a/src/expat/escape_json.cc +++ b/src/expat/escape_json.cc @@ -44,3 +44,16 @@ std::string escape_cstr(const std::string& s) } return result; } + + +std::ostream& operator<<(std::ostream& out, const Json_Escaped& e) +{ + // escape_cstr changes exactly the characters '"', '\\' and those below 32. + for (std::string::size_type i = 0; i < e.s.size(); ++i) + { + unsigned char c = e.s[i]; + if (c == '\"' || c == '\\' || c < 32) + return out< #include std::string escape_cstr(const std::string& s); +/* Writes escape_cstr(s) to a stream. Most strings need no escaping; those are written + directly, without building an escaped copy. Use as out<' and those below 32. + for (std::string::size_type i = 0; i < e.s.size(); ++i) + { + unsigned char c = e.s[i]; + if (c == '&' || c == '\"' || c == '<' || c == '>' || c < 32) + return out< #include std::string escape_xml(const std::string& s); +/* Writes escape_xml(s) to a stream. Most strings need no escaping; those are written + directly, without building an escaped copy. Use as out<::const_iterator rit = roles.find(entry.role); - result<second : "???"); + result<second : "???"); } else if (raw_template.substr(new_pos, 8) == "{{first:") ; diff --git a/src/overpass_api/output_formats/output_json.cc b/src/overpass_api/output_formats/output_json.cc index 64a1afa35..cfac0e609 100644 --- a/src/overpass_api/output_formats/output_json.cc +++ b/src/overpass_api/output_formats/output_json.cc @@ -86,7 +86,7 @@ void print_meta_json(const OSM_Element_Metadata_Skeleton< Id_Type >& meta, ",\n \"changeset\": "<::const_iterator it = users.find(meta.user_id); if (it != users.end()) - std::cout<<",\n \"user\": \""<second)<<"\""; + std::cout<<",\n \"user\": \""<second)<<"\""; std::cout<<",\n \"uid\": "< >* tags { std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin(); std::cout<<",\n \"tags\": {" - "\n \""<first)<<"\": \""<second)<<"\""; + "\n \""<first)<<"\": \""<second)<<"\""; for (++it; it != tags->end(); ++it) - std::cout<<",\n \""<first)<<"\": \""<second)<<"\""; + std::cout<<",\n \""<first)<<"\": \""<second)<<"\""; std::cout<<"\n }"; } } @@ -242,7 +242,7 @@ void Output_JSON::print_item(const Relation_Skeleton& skel, std::cout <<"\n {" "\n \"type\": \""<end() ? rit->second : "???") << "\""; + ",\n \"role\": \""<end() ? rit->second : "???") << "\""; if (skel.members[i].type == Relation_Entry::NODE && geometry.has_faithful_relation_geometry() && geometry.relation_pos_is_valid(i)) diff --git a/src/overpass_api/output_formats/output_xml.cc b/src/overpass_api/output_formats/output_xml.cc index 6b8784ccc..49ee548ce 100644 --- a/src/overpass_api/output_formats/output_xml.cc +++ b/src/overpass_api/output_formats/output_xml.cc @@ -81,7 +81,7 @@ void print_meta_xml(const OSM_Element_Metadata_Skeleton< Id_Type >& meta, <<"\" changeset=\""<::const_iterator it = users.find(meta.user_id); if (it != users.end()) - std::cout<<" user=\""<second)<<"\""; + std::cout<<" user=\""<second)<<"\""; } @@ -152,7 +152,7 @@ void print_tags(const std::vector< std::pair< std::string, std::string > >* tags } for (std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin(); it != tags->end(); ++it) - std::cout<<" first)<<"\" v=\""<second)<<"\"/>\n"; + std::cout<<" first)<<"\" v=\""<second)<<"\"/>\n"; } } @@ -322,7 +322,7 @@ void print_members(const Relation_Skeleton& skel, const Opaque_Geometry& geometr std::map< uint32, std::string >::const_iterator it = roles.find(skel.members[i].role); std::cout<<" second : "???")<<"\""; + <<"\" role=\""<second : "???")<<"\""; if (skel.members[i].type == Relation_Entry::NODE) {