Commit dd15200c authored by twanvl's avatar twanvl

Use Regex instead of wxRegEx everywhere

parent 9af99b57
...@@ -12,9 +12,9 @@ ...@@ -12,9 +12,9 @@
#include <data/stylesheet.hpp> #include <data/stylesheet.hpp>
#include <data/symbol_font.hpp> #include <data/symbol_font.hpp>
#include <util/io/package_manager.hpp> #include <util/io/package_manager.hpp>
#include <util/regex.hpp>
#include <script/to_value.hpp> #include <script/to_value.hpp>
#include <wx/wfstream.h> #include <wx/wfstream.h>
#include <wx/regex.h>
#include <wx/stdpaths.h> #include <wx/stdpaths.h>
#if defined(__WXMSW__) #if defined(__WXMSW__)
...@@ -70,7 +70,7 @@ IMPLEMENT_REFLECTION_NO_GET_MEMBER(SubLocale) { ...@@ -70,7 +70,7 @@ IMPLEMENT_REFLECTION_NO_GET_MEMBER(SubLocale) {
// ----------------------------------------------------------------------------- : Wildcards // ----------------------------------------------------------------------------- : Wildcards
bool match_wildcard(const String& wildcard, const String& name) { bool match_wildcard(const String& wildcard, const String& name) {
return wxRegEx(replace_all(replace_all(wildcard, _("."), _("\\.")), _("*"), _(".*"))).Matches(name); return Regex(replace_all(replace_all(wildcard, _("."), _("\\.")), _("*"), _(".*"))).matches(name);
} }
SubLocaleP find_wildcard(map<String,SubLocaleP>& items, const String& name) { SubLocaleP find_wildcard(map<String,SubLocaleP>& items, const String& name) {
......
...@@ -88,7 +88,7 @@ class SymbolInFont : public IntrusivePtrBase<SymbolInFont> { ...@@ -88,7 +88,7 @@ class SymbolInFont : public IntrusivePtrBase<SymbolInFont> {
Scriptable<bool> enabled; ///< Is this symbol enabled? Scriptable<bool> enabled; ///< Is this symbol enabled?
bool regex; ///< Should this symbol be matched by a regex? bool regex; ///< Should this symbol be matched by a regex?
int draw_text; ///< The index of the captured regex expression to draw, or -1 to not draw text int draw_text; ///< The index of the captured regex expression to draw, or -1 to not draw text
wxRegEx code_regex; ///< Regex for matching the symbol code Regex code_regex; ///< Regex for matching the symbol code
FontP text_font; ///< Font to draw text in. FontP text_font; ///< Font to draw text in.
Alignment text_alignment; Alignment text_alignment;
double text_margin_left; double text_margin_left;
...@@ -109,7 +109,6 @@ SymbolInFont::SymbolInFont() ...@@ -109,7 +109,6 @@ SymbolInFont::SymbolInFont()
: enabled(true) : enabled(true)
, regex(false) , regex(false)
, draw_text(-1) , draw_text(-1)
, code_regex()
, text_alignment(ALIGN_MIDDLE_CENTER) , text_alignment(ALIGN_MIDDLE_CENTER)
, text_margin_left(0), text_margin_right(0) , text_margin_left(0), text_margin_right(0)
, text_margin_top(0), text_margin_bottom(0) , text_margin_top(0), text_margin_bottom(0)
...@@ -177,7 +176,7 @@ IMPLEMENT_REFLECTION(SymbolInFont) { ...@@ -177,7 +176,7 @@ IMPLEMENT_REFLECTION(SymbolInFont) {
REFLECT(regex); REFLECT(regex);
REFLECT_IF_READING REFLECT_IF_READING
if (regex) if (regex)
code_regex.Compile(code, wxRE_ADVANCED); code_regex.assign(code);
REFLECT(draw_text); REFLECT(draw_text);
REFLECT(text_font); REFLECT(text_font);
REFLECT(text_alignment); REFLECT(text_alignment);
...@@ -198,28 +197,33 @@ void SymbolFont::split(const String& text, SplitSymbols& out) const { ...@@ -198,28 +197,33 @@ void SymbolFont::split(const String& text, SplitSymbols& out) const {
// check symbol list // check symbol list
FOR_EACH_CONST(sym, symbols) { FOR_EACH_CONST(sym, symbols) {
if (!sym->code.empty() && sym->enabled) { if (!sym->code.empty() && sym->enabled) {
size_t start, len = 1; if (sym->regex) {
if (sym->regex && sym->code_regex.IsValid() && sym->code_regex.Matches(text.substr(pos), wxRE_NOTBOL | wxRE_NOTEOL) && if (sym->code_regex.empty()) {
sym->code_regex.GetMatch(&start, &len) && start == 0 && len > 0) { //Matches the regex sym->code_regex.assign(sym->code);
if (sym->draw_text >= 0 && sym->draw_text < (int)sym->code_regex.GetMatchCount()) { }
size_t draw_end; Regex::Results results;
sym->code_regex.GetMatch(&start, &draw_end, sym->draw_text); if (sym->code_regex.matches(results,text.begin() + pos, text.end())
out.push_back(DrawableSymbol( && results.position() == 0 && results.length() > 0) { //Matches the regex
text.substr(pos, len), if (sym->draw_text >= 0 && sym->draw_text < (int)results.size()) {
text.substr(pos + start, draw_end - start), out.push_back(DrawableSymbol(
*sym)); results.str(),
} else { results.str(sym->draw_text),
out.push_back(DrawableSymbol( *sym));
text.substr(pos, len), } else {
_(""), out.push_back(DrawableSymbol(
*sym)); results.str(),
_(""),
*sym));
}
pos += results.length();
goto next_symbol;
}
} else {
if (is_substr(text, pos, sym->code)) {
out.push_back(DrawableSymbol(sym->code, sym->draw_text >= 0 ? sym->code : _(""), *sym));
pos += sym->code.size();
goto next_symbol; // continue two levels
} }
pos += len;
goto next_symbol;
} else if (is_substr(text, pos, sym->code)) {
out.push_back(DrawableSymbol(sym->code, sym->draw_text >= 0 ? sym->code : _(""), *sym));
pos += sym->code.size();
goto next_symbol; // continue two levels
} }
} }
} }
...@@ -236,14 +240,18 @@ size_t SymbolFont::recognizePrefix(const String& text, size_t start) const { ...@@ -236,14 +240,18 @@ size_t SymbolFont::recognizePrefix(const String& text, size_t start) const {
// check symbol list // check symbol list
FOR_EACH_CONST(sym, symbols) { FOR_EACH_CONST(sym, symbols) {
if (!sym->code.empty() && sym->enabled) { if (!sym->code.empty() && sym->enabled) {
size_t start, len = 1; if (sym->regex) {
if (sym->regex && sym->code_regex.IsValid() && sym->code_regex.Matches(text.substr(pos), wxRE_NOTBOL | wxRE_NOTEOL) && Regex::Results results;
sym->code_regex.GetMatch(&start, &len) && start == 0 && len > 0) { //Matches the regex if (!sym->code_regex.empty() && sym->code_regex.matches(results,text.begin() + pos, text.end())
pos += len; && results.position() == 0 && results.length() > 0) { //Matches the regex
goto next_symbol; pos += results.length();
} else if (is_substr(text, pos, sym->code)) { goto next_symbol;
pos += sym->code.size(); }
goto next_symbol; // continue two levels } else {
if (is_substr(text, pos, sym->code)) {
pos += sym->code.size();
goto next_symbol; // continue two levels
}
} }
} }
} }
...@@ -256,7 +264,7 @@ next_symbol:; ...@@ -256,7 +264,7 @@ next_symbol:;
SymbolInFont* SymbolFont::defaultSymbol() const { SymbolInFont* SymbolFont::defaultSymbol() const {
FOR_EACH_CONST(sym, symbols) { FOR_EACH_CONST(sym, symbols) {
if (sym->regex && sym->code_regex.Matches(_("0")) && sym->enabled) return sym.get(); if (sym->enabled && sym->regex && sym->code_regex.matches(_("0"))) return sym.get();
} }
return nullptr; return nullptr;
} }
......
...@@ -100,6 +100,7 @@ struct TextElementsFromString { ...@@ -100,6 +100,7 @@ struct TextElementsFromString {
// text element before this tag? // text element before this tag?
addText(te, text, text_start, pos, style, ctx); addText(te, text, text_start, pos, style, ctx);
} }
// a (formatting) tag
size_t tag_start = pos; size_t tag_start = pos;
pos = skip_tag(text, tag_start); pos = skip_tag(text, tag_start);
if (is_substr(text, tag_start, _( "<b"))) bold += 1; if (is_substr(text, tag_start, _( "<b"))) bold += 1;
...@@ -177,18 +178,21 @@ struct TextElementsFromString { ...@@ -177,18 +178,21 @@ struct TextElementsFromString {
} else { } else {
// ignore other tags // ignore other tags
} }
// text starts again after the tag
text_start = pos; text_start = pos;
} else { } else {
// normal text
pos += 1; pos += 1;
} }
} }
if (text_start < end) { if (text_start < end) {
// remaining text at the end
addText(te, text, text_start, end, style, ctx); addText(te, text, text_start, end, style, ctx);
} }
} }
private: private:
/// Create a text element for a piece of text /// Create a text element for a piece of text, text[start..end)
void addText(TextElements& te, const String& text, size_t start, size_t end, const TextStyle& style, Context& ctx) { void addText(TextElements& te, const String& text, size_t start, size_t end, const TextStyle& style, Context& ctx) {
String content = untag(text.substr(start, end - start)); String content = untag(text.substr(start, end - start));
assert(content.size() == end-start); assert(content.size() == end-start);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment