2014-10-13 16:12:33 +04:00
|
|
|
#ifndef regex_hh_INCLUDED
|
|
|
|
#define regex_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "string.hh"
|
2015-07-14 23:06:41 +03:00
|
|
|
#include "exception.hh"
|
2014-10-13 16:12:33 +04:00
|
|
|
|
2014-10-13 22:40:27 +04:00
|
|
|
#ifdef KAK_USE_STDREGEX
|
2014-10-13 16:12:33 +04:00
|
|
|
#include <regex>
|
|
|
|
#else
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-07-14 23:06:41 +03:00
|
|
|
struct regex_error : runtime_error
|
|
|
|
{
|
|
|
|
regex_error(StringView desc)
|
|
|
|
: runtime_error{format("regex error: '{}'", desc)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2014-10-13 22:40:27 +04:00
|
|
|
#ifdef KAK_USE_STDREGEX
|
|
|
|
// Regex that keeps track of its string representation
|
|
|
|
struct Regex : std::regex
|
|
|
|
{
|
|
|
|
Regex() = default;
|
|
|
|
|
2015-07-14 23:06:41 +03:00
|
|
|
explicit Regex(StringView re, flag_type flags = ECMAScript) try
|
2015-09-15 15:28:36 +03:00
|
|
|
: std::regex(re.begin(), re.end(), flags), m_str(re.str()) {}
|
2015-07-14 23:06:41 +03:00
|
|
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
2014-10-13 22:40:27 +04:00
|
|
|
|
|
|
|
template<typename Iterator>
|
2015-07-14 23:06:41 +03:00
|
|
|
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript) try
|
2014-10-13 22:40:27 +04:00
|
|
|
: std::regex(begin, end, flags), m_str(begin, end) {}
|
2015-07-14 23:06:41 +03:00
|
|
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
2014-10-13 22:40:27 +04:00
|
|
|
|
|
|
|
bool empty() const { return m_str.empty(); }
|
2015-04-09 01:16:27 +03:00
|
|
|
bool operator==(const Regex& other) const { return m_str == other.m_str; }
|
|
|
|
bool operator!=(const Regex& other) const { return m_str != other.m_str; }
|
2014-10-13 22:40:27 +04:00
|
|
|
|
2015-03-10 22:33:46 +03:00
|
|
|
const String& str() const { return m_str; }
|
2014-10-13 22:40:27 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
String m_str;
|
|
|
|
};
|
|
|
|
namespace regex_ns = std;
|
|
|
|
#else
|
2015-03-10 22:33:46 +03:00
|
|
|
struct Regex : boost::regex
|
|
|
|
{
|
|
|
|
Regex() = default;
|
|
|
|
|
2015-07-14 23:06:41 +03:00
|
|
|
explicit Regex(StringView re, flag_type flags = ECMAScript) try
|
2015-03-10 22:33:46 +03:00
|
|
|
: boost::regex(re.begin(), re.end(), flags) {}
|
2015-07-14 23:06:41 +03:00
|
|
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
2015-03-10 22:33:46 +03:00
|
|
|
|
|
|
|
template<typename Iterator>
|
2015-07-14 23:06:41 +03:00
|
|
|
Regex(Iterator begin, Iterator end, flag_type flags = ECMAScript) try
|
2015-03-10 22:33:46 +03:00
|
|
|
: boost::regex(begin, end, flags) {}
|
2015-07-14 23:06:41 +03:00
|
|
|
catch (std::runtime_error& err) { throw regex_error(err.what()); }
|
2015-03-10 22:33:46 +03:00
|
|
|
|
2015-04-29 15:45:53 +03:00
|
|
|
String str() const { auto s = boost::regex::str(); return {s.begin(), s.end()}; }
|
2015-03-10 22:33:46 +03:00
|
|
|
};
|
2014-10-13 22:40:27 +04:00
|
|
|
namespace regex_ns = boost;
|
|
|
|
#endif
|
2014-10-13 16:12:33 +04:00
|
|
|
|
|
|
|
template<typename Iterator>
|
2014-10-13 22:40:27 +04:00
|
|
|
using RegexIterator = regex_ns::regex_iterator<Iterator>;
|
2014-10-13 16:12:33 +04:00
|
|
|
|
|
|
|
template<typename Iterator>
|
2014-10-13 22:40:27 +04:00
|
|
|
using MatchResults = regex_ns::match_results<Iterator>;
|
2014-10-13 16:12:33 +04:00
|
|
|
|
|
|
|
String option_to_string(const Regex& re);
|
|
|
|
void option_from_string(StringView str, Regex& re);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // regex_hh_INCLUDED
|