From 8846d7784588c278beb9801c340f82102e29c34d Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 28 Jan 2013 20:36:54 -0800 Subject: [PATCH] Use ICU regular expressions for user-entered regexps --- aegisub/src/search_replace_engine.cpp | 20 +++++++++++--------- aegisub/src/search_replace_engine.h | 6 +++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/aegisub/src/search_replace_engine.cpp b/aegisub/src/search_replace_engine.cpp index 6fb45d60a..105f14ef0 100644 --- a/aegisub/src/search_replace_engine.cpp +++ b/aegisub/src/search_replace_engine.cpp @@ -57,7 +57,7 @@ public: return (d->*field).get().substr(s); } - MatchState make_match_state(size_t s, size_t e, boost::regex *r = nullptr) { + MatchState make_match_state(size_t s, size_t e, boost::u32regex *r = nullptr) { return MatchState(s + start, e + start, r); } }; @@ -107,7 +107,7 @@ public: return out; } - MatchState make_match_state(size_t s, size_t e, boost::regex *r = nullptr) { + MatchState make_match_state(size_t s, size_t e, boost::u32regex *r = nullptr) { s += start; e += start; @@ -141,16 +141,16 @@ public: template matcher get_matcher(SearchReplaceSettings const& settings, Accessor&& a) { if (settings.use_regex) { - int flags = boost::regex::perl; + int flags = boost::u32regex::perl; if (!settings.match_case) - flags |= boost::regex::icase; + flags |= boost::u32regex::icase; - boost::regex regex(settings.find, flags); + auto regex = boost::make_u32regex(settings.find, flags); return [=](const AssDialogue *diag, size_t start) mutable -> MatchState { boost::smatch result; auto const& str = a.get(diag, start); - if (!regex_search(str, result, regex, start > 0 ? boost::match_not_bol : boost::match_default)) + if (!u32regex_search(str, result, regex, start > 0 ? boost::match_not_bol : boost::match_default)) return MatchState(); return a.make_match_state(result.position(), result.position() + result.length(), ®ex); }; @@ -207,7 +207,7 @@ void SearchReplaceEngine::Replace(AssDialogue *diag, MatchState &ms) { std::string replacement = settings.replace_with; if (ms.re) { auto to_replace = text.substr(ms.start, ms.end - ms.start); - replacement = regex_replace(to_replace, *ms.re, replacement, boost::format_first_only); + replacement = u32regex_replace(to_replace, *ms.re, replacement, boost::format_first_only); } diag_field = text.substr(0, ms.start) + replacement + text.substr(ms.end); @@ -309,8 +309,10 @@ bool SearchReplaceEngine::ReplaceAll() { if (MatchState ms = matches(diag, 0)) { auto& diag_field = diag->*get_dialogue_field(settings.field); std::string const& text = diag_field.get(); - count += distance(boost::sregex_iterator(begin(text), end(text), *ms.re), boost::sregex_iterator()); - diag_field = regex_replace(text, *ms.re, settings.replace_with); + count += distance( + boost::u32regex_iterator(begin(text), end(text), *ms.re), + boost::u32regex_iterator()); + diag_field = u32regex_replace(text, *ms.re, settings.replace_with); } continue; } diff --git a/aegisub/src/search_replace_engine.h b/aegisub/src/search_replace_engine.h index 5ea41d12d..a81b5164a 100644 --- a/aegisub/src/search_replace_engine.h +++ b/aegisub/src/search_replace_engine.h @@ -15,18 +15,18 @@ // Aegisub Project http://www.aegisub.org/ #include -#include +#include #include namespace agi { struct Context; } class AssDialogue; struct MatchState { - boost::regex *re; + boost::u32regex *re; size_t start, end; MatchState() : re(nullptr), start(0), end(-1) { } - MatchState(size_t s, size_t e, boost::regex *re) : re(re), start(s), end(e) { } + MatchState(size_t s, size_t e, boost::u32regex *re) : re(re), start(s), end(e) { } operator bool() const { return end != (size_t)-1; } };