Skip to content

Commit

Permalink
Improved string escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
adamritter committed Dec 5, 2023
1 parent 81c6d56 commit 71cdbe8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/jsonutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ inline int raw_json_string_length(string_view str)
return s - str.begin();
}
break;
case '\n':
case '\r':
case 0:
return -1;
default:
if (in_escape)
{
Expand Down
9 changes: 6 additions & 3 deletions src/parse_gron.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string_view>
#include "parse_gron.hpp"
#include "jsonutils.hpp"
#include <stdexcept>


Expand Down Expand Up @@ -48,11 +49,13 @@ void parse_gron(string_view line, Builder &builder, int offset,

// find end of key
size_t end = 2;
while (end < line.size() && line[end] != '"')
size_t len = raw_json_string_length(line.substr(2));
if (len == -1)
{
end++;
throw std::runtime_error("Expected \"");
}
string key(line.substr(2, end - 2));
end += len;
string key(line.substr(2, len));
auto child = map_alt.find(key);
if (child == map_alt.end())
{
Expand Down
23 changes: 14 additions & 9 deletions src/print_gron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ void recursive_print_gron(simdjson::ondemand::value element, growing_string &pat
growing_string out2;
for (auto field : element.get_object())
{
auto key = field.unescaped_key();
string key_str(key.value());
if (!is_js_identifier(key.value()))
auto key_orig = field.key();
auto key_value_raw = key_orig.value().raw();
auto key = string_view(key_value_raw, raw_json_string_length(key_value_raw));

string key_str(key);
if (!is_js_identifier(key))
{
if (flags & COLOR)
path.append("\033[1;34m[\033[1;35m\"");
else
path.append("[\"");
path.append(key.value());
path.append(key);

if (flags & COLOR)
path.append("\"\033[1;34m]\033[0m");
Expand All @@ -109,7 +112,7 @@ void recursive_print_gron(simdjson::ondemand::value element, growing_string &pat
path.append(".");
if (flags & COLOR)
path.append("\033[1;34m");
path.append(key.value());
path.append(key);
if (flags & COLOR)
path.append("\033[0m");
}
Expand All @@ -130,14 +133,16 @@ void recursive_print_gron(simdjson::ondemand::value element, growing_string &pat

for (auto field : element.get_object())
{
auto key = field.unescaped_key();
if (!is_js_identifier(key.value()))
auto key_orig = field.key();
auto key_value_raw = key_orig.value().raw();
auto key = string_view(key_value_raw, raw_json_string_length(key_value_raw));
if (!is_js_identifier(key))
{
if (flags & COLOR)
path.append("\033[1;34m[\033[1;35m\"");
else
path.append("[\"");
path.append(key.value());
path.append(key);
if (flags & COLOR)
path.append("\"\033[1;34m]\033[0m");
else
Expand All @@ -148,7 +153,7 @@ void recursive_print_gron(simdjson::ondemand::value element, growing_string &pat
path.append(".");
if (flags & COLOR)
path.append("\033[1;34m");
path.append(key.value());
path.append(key);
if (flags & COLOR)
path.append("\033[0m");
}
Expand Down

0 comments on commit 71cdbe8

Please sign in to comment.