From fa85f0fc32b105bef5948585a7d7a38c2910854b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 13 Oct 2014 13:12:33 +0100 Subject: [PATCH] Refactor regex uses, do not reference boost except in regex.hh --- src/buffer_utils.cc | 1 + src/color.cc | 3 ++- src/command_manager.cc | 2 +- src/commands.cc | 3 ++- src/env_vars.cc | 2 -- src/file.cc | 5 +++-- src/file.hh | 3 ++- src/highlighters.cc | 24 ++++++++++++------------ src/hook_manager.cc | 3 ++- src/input_handler.cc | 13 +++++++------ src/insert_completer.cc | 7 ++++--- src/ncurses.cc | 7 ++++--- src/normal.cc | 8 ++++---- src/option_manager.hh | 1 + src/regex.cc | 25 +++++++++++++++++++++++++ src/regex.hh | 33 +++++++++++++++++++++++++++++++++ src/selectors.cc | 11 ++++++----- src/selectors.hh | 21 ++++++++++----------- src/shell_manager.cc | 7 +++---- src/shell_manager.hh | 1 + src/string.cc | 17 ----------------- src/string.hh | 8 ++------ 22 files changed, 125 insertions(+), 80 deletions(-) create mode 100644 src/regex.cc create mode 100644 src/regex.hh diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index eadf6415..1d7e7d25 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -3,6 +3,7 @@ #include "buffer_manager.hh" #include "event_manager.hh" +#include #include namespace Kakoune diff --git a/src/color.cc b/src/color.cc index 2dc71af0..d056fb25 100644 --- a/src/color.cc +++ b/src/color.cc @@ -1,6 +1,7 @@ #include "color.hh" #include "exception.hh" +#include "regex.hh" namespace Kakoune { @@ -31,7 +32,7 @@ Color str_to_color(StringView color) if (color == "white") return Colors::White; static const Regex rgb_regex{"rgb:[0-9a-fA-F]{6}"}; - if (boost::regex_match(color.begin(), color.end(), rgb_regex)) + if (regex_match(color.begin(), color.end(), rgb_regex)) { unsigned l; sscanf(color.zstr() + 4, "%x", &l); diff --git a/src/command_manager.cc b/src/command_manager.cc index 96f4cb09..71131048 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -263,7 +263,7 @@ TokenList parse(StringView line) String token = line.substr(token_start, pos - token_start); static const Regex regex{R"(\\([ \t;\n]))"}; result.emplace_back(Token::Type::Raw, token_start, pos, - boost::regex_replace(token, regex, "\\1")); + regex_replace(token, regex, "\\1")); } } diff --git a/src/commands.cc b/src/commands.cc index 591f4622..1f8a72c5 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -27,6 +27,7 @@ #include #include #include +#include namespace Kakoune { @@ -549,7 +550,7 @@ const CommandDesc add_hook_cmd = { if (context.are_user_hooks_disabled()) return; - if (boost::regex_match(param.begin(), param.end(), regex)) + if (regex_match(param.begin(), param.end(), regex)) CommandManager::instance().execute(command, context, {}, { { "hook_param", param } }); }; diff --git a/src/env_vars.cc b/src/env_vars.cc index afee98aa..7a759399 100644 --- a/src/env_vars.cc +++ b/src/env_vars.cc @@ -1,8 +1,6 @@ #include "env_vars.hh" -#if __APPLE__ extern char **environ; -#endif namespace Kakoune { diff --git a/src/file.cc b/src/file.cc index b7697dab..66064bbf 100644 --- a/src/file.cc +++ b/src/file.cc @@ -7,6 +7,7 @@ #include "completion.hh" #include "debug.hh" #include "unicode.hh" +#include "regex.hh" #include #include @@ -300,12 +301,12 @@ std::vector complete_filename(StringView prefix, } const bool check_ignored_regex = not ignored_regex.empty() and - not boost::regex_match(fileprefix.c_str(), ignored_regex); + not regex_match(fileprefix.c_str(), ignored_regex); auto filter = [&](const dirent& entry) { return not check_ignored_regex or - not boost::regex_match(entry.d_name, ignored_regex); + not regex_match(entry.d_name, ignored_regex); }; std::vector res = list_files(fileprefix, dirname, filter); for (auto& file : res) diff --git a/src/file.hh b/src/file.hh index e1561ca9..3fe4bee9 100644 --- a/src/file.hh +++ b/src/file.hh @@ -1,8 +1,9 @@ #ifndef file_hh_INCLUDED #define file_hh_INCLUDED -#include "string.hh" #include "exception.hh" +#include "string.hh" +#include "regex.hh" namespace Kakoune { diff --git a/src/highlighters.cc b/src/highlighters.cc index a42b6c94..d9942b85 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -22,8 +22,6 @@ namespace Kakoune using namespace std::placeholders; -using RegexIterator = boost::regex_iterator; - template void highlight_range(DisplayBuffer& display_buffer, ByteCoord begin, ByteCoord end, @@ -256,9 +254,11 @@ private: cache.m_timestamp = buffer.timestamp(); cache.m_matches.clear(); - RegexIterator re_it{buffer.iterator_at(cache.m_range.first), - buffer.iterator_at(cache.m_range.second+1), m_regex}; - RegexIterator re_end; + + using RegexIt = RegexIterator; + RegexIt re_it{buffer.iterator_at(cache.m_range.first), + buffer.iterator_at(cache.m_range.second+1), m_regex}; + RegexIt re_end; for (; re_it != re_end; ++re_it) { cache.m_matches.emplace_back(); @@ -281,8 +281,8 @@ HighlighterAndId highlight_regex_factory(HighlighterParameters params) FacesSpec faces; for (auto it = params.begin() + 1; it != params.end(); ++it) { - boost::smatch res; - if (not boost::regex_match(it->begin(), it->end(), res, face_spec_ex)) + MatchResults res; + if (not regex_match(it->begin(), it->end(), res, face_spec_ex)) throw runtime_error("wrong face spec: '" + *it + "' expected :"); get_face(res[2].str()); // throw if wrong face spec @@ -299,7 +299,7 @@ HighlighterAndId highlight_regex_factory(HighlighterParameters params) return HighlighterAndId(id, RegexHighlighter(std::move(ex), std::move(faces))); } - catch (boost::regex_error& err) + catch (RegexError& err) { throw runtime_error(String("regex error: ") + err.what()); } @@ -364,7 +364,7 @@ HighlighterAndId highlight_search_factory(HighlighterParameters params) { return s.empty() ? Regex{} : Regex{s.begin(), s.end()}; } - catch (boost::regex_error& err) + catch (RegexError& err) { return Regex{}; } @@ -725,7 +725,7 @@ void find_matches(const Buffer& buffer, RegexMatchList& matches, const Regex& re for (auto line = 0_line, end = buffer.line_count(); line < end; ++line) { auto l = buffer[line]; - for (boost::regex_iterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) + for (RegexIterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) { ByteCount b = (int)((*it)[0].first - l.begin()); ByteCount e = (int)((*it)[0].second - l.begin()); @@ -779,7 +779,7 @@ void update_matches(const Buffer& buffer, memoryview modifs, line < buffer.line_count(); ++line) { auto l = buffer[line]; - for (boost::regex_iterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) + for (RegexIterator it{l.begin(), l.end(), regex}, end{}; it != end; ++it) { ByteCount b = (int)((*it)[0].first - l.begin()); ByteCount e = (int)((*it)[0].second - l.begin()); @@ -1069,7 +1069,7 @@ HighlighterAndId regions_factory(HighlighterParameters params) HierachicalHighlighter( RegionsHighlighter(std::move(regions), std::move(default_group)), std::move(groups))}; } - catch (boost::regex_error& err) + catch (RegexError& err) { throw runtime_error(String("regex error: ") + err.what()); } diff --git a/src/hook_manager.cc b/src/hook_manager.cc index 7d804696..cfade4d7 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -2,6 +2,7 @@ #include "context.hh" #include "debug.hh" +#include "regex.hh" namespace Kakoune { @@ -50,7 +51,7 @@ void HookManager::run_hook(const String& hook_name, for (auto& hook : hook_list_it->second) { if (not hook.first.empty() and not disabled_hooks.empty() and - boost::regex_match(hook.first, disabled_hooks)) + regex_match(hook.first, disabled_hooks)) continue; try diff --git a/src/input_handler.cc b/src/input_handler.cc index d8c1e373..e4368e7f 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -7,6 +7,7 @@ #include "face_registry.hh" #include "insert_completer.hh" #include "normal.hh" +#include "regex.hh" #include "register_manager.hh" #include "user_interface.hh" #include "utf8.hh" @@ -338,7 +339,7 @@ public: void on_key(Key key) override { auto match_filter = [this](const String& str) { - return boost::regex_match(str.begin(), str.end(), m_filter); + return regex_match(str.begin(), str.end(), m_filter); }; if (key == ctrl('m')) @@ -356,7 +357,7 @@ public: if (m_edit_filter) { m_edit_filter = false; - m_filter = boost::regex(".*"); + m_filter = Regex(".*"); m_filter_editor.reset(""); context().print_status(DisplayLine{}); } @@ -395,7 +396,7 @@ public: m_filter_editor.handle_key(key); auto search = ".*" + m_filter_editor.line() + ".*"; - m_filter = boost::regex(search.begin(), search.end()); + m_filter = Regex(search.begin(), search.end()); auto it = std::find_if(m_selected, m_choices.end(), match_filter); if (it == m_choices.end()) it = std::find_if(m_choices.begin(), m_selected, match_filter); @@ -435,9 +436,9 @@ private: m_callback(selected, MenuEvent::Select, context()); } - boost::regex m_filter = boost::regex(".*"); - bool m_edit_filter = false; - LineEditor m_filter_editor; + Regex m_filter = Regex(".*"); + bool m_edit_filter = false; + LineEditor m_filter_editor; }; String common_prefix(memoryview strings) diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 1e50d189..da374d27 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -7,6 +7,7 @@ #include "display_buffer.hh" #include "face_registry.hh" #include "file.hh" +#include "regex.hh" #include "user_interface.hh" #include "window.hh" #include "word_db.hh" @@ -162,14 +163,14 @@ InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos, InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos, OptionManager& options, StringView option_name) { - const StringList& opt = options[option_name].get();; + const StringList& opt = options[option_name].get(); if (opt.empty()) return {}; auto& desc = opt[0]; static const Regex re(R"((\d+)\.(\d+)(?:\+(\d+))?@(\d+))"); - boost::smatch match; - if (boost::regex_match(desc.begin(), desc.end(), match, re)) + MatchResults match; + if (regex_match(desc.begin(), desc.end(), match, re)) { ByteCoord coord{ str_to_int(match[1].str()) - 1, str_to_int(match[2].str()) - 1 }; if (not buffer.is_valid(coord)) diff --git a/src/ncurses.cc b/src/ncurses.cc index 15d38d46..ef1bba40 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -16,10 +16,11 @@ #include #endif -#include -#include -#include #include +#include +#include +#include +#include namespace Kakoune { diff --git a/src/normal.cc b/src/normal.cc index fbd23ae8..a5f75269 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -546,7 +546,7 @@ void regex_prompt(Context& context, const String prompt, T func) context.push_jump(); func(str.empty() ? Regex{} : Regex{str}, event, context); } - catch (boost::regex_error& err) + catch (RegexError& err) { if (event == PromptEvent::Validate) throw runtime_error("regex error: "_str + err.what()); @@ -607,7 +607,7 @@ void search_next(Context& context, int param) select_next_match(context.buffer(), context.selections(), ex); } while (--param > 0); } - catch (boost::regex_error& err) + catch (RegexError& err) { throw runtime_error("regex error: "_str + err.what()); } @@ -732,8 +732,8 @@ void keep(Context& context, int) std::vector keep; for (auto& sel : context.selections()) { - if (boost::regex_search(buffer.iterator_at(sel.min()), - utf8::next(buffer.iterator_at(sel.max()), buffer.end()), ex) == matching) + if (regex_search(buffer.iterator_at(sel.min()), + utf8::next(buffer.iterator_at(sel.max()), buffer.end()), ex) == matching) keep.push_back(sel); } if (keep.empty()) diff --git a/src/option_manager.hh b/src/option_manager.hh index 93e6a943..32d55e70 100644 --- a/src/option_manager.hh +++ b/src/option_manager.hh @@ -5,6 +5,7 @@ #include "exception.hh" #include "option_types.hh" #include "utils.hh" +#include "regex.hh" #include diff --git a/src/regex.cc b/src/regex.cc new file mode 100644 index 00000000..d2c8f615 --- /dev/null +++ b/src/regex.cc @@ -0,0 +1,25 @@ +#include "regex.hh" + +#include "exception.hh" + +namespace Kakoune +{ + +String option_to_string(const Regex& re) +{ + return String{re.str()}; +} + +void option_from_string(StringView str, Regex& re) +{ + try + { + re = Regex{str.begin(), str.end()}; + } + catch (RegexError& err) + { + throw runtime_error("unable to create regex: "_str + err.what()); + } +} + +} diff --git a/src/regex.hh b/src/regex.hh new file mode 100644 index 00000000..a3e911bd --- /dev/null +++ b/src/regex.hh @@ -0,0 +1,33 @@ +#ifndef regex_hh_INCLUDED +#define regex_hh_INCLUDED + +#include "string.hh" + +#if 0 +#include +namespace kak_regex_ns = std; +#else +#include +namespace kak_regex_ns = boost; +#endif + +namespace Kakoune +{ + +using Regex = kak_regex_ns::regex; + +template +using RegexIterator = kak_regex_ns::regex_iterator; + +template +using MatchResults = kak_regex_ns::match_results; + +using RegexError = kak_regex_ns::regex_error; + +String option_to_string(const Regex& re); +void option_from_string(StringView str, Regex& re); + +} + +#endif // regex_hh_INCLUDED + diff --git a/src/selectors.cc b/src/selectors.cc index f4569600..d6c9110a 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -496,6 +496,8 @@ void select_buffer(SelectionList& selections) selections = SelectionList{ buffer, target_eol({{0,0}, buffer.back_coord()}) }; } +using RegexIt = RegexIterator; + void select_all_matches(SelectionList& selections, const Regex& regex) { std::vector result; @@ -503,8 +505,8 @@ void select_all_matches(SelectionList& selections, const Regex& regex) for (auto& sel : selections) { auto sel_end = utf8::next(buffer.iterator_at(sel.max()), buffer.end()); - RegexIterator re_it(buffer.iterator_at(sel.min()), sel_end, regex); - RegexIterator re_end; + RegexIt re_it(buffer.iterator_at(sel.min()), sel_end, regex); + RegexIt re_end; for (; re_it != re_end; ++re_it) { @@ -537,9 +539,8 @@ void split_selections(SelectionList& selections, const Regex& regex) { auto begin = buffer.iterator_at(sel.min()); auto sel_end = utf8::next(buffer.iterator_at(sel.max()), buffer.end()); - RegexIterator re_it(begin, sel_end, regex, - boost::regex_constants::match_nosubs); - RegexIterator re_end; + RegexIt re_it(begin, sel_end, regex); + RegexIt re_end; for (; re_it != re_end; ++re_it) { diff --git a/src/selectors.hh b/src/selectors.hh index 1cd17b0a..3df24f26 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -5,6 +5,7 @@ #include "buffer_utils.hh" #include "unicode.hh" #include "utf8_iterator.hh" +#include "regex.hh" namespace Kakoune { @@ -59,8 +60,6 @@ inline Selection utf8_range(const Utf8Iterator& first, const Utf8Iterator& last) return {first.base().coord(), last.base().coord()}; } -using RegexIterator = boost::regex_iterator; - template Selection select_to_next_word(const Buffer& buffer, const Selection& selection) { @@ -230,13 +229,12 @@ void select_buffer(SelectionList& selections); enum Direction { Forward, Backward }; -using MatchResults = boost::match_results; - inline bool find_last_match(BufferIterator begin, const BufferIterator& end, - MatchResults& res, const Regex& regex) + MatchResults& res, + const Regex& regex) { - MatchResults matches; - while (boost::regex_search(begin, end, matches, regex)) + MatchResults matches; + while (regex_search(begin, end, matches, regex)) { if (begin == matches[0].second) break; @@ -248,11 +246,12 @@ inline bool find_last_match(BufferIterator begin, const BufferIterator& end, template bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos, - MatchResults& matches, const Regex& ex) + MatchResults& matches, + const Regex& ex) { if (direction == Forward) - return (boost::regex_search(pos, buffer.end(), matches, ex) or - boost::regex_search(buffer.begin(), buffer.end(), matches, ex)); + return (regex_search(pos, buffer.end(), matches, ex) or + regex_search(buffer.begin(), buffer.end(), matches, ex)); else return (find_last_match(buffer.begin(), pos, matches, ex) or find_last_match(buffer.begin(), buffer.end(), matches, ex)); @@ -265,7 +264,7 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege auto end = begin; CaptureList captures; - MatchResults matches; + MatchResults matches; bool found = false; auto pos = direction == Forward ? utf8::next(begin, buffer.end()) : utf8::previous(begin, buffer.begin()); diff --git a/src/shell_manager.cc b/src/shell_manager.cc index 08c34160..e105f102 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -87,8 +87,8 @@ String ShellManager::pipe(StringView input, dup2(error_pipe[1], 2); close(error_pipe[1]); dup2(write_pipe[0], 0); close(write_pipe[0]); - boost::regex_iterator it(cmdline.begin(), cmdline.end(), env_var_regex); - boost::regex_iterator end; + RegexIterator it(cmdline.begin(), cmdline.end(), env_var_regex); + RegexIterator end; while (it != end) { @@ -145,8 +145,7 @@ String ShellManager::get_val(StringView name, const Context& context) const auto env_var = std::find_if( m_env_vars.begin(), m_env_vars.end(), [&](const std::pair& pair) - { return boost::regex_match(name.begin(), name.end(), - pair.first); }); + { return regex_match(name.begin(), name.end(), pair.first); }); if (env_var == m_env_vars.end()) throw runtime_error("no such env var: " + name); diff --git a/src/shell_manager.hh b/src/shell_manager.hh index f5d27a03..e2af4241 100644 --- a/src/shell_manager.hh +++ b/src/shell_manager.hh @@ -2,6 +2,7 @@ #define shell_manager_hh_INCLUDED #include "string.hh" +#include "regex.hh" #include "utils.hh" #include "env_vars.hh" diff --git a/src/string.cc b/src/string.cc index 172193d4..f31ed83f 100644 --- a/src/string.cc +++ b/src/string.cc @@ -85,23 +85,6 @@ String to_string(int val) return buf; } -String option_to_string(const Regex& re) -{ - return String{re.str()}; -} - -void option_from_string(StringView str, Regex& re) -{ - try - { - re = Regex{str.begin(), str.end()}; - } - catch (boost::regex_error& err) - { - throw runtime_error("unable to create regex: "_str + err.what()); - } -} - bool prefix_match(StringView str, StringView prefix) { auto it = str.begin(); diff --git a/src/string.hh b/src/string.hh index 573853fe..e31a3173 100644 --- a/src/string.hh +++ b/src/string.hh @@ -6,13 +6,12 @@ #include "utf8.hh" #include -#include +#include +#include namespace Kakoune { -using Regex = boost::regex; - class StringView; class String : public std::string @@ -288,9 +287,6 @@ inline String codepoint_to_str(Codepoint cp) return String(str); } -String option_to_string(const Regex& re); -void option_from_string(StringView str, Regex& re); - int str_to_int(StringView str); String to_string(int val);