Skip to content

Commit

Permalink
--semicolon
Browse files Browse the repository at this point in the history
  • Loading branch information
adamritter committed Jun 1, 2023
1 parent e1f9607 commit 1cfa399
Showing 1 changed file with 54 additions and 10 deletions.
64 changes: 54 additions & 10 deletions src/fastgron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ using namespace std;

string out;

bool semicolon = false;

struct growing_string
{
std::vector<char> data;
Expand Down Expand Up @@ -263,7 +265,12 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
case ondemand::json_type::array:
{
size_t orig_base_len = path.size();
path.append(" = [];\n");
path.append(" = []");
if (semicolon)
{
path.append(';');
}
path.append('\n');
gprint(path, out_growing_string);
path.erase(orig_base_len);
uint64_t index = 0;
Expand All @@ -284,7 +291,12 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
case ondemand::json_type::object:
{
size_t base_len = path.size();
path.append(" = {};\n");
path.append(" = {}");
if (semicolon)
{
path.append(';');
}
path.append('\n');
gprint(path, out_growing_string);
path.erase(base_len);
if (sort_output)
Expand Down Expand Up @@ -358,7 +370,10 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
}
memcpy(ptr, s.data(), s.size());
ptr += s.size();
*ptr++ = ';';
if (semicolon)
{
*ptr++ = ';';
}
*ptr++ = '\n';
if (can_show(string_view(&out_growing_string.data[orig_out_len], ptr - &out_growing_string.data[orig_out_len])))
{
Expand All @@ -381,7 +396,10 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
}
memcpy(ptr, s.data(), s.size());
ptr += s.size();
*ptr++ = ';';
if (semicolon)
{
*ptr++ = ';';
}
*ptr++ = '\n';
path.len = ptr - &path.data[0];
gprint(path, out_growing_string);
Expand All @@ -393,11 +411,21 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
size_t base_len = path.size();
if (element.get_bool())
{
path.append(" = true;\n");
path.append(" = true");
if (semicolon)
{
path.append(';');
}
path.append('\n');
}
else
{
path.append(" = false;\n");
path.append(" = false");
if (semicolon)
{
path.append(';');
}
path.append('\n');
}
gprint(path, out_growing_string);
path.erase(base_len);
Expand All @@ -408,7 +436,12 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
if (element.is_null())
{
size_t base_len = path.size();
path.append(" = null;\n");
path.append(" = null");
if (semicolon)
{
path.append(';');
}
path.append('\n');
gprint(path, out_growing_string);
path.erase(base_len);
}
Expand All @@ -429,7 +462,6 @@ struct options
std::string filtered_path;
};
string root = "json";
bool semicolon = false;

string user_agent = "fastgron";
bool no_indent = false;
Expand Down Expand Up @@ -523,6 +555,16 @@ options parse_options(int argc, char *argv[])
}
root = argv[++i];
}
else if (strcmp(argv[i], "--semicolon") == 0)
{
semicolon = true;
}

else if (argv[i][0] == '-')
{
cerr << "Unknown option: " << argv[i] << "\n";
exit(EXIT_FAILURE);
}
else
{
if (access(argv[i], F_OK) == -1 && argv[i] != string("-"))
Expand Down Expand Up @@ -747,8 +789,10 @@ void print_help()
" -p, -path filter path, for example .#.3.population or cities.#.population\n"
" -p is optional if path starts with . and file with that name doesn't exist\n"
" --no-indent don't indent output\n"
" --root root path, default is json\n\n"
"Home page with more information: https://github.com/adamritter/fastgron\n";
" --root root path, default is json\n"
" --semicolon add semicolon to the end of each line\n"
" --no-space don't add space before and after =\n"
"\nHome page with more information: https://github.com/adamritter/fastgron\n";
}

void print_version()
Expand Down

0 comments on commit 1cfa399

Please sign in to comment.