Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoformat C++ via Clang Format #23

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
BasedOnStyle: LLVM
Cpp11BracedListStyle: true
IndentWidth: 4
AlignAfterOpenBracket: BlockIndent
BreakBeforeBraces: Custom
BraceWrapping:
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterStruct: true
BeforeElse: true
SplitEmptyFunction: false
BinPackParameters: false
BreakBeforeBraces: Allman
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.cpp]
indent_size = 4
indent_style = space
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/github-workflow.json
---
name: Continuous Integration

on:
pull_request:
branches:
- main
workflow_dispatch: {}

jobs:
nix-flake-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v24
- run: nix flake check --show-trace
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
check.enable = true;
settings.hooks = {
alejandra.enable = true;
clang-format = {
enable = true;
excludes = ["extern/.*"];
};
};
};

Expand Down
5 changes: 1 addition & 4 deletions src/batched_print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ inline void batched_print(string_view s)
batched_print_flush_if_needed();
}

inline void batched_print_no_flush(string_view s)
{
batched_out.append(s);
}
inline void batched_print_no_flush(string_view s) { batched_out.append(s); }

inline void batched_print(char c)
{
Expand Down
4 changes: 2 additions & 2 deletions src/builder.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <string_view>
#include <map>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include <map>
using std::string;
using std::string_view;

Expand Down
103 changes: 69 additions & 34 deletions src/fastgron.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "simdjson.h"
#include <algorithm>
#include <cstring> // for strcmp
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <string_view>
#include <map>
#include <cstring> // for strcmp
#include <variant>
#include <sys/stat.h>
#include <variant>
#include <vector>
#include <version.h>

#ifndef FASTGRON_VERSION
Expand All @@ -34,12 +34,12 @@ using namespace std;

string out;

#include "growing_string.hpp"
#include "batched_print.hpp"
#include "growing_string.hpp"
#include "jsonutils.hpp"
#include "print_gron.hpp"
#include "print_filtered_path.hpp"
#include "parse_gron.hpp"
#include "print_filtered_path.hpp"
#include "print_gron.hpp"
#include "print_json.hpp"

// Parse command-line options
Expand Down Expand Up @@ -70,8 +70,10 @@ bool is_url(string_view url)
void print_simdjson_version()
{
cerr << "simdjson v" << SIMDJSON_VERSION << endl;
cerr << " Detected the best implementation for your machine: " << simdjson::get_active_implementation()->name();
cerr << "(" << simdjson::get_active_implementation()->description() << ")" << endl;
cerr << " Detected the best implementation for your machine: "
<< simdjson::get_active_implementation()->name();
cerr << "(" << simdjson::get_active_implementation()->description() << ")"
<< endl;
}

growing_string indent = "";
Expand All @@ -96,26 +98,35 @@ void print_help()
" -V, --version show version information and exit\n"
" -s, --stream enable stream mode\n"
" -F, --fixed-string PATTERN filter output by fixed string.\n"
" If -F is provided multiple times, multiple patterns are searched.\n"
" -v, --invert-match select non-matching lines for fixed string search\n"
" If -F is provided multiple times, multiple "
"patterns are searched.\n"
" -v, --invert-match select non-matching lines for fixed string "
"search\n"
" -i, --ignore-case ignore case distinctions in PATTERN\n"
" --sort sort output by key\n"
" --user-agent set user agent\n"
" --header Name:value set custom HTTP header, can be used multiple times\n"
" --header Name:value set custom HTTP header, can be used "
"multiple times\n"
" -u, --ungron ungron: convert gron output back to JSON\n"
" -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"
" More complex path expressions: .{id,users[1:-3:2].{name,address}}\n"
" [[3]] is an index accessor without outputting on the path.\n"
" -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"
" More complex path expressions: "
".{id,users[1:-3:2].{name,address}}\n"
" [[3]] is an index accessor without outputting on the "
"path.\n"
" --no-indent don't indent output\n"
" --no-newline no newline inside JSON output\n"
" --root root path, default is json\n"
" --semicolon add semicolon to the end of each line\n"
" --no-spaces don't add spaces around =\n"
" -c, --color colorize output\n"
" --no-color don't colorize output\n"
"\nHome page with more information: https://github.com/adamritter/fastgron\n"
"\nIf you have a feature that would help you, open an issue here:\nhttps://github.com/adamritter/fastgron/issues\n";
"\nHome page with more information: "
"https://github.com/adamritter/fastgron\n"
"\nIf you have a feature that would help you, open an issue "
"here:\nhttps://github.com/adamritter/fastgron/issues\n";
}

void print_version()
Expand Down Expand Up @@ -214,7 +225,8 @@ std::string download(options opts)
if (res != CURLE_OK)
{
const char *curl_err_msg = curl_easy_strerror(res);
cerr << "Error when downloading data: " << string(curl_err_msg) << "\n";
cerr << "Error when downloading data: " << string(curl_err_msg)
<< "\n";
exit(EXIT_FAILURE);
}
free(s.ptr);
Expand Down Expand Up @@ -363,7 +375,8 @@ options parse_options(int argc, char *argv[])
}
else
{
if (!is_url(argv[i]) && access(argv[i], F_OK) == -1 && argv[i] != string("-"))
if (!is_url(argv[i]) && access(argv[i], F_OK) == -1 &&
argv[i] != string("-"))
{
// Treat strings starting with . as paths
if (argv[i][0] == '.')
Expand Down Expand Up @@ -400,8 +413,10 @@ int main(int argc, char *argv[])
{
for (auto &filter : filters)
{
std::transform(filter.begin(), filter.end(), filter.begin(), [](unsigned char c)
{ return fast_tolower(c); });
std::transform(
filter.begin(), filter.end(), filter.begin(),
[](unsigned char c) { return fast_tolower(c); }
);
}
}

Expand Down Expand Up @@ -429,7 +444,8 @@ int main(int argc, char *argv[])
// Load string from stdin
json = padded_string(readFileIntoString(0));
}
else if (curl_found && opts.filename.compare(0, 7, "http://") == 0 || opts.filename.compare(0, 8, "https://") == 0)
else if (curl_found && opts.filename.compare(0, 7, "http://") == 0 ||
opts.filename.compare(0, 8, "https://") == 0)
{
json = padded_string(download(opts));
}
Expand Down Expand Up @@ -463,31 +479,48 @@ int main(int argc, char *argv[])
string_view line = string_view(data, end - data);
// find commonality with last line
int common = 0;
while (common < line.size() && common < last_line.size() && line[common] == last_line[common])
while (common < line.size() && common < last_line.size() &&
line[common] == last_line[common])
{
common++;
}
// if it's not a token ending, make sure we don't use the last token
if (common < line.size() && line[common] != ' ' && line[common] != '=' && line[common] != '.' && line[common] != '[')
if (common < line.size() && line[common] != ' ' &&
line[common] != '=' && line[common] != '.' &&
line[common] != '[')
{
common--;
}
int index = 0;
while (index < parse_gron_builder_offsets.size() && parse_gron_builder_offsets[index] <= common)
while (index < parse_gron_builder_offsets.size() &&
parse_gron_builder_offsets[index] <= common)
{
index++;
}
parse_gron_builders.erase(parse_gron_builders.begin() + index, parse_gron_builders.end());
parse_gron_builder_offsets.erase(parse_gron_builder_offsets.begin() + index, parse_gron_builder_offsets.end());
Builder &passed_builder = parse_gron_builders.empty() ? builder : *parse_gron_builders.back();
int offset = parse_gron_builders.empty() ? 0 : parse_gron_builder_offsets.back();
parse_gron(line.substr(offset), passed_builder, offset, parse_gron_builders, parse_gron_builder_offsets);
parse_gron_builders.erase(
parse_gron_builders.begin() + index, parse_gron_builders.end()
);
parse_gron_builder_offsets.erase(
parse_gron_builder_offsets.begin() + index,
parse_gron_builder_offsets.end()
);
Builder &passed_builder = parse_gron_builders.empty()
? builder
: *parse_gron_builders.back();
int offset = parse_gron_builders.empty()
? 0
: parse_gron_builder_offsets.back();
parse_gron(
line.substr(offset), passed_builder, offset,
parse_gron_builders, parse_gron_builder_offsets
);

// parse_gron(line, builder, 0);
last_line = line;
data = end + 1;
}
if (std::holds_alternative<string_variant>(builder) && std::get<string_variant>(builder) == "")
if (std::holds_alternative<string_variant>(builder) &&
std::get<string_variant>(builder) == "")
{
cerr << "Builder is not assigned\n";
return EXIT_FAILURE;
Expand All @@ -506,7 +539,9 @@ int main(int argc, char *argv[])
else
{
// Error: not a stream
cerr << "Error: input gron file must be an array to be able to output a stream\n";
cerr << "Error: input gron file must be an array to be able to "
"output "
"a stream\n";
}
}
else
Expand Down
30 changes: 8 additions & 22 deletions src/growing_string.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#pragma once

