230531.11 · 57afa8b856bcc3405db013ca737152da9c88d55c
Moved headers inside a hypergrep namespace directory
reading declarations
include/compiler.hpp
@@ -1,9 +0,0 @@-#pragma once-#include <fmt/core.h>-#include <hs/hs.h>-#include <vector>-#include <string>--struct search_options;--void compile_hs_database(hs_database** database, hs_scratch** scratch, search_options& options, const std::vector<std::string> &pattern_list);
include/constants.hpp
@@ -1,12 +0,0 @@-#pragma once-#include <string_view>--constexpr static inline std::string_view NAME = "hg";-constexpr static inline std::string_view DESCRIPTION =- "Recursively search directories for a regex pattern";-constexpr static inline std::string_view VERSION = "0.1.0";-constexpr static inline std::size_t TYPICAL_FILESYSTEM_BLOCK_SIZE = 4096;-constexpr static inline std::size_t FILE_CHUNK_SIZE =- 16 * TYPICAL_FILESYSTEM_BLOCK_SIZE;-constexpr static inline std::size_t LARGE_FILE_SIZE = 1024 * 1024;-constexpr static inline std::size_t MAX_LINE_LENGTH = 4096;
include/directory_search.hpp
@@ -1,86 +0,0 @@-#pragma once-#include <argparse/argparse.hpp>-#include <atomic>-#include <chrono>-#include <compiler.hpp>-#include <concurrentqueue/concurrentqueue.h>-#include <constants.hpp>-#include <search_options.hpp>-#include <dirent.h>-#include <fcntl.h>-#include <file_filter.hpp>-#include <file_search.hpp>-#include <filesystem>-#include <fmt/color.h>-#include <fmt/format.h>-#include <fstream>-#include <git2.h>-#include <git_index_search.hpp>-#include <hs/hs.h>-#include <is_binary.hpp>-#include <limits>-#include <match_handler.hpp>-#include <numeric>-#include <size_to_bytes.hpp>-#include <sys/mman.h>-#include <sys/stat.h>-#include <thread>-#include <unistd.h>-#include <vector>--class directory_search {-public:- directory_search(std::string &pattern, const std::filesystem::path &path,- argparse::ArgumentParser &program);- ~directory_search();- void run(std::filesystem::path path);--private:- bool process_file(std::string &&filename, hs_scratch_t *local_scratch,- char *buffer, std::string &lines);-- bool try_dequeue_and_process_path(moodycamel::ConsumerToken &ctok,- hs_scratch_t *local_scratch, char *buffer,- std::string &lines);-- void search_thread_function();-- void visit_directory_and_enqueue(moodycamel::ProducerToken &ptok,- std::string directory,- hs_scratch *local_file_filter_scratch);--private:- std::filesystem::path search_path;-- // Directory traversal- moodycamel::ConcurrentQueue<std::string> subdirectories;- std::atomic<std::size_t> num_dirs_enqueued{0};-- moodycamel::ConcurrentQueue<std::string> queue;-- std::atomic<bool> running{true};- std::atomic<std::size_t> num_files_enqueued{0};- std::atomic<std::size_t> num_files_dequeued{0};-- hs_database_t *database = NULL;- hs_scratch_t *scratch = NULL;- hs_database_t *file_filter_database = NULL;- hs_scratch_t *file_filter_scratch = NULL;- // If the filter pattern starts with '!'- // then negate the result of the filter- bool negate_filter{false};-- search_options options;-- // Optimizations for large files- struct large_file {- std::string path;- std::size_t size;- };- moodycamel::ConcurrentQueue<large_file> large_file_backlog;- std::atomic<std::size_t> num_large_files_enqueued{0};-- // Optimizations for git repos- moodycamel::ConcurrentQueue<std::string> git_repo_paths;- std::atomic<std::size_t> num_git_repos_enqueued{0};-};
include/file_context.hpp
@@ -1,12 +0,0 @@-#pragma once-#include <atomic>-#include <hs/hs.h>-#include <mutex>-#include <vector>--struct file_context {- std::atomic<size_t> &number_of_matches;- std::vector<std::pair<unsigned long long, unsigned long long>> &matches;- std::mutex &match_mutex;- bool option_print_only_filenames;-};
include/file_filter.hpp
@@ -1,17 +0,0 @@-#pragma once-#include <hs/hs.h>-#include <cstdio>-#include <cstring>-#include <search_options.hpp>--struct filter_context {- bool result{false};-};--bool construct_file_filtering_hs_database(hs_database** file_filter_database, hs_scratch** file_filter_scratch, search_options& options);--int on_file_filter_match(unsigned int id, unsigned long long from,- unsigned long long to, unsigned int flags,- void *ctx);--bool filter_file(const char *path, hs_database* file_filter_database, hs_scratch *local_file_filter_scratch, const bool& negate_filter);
include/file_search.hpp
@@ -1,56 +0,0 @@-#pragma once-#include <argparse/argparse.hpp>-#include <atomic>-#include <chrono>-#include <compiler.hpp>-#include <concurrentqueue/concurrentqueue.h>-#include <constants.hpp>-#include <fcntl.h>-#include <search_options.hpp>-#include <filesystem>-#include <fmt/color.h>-#include <fmt/format.h>-#include <fstream>-#include <git2.h>-#include <hs/hs.h>-#include <is_binary.hpp>-#include <limits>-#include <match_handler.hpp>-#include <numeric>-#include <sys/mman.h>-#include <sys/stat.h>-#include <thread>-#include <unistd.h>-#include <vector>--class file_search {-public:- file_search(std::string &pattern, argparse::ArgumentParser &program);- file_search(hs_database_t *database, hs_scratch_t *scratch,- const search_options &options);- ~file_search();-- void run(std::filesystem::path path,- std::optional<std::size_t> maybe_file_size = {});- bool scan_line(std::string &line, std::size_t ¤t_line_number,- bool &break_loop);--private:- bool mmap_and_scan(std::string &&filename,- std::optional<std::size_t> maybe_file_size = {});--private:- bool non_owning_database{false};-- hs_database_t *database = NULL;- hs_scratch_t *scratch = NULL;- hs_database_t *file_filter_database = NULL;- hs_scratch_t *file_filter_scratch = NULL;- // If the filter pattern starts with '!'- // then negate the result of the filter- bool negate_filter{false};-- std::vector<hs_scratch *> thread_local_scratch;- std::vector<hs_scratch *> thread_local_scratch_per_line;- search_options options;-};
include/git_index_search.hpp
@@ -1,84 +0,0 @@-#pragma once-#include <argparse/argparse.hpp>-#include <atomic>-#include <chrono>-#include <compiler.hpp>-#include <concurrentqueue/concurrentqueue.h>-#include <constants.hpp>-#include <search_options.hpp>-#include <fcntl.h>-#include <file_filter.hpp>-#include <file_search.hpp>-#include <filesystem>-#include <fmt/color.h>-#include <fmt/format.h>-#include <fstream>-#include <git2.h>-#include <hs/hs.h>-#include <is_binary.hpp>-#include <limits>-#include <match_handler.hpp>-#include <numeric>-#include <size_to_bytes.hpp>-#include <sys/mman.h>-#include <sys/stat.h>-#include <thread>-#include <unistd.h>-#include <vector>--class git_index_search {-public:- git_index_search(std::string &pattern, const std::filesystem::path &path,- argparse::ArgumentParser &program);- git_index_search(hs_database_t *database, hs_scratch_t *scratch,- hs_database_t *file_filter_database,- hs_scratch_t *file_filter_scratch, const search_options &options,- const std::filesystem::path &path);- ~git_index_search();- void run(std::filesystem::path path);--private:- bool process_file(const char *filename, hs_scratch_t *local_scratch,- char *buffer, std::string &lines);-- bool search_submodules(const char *dir, git_repository *this_repo);-- bool visit_git_index(const std::filesystem::path &dir, git_index *index);-- bool visit_git_repo(const std::filesystem::path &dir,- git_repository *repo = nullptr);-- bool try_dequeue_and_process_path(hs_scratch_t *local_scratch, char *buffer,- std::string &lines);-- void search_thread_function();--private:- static inline bool libgit2_initialized{false};- std::filesystem::path basepath{};-- moodycamel::ConcurrentQueue<const char *> queue;- moodycamel::ProducerToken ptok{queue};-- std::atomic<bool> running{true};- std::atomic<std::size_t> num_files_enqueued{0};- std::atomic<std::size_t> num_files_dequeued{0};-- bool non_owning_database{false};- hs_database_t *database = NULL;- hs_scratch_t *scratch = NULL;- hs_database_t *file_filter_database = NULL;- hs_scratch_t *file_filter_scratch = NULL;- // If the filter pattern starts with '!'- // then negate the result of the filter- bool negate_filter{false};-- search_options options;-- std::vector<git_repository *> garbage_collect_repo;- std::vector<git_index *> garbage_collect_index;- std::vector<git_index_iterator *> garbage_collect_index_iterator;-- // Backlog- std::vector<std::filesystem::path> submodule_paths;-};
include/hypergrep/compiler.hpp
@@ -0,0 +1,11 @@+#pragma once+#include <fmt/core.h>+#include <hs/hs.h>+#include <string>+#include <vector>++struct search_options;++void compile_hs_database(hs_database **database, hs_scratch **scratch,+ search_options &options,+ const std::vector<std::string> &pattern_list);
include/hypergrep/constants.hpp
@@ -0,0 +1,12 @@+#pragma once+#include <string_view>++constexpr static inline std::string_view NAME = "hg";+constexpr static inline std::string_view DESCRIPTION =+ "Recursively search directories for a regex pattern";+constexpr static inline std::string_view VERSION = "0.1.0";+constexpr static inline std::size_t TYPICAL_FILESYSTEM_BLOCK_SIZE = 4096;+constexpr static inline std::size_t FILE_CHUNK_SIZE =+ 16 * TYPICAL_FILESYSTEM_BLOCK_SIZE;+constexpr static inline std::size_t LARGE_FILE_SIZE = 1024 * 1024;+constexpr static inline std::size_t MAX_LINE_LENGTH = 4096;
include/hypergrep/directory_search.hpp
@@ -0,0 +1,86 @@+#pragma once+#include <argparse/argparse.hpp>+#include <atomic>+#include <chrono>+#include <concurrentqueue/concurrentqueue.h>+#include <dirent.h>+#include <fcntl.h>+#include <filesystem>+#include <fmt/color.h>+#include <fmt/format.h>+#include <fstream>+#include <git2.h>+#include <hs/hs.h>+#include <hypergrep/compiler.hpp>+#include <hypergrep/constants.hpp>+#include <hypergrep/file_filter.hpp>+#include <hypergrep/file_search.hpp>+#include <hypergrep/git_index_search.hpp>+#include <hypergrep/is_binary.hpp>+#include <hypergrep/match_handler.hpp>+#include <hypergrep/search_options.hpp>+#include <hypergrep/size_to_bytes.hpp>+#include <limits>+#include <numeric>+#include <sys/mman.h>+#include <sys/stat.h>+#include <thread>+#include <unistd.h>+#include <vector>++class directory_search {+public:+ directory_search(std::string &pattern, const std::filesystem::path &path,+ argparse::ArgumentParser &program);+ ~directory_search();+ void run(std::filesystem::path path);++private:+ bool process_file(std::string &&filename, hs_scratch_t *local_scratch,+ char *buffer, std::string &lines);++ bool try_dequeue_and_process_path(moodycamel::ConsumerToken &ctok,+ hs_scratch_t *local_scratch, char *buffer,+ std::string &lines);++ void search_thread_function();++ void visit_directory_and_enqueue(moodycamel::ProducerToken &ptok,+ std::string directory,+ hs_scratch *local_file_filter_scratch);++private:+ std::filesystem::path search_path;++ // Directory traversal+ moodycamel::ConcurrentQueue<std::string> subdirectories;+ std::atomic<std::size_t> num_dirs_enqueued{0};++ moodycamel::ConcurrentQueue<std::string> queue;++ std::atomic<bool> running{true};+ std::atomic<std::size_t> num_files_enqueued{0};+ std::atomic<std::size_t> num_files_dequeued{0};++ hs_database_t *database = NULL;+ hs_scratch_t *scratch = NULL;+ hs_database_t *file_filter_database = NULL;+ hs_scratch_t *file_filter_scratch = NULL;+ // If the filter pattern starts with '!'+ // then negate the result of the filter+ bool negate_filter{false};++ search_options options;++ // Optimizations for large files+ struct large_file {+ std::string path;+ std::size_t size;+ };+ moodycamel::ConcurrentQueue<large_file> large_file_backlog;+ std::atomic<std::size_t> num_large_files_enqueued{0};++ // Optimizations for git repos+ moodycamel::ConcurrentQueue<std::string> git_repo_paths;+ std::atomic<std::size_t> num_git_repos_enqueued{0};+};
include/hypergrep/file_context.hpp
@@ -0,0 +1,12 @@+#pragma once+#include <atomic>+#include <hs/hs.h>+#include <mutex>+#include <vector>++struct file_context {+ std::atomic<size_t> &number_of_matches;+ std::vector<std::pair<unsigned long long, unsigned long long>> &matches;+ std::mutex &match_mutex;+ bool option_print_only_filenames;+};
include/hypergrep/file_filter.hpp
@@ -0,0 +1,20 @@+#pragma once+#include <cstdio>+#include <cstring>+#include <hs/hs.h>+#include <hypergrep/search_options.hpp>++struct filter_context {+ bool result{false};+};++bool construct_file_filtering_hs_database(hs_database **file_filter_database,+ hs_scratch **file_filter_scratch,+ search_options &options);++int on_file_filter_match(unsigned int id, unsigned long long from,+ unsigned long long to, unsigned int flags, void *ctx);++bool filter_file(const char *path, hs_database *file_filter_database,+ hs_scratch *local_file_filter_scratch,+ const bool &negate_filter);
include/hypergrep/file_search.hpp
@@ -0,0 +1,56 @@+#pragma once+#include <argparse/argparse.hpp>+#include <atomic>+#include <chrono>+#include <concurrentqueue/concurrentqueue.h>+#include <fcntl.h>+#include <filesystem>+#include <fmt/color.h>+#include <fmt/format.h>+#include <fstream>+#include <git2.h>+#include <hs/hs.h>+#include <hypergrep/compiler.hpp>+#include <hypergrep/constants.hpp>+#include <hypergrep/is_binary.hpp>+#include <hypergrep/match_handler.hpp>+#include <hypergrep/search_options.hpp>+#include <limits>+#include <numeric>+#include <sys/mman.h>+#include <sys/stat.h>+#include <thread>+#include <unistd.h>+#include <vector>++class file_search {+public:+ file_search(std::string &pattern, argparse::ArgumentParser &program);+ file_search(hs_database_t *database, hs_scratch_t *scratch,+ const search_options &options);+ ~file_search();++ void run(std::filesystem::path path,+ std::optional<std::size_t> maybe_file_size = {});+ bool scan_line(std::string &line, std::size_t ¤t_line_number,+ bool &break_loop);++private:+ bool mmap_and_scan(std::string &&filename,+ std::optional<std::size_t> maybe_file_size = {});++private:+ bool non_owning_database{false};++ hs_database_t *database = NULL;+ hs_scratch_t *scratch = NULL;+ hs_database_t *file_filter_database = NULL;+ hs_scratch_t *file_filter_scratch = NULL;+ // If the filter pattern starts with '!'+ // then negate the result of the filter+ bool negate_filter{false};++ std::vector<hs_scratch *> thread_local_scratch;+ std::vector<hs_scratch *> thread_local_scratch_per_line;+ search_options options;+};
include/hypergrep/git_index_search.hpp
@@ -0,0 +1,85 @@+#pragma once+#include <argparse/argparse.hpp>+#include <atomic>+#include <chrono>+#include <concurrentqueue/concurrentqueue.h>+#include <fcntl.h>+#include <filesystem>+#include <fmt/color.h>+#include <fmt/format.h>+#include <fstream>+#include <git2.h>+#include <hs/hs.h>+#include <hypergrep/compiler.hpp>+#include <hypergrep/constants.hpp>+#include <hypergrep/file_filter.hpp>+#include <hypergrep/file_search.hpp>+#include <hypergrep/is_binary.hpp>+#include <hypergrep/match_handler.hpp>+#include <hypergrep/search_options.hpp>+#include <hypergrep/size_to_bytes.hpp>+#include <limits>+#include <numeric>+#include <sys/mman.h>+#include <sys/stat.h>+#include <thread>+#include <unistd.h>+#include <vector>++class git_index_search {+public:+ git_index_search(std::string &pattern, const std::filesystem::path &path,+ argparse::ArgumentParser &program);+ git_index_search(hs_database_t *database, hs_scratch_t *scratch,+ hs_database_t *file_filter_database,+ hs_scratch_t *file_filter_scratch,+ const search_options &options,+ const std::filesystem::path &path);+ ~git_index_search();+ void run(std::filesystem::path path);++private:+ bool process_file(const char *filename, hs_scratch_t *local_scratch,+ char *buffer, std::string &lines);++ bool search_submodules(const char *dir, git_repository *this_repo);++ bool visit_git_index(const std::filesystem::path &dir, git_index *index);++ bool visit_git_repo(const std::filesystem::path &dir,+ git_repository *repo = nullptr);++ bool try_dequeue_and_process_path(hs_scratch_t *local_scratch, char *buffer,+ std::string &lines);++ void search_thread_function();++private:+ static inline bool libgit2_initialized{false};+ std::filesystem::path basepath{};++ moodycamel::ConcurrentQueue<const char *> queue;+ moodycamel::ProducerToken ptok{queue};++ std::atomic<bool> running{true};+ std::atomic<std::size_t> num_files_enqueued{0};+ std::atomic<std::size_t> num_files_dequeued{0};++ bool non_owning_database{false};+ hs_database_t *database = NULL;+ hs_scratch_t *scratch = NULL;+ hs_database_t *file_filter_database = NULL;+ hs_scratch_t *file_filter_scratch = NULL;+ // If the filter pattern starts with '!'+ // then negate the result of the filter+ bool negate_filter{false};++ search_options options;++ std::vector<git_repository *> garbage_collect_repo;+ std::vector<git_index *> garbage_collect_index;+ std::vector<git_index_iterator *> garbage_collect_index_iterator;++ // Backlog+ std::vector<std::filesystem::path> submodule_paths;+};
include/hypergrep/is_binary.hpp
@@ -0,0 +1,23 @@+#pragma once+#include <cstring>+#include <string_view>++bool is_elf_header(const char *buffer);++bool is_archive_header(const char *buffer);++bool is_jpeg(const char *buffer);++bool is_png(const char *buffer);++bool is_zip(const char *buffer);++bool is_gzip(const char *buffer);++bool is_tar(const char *buffer, const std::size_t &bytes_read);++bool is_pdf(const char *buffer);++bool starts_with_magic_bytes(const char *buffer, const std::size_t &bytes_read);++bool has_null_bytes(char *buffer, std::size_t search_size);
include/hypergrep/match_handler.hpp
@@ -0,0 +1,34 @@+#pragma once+#include <algorithm>+#include <fmt/color.h>+#include <fmt/format.h>+#include <hypergrep/constants.hpp>+#include <hypergrep/file_context.hpp>+#include <optional>+#include <string_view>+#include <vector>++int on_match(unsigned int id, unsigned long long from, unsigned long long to,+ unsigned int flags, void *ctx);++int print_match_in_red_color(unsigned int id, unsigned long long from,+ unsigned long long to, unsigned int flags,+ void *ctx);++std::size_t process_matches(+ const char *filename, char *buffer, std::size_t bytes_read,+ std::vector<std::pair<unsigned long long, unsigned long long>> &matches,+ std::size_t ¤t_line_number, std::string &lines, bool print_filename,+ bool is_stdout, bool show_line_numbers, bool show_column_numbers,+ bool show_byte_offset, bool print_only_matching_parts,+ const std::optional<std::size_t> &max_column_limit,+ std::size_t byte_offset);++std::size_t process_matches_nocolor_nostdout(+ const char *filename, char *buffer, std::size_t bytes_read,+ std::vector<std::pair<unsigned long long, unsigned long long>> &matches,+ std::size_t ¤t_line_number, std::string &lines, bool print_filename,+ bool is_stdout, bool show_line_numbers, bool show_column_numbers,+ bool show_byte_offset, bool print_only_matching_parts,+ const std::optional<std::size_t> &max_column_limit,+ std::size_t byte_offset);
include/hypergrep/print_help.hpp
@@ -0,0 +1,5 @@+#pragma once+#include <fmt/color.h>+#include <fmt/core.h>++void print_help();
include/hypergrep/search_options.hpp
@@ -0,0 +1,40 @@+#pragma once+#include <argparse/argparse.hpp>+#include <cstdint>+#include <hypergrep/file_filter.hpp>+#include <hypergrep/size_to_bytes.hpp>+#include <optional>+#include <string>+#include <unistd.h>++struct search_options {+ bool perform_search{true};+ bool is_stdout{true};+ bool show_line_numbers{false};+ bool show_column_numbers{false};+ bool show_byte_offset{false};+ bool ignore_case{false};+ bool print_only_filenames{false};+ bool count_matching_lines{false};+ bool count_matches{false};+ bool count_include_zeros{false};+ bool use_ucp{false};+ bool exclude_submodules{false};+ std::size_t num_threads{0};+ bool filter_files{false};+ std::string filter_file_pattern{};+ bool negate_filter{false};+ std::optional<unsigned long long> max_file_size{};+ bool search_hidden_files{false};+ bool print_only_matching_parts{false};+ std::optional<std::size_t> max_column_limit{};+ bool print_filenames{true};+ bool search_binary_files{false};+ bool ignore_gitindex{false};+ bool compile_pattern_as_literal{false};+};++void initialize_search(std::string &pattern, argparse::ArgumentParser &program,+ search_options &options, hs_database **database,+ hs_scratch **scratch, hs_database **file_filter_database,+ hs_scratch **file_filter_scratch);
include/hypergrep/size_to_bytes.hpp
@@ -0,0 +1,4 @@+#pragma once+#include <string_view>++unsigned long long size_to_bytes(std::string_view input);
include/is_binary.hpp
@@ -1,23 +0,0 @@-#pragma once-#include <cstring>-#include <string_view>--bool is_elf_header(const char *buffer);--bool is_archive_header(const char *buffer);--bool is_jpeg(const char *buffer);--bool is_png(const char *buffer);--bool is_zip(const char *buffer);--bool is_gzip(const char *buffer);--bool is_tar(const char *buffer, const std::size_t &bytes_read);--bool is_pdf(const char *buffer);--bool starts_with_magic_bytes(const char *buffer, const std::size_t &bytes_read);--bool has_null_bytes(char *buffer, std::size_t search_size);
include/match_handler.hpp
@@ -1,34 +0,0 @@-#pragma once-#include <algorithm>-#include <constants.hpp>-#include <file_context.hpp>-#include <fmt/color.h>-#include <fmt/format.h>-#include <optional>-#include <string_view>-#include <vector>--int on_match(unsigned int id, unsigned long long from, unsigned long long to,- unsigned int flags, void *ctx);--int print_match_in_red_color(unsigned int id, unsigned long long from,- unsigned long long to, unsigned int flags,- void *ctx);--std::size_t process_matches(- const char *filename, char *buffer, std::size_t bytes_read,- std::vector<std::pair<unsigned long long, unsigned long long>> &matches,- std::size_t ¤t_line_number, std::string &lines, bool print_filename,- bool is_stdout, bool show_line_numbers, bool show_column_numbers,- bool show_byte_offset, bool print_only_matching_parts,- const std::optional<std::size_t> &max_column_limit,- std::size_t byte_offset);--std::size_t process_matches_nocolor_nostdout(- const char *filename, char *buffer, std::size_t bytes_read,- std::vector<std::pair<unsigned long long, unsigned long long>> &matches,- std::size_t ¤t_line_number, std::string &lines, bool print_filename,- bool is_stdout, bool show_line_numbers, bool show_column_numbers,- bool show_byte_offset, bool print_only_matching_parts,- const std::optional<std::size_t> &max_column_limit,- std::size_t byte_offset);
include/print_help.hpp
@@ -1,5 +0,0 @@-#pragma once-#include <fmt/color.h>-#include <fmt/core.h>--void print_help();
include/search_options.hpp
@@ -1,38 +0,0 @@-#pragma once-#include <argparse/argparse.hpp>-#include <cstdint>-#include <file_filter.hpp>-#include <optional>-#include <size_to_bytes.hpp>-#include <string>-#include <unistd.h>--struct search_options {- bool perform_search{true};- bool is_stdout{true};- bool show_line_numbers{false};- bool show_column_numbers{false};- bool show_byte_offset{false};- bool ignore_case{false};- bool print_only_filenames{false};- bool count_matching_lines{false};- bool count_matches{false};- bool count_include_zeros{false};- bool use_ucp{false};- bool exclude_submodules{false};- std::size_t num_threads{0};- bool filter_files{false};- std::string filter_file_pattern{};- bool negate_filter{false};- std::optional<unsigned long long> max_file_size{};- bool search_hidden_files{false};- bool print_only_matching_parts{false};- std::optional<std::size_t> max_column_limit{};- bool print_filenames{true};- bool search_binary_files{false};- bool ignore_gitindex{false};- bool compile_pattern_as_literal{false};-};--void initialize_search(std::string &pattern, argparse::ArgumentParser& program, search_options& options, - hs_database** database, hs_scratch** scratch, hs_database** file_filter_database, hs_scratch** file_filter_scratch);
include/size_to_bytes.hpp
@@ -1,4 +0,0 @@-#pragma once-#include <string_view>--unsigned long long size_to_bytes(std::string_view input);
@@ -1,22 +1,24 @@-#include <compiler.hpp>-#include <search_options.hpp>+#include <hypergrep/compiler.hpp>+#include <hypergrep/search_options.hpp> -void compile_hs_database(hs_database** database, hs_scratch** scratch, search_options& options, const std::vector<std::string> &pattern_list) {+void compile_hs_database(hs_database **database, hs_scratch **scratch,+ search_options &options,+ const std::vector<std::string> &pattern_list) { hs_error_t error_code; hs_compile_error_t *compile_error = NULL; if (pattern_list.size() == 1) {- const auto& pattern = pattern_list[0];+ const auto &pattern = pattern_list[0]; if (options.compile_pattern_as_literal) { error_code = hs_compile_lit( pattern.data(), (options.ignore_case ? HS_FLAG_CASELESS : 0) | (options.is_stdout || options.print_only_matching_parts ||- options.show_column_numbers || options.show_byte_offset- ? HS_FLAG_SOM_LEFTMOST- : 0),+ options.show_column_numbers || options.show_byte_offset+ ? HS_FLAG_SOM_LEFTMOST+ : 0), pattern.size(), HS_MODE_BLOCK, NULL, database, &compile_error); } else { error_code = hs_compile(@@ -24,31 +26,32 @@ void compile_hs_database(hs_database** database, hs_scratch** scratch, search_op (options.ignore_case ? HS_FLAG_CASELESS : 0) | HS_FLAG_UTF8 | (options.use_ucp ? HS_FLAG_UCP : 0) | (options.is_stdout || options.print_only_matching_parts ||- options.show_column_numbers || options.show_byte_offset- ? HS_FLAG_SOM_LEFTMOST- : 0),+ options.show_column_numbers || options.show_byte_offset+ ? HS_FLAG_SOM_LEFTMOST+ : 0), HS_MODE_BLOCK, NULL, database, &compile_error); }- }- else {+ } else { // Compile multiple patterns // using hs_compile_multi // Search patterns- std::vector<const char*> pattern_list_c;+ std::vector<const char *> pattern_list_c; pattern_list_c.reserve(pattern_list.size());- for (const std::string& str : pattern_list) {+ for (const std::string &str : pattern_list) { pattern_list_c.push_back(str.data()); } // Search flags- const auto flag = (options.ignore_case ? HS_FLAG_CASELESS : 0) | - (options.compile_pattern_as_literal ? 0 : HS_FLAG_UTF8) |- (!options.compile_pattern_as_literal && options.use_ucp ? HS_FLAG_UCP : 0) |- (options.is_stdout || options.print_only_matching_parts ||- options.show_column_numbers || options.show_byte_offset- ? HS_FLAG_SOM_LEFTMOST- : 0);+ const auto flag =+ (options.ignore_case ? HS_FLAG_CASELESS : 0) |+ (options.compile_pattern_as_literal ? 0 : HS_FLAG_UTF8) |+ (!options.compile_pattern_as_literal && options.use_ucp ? HS_FLAG_UCP+ : 0) |+ (options.is_stdout || options.print_only_matching_parts ||+ options.show_column_numbers || options.show_byte_offset+ ? HS_FLAG_SOM_LEFTMOST+ : 0); std::vector<unsigned int> flags; flags.reserve(pattern_list.size()); for (std::size_t i = 0; i < pattern_list.size(); ++i) {@@ -59,30 +62,26 @@ void compile_hs_database(hs_database** database, hs_scratch** scratch, search_op std::vector<size_t> lens; lens.reserve(pattern_list.size());- for (const std::string& str : pattern_list) {- lens.push_back(str.size());+ for (const std::string &str : pattern_list) {+ lens.push_back(str.size()); } - error_code = hs_compile_lit_multi(- pattern_list_c.data(),- flags.data(),- NULL, // list of IDs - NULL means all zero- lens.data(), - pattern_list.size(), HS_MODE_BLOCK, NULL, database, &compile_error);- }- else {- error_code = hs_compile_multi(- pattern_list_c.data(),- flags.data(),- NULL, // list of IDs - NULL means all zero- pattern_list.size(),- HS_MODE_BLOCK, NULL, database, &compile_error);+ error_code =+ hs_compile_lit_multi(pattern_list_c.data(), flags.data(),+ NULL, // list of IDs - NULL means all zero+ lens.data(), pattern_list.size(), HS_MODE_BLOCK,+ NULL, database, &compile_error);+ } else {+ error_code = hs_compile_multi(pattern_list_c.data(), flags.data(),+ NULL, // list of IDs - NULL means all zero+ pattern_list.size(), HS_MODE_BLOCK, NULL,+ database, &compile_error); } } if (error_code != HS_SUCCESS) { throw std::runtime_error(std::string{"Error compiling pattern: "} +- compile_error->message);+ compile_error->message); } auto database_error = hs_alloc_scratch(*database, scratch);
@@ -1,11 +1,12 @@-#include <directory_search.hpp>-#include <is_binary.hpp>+#include <hypergrep/directory_search.hpp>+#include <hypergrep/is_binary.hpp> directory_search::directory_search(std::string &pattern, const std::filesystem::path &path, argparse::ArgumentParser &program) : search_path(path) {- initialize_search(pattern, program, options, &database, &scratch, &file_filter_database, &file_filter_scratch);+ initialize_search(pattern, program, options, &database, &scratch,+ &file_filter_database, &file_filter_scratch); } directory_search::~directory_search() {@@ -103,7 +104,7 @@ void directory_search::run(std::filesystem::path path) { // Just go deeper moodycamel::ProducerToken ptok{queue}; visit_directory_and_enqueue(ptok, subdir,- local_file_filter_scratch); + local_file_filter_scratch); } num_dirs_enqueued -= 1; }@@ -454,7 +455,8 @@ void directory_search::visit_directory_and_enqueue( if (!options.filter_files || (options.filter_files &&- filter_file(path.c_str(), file_filter_database, local_file_filter_scratch, options.negate_filter))) {+ filter_file(path.c_str(), file_filter_database,+ local_file_filter_scratch, options.negate_filter))) { if (options.perform_search) { queue.enqueue(ptok, std::move(path)); ++num_files_enqueued;
@@ -1,6 +1,8 @@-#include <file_filter.hpp>+#include <hypergrep/file_filter.hpp> -bool construct_file_filtering_hs_database(hs_database** file_filter_database, hs_scratch** file_filter_scratch, search_options& options) {+bool construct_file_filtering_hs_database(hs_database **file_filter_database,+ hs_scratch **file_filter_scratch,+ search_options &options) { options.negate_filter = options.filter_file_pattern[0] == '!'; if (options.negate_filter) {@@ -29,14 +31,15 @@ bool construct_file_filtering_hs_database(hs_database** file_filter_database, hs } int on_file_filter_match(unsigned int id, unsigned long long from,- unsigned long long to, unsigned int flags,- void *ctx) {+ unsigned long long to, unsigned int flags, void *ctx) { filter_context *fctx = (filter_context *)(ctx); fctx->result = true; return HS_SUCCESS; } -bool filter_file(const char *path, hs_database* file_filter_database, hs_scratch *local_file_filter_scratch, const bool& negate_filter) {+bool filter_file(const char *path, hs_database *file_filter_database,+ hs_scratch *local_file_filter_scratch,+ const bool &negate_filter) { // Result of `true` means that the file will be searched // Result of `false` means that the file will be ignored
@@ -1,8 +1,9 @@-#include <file_search.hpp>+#include <hypergrep/file_search.hpp> file_search::file_search(std::string &pattern, argparse::ArgumentParser &program) {- initialize_search(pattern, program, options, &database, &scratch, &file_filter_database, &file_filter_scratch);+ initialize_search(pattern, program, options, &database, &scratch,+ &file_filter_database, &file_filter_scratch); if (!program.is_used("-j")) { options.num_threads += 1;
@@ -1,12 +1,13 @@-#include <git_index_search.hpp>-#include <is_binary.hpp>+#include <hypergrep/git_index_search.hpp>+#include <hypergrep/is_binary.hpp> #include <unordered_set> git_index_search::git_index_search(std::string &pattern, const std::filesystem::path &path, argparse::ArgumentParser &program) : basepath(std::filesystem::relative(path)) {- initialize_search(pattern, program, options, &database, &scratch, &file_filter_database, &file_filter_scratch);+ initialize_search(pattern, program, options, &database, &scratch,+ &file_filter_database, &file_filter_scratch); } git_index_search::git_index_search(hs_database_t *database,@@ -15,8 +16,8 @@ git_index_search::git_index_search(hs_database_t *database, hs_scratch_t *file_filter_scratch, const search_options &options, const std::filesystem::path &path)- : basepath(path), database(database),- scratch(scratch), file_filter_database(file_filter_database),+ : basepath(path), database(database), scratch(scratch),+ file_filter_database(file_filter_database), file_filter_scratch(file_filter_scratch), options(options) { non_owning_database = true; }@@ -106,8 +107,7 @@ void git_index_search::run(std::filesystem::path path) { const auto current_path = std::filesystem::current_path(); for (const auto &sm_path : submodule_paths) { git_index_search git_index_searcher(- database, scratch, file_filter_database, file_filter_scratch,- options,+ database, scratch, file_filter_database, file_filter_scratch, options, basepath / std::filesystem::relative(std::filesystem::canonical(sm_path))); if (chdir(sm_path.c_str()) == 0) {@@ -360,8 +360,11 @@ bool git_index_search::visit_git_index(const std::filesystem::path &dir, const git_index_entry *entry = nullptr; while (git_index_iterator_next(&entry, iter) != GIT_ITEROVER) {- if (entry && (!options.filter_files ||- (options.filter_files && filter_file(entry->path, file_filter_database, file_filter_scratch, options.negate_filter)))) {+ if (entry &&+ (!options.filter_files ||+ (options.filter_files &&+ filter_file(entry->path, file_filter_database, file_filter_scratch,+ options.negate_filter)))) { // Skip directories and symlinks if ((entry->mode & S_IFMT) == S_IFDIR ||
@@ -1,4 +1,4 @@-#include <is_binary.hpp>+#include <hypergrep/is_binary.hpp> bool is_elf_header(const char *buffer) { static constexpr std::string_view elf_magic = "\x7f"
@@ -1,8 +1,8 @@-#include <constants.hpp>-#include <directory_search.hpp>-#include <file_search.hpp>-#include <git_index_search.hpp>-#include <print_help.hpp>+#include <hypergrep/constants.hpp>+#include <hypergrep/directory_search.hpp>+#include <hypergrep/file_search.hpp>+#include <hypergrep/git_index_search.hpp>+#include <hypergrep/print_help.hpp> void perform_search(std::string &pattern, std::string_view path, argparse::ArgumentParser &program) {@@ -31,8 +31,7 @@ void perform_search(std::string &pattern, std::string_view path, // Just run directory search directory_search s(pattern, path, program); s.run(path);- }- else {+ } else { // Check if search path is a git repo const auto current_path = std::filesystem::current_path(); @@ -81,8 +80,8 @@ int main(int argc, char **argv) { .implicit_value(true); program.add_argument("-e", "--regexp")- .default_value<std::vector<std::string>>({})- .append();+ .default_value<std::vector<std::string>>({})+ .append(); program.add_argument("--files").default_value(false).implicit_value(true);
@@ -1,5 +1,5 @@+#include <hypergrep/match_handler.hpp> #include <map>-#include <match_handler.hpp> #include <unordered_map> #include <vector>
@@ -1,5 +1,5 @@-#include <constants.hpp>-#include <print_help.hpp>+#include <hypergrep/constants.hpp>+#include <hypergrep/print_help.hpp> #include <unistd.h> void print_heading(bool is_stdout, std::string_view name) {@@ -161,21 +161,17 @@ void print_help() { print_description_line( "individual matches of the given pattern for each file searched\n"); - // Pattern argument + // Pattern argument print_option_name(is_stdout, "-e, --regexp", "<PATTERN>..."); print_description_line( "A pattern to search for. This option can be provided multiple times,"); print_description_line(- "where all patterns given are searched. Lines matching at least one"- );- print_description_line(- "of the provided patterns are printed, e.g.,\n"- );+ "where all patterns given are searched. Lines matching at least one");+ print_description_line("of the provided patterns are printed, e.g.,\n"); print_option_name(is_stdout, " hg -e 'myFunctionCall' -e 'myErrorCallback'\n"); print_description_line(- "will search for any occurrence of either of the patterns.\n"- );+ "will search for any occurrence of either of the patterns.\n"); // Files print_option_name(is_stdout, "--files");@@ -186,17 +182,14 @@ void print_help() { // Filter print_option_name(is_stdout, "--filter", "<FILTER_PATTERN>"); print_description_line("Filter paths based on a regex pattern, e.g.,\n");- print_option_name(is_stdout,- " hg --filter '(include|src)/.*\\.(c|cpp|h|hpp)$'\n");+ print_option_name(+ is_stdout, " hg --filter '(include|src)/.*\\.(c|cpp|h|hpp)$'\n"); print_description_line( "will search C/C++ files in the any */include/* and */src/* paths.\n"); print_description_line(- "A filter can be negated by prefixing the pattern with !, e.g.,\n"- );- print_option_name(is_stdout,- " hg --filter '!\\.html$'\n");- print_description_line(- "will search any files that are not HTML files.\n");+ "A filter can be negated by prefixing the pattern with !, e.g.,\n");+ print_option_name(is_stdout, " hg --filter '!\\.html$'\n");+ print_description_line("will search any files that are not HTML files.\n"); // Fixed Strings print_option_name(is_stdout, "-F, --fixed-strings");@@ -231,23 +224,17 @@ void print_help() { // Ignore git index print_option_name(is_stdout, "--ignore-gitindex"); print_description_line(- "By default, hypergrep will check for the presence of a `.git/`"- );+ "By default, hypergrep will check for the presence of a `.git/`"); print_description_line(- "directory in any path being searched. If a `.git/` directory is"- );+ "directory in any path being searched. If a `.git/` directory is"); print_description_line(- "found, hypergrep will attempt to find and load the git index file."- );+ "found, hypergrep will attempt to find and load the git index file."); print_description_line(- "Once loaded, the git index entries will be iterated and searched."- );+ "Once loaded, the git index entries will be iterated and searched."); print_description_line(- "Using --ignore-gitindex will disable this behavior. Instead,"- );+ "Using --ignore-gitindex will disable this behavior. Instead,"); print_description_line(- "hypergrep will search this path as if it were a normal directory.\n"- );+ "hypergrep will search this path as if it were a normal directory.\n"); // Exclude git submodules print_option_name(is_stdout, "--ignore-submodules");
@@ -1,9 +1,10 @@-#include <compiler.hpp>-#include <search_options.hpp>+#include <hypergrep/compiler.hpp>+#include <hypergrep/search_options.hpp> -void initialize_search(std::string &pattern, argparse::ArgumentParser& program, search_options& options, - hs_database** database, hs_scratch** scratch,- hs_database** file_filter_database, hs_scratch** file_filter_scratch) {+void initialize_search(std::string &pattern, argparse::ArgumentParser &program,+ search_options &options, hs_database **database,+ hs_scratch **scratch, hs_database **file_filter_database,+ hs_scratch **file_filter_scratch) { options.search_binary_files = program.get<bool>("--text"); options.count_matching_lines = program.get<bool>("-c");@@ -20,7 +21,8 @@ void initialize_search(std::string &pattern, argparse::ArgumentParser& program, if (program.is_used("--filter")) { options.filter_file_pattern = program.get<std::string>("--filter"); options.filter_files = true;- if (!construct_file_filtering_hs_database(file_filter_database, file_filter_scratch, options)) {+ if (!construct_file_filtering_hs_database(file_filter_database,+ file_filter_scratch, options)) { throw std::runtime_error("Error compiling pattern " + options.filter_file_pattern); }@@ -74,7 +76,7 @@ void initialize_search(std::string &pattern, argparse::ArgumentParser& program, if (program.get<bool>("-w")) { // Add word boundary around each pattern- for (auto& pattern : pattern_list) {+ for (auto &pattern : pattern_list) { pattern = "\\b" + pattern + "\\b"; }
@@ -1,6 +1,6 @@ #include <algorithm> #include <cmath>-#include <size_to_bytes.hpp>+#include <hypergrep/size_to_bytes.hpp> #include <stdexcept> #include <string> #include <string_view>