1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-15 18:15:53 +03:00

Regex: Make boost checking disableable at compile time

This commit is contained in:
Maxime Coste 2017-10-09 14:37:43 +08:00
parent 065bbc8f59
commit 785cd34b4b
3 changed files with 36 additions and 12 deletions

View File

@ -1,10 +1,13 @@
#include "regex.hh"
#ifdef REGEX_CHECK_WITH_BOOST
#include "buffer_utils.hh"
#endif
namespace Kakoune
{
#ifdef REGEX_CHECK_WITH_BOOST
using Utf8It = RegexUtf8It<const char*>;
boost::regbase::flag_type convert_flags(RegexCompileFlags flags)
@ -41,12 +44,19 @@ boost::regex_constants::match_flag_type convert_flags(RegexExecFlags flags)
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) try
: m_impl{new CompiledRegex{compile_regex(re, flags)}},
m_str{re.str()},
m_boost_impl{Utf8It{re.begin(), re}, Utf8It{re.end(), re}, convert_flags(flags)}
{
} catch (std::runtime_error& err) { throw regex_error(err.what()); }
m_str{re.str()}
#ifdef REGEX_CHECK_WITH_BOOST
, m_boost_impl{Utf8It{re.begin(), re}, Utf8It{re.end(), re}, convert_flags(flags)}
#endif
{} catch (std::runtime_error& err) { throw regex_error(err.what()); }
String option_to_string(const Regex& re)
{
@ -58,9 +68,4 @@ void option_from_string(StringView str, Regex& re)
re = Regex{str};
}
void regex_mismatch(const Regex& re)
{
write_to_debug_buffer(format("regex mismatch for '{}'", re.str()));
}
}

View File

@ -2,12 +2,16 @@
#define regex_hh_INCLUDED
#include "string.hh"
#include "string_utils.hh"
#include "exception.hh"
#include "utf8_iterator.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
{
@ -31,13 +35,17 @@ public:
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:
RefPtr<CompiledRegex> m_impl;
String m_str;
#ifdef REGEX_CHECK_WITH_BOOST
BoostImpl m_boost_impl;
#endif
};
template<typename Iterator>
@ -115,6 +123,7 @@ 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<typename It>
@ -144,6 +153,7 @@ void check_captures(const Regex& re, const boost::match_results<RegexUtf8It<It>>
boost::regbase::flag_type convert_flags(RegexCompileFlags flags);
boost::regex_constants::match_flag_type convert_flags(RegexExecFlags flags);
#endif
template<typename It>
bool regex_match(It begin, It end, const Regex& re)
@ -151,10 +161,12 @@ bool regex_match(It begin, It end, const Regex& re)
try
{
const bool matched = regex_match(begin, end, *re.impl());
#ifdef REGEX_CHECK_WITH_BOOST
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);
#endif
return matched;
}
catch (std::runtime_error& err)
@ -171,6 +183,7 @@ bool regex_match(It begin, It end, MatchResults<It>& res, const Regex& re)
Vector<It> captures;
const bool matched = regex_match(begin, end, captures, *re.impl());
#ifdef REGEX_CHECK_WITH_BOOST
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},
@ -178,6 +191,7 @@ bool regex_match(It begin, It end, MatchResults<It>& res, const Regex& re)
regex_mismatch(re);
if (not re.boost_impl().empty() and matched)
check_captures(re, boost_res, captures);
#endif
res = matched ? MatchResults<It>{std::move(captures)} : MatchResults<It>{};
return matched;
@ -196,11 +210,13 @@ bool regex_search(It begin, It end, const Regex& re,
{
const bool matched = regex_search(begin, end, *re.impl(), flags);
#ifdef REGEX_CHECK_WITH_BOOST
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);
#endif
return matched;
}
catch (std::runtime_error& err)
@ -218,6 +234,7 @@ bool regex_search(It begin, It end, MatchResults<It>& res, const Regex& re,
Vector<It> captures;
const bool matched = regex_search(begin, end, captures, *re.impl(), flags);
#ifdef REGEX_CHECK_WITH_BOOST
auto first = (flags & RegexExecFlags::PrevAvailable) ? begin-1 : begin;
boost::match_results<RegexUtf8It<It>> boost_res;
if (not re.boost_impl().empty() and
@ -226,6 +243,7 @@ bool regex_search(It begin, It end, MatchResults<It>& res, const Regex& re,
regex_mismatch(re);
if (not re.boost_impl().empty() and matched)
check_captures(re, boost_res, captures);
#endif
res = matched ? MatchResults<It>{std::move(captures)} : MatchResults<It>{};
return matched;

View File

@ -6,6 +6,7 @@
#include "unit_tests.hh"
#include "utf8.hh"
#include "utf8_iterator.hh"
#include "string_utils.hh"
#include "vector.hh"
namespace Kakoune