#include <string_view>
#include <algorithm>
#include <cstring>
#include <string_view>
using std::max;
using std::string_view;

// Provides a dynamic string storage that grows as needed.
// Unlike std::string, it doesn't shrink the allocated storage capacity when the end of the string is erased.
// Unlike std::string, it doesn't shrink the allocated storage capacity when the
// end of the string is erased.
struct growing_string
{
char *data;
size_t len = 0;
size_t capacity;

growing_string() : capacity(1000)
{
data = new char[capacity];
}
growing_string() : capacity(1000) { data = new char[capacity]; }

bool starts_with(string_view s) const
{
Expand All @@ -41,10 +39,7 @@ struct growing_string
memcpy(data, s, len);
}

~growing_string()
{
delete[] data;
}
~growing_string() { delete[] data; }
void reserve_extra(size_t extra)
{
if (len + extra > capacity)
Expand All @@ -57,10 +52,7 @@ struct growing_string
}
}

string_view view() const
{
return std::string_view(data, len);
}
string_view view() const { return std::string_view(data, len); }

growing_string &append(string_view s)
{
Expand All @@ -86,15 +78,9 @@ struct growing_string
return *this;
}

inline size_t size() const
{
return len;
}
inline size_t size() const { return len; }

operator string_view() const
{
return {data, len};
}
operator string_view() const { return {data, len}; }

void clear_mem()
{
Expand Down
14 changes: 7 additions & 7 deletions src/jsonutils.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <string_view>
#include <algorithm>
#include <string>
#include <string_view>
#include <vector>
#include <algorithm>

using std::string;
using std::string_view;
Expand Down Expand Up @@ -86,7 +86,6 @@ inline int raw_json_string_length(string_view str)
return -1;
}


// Returns -1 if end is reached before closing quote
inline int raw_json_string_length(const char *s)
{
Expand Down Expand Up @@ -124,7 +123,8 @@ inline int raw_json_string_length(const char *s)
return -1;
}

inline bool can_show(string_view s, const unsigned flags, vector<string> &filters)
inline bool
can_show(string_view s, const unsigned flags, vector<string> &filters)
{
if (!filters.empty())
{
Expand All @@ -135,10 +135,10 @@ inline bool can_show(string_view s, const unsigned flags, vector<string> &filter
{
// std::tolower is slow, and doesn't handle UTF-8
auto it = std::search(
s.begin(), s.end(),
filter.begin(), filter.end(),
s.begin(), s.end(), filter.begin(), filter.end(),
[](unsigned char ch1, unsigned char ch2)
{ return fast_tolower(ch1) == (ch2); });
{ return fast_tolower(ch1) == (ch2); }
);
if (it != s.end())
{
found = true;
Expand Down
Loading