notification: compile static regexes only once

parent 6f87653a
...@@ -113,101 +113,28 @@ namespace XimperShellNotificationCenter { ...@@ -113,101 +113,28 @@ namespace XimperShellNotificationCenter {
private const RegexMatchFlags REGEX_MATCH_FLAGS = RegexMatchFlags.NOTEMPTY; private const RegexMatchFlags REGEX_MATCH_FLAGS = RegexMatchFlags.NOTEMPTY;
public virtual bool matches_notification (NotifyParams param) { private static bool matches_field (string ?pattern, string ?value,
if (app_name != null) { RegexCompileFlags compile_flags
if (param.app_name == null) { = REGEX_COMPILE_OPTIONS) {
return false; if (pattern == null) {
} return true;
bool result = Regex.match_simple (
app_name, param.app_name,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
} }
if (desktop_entry != null) { if (value == null) {
if (param.desktop_entry == null) { return false;
return false;
}
bool result = Regex.match_simple (
desktop_entry, param.desktop_entry,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
}
if (summary != null) {
if (param.summary == null) {
return false;
}
bool result = Regex.match_simple (
summary, param.summary,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
}
if (body != null) {
if (param.body == null) {
return false;
}
bool result = Regex.match_simple (
body, param.body,
0,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
}
if (urgency != null) {
bool result = Regex.match_simple (
urgency, param.urgency.to_string (),
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
}
if (category != null) {
if (param.category == null) {
return false;
}
bool result = Regex.match_simple (
category, param.category,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
}
if (sound_file != null) {
if (param.sound_file == null) {
return false;
}
bool result = Regex.match_simple (
sound_file, param.sound_file,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
}
if (sound_name != null) {
if (param.sound_name == null) {
return false;
}
bool result = Regex.match_simple (
sound_name, param.sound_name,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
} }
return true; return Regex.match_simple (pattern, value,
compile_flags, REGEX_MATCH_FLAGS);
}
public virtual bool matches_notification (NotifyParams param) {
return matches_field (app_name, param.app_name)
&& matches_field (desktop_entry, param.desktop_entry)
&& matches_field (summary, param.summary)
&& matches_field (body, param.body, 0)
&& matches_field (urgency, param.urgency.to_string ())
&& matches_field (category, param.category)
&& matches_field (sound_file, param.sound_file)
&& matches_field (sound_name, param.sound_name);
} }
public string to_string () { public string to_string () {
......
...@@ -87,18 +87,10 @@ namespace XimperShellNotificationCenter { ...@@ -87,18 +87,10 @@ namespace XimperShellNotificationCenter {
public bool has_inline_reply { get; private set; default = false; } public bool has_inline_reply { get; private set; default = false; }
private static Regex code_regex; private static Regex code_regex;
private static Regex tag_regex; private static Regex tag_regex;
private static Regex tag_unescape_regex; private static Regex tag_unescape_regex;
private static Regex img_tag_regex; private static Regex img_tag_regex;
private const string[] TAGS = { "b", "u", "i" }; private static bool regexes_initialized = false;
private const string[] UNESCAPE_CHARS = {
"lt;", "#60;", "#x3C;", "#x3c;", // <
"gt;", "#62;", "#x3E;", "#x3e;", // >
"apos;", "#39;", // '
"quot;", "#34;", // "
"amp;" // &
};
public bool dismissed { get; private set; default = false; } public bool dismissed { get; private set; default = false; }
public bool dismissed_by_swipe { get; private set; default = false; } public bool dismissed_by_swipe { get; private set; default = false; }
...@@ -130,18 +122,39 @@ namespace XimperShellNotificationCenter { ...@@ -130,18 +122,39 @@ namespace XimperShellNotificationCenter {
build_noti (); build_noti ();
} }
construct { private const string[] TAGS = { "b", "u", "i" };
private const string[] UNESCAPE_CHARS = {
"lt;", "#60;", "#x3C;", "#x3c;", // <
"gt;", "#62;", "#x3E;", "#x3e;", // >
"apos;", "#39;", // '
"quot;", "#34;", // "
"amp;" // &
};
private static void init_regexes () {
if (regexes_initialized) {
return;
}
try { try {
code_regex = new Regex ("(?<= |^)(\\d{3}(-| )\\d{3}|\\d{4,8})(?= |$|\\.|,)", code_regex = new Regex (
RegexCompileFlags.MULTILINE); "(?<= |^)(\\d{3}(-| )\\d{3}|\\d{4,8})(?= |$|\\.|,)",
RegexCompileFlags.MULTILINE);
string joined_tags = string.joinv ("|", TAGS); string joined_tags = string.joinv ("|", TAGS);
tag_regex = new Regex ("&lt;(/?(?:%s))&gt;".printf (joined_tags)); tag_regex = new Regex (
"&lt;(/?(?:%s))&gt;".printf (joined_tags));
string unescaped = string.joinv ("|", UNESCAPE_CHARS); string unescaped = string.joinv ("|", UNESCAPE_CHARS);
tag_unescape_regex = new Regex ("&amp;(?=%s)".printf (unescaped)); tag_unescape_regex = new Regex (
img_tag_regex = new Regex ("<img[^>]* src=((\"([^\"]*)\")|(\'([^\']*)\'))[^>]*>"); "&amp;(?=%s)".printf (unescaped));
img_tag_regex = new Regex (
"<img[^>]* src=((\"([^\"]*)\")|(\'([^\']*)\'))[^>]*>");
regexes_initialized = true;
} catch (Error e) { } catch (Error e) {
stderr.printf ("Invalid regex: %s", e.message); stderr.printf ("Invalid regex: %s", e.message);
} }
}
construct {
init_regexes ();
bind_property ("dismissed", bind_property ("dismissed",
this, "sensitive", this, "sensitive",
......
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