notification: compile static regexes only once

parent 6f87653a
......@@ -113,101 +113,28 @@ namespace XimperShellNotificationCenter {
private const RegexMatchFlags REGEX_MATCH_FLAGS = RegexMatchFlags.NOTEMPTY;
public virtual bool matches_notification (NotifyParams param) {
if (app_name != null) {
if (param.app_name == null) {
return false;
}
bool result = Regex.match_simple (
app_name, param.app_name,
REGEX_COMPILE_OPTIONS,
REGEX_MATCH_FLAGS);
if (!result) {
return false;
}
private static bool matches_field (string ?pattern, string ?value,
RegexCompileFlags compile_flags
= REGEX_COMPILE_OPTIONS) {
if (pattern == null) {
return true;
}
if (desktop_entry != null) {
if (param.desktop_entry == null) {
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;
}
if (value == null) {
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 () {
......
......@@ -87,18 +87,10 @@ namespace XimperShellNotificationCenter {
public bool has_inline_reply { get; private set; default = false; }
private static Regex code_regex;
private static Regex tag_regex;
private static Regex tag_unescape_regex;
private static Regex img_tag_regex;
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 bool regexes_initialized = false;
public bool dismissed { get; private set; default = false; }
public bool dismissed_by_swipe { get; private set; default = false; }
......@@ -130,18 +122,39 @@ namespace XimperShellNotificationCenter {
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 {
code_regex = new Regex ("(?<= |^)(\\d{3}(-| )\\d{3}|\\d{4,8})(?= |$|\\.|,)",
RegexCompileFlags.MULTILINE);
code_regex = new Regex (
"(?<= |^)(\\d{3}(-| )\\d{3}|\\d{4,8})(?= |$|\\.|,)",
RegexCompileFlags.MULTILINE);
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);
tag_unescape_regex = new Regex ("&amp;(?=%s)".printf (unescaped));
img_tag_regex = new Regex ("<img[^>]* src=((\"([^\"]*)\")|(\'([^\']*)\'))[^>]*>");
tag_unescape_regex = new Regex (
"&amp;(?=%s)".printf (unescaped));
img_tag_regex = new Regex (
"<img[^>]* src=((\"([^\"]*)\")|(\'([^\']*)\'))[^>]*>");
regexes_initialized = true;
} catch (Error e) {
stderr.printf ("Invalid regex: %s", e.message);
}
}
construct {
init_regexes ();
bind_property ("dismissed",
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