Refactor regex uses, do not reference boost except in regex.hh

This commit is contained in:
Maxime Coste 2014-10-13 13:12:33 +01:00
parent b6f2b872b0
commit fa85f0fc32
22 changed files with 125 additions and 80 deletions

View File

@ -3,6 +3,7 @@
#include "buffer_manager.hh"
#include "event_manager.hh"
#include <unistd.h>
#include <sys/select.h>
namespace Kakoune

View File

@ -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);

View File

@ -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"));
}
}

View File

@ -27,6 +27,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
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 } });
};

View File

@ -1,8 +1,6 @@
#include "env_vars.hh"
#if __APPLE__
extern char **environ;
#endif
namespace Kakoune
{

View File

@ -7,6 +7,7 @@
#include "completion.hh"
#include "debug.hh"
#include "unicode.hh"
#include "regex.hh"
#include <errno.h>
#include <sys/types.h>
@ -300,12 +301,12 @@ std::vector<String> 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<String> res = list_files(fileprefix, dirname, filter);
for (auto& file : res)

View File

@ -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
{

View File

@ -22,8 +22,6 @@ namespace Kakoune
using namespace std::placeholders;
using RegexIterator = boost::regex_iterator<BufferIterator>;
template<typename T>
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<BufferIterator>;
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<String::const_iterator> res;
if (not regex_match(it->begin(), it->end(), res, face_spec_ex))
throw runtime_error("wrong face spec: '" + *it +
"' expected <capture>:<facespec>");
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<const char*> it{l.begin(), l.end(), regex}, end{}; it != end; ++it)
for (RegexIterator<const char*> 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<LineModification> modifs,
line < buffer.line_count(); ++line)
{
auto l = buffer[line];
for (boost::regex_iterator<const char*> it{l.begin(), l.end(), regex}, end{}; it != end; ++it)
for (RegexIterator<const char*> 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());
}

View File

@ -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

View File

@ -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<String> strings)

View File

@ -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<StringList>();;
const StringList& opt = options[option_name].get<StringList>();
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<String::const_iterator> 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))

View File

@ -16,10 +16,11 @@
#include <ncursesw/ncurses.h>
#endif
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
namespace Kakoune
{

View File

@ -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<direction, mode>(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<Selection> 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())

View File

@ -5,6 +5,7 @@
#include "exception.hh"
#include "option_types.hh"
#include "utils.hh"
#include "regex.hh"
#include <unordered_map>

25
src/regex.cc Normal file
View File

@ -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());
}
}
}

33
src/regex.hh Normal file
View File

@ -0,0 +1,33 @@
#ifndef regex_hh_INCLUDED
#define regex_hh_INCLUDED
#include "string.hh"
#if 0
#include <regex>
namespace kak_regex_ns = std;
#else
#include <boost/regex.hpp>
namespace kak_regex_ns = boost;
#endif
namespace Kakoune
{
using Regex = kak_regex_ns::regex;
template<typename Iterator>
using RegexIterator = kak_regex_ns::regex_iterator<Iterator>;
template<typename Iterator>
using MatchResults = kak_regex_ns::match_results<Iterator>;
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

View File

@ -496,6 +496,8 @@ void select_buffer(SelectionList& selections)
selections = SelectionList{ buffer, target_eol({{0,0}, buffer.back_coord()}) };
}
using RegexIt = RegexIterator<BufferIterator>;
void select_all_matches(SelectionList& selections, const Regex& regex)
{
std::vector<Selection> 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)
{

View File

@ -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<BufferIterator>;
template<WordType word_type>
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<BufferIterator>;
inline bool find_last_match(BufferIterator begin, const BufferIterator& end,
MatchResults& res, const Regex& regex)
MatchResults<BufferIterator>& res,
const Regex& regex)
{
MatchResults matches;
while (boost::regex_search(begin, end, matches, regex))
MatchResults<BufferIterator> 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<Direction direction>
bool find_match_in_buffer(const Buffer& buffer, const BufferIterator pos,
MatchResults& matches, const Regex& ex)
MatchResults<BufferIterator>& 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<BufferIterator> matches;
bool found = false;
auto pos = direction == Forward ? utf8::next(begin, buffer.end())
: utf8::previous(begin, buffer.begin());

View File

@ -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<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
boost::regex_iterator<StringView::iterator> end;
RegexIterator<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
RegexIterator<StringView::iterator> 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<Regex, EnvVarRetriever>& 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);

View File

@ -2,6 +2,7 @@
#define shell_manager_hh_INCLUDED
#include "string.hh"
#include "regex.hh"
#include "utils.hh"
#include "env_vars.hh"

View File

@ -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();

View File

@ -6,13 +6,12 @@
#include "utf8.hh"
#include <string>
#include <boost/regex.hpp>
#include <climits>
#include <cstring>
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);