- output only get escaped when actually output. Better seperation of functionality

- refactor facade::GetEscapeName() into get_name_for_id() call that is implemented in subclasses
- remove dead code
- fix failing tests where names got double-escaped
- fixes https://github.com/Project-OSRM/node-osrm/issues/83
This commit is contained in:
Dennis Luxen
2015-02-26 10:11:33 +01:00
parent 37fb89c691
commit 51e42ded44
9 changed files with 50 additions and 84 deletions
+6 -39
View File
@@ -32,6 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define JSON_RENDERER_HPP
#include "cast.hpp"
#include "string_util.hpp"
#include <osrm/json_container.hpp>
@@ -40,31 +41,14 @@ namespace osrm
namespace json
{
struct Renderer : mapbox::util::static_visitor<>
{
explicit Renderer(std::ostream &_out) : out(_out) {}
void operator()(const String &string) const {
void operator()(const String &string) const
{
out << "\"";
// check if we need escaping
std::size_t pos = string.value.find_first_of('\\');
if (pos != std::string::npos)
{
std::string escapedString(string.value);
do
{
escapedString.insert(pos, 1, '\\');
pos = escapedString.find_first_of('\\', pos);
} while (pos != std::string::npos);
}
// no need to escape
else
{
out << string.value;
}
out << escape_JSON(string.value);
out << "\"";
}
@@ -123,25 +107,8 @@ struct ArrayRenderer : mapbox::util::static_visitor<>
void operator()(const String &string) const
{
out.push_back('\"');
// check if we need escaping
std::size_t pos = string.value.find_first_of('\\');
if (pos != std::string::npos)
{
std::string escapedString(string.value);
do
{
escapedString.insert(pos, 1, '\\');
pos = escapedString.find_first_of('\\', pos+2);
} while (pos != std::string::npos);
out.insert(out.end(), escapedString.begin(), escapedString.end());
}
// no need to escape
else
{
out.insert(out.end(), string.value.begin(), string.value.end());
}
const auto string_to_insert = escape_JSON(string.value);
out.insert(std::end(out), std::begin(string_to_insert), std::end(string_to_insert));
out.push_back('\"');
}
+9 -2
View File
@@ -77,10 +77,17 @@ inline void replaceAll(std::string &s, const std::string &sub, const std::string
boost::replace_all(s, sub, other);
}
inline std::string EscapeJSONString(const std::string &input)
inline std::string escape_JSON(const std::string &input)
{
// return the input if no backslash can be found -> no allocations
if (input.find_first_of('\\') == std::string::npos)
{
return input;
}
// escape and skip reallocations if possible
std::string output;
output.reserve(input.size());
output.reserve(input.size() + 4); // +4 assumes two backslashes on avg
for (auto iter = input.begin(); iter != input.end(); ++iter)
{
switch (iter[0])