Regex: Remove boost related code

This commit is contained in:
Maxime Coste 2017-11-01 14:08:03 +08:00
parent 2b295a265e
commit 51de90f366
3 changed files with 11 additions and 185 deletions

View File

@ -30,24 +30,24 @@ mandir := $(DESTDIR)$(PREFIX)/share/man/man1
os := $(shell uname) os := $(shell uname)
ifeq ($(os),Darwin) ifeq ($(os),Darwin)
LIBS += -lncurses -lboost_regex LIBS += -lncurses
CPPFLAGS += -I$(PREFIX)/opt/ncurses/include -I$(PREFIX)/opt/boost/include -I/opt/local/include CPPFLAGS += -I$(PREFIX)/opt/ncurses/include -I/opt/local/include
LDFLAGS += -L$(PREFIX)/opt/ncurses/lib -L$(PREFIX)/opt/boost/lib -L/opt/local/lib LDFLAGS += -L$(PREFIX)/opt/ncurses/lib -L/opt/local/lib
else ifeq ($(os),FreeBSD) else ifeq ($(os),FreeBSD)
LIBS += -ltinfow -lncursesw -lboost_regex LIBS += -ltinfow -lncursesw
CPPFLAGS += -I/usr/local/include CPPFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib LDFLAGS += -L/usr/local/lib
else ifeq ($(os),Haiku) else ifeq ($(os),Haiku)
LIBS += -lncursesw -lboost_regex -lnetwork -lbe LIBS += -lncursesw -lnetwork -lbe
else ifeq ($(os),DragonFly) else ifeq ($(os),DragonFly)
LIBS += -lncursesw -lboost_regex LIBS += -lncursesw
CPPFLAGS += -I/usr/local/include CPPFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib LDFLAGS += -L/usr/local/lib
else ifneq (,$(findstring CYGWIN,$(os))) else ifneq (,$(findstring CYGWIN,$(os)))
CPPFLAGS += -D_XOPEN_SOURCE=700 CPPFLAGS += -D_XOPEN_SOURCE=700
LIBS += -lncursesw -lboost_regex -ldbghelp LIBS += -lncursesw -ldbghelp
else else
LIBS += $(shell pkg-config --libs ncursesw) -lboost_regex LIBS += $(shell pkg-config --libs ncursesw)
CPPFLAGS += $(shell pkg-config --cflags ncursesw) CPPFLAGS += $(shell pkg-config --cflags ncursesw)
LDFLAGS += -rdynamic LDFLAGS += -rdynamic
endif endif

View File

@ -1,67 +1,12 @@
#include "regex.hh" #include "regex.hh"
#ifdef REGEX_CHECK_WITH_BOOST
#include "buffer_utils.hh"
#endif
namespace Kakoune namespace Kakoune
{ {
#ifdef REGEX_CHECK_WITH_BOOST
using Utf8It = RegexUtf8It<const char*>;
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) Regex::Regex(StringView re, RegexCompileFlags flags, MatchDirection direction)
: m_impl{new CompiledRegex{compile_regex(re, flags, direction)}}, : m_impl{new CompiledRegex{compile_regex(re, flags, direction)}},
m_str{re.str()} 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) String option_to_string(const Regex& re)
{ {

View File

@ -4,15 +4,6 @@
#include "string.hh" #include "string.hh"
#include "regex_impl.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 <boost/regex.hpp>
#endif
namespace Kakoune namespace Kakoune
{ {
@ -36,17 +27,9 @@ public:
const CompiledRegex* impl() const { return m_impl.get(); } const CompiledRegex* impl() const { return m_impl.get(); }
#ifdef REGEX_CHECK_WITH_BOOST
using BoostImpl = boost::basic_regex<wchar_t, boost::c_regex_traits<wchar_t>>;
const BoostImpl& boost_impl() const { return m_boost_impl; }
#endif
private: private:
RefPtr<CompiledRegex> m_impl; RefPtr<CompiledRegex> m_impl;
String m_str; String m_str;
#ifdef REGEX_CHECK_WITH_BOOST
BoostImpl m_boost_impl;
#endif
}; };
template<typename Iterator> template<typename Iterator>
@ -124,56 +107,10 @@ inline RegexExecFlags match_flags(bool bol, bool eol, bool bow, bool eow)
(eow ? RegexExecFlags::None : RegexExecFlags::NotEndOfWord); (eow ? RegexExecFlags::None : RegexExecFlags::NotEndOfWord);
} }
#ifdef REGEX_CHECK_WITH_BOOST
void regex_mismatch(const Regex& re);
template<typename It>
using RegexUtf8It = utf8::iterator<It, wchar_t, ssize_t>;
template<typename It>
void check_captures(const Regex& re, const boost::match_results<RegexUtf8It<It>>& res, const Vector<It>& 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<typename It> template<typename It>
bool regex_match(It begin, It end, const Regex& re) bool regex_match(It begin, It end, const Regex& re)
{ {
const bool matched = regex_match(begin, end, *re.impl()); return regex_match(begin, end, *re.impl());
#ifdef REGEX_CHECK_WITH_BOOST
try
{
if (not re.boost_impl().empty() and
matched != boost::regex_match<RegexUtf8It<It>>({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;
} }
template<typename It> template<typename It>
@ -181,24 +118,6 @@ bool regex_match(It begin, It end, MatchResults<It>& res, const Regex& re)
{ {
Vector<It> captures; Vector<It> captures;
const bool matched = regex_match(begin, end, captures, *re.impl()); const bool matched = regex_match(begin, end, captures, *re.impl());
#ifdef REGEX_CHECK_WITH_BOOST
try
{
boost::match_results<RegexUtf8It<It>> boost_res;
if (not re.boost_impl().empty() and
matched != boost::regex_match<RegexUtf8It<It>>({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<It>{std::move(captures)} : MatchResults<It>{}; res = matched ? MatchResults<It>{std::move(captures)} : MatchResults<It>{};
return matched; return matched;
} }
@ -207,23 +126,7 @@ template<typename It>
bool regex_search(It begin, It end, const Regex& re, bool regex_search(It begin, It end, const Regex& re,
RegexExecFlags flags = RegexExecFlags::None) RegexExecFlags flags = RegexExecFlags::None)
{ {
const bool matched = regex_search(begin, end, *re.impl(), flags); return 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<RegexUtf8It<It>>({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;
} }
template<typename It, MatchDirection direction = MatchDirection::Forward> template<typename It, MatchDirection direction = MatchDirection::Forward>
@ -232,28 +135,6 @@ bool regex_search(It begin, It end, MatchResults<It>& res, const Regex& re,
{ {
Vector<It> captures; Vector<It> captures;
const bool matched = regex_search<It, direction>(begin, end, captures, *re.impl(), flags); const bool matched = regex_search<It, direction>(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<RegexUtf8It<It>> boost_res;
if (not re.boost_impl().empty() and
matched != boost::regex_search<RegexUtf8It<It>>({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<It>{std::move(captures)} : MatchResults<It>{}; res = matched ? MatchResults<It>{std::move(captures)} : MatchResults<It>{};
return matched; return matched;
} }