Skip to content

Commit

Permalink
Merge pull request nixprime#60 from 'ompugao/feature/remove_boost_dep…
Browse files Browse the repository at this point in the history
…endency'
  • Loading branch information
presuku committed Oct 28, 2023
2 parents 3054517 + ed78f8d commit f60e27c
Show file tree
Hide file tree
Showing 13 changed files with 2,830 additions and 106 deletions.
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${PROJECT_SOURCE_DIR}/src)
set(CMAKE_BUILD_TYPE Release)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang|Intel")
add_compile_options(-std=c++0x)
add_compile_options(-std=c++17)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# Versions of MSVC that support C++11 at all support it by default, so hope
# for the best...
Expand All @@ -17,10 +17,6 @@ set(CMAKE_MACOSX_RPATH 1)

find_package(Threads REQUIRED)

set(Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED COMPONENTS program_options)
include_directories(${Boost_INCLUDE_DIRS})

if(PY3)
set(Python_ADDITIONAL_VERSIONS 3.8 3.7 3.6 3.5 3.4 3.3)
find_package(PythonInterp 3 REQUIRED)
Expand Down Expand Up @@ -54,7 +50,7 @@ endif()
install(TARGETS cpsm_py DESTINATION ${PROJECT_SOURCE_DIR}/autoload)

add_executable(cpsm_cli src/cpsm_cli_main.cc)
target_link_libraries(cpsm_cli cpsm_core ${Boost_PROGRAM_OPTIONS_LIBRARIES})
target_link_libraries(cpsm_cli cpsm_core ${CMAKE_THREAD_LIBS_INIT})
install(TARGETS cpsm_cli DESTINATION ${PROJECT_SOURCE_DIR}/bin)

enable_testing()
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ Requirements

- A C++ compiler supporting C++11.

- Boost (Ubuntu: package `libboost-all-dev`).

- CMake (Ubuntu: package `cmake`).

- Python headers (Ubuntu: package `python-dev`).
Expand Down
35 changes: 21 additions & 14 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
#include <cstdint>
#include <vector>

#include <boost/utility/string_ref.hpp>
#if __has_include("string_view")
#include <string_view>
using std::string_view;
#else
#include <experimental/string_view>
using std::experimental::string_view;
#endif


#include "matcher.h"
#include "par_util.h"
Expand All @@ -33,7 +40,7 @@ namespace cpsm {
struct Options {
private:
// The currently open file.
boost::string_ref crfile_;
string_view crfile_;

// The maximum number of matches to return. If 0, there is no limit.
std::size_t limit_ = 0;
Expand All @@ -55,8 +62,8 @@ struct Options {
bool want_match_info_ = false;

public:
boost::string_ref crfile() const { return crfile_; }
Options& set_crfile(boost::string_ref const crfile) {
string_view crfile() const { return crfile_; }
Options& set_crfile(string_view const crfile) {
crfile_ = crfile;
return *this;
}
Expand Down Expand Up @@ -105,7 +112,7 @@ namespace detail {

template <typename PathTraits, typename StringTraits, typename Item,
typename Source, typename Sink>
void for_each_match(boost::string_ref const query, Options const& opts,
void for_each_match(string_view const query, Options const& opts,
Source&& src, Sink&& dst);

} // namespace detail
Expand All @@ -116,7 +123,7 @@ void for_each_match(boost::string_ref const query, Options const& opts,
//
// `Item` must be a default-constructable, movable type with the following
// member functions:
// - `match_key`, which returns a `boost::string_ref` representing the string
// - `match_key`, which returns a `string_view` representing the string
// that the query should match against.
// - `sort_key`, which returns a value of unspecified type that can be compared
// to other values of the same type with operator `<`. When the matcher is
Expand Down Expand Up @@ -151,7 +158,7 @@ void for_each_match(boost::string_ref const query, Options const& opts,
// std::cout << item.item << std::endl;
// });
template <typename Item, typename Source, typename Sink>
void for_each_match(boost::string_ref const query, Options const& opts,
void for_each_match(string_view const query, Options const& opts,
Source&& src, Sink&& dst) {
if (opts.path()) {
if (opts.unicode()) {
Expand All @@ -172,18 +179,18 @@ void for_each_match(boost::string_ref const query, Options const& opts,
}
}

// Simple Item type wrapping a `boost::string_ref`.
// Simple Item type wrapping a `string_view`.
class StringRefItem {
public:
StringRefItem() {}
explicit StringRefItem(boost::string_ref const item) : item_(item) {}
explicit StringRefItem(string_view const item) : item_(item) {}

boost::string_ref item() const { return item_; }
boost::string_ref match_key() const { return item_; }
boost::string_ref sort_key() const { return item_; }
string_view item() const { return item_; }
string_view match_key() const { return item_; }
string_view sort_key() const { return item_; }

private:
boost::string_ref item_;
string_view item_;
};

// Thread-unsafe source functor that constructs items from elements of a range
Expand Down Expand Up @@ -238,7 +245,7 @@ struct Matched {

template <typename PathTraits, typename StringTraits, typename Item,
typename Source, typename Sink>
void for_each_match(boost::string_ref const query, Options const& opts,
void for_each_match(string_view const query, Options const& opts,
Source&& src, Sink&& dst) {
MatcherOptions mopts;
mopts.crfile = opts.crfile();
Expand Down
34 changes: 19 additions & 15 deletions src/cpsm_cli_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,40 @@
#include <utility>
#include <vector>

#include <boost/program_options.hpp>
#include <boost/utility/string_ref.hpp>
#if __has_include("string_view")
#include <string_view>
using std::string_view;
#else
#include <experimental/string_view>
using std::experimental::string_view;
#endif

#include "api.h"
#include "str_util.h"

namespace po = boost::program_options;
#include "cxxopts.hpp"

int main(int argc, char** argv) {
std::cin.sync_with_stdio(false);
std::cout.sync_with_stdio(false);
std::cerr.sync_with_stdio(false);

po::options_description opts_desc("Options");
opts_desc.add_options()
("crfile", po::value<std::string>()->default_value(""),
"'currently open file' passed to the matcher")
("limit", po::value<std::size_t>()->default_value(10),
"maximum number of matches to return")
("query", po::value<std::string>()->default_value(""),
"query to match items against")
cxxopts::Options options("Options", "Options for cpsm cli");

options.add_options()
("crfile", "'currently open file' passed to the matcher",
cxxopts::value<std::string>()->default_value(""))
("limit", "maximum number of matches to return",
cxxopts::value<std::size_t>()->default_value("10"))
("query", "query to match items against",
cxxopts::value<std::string>()->default_value(""))
("help", "display this help and exit")
;

po::variables_map opts;
po::store(po::parse_command_line(argc, argv, opts_desc), opts);
po::notify(opts);
const auto opts = options.parse(argc, argv);

if (opts.count("help")) {
std::cout << opts_desc << std::endl;
std::cout << options.help() << std::endl;
return 0;
}

Expand Down
10 changes: 5 additions & 5 deletions src/ctrlp_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::vector<std::pair<std::size_t, std::size_t>> group_positions_basic(
}

std::vector<std::pair<std::size_t, std::size_t>> group_positions(
boost::string_ref const mode, std::vector<std::size_t> const& positions) {
string_view const mode, std::vector<std::size_t> const& positions) {
if (mode.empty() || mode == "none") {
return std::vector<std::pair<std::size_t, std::size_t>>();
} else if (mode == "basic") {
Expand All @@ -70,7 +70,7 @@ std::vector<std::pair<std::size_t, std::size_t>> group_positions(

} // anonymous namespace

CtrlPMatchMode parse_ctrlp_match_mode(boost::string_ref const mmode) {
CtrlPMatchMode parse_ctrlp_match_mode(string_view const mmode) {
if (mmode.empty() || mmode == "full-line") {
return CtrlPMatchMode::FULL_LINE;
} else if (mmode == "filename-only") {
Expand All @@ -83,11 +83,11 @@ CtrlPMatchMode parse_ctrlp_match_mode(boost::string_ref const mmode) {
throw Error("unknown match mode ", mmode);
}

void get_highlight_regexes(boost::string_ref const mode,
boost::string_ref const item,
void get_highlight_regexes(string_view const mode,
string_view const item,
std::vector<std::size_t> const& positions,
std::vector<std::string>& regexes,
boost::string_ref const line_prefix) {
string_view const line_prefix) {
for (auto const group : group_positions(mode, positions)) {
// Each match group's regex has the same structure:
// - "\V": very nomagic (only "\" needs to be escaped)
Expand Down
27 changes: 17 additions & 10 deletions src/ctrlp_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
#include <utility>
#include <vector>

#include <boost/utility/string_ref.hpp>
#if __has_include("string_view")
#include <string_view>
using std::string_view;
#else
#include <experimental/string_view>
using std::experimental::string_view;
#endif


#include "path_util.h"
#include "str_util.h"
Expand All @@ -46,33 +53,33 @@ enum class CtrlPMatchMode {
};

// Parses a CtrlP match mode.
CtrlPMatchMode parse_ctrlp_match_mode(boost::string_ref mmode);
CtrlPMatchMode parse_ctrlp_match_mode(string_view mmode);

// Functor types implementing transformations for each CtrlP match mode.

struct FullLineMatch {
boost::string_ref operator()(boost::string_ref const item) const {
string_view operator()(string_view const item) const {
return item;
}
};

struct FilenameOnlyMatch {
boost::string_ref operator()(boost::string_ref const item) const {
string_view operator()(string_view const item) const {
return ref_str_iters(
path_basename<PlatformPathTraits>(item.cbegin(), item.cend()),
item.cend());
}
};

struct FirstNonTabMatch {
boost::string_ref operator()(boost::string_ref const item) const {
string_view operator()(string_view const item) const {
return ref_str_iters(item.cbegin(),
std::find(item.cbegin(), item.cend(), '\t'));
}
};

struct UntilLastTabMatch {
boost::string_ref operator()(boost::string_ref const item) const {
string_view operator()(string_view const item) const {
auto const item_rend = item.crend();
auto const last_tab_rit = std::find(item.crbegin(), item_rend, '\t');
return ref_str_iters(item.cbegin(), (last_tab_rit == item_rend)
Expand All @@ -90,16 +97,16 @@ struct CtrlPItem {
CtrlPItem() {}
explicit CtrlPItem(InnerItem inner) : inner(std::move(inner)) {}

boost::string_ref match_key() const { return MatchMode()(inner.match_key()); }
boost::string_ref sort_key() const { return inner.sort_key(); }
string_view match_key() const { return MatchMode()(inner.match_key()); }
string_view sort_key() const { return inner.sort_key(); }
};

// Appends a set of Vim regexes to highlight the bytes at `positions` in `item`
// for the given highlight mode. `positions` must be sorted.
void get_highlight_regexes(boost::string_ref mode, boost::string_ref item,
void get_highlight_regexes(string_view mode, string_view item,
std::vector<std::size_t> const& positions,
std::vector<std::string>& regexes,
boost::string_ref line_prefix);
string_view line_prefix);

} // namespace cpsm

Expand Down
Loading

0 comments on commit f60e27c

Please sign in to comment.