From 51de90f366685e6112866f34d330495c2aa54571 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 1 Nov 2017 14:08:03 +0800 Subject: [PATCH] Regex: Remove boost related code --- src/Makefile | 16 +++---- src/regex.cc | 57 +----------------------- src/regex.hh | 123 +-------------------------------------------------- 3 files changed, 11 insertions(+), 185 deletions(-) diff --git a/src/Makefile b/src/Makefile index 12c6cf88..624cabd0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -30,24 +30,24 @@ mandir := $(DESTDIR)$(PREFIX)/share/man/man1 os := $(shell uname) ifeq ($(os),Darwin) - LIBS += -lncurses -lboost_regex - CPPFLAGS += -I$(PREFIX)/opt/ncurses/include -I$(PREFIX)/opt/boost/include -I/opt/local/include - LDFLAGS += -L$(PREFIX)/opt/ncurses/lib -L$(PREFIX)/opt/boost/lib -L/opt/local/lib + LIBS += -lncurses + CPPFLAGS += -I$(PREFIX)/opt/ncurses/include -I/opt/local/include + LDFLAGS += -L$(PREFIX)/opt/ncurses/lib -L/opt/local/lib else ifeq ($(os),FreeBSD) - LIBS += -ltinfow -lncursesw -lboost_regex + LIBS += -ltinfow -lncursesw CPPFLAGS += -I/usr/local/include LDFLAGS += -L/usr/local/lib else ifeq ($(os),Haiku) - LIBS += -lncursesw -lboost_regex -lnetwork -lbe + LIBS += -lncursesw -lnetwork -lbe else ifeq ($(os),DragonFly) - LIBS += -lncursesw -lboost_regex + LIBS += -lncursesw CPPFLAGS += -I/usr/local/include LDFLAGS += -L/usr/local/lib else ifneq (,$(findstring CYGWIN,$(os))) CPPFLAGS += -D_XOPEN_SOURCE=700 - LIBS += -lncursesw -lboost_regex -ldbghelp + LIBS += -lncursesw -ldbghelp else - LIBS += $(shell pkg-config --libs ncursesw) -lboost_regex + LIBS += $(shell pkg-config --libs ncursesw) CPPFLAGS += $(shell pkg-config --cflags ncursesw) LDFLAGS += -rdynamic endif diff --git a/src/regex.cc b/src/regex.cc index 5f0a5fca..e3170d6e 100644 --- a/src/regex.cc +++ b/src/regex.cc @@ -1,67 +1,12 @@ #include "regex.hh" -#ifdef REGEX_CHECK_WITH_BOOST -#include "buffer_utils.hh" -#endif - namespace Kakoune { -#ifdef REGEX_CHECK_WITH_BOOST -using Utf8It = RegexUtf8It; - -boost::regbase::flag_type convert_flags(RegexCompileFlags flags) -{ - boost::regbase::flag_type res = boost::regbase::ECMAScript; - if (flags & RegexCompileFlags::NoSubs) - res |= boost::regbase::nosubs; - if (flags & RegexCompileFlags::Optimize) - res |= boost::regbase::optimize; - return res; -} - -boost::regex_constants::match_flag_type convert_flags(RegexExecFlags flags) -{ - boost::regex_constants::match_flag_type res = boost::regex_constants::match_default; - - if (flags & RegexExecFlags::NotBeginOfLine) - res |= boost::regex_constants::match_not_bol; - if (flags & RegexExecFlags::NotEndOfLine) - res |= boost::regex_constants::match_not_eol; - if (flags & RegexExecFlags::NotBeginOfWord) - res |= boost::regex_constants::match_not_bow; - if (flags & RegexExecFlags::NotEndOfWord) - res |= boost::regex_constants::match_not_eow; - if (flags & RegexExecFlags::NotBeginOfSubject) - res |= boost::regex_constants::match_not_bob; - if (flags & RegexExecFlags::NotInitialNull) - res |= boost::regex_constants::match_not_initial_null; - if (flags & RegexExecFlags::AnyMatch) - res |= boost::regex_constants::match_any; - if (flags & RegexExecFlags::PrevAvailable) - res |= boost::regex_constants::match_prev_avail; - - return res; -} - -void regex_mismatch(const Regex& re) -{ - write_to_debug_buffer(format("regex mismatch for '{}'", re.str())); -} -#endif - Regex::Regex(StringView re, RegexCompileFlags flags, MatchDirection direction) : m_impl{new CompiledRegex{compile_regex(re, flags, direction)}}, m_str{re.str()} -{ -#ifdef REGEX_CHECK_WITH_BOOST - if (direction == MatchDirection::Forward) try - { - m_boost_impl.assign({Utf8It{re.begin(), re}, Utf8It{re.end(), re}, convert_flags(flags)}); - } - catch (std::runtime_error& err) { throw regex_error(err.what()); } -#endif -} +{} String option_to_string(const Regex& re) { diff --git a/src/regex.hh b/src/regex.hh index 0d565170..8e6c2dac 100644 --- a/src/regex.hh +++ b/src/regex.hh @@ -4,15 +4,6 @@ #include "string.hh" #include "regex_impl.hh" -#define REGEX_CHECK_WITH_BOOST - -#ifdef REGEX_CHECK_WITH_BOOST -#include "exception.hh" -#include "string_utils.hh" -#include "utf8_iterator.hh" -#include -#endif - namespace Kakoune { @@ -36,17 +27,9 @@ public: const CompiledRegex* impl() const { return m_impl.get(); } -#ifdef REGEX_CHECK_WITH_BOOST - using BoostImpl = boost::basic_regex>; - const BoostImpl& boost_impl() const { return m_boost_impl; } -#endif - private: RefPtr m_impl; String m_str; -#ifdef REGEX_CHECK_WITH_BOOST - BoostImpl m_boost_impl; -#endif }; template @@ -124,56 +107,10 @@ inline RegexExecFlags match_flags(bool bol, bool eol, bool bow, bool eow) (eow ? RegexExecFlags::None : RegexExecFlags::NotEndOfWord); } -#ifdef REGEX_CHECK_WITH_BOOST -void regex_mismatch(const Regex& re); - -template -using RegexUtf8It = utf8::iterator; - -template -void check_captures(const Regex& re, const boost::match_results>& res, const Vector& captures) -{ - if (res.size() > captures.size() * 2) - return regex_mismatch(re); - - for (size_t i = 0; i < res.size(); ++i) - { - if (not res[i].matched) - { - if (captures[i*2] != It{} or captures[i*2+1] != It{}) - regex_mismatch(re); - continue; - } - - if (res[i].first != captures[i*2]) - regex_mismatch(re); - if (res[i].second != captures[i*2+1]) - regex_mismatch(re); - } -} - -boost::regbase::flag_type convert_flags(RegexCompileFlags flags); -boost::regex_constants::match_flag_type convert_flags(RegexExecFlags flags); -#endif - template bool regex_match(It begin, It end, const Regex& re) { - const bool matched = regex_match(begin, end, *re.impl()); -#ifdef REGEX_CHECK_WITH_BOOST - try - { - if (not re.boost_impl().empty() and - matched != boost::regex_match>({begin, begin, end}, {end, begin, end}, - re.boost_impl())) - regex_mismatch(re); - } - catch (std::runtime_error& err) - { - throw runtime_error{format("Regex matching error: {}", err.what())}; - } -#endif - return matched; + return regex_match(begin, end, *re.impl()); } template @@ -181,24 +118,6 @@ bool regex_match(It begin, It end, MatchResults& res, const Regex& re) { Vector captures; const bool matched = regex_match(begin, end, captures, *re.impl()); - -#ifdef REGEX_CHECK_WITH_BOOST - try - { - boost::match_results> boost_res; - if (not re.boost_impl().empty() and - matched != boost::regex_match>({begin, begin, end}, {end, begin, end}, - boost_res, re.boost_impl())) - regex_mismatch(re); - if (not re.boost_impl().empty() and matched) - check_captures(re, boost_res, captures); - } - catch (std::runtime_error& err) - { - throw runtime_error{format("Regex matching error: {}", err.what())}; - } -#endif - res = matched ? MatchResults{std::move(captures)} : MatchResults{}; return matched; } @@ -207,23 +126,7 @@ template bool regex_search(It begin, It end, const Regex& re, RegexExecFlags flags = RegexExecFlags::None) { - const bool matched = regex_search(begin, end, *re.impl(), flags); - -#ifdef REGEX_CHECK_WITH_BOOST - try - { - auto first = (flags & RegexExecFlags::PrevAvailable) ? begin-1 : begin; - if (not re.boost_impl().empty() and - matched != boost::regex_search>({begin, first, end}, {end, first, end}, - re.boost_impl(), convert_flags(flags))) - regex_mismatch(re); - } - catch (std::runtime_error& err) - { - throw runtime_error{format("Regex searching error: {}", err.what())}; - } -#endif - return matched; + return regex_search(begin, end, *re.impl(), flags); } template @@ -232,28 +135,6 @@ bool regex_search(It begin, It end, MatchResults& res, const Regex& re, { Vector captures; const bool matched = regex_search(begin, end, captures, *re.impl(), flags); - -#ifdef REGEX_CHECK_WITH_BOOST - try - { - if (direction == MatchDirection::Forward) - { - auto first = (flags & RegexExecFlags::PrevAvailable) ? begin-1 : begin; - boost::match_results> boost_res; - if (not re.boost_impl().empty() and - matched != boost::regex_search>({begin, first, end}, {end, first, end}, - boost_res, re.boost_impl(), convert_flags(flags))) - regex_mismatch(re); - if (not re.boost_impl().empty() and matched) - check_captures(re, boost_res, captures); - } - } - catch (std::runtime_error& err) - { - throw runtime_error{format("Regex searching error: {}", err.what())}; - } -#endif - res = matched ? MatchResults{std::move(captures)} : MatchResults{}; return matched; }