diff --git a/src/expat/escape_json.cc b/src/expat/escape_json.cc index 27a183b71..948444202 100644 --- a/src/expat/escape_json.cc +++ b/src/expat/escape_json.cc @@ -16,31 +16,82 @@ * along with Overpass_API. If not, see . */ +#include #include #include "escape_json.h" -std::string escape_cstr(const std::string& s) +namespace { - std::string result; - result.reserve(s.length()*2); - for (std::string::size_type i(0); i < s.size(); ++i) + +struct Replacements +{ + Replacements() + { + for (int c = 0; c < 256; ++c) + replacement[c] = c < 32 ? "?" : 0; + replacement[(unsigned char)'\"'] = "\\\""; + replacement[(unsigned char)'\\'] = "\\\\"; + replacement[(unsigned char)'\n'] = "\\n"; + replacement[(unsigned char)'\t'] = "\\t"; + replacement[(unsigned char)'\r'] = "\\r"; + } + + // What each byte is written as, or 0 if it is written as it is. + const char* replacement[256]; +}; + + +const Replacements& replacements() +{ + static const Replacements result; + return result; +} + + +struct Stream_Sink +{ + std::ostream& out; + void append(const char* s, std::string::size_type n) { out.write(s, n); } + void append(const char* s) { out< +void write_escaped(const std::string& s, Sink& sink) +{ + const Replacements& table = replacements(); + const char* run = s.data(); + const char* end = s.data() + s.size(); + for (const char* pos = run; pos != end; ++pos) { - if (s[i] == '\"') - result += "\\\""; - else if (s[i] == '\\') - result += "\\\\"; - else if (s[i] == '\n') - result += "\\n"; - else if (s[i] == '\t') - result += "\\t"; - else if (s[i] == '\r') - result += "\\r"; - else if ((unsigned char)s[i] < 32) - result += '?'; - else - result += s[i]; + const char* replacement = table.replacement[(unsigned char)*pos]; + if (replacement) + { + sink.append(run, pos - run); + sink.append(replacement); + run = pos + 1; + } } + sink.append(run, end - run); +} + +} + + +std::string escape_cstr(const std::string& s) +{ + std::string result; + result.reserve(s.size()); + write_escaped(s, result); return result; } + + +std::ostream& operator<<(std::ostream& out, const Json_Escaped& e) +{ + Stream_Sink sink{out}; + write_escaped(e.s, sink); + return out; +} diff --git a/src/expat/escape_json.h b/src/expat/escape_json.h index 78c57b0d6..dfc1b0d18 100644 --- a/src/expat/escape_json.h +++ b/src/expat/escape_json.h @@ -19,10 +19,21 @@ #ifndef DE__OSM3S___EXPAT__ESCAPE_JSON_H #define DE__OSM3S___EXPAT__ESCAPE_JSON_H +#include #include std::string escape_cstr(const std::string& s); +// Writes escape_cstr(s) to a stream without building the escaped copy. s must outlive the expression. +struct Json_Escaped +{ + explicit Json_Escaped(const std::string& s_) : s(s_) {} + const std::string& s; +}; + +std::ostream& operator<<(std::ostream& out, const Json_Escaped& e); + + #endif diff --git a/src/expat/escape_xml.cc b/src/expat/escape_xml.cc index d80cf441b..77d2ac9ea 100644 --- a/src/expat/escape_xml.cc +++ b/src/expat/escape_xml.cc @@ -16,36 +16,84 @@ * along with Overpass_API. If not, see . */ +#include #include #include "escape_xml.h" -std::string escape_xml(const std::string& s) +namespace { - const char* digit = "0123456789abcdef"; - std::string result; - result.reserve(s.length()*2); - for (std::string::size_type i(0); i < s.size(); ++i) +struct Replacements +{ + Replacements() { - if (s[i] == '&') - result += "&"; - else if (s[i] == '\"') - result += """; - else if (s[i] == '<') - result += "<"; - else if (s[i] == '>') - result += ">"; - else if ((unsigned char)s[i] < 32) + for (int c = 0; c < 256; ++c) + replacement[c] = c < 32 ? "?" : 0; + replacement[(unsigned char)'&'] = "&"; + replacement[(unsigned char)'\"'] = """; + replacement[(unsigned char)'<'] = "<"; + replacement[(unsigned char)'>'] = ">"; + replacement[(unsigned char)'\t'] = " "; + replacement[(unsigned char)'\n'] = " "; + replacement[(unsigned char)'\r'] = " "; + } + + // What each byte is written as, or 0 if it is written as it is. + const char* replacement[256]; +}; + + +const Replacements& replacements() +{ + static const Replacements result; + return result; +} + + +struct Stream_Sink +{ + std::ostream& out; + void append(const char* s, std::string::size_type n) { out.write(s, n); } + void append(const char* s) { out< +void write_escaped(const std::string& s, Sink& sink) +{ + const Replacements& table = replacements(); + const char* run = s.data(); + const char* end = s.data() + s.size(); + for (const char* pos = run; pos != end; ++pos) + { + const char* replacement = table.replacement[(unsigned char)*pos]; + if (replacement) { - if ((s[i] == '\n') || (s[i] == '\t') || (s[i] == '\r')) - result += std::string("&#x") + digit[s[i] / 16] + digit[s[i] % 16] + ';'; - else - result += '?'; + sink.append(run, pos - run); + sink.append(replacement); + run = pos + 1; } - else - result += s[i]; } + sink.append(run, end - run); +} + +} + + +std::string escape_xml(const std::string& s) +{ + std::string result; + result.reserve(s.size()); + write_escaped(s, result); return result; } + + +std::ostream& operator<<(std::ostream& out, const Xml_Escaped& e) +{ + Stream_Sink sink{out}; + write_escaped(e.s, sink); + return out; +} diff --git a/src/expat/escape_xml.h b/src/expat/escape_xml.h index 36284682e..e6d2b5644 100644 --- a/src/expat/escape_xml.h +++ b/src/expat/escape_xml.h @@ -19,10 +19,21 @@ #ifndef DE__OSM3S___EXPAT__ESCAPE_XML_H #define DE__OSM3S___EXPAT__ESCAPE_XML_H +#include #include std::string escape_xml(const std::string& s); +// Writes escape_xml(s) to a stream without building the escaped copy. s must outlive the expression. +struct Xml_Escaped +{ + explicit Xml_Escaped(const std::string& s_) : s(s_) {} + const std::string& s; +}; + +std::ostream& operator<<(std::ostream& out, const Xml_Escaped& e); + + #endif diff --git a/src/overpass_api/output_formats/output_custom.cc b/src/overpass_api/output_formats/output_custom.cc index 0ac46c192..6bc78b7c6 100644 --- a/src/overpass_api/output_formats/output_custom.cc +++ b/src/overpass_api/output_formats/output_custom.cc @@ -262,7 +262,7 @@ std::string process_members(const std::string& raw_template, uint32 id, const Re else if (raw_template.substr(new_pos, 10) == "{{{role}}}") { std::map< uint32, std::string >::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) {