Merge lp:~jacobparker1992/slingshot/conversion-plugin into lp:~parnold-x/slingshot/conversion-plugin

Proposed by Jacob Parker
Status: Merged
Merged at revision: 437
Proposed branch: lp:~jacobparker1992/slingshot/conversion-plugin
Merge into: lp:~parnold-x/slingshot/conversion-plugin
Diff against target: 49 lines (+12/-5)
1 file modified
lib/synapse-plugins/conversion-plugin.vala (+12/-5)
To merge this branch: bzr merge lp:~jacobparker1992/slingshot/conversion-plugin
Reviewer Review Type Date Requested Status
Djax Pending
Review via email: mp+230900@code.launchpad.net

Description of the change

I fixed your regex. It now works with Kelvin, and the regex expressions are much more lenient.

To post a comment you must log in.
438. By Jacob Parker

Those braces weren't meant to be there

Revision history for this message
Djax (parnold-x) wrote :

Hi, thanks! Was my first time with regex as you noticed :)
I commented inline. Do you know a solution?

Revision history for this message
Djax (parnold-x) wrote :

For now I do the replacement after the split but if you know a regex solution ...

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/synapse-plugins/conversion-plugin.vala'
2--- lib/synapse-plugins/conversion-plugin.vala 2014-08-09 20:12:30 +0000
3+++ lib/synapse-plugins/conversion-plugin.vala 2014-08-14 22:27:31 +0000
4@@ -83,7 +83,7 @@
5 match_regex = new Regex (@"(?<=\\D)\\s(?:$CONVERSION_WORD)\\s",
6 RegexCompileFlags.OPTIMIZE);
7 // regex to match the temperature units
8- temp_regex = new Regex ("((?<=\\d)F\\s|°C|fahrenheit|celsius|(?<=\\d)R\\s|rankine|(?<=\\d)K\\s|reaumur)",
9+ temp_regex = new Regex ("(?i)(?<=\\d)[FCKR]\\s|fahrenheit|celsius|kelvin|reaumur",
10 RegexCompileFlags.OPTIMIZE);
11 digit_regex = new Regex ("\\d*",
12 RegexCompileFlags.OPTIMIZE);
13@@ -106,25 +106,32 @@
14 public bool handles_query (Query query) {
15 return (QueryFlags.ACTIONS in query.query_type);
16 }
17+
18+ static bool regex_function (MatchInfo info, StringBuilder resource) {
19+ string match = info.fetch (0).slice (0,1).up ();
20+ resource.append ("temp" + match + " ");
21+
22+ return false;
23+ }
24
25 public async ResultSet? search (Query query) throws SearchError {
26 string input = query.query_string;
27 bool matched = match_regex.match (input);
28 if (matched) {
29 try {
30- // replace some characters, more needed?
31+ // replace some characters, more needed? - Author
32+ // I not convinced any of these are tbh - Jacob
33 input=input.replace ("²","^2").replace ("³","^3");
34 // to support the conversion of temperatures see http://www.gnu.org/software/units/manual/units.html#Temperature-Conversions
35 bool temparaturematch = temp_regex.match (input);
36 if (temparaturematch)
37- input = temp_regex.replace (input,input.length,0,"""temp\g<0>""").replace("°C","C");
38+ input = temp_regex.replace_eval (input,input.length,0,RegexMatchFlags.NOTBOL,(RegexEvalCallback) regex_function);
39 string[] inputsplit = match_regex.split (input);
40 if (temparaturematch) {
41 MatchInfo info;
42 digit_regex.match (inputsplit[0],0,out info);
43 string digit = info.fetch (0);
44- inputsplit[0] = inputsplit[0].replace (digit,"");
45- inputsplit[0] = inputsplit[0]+@"($digit)";
46+ inputsplit[0] = inputsplit[0].replace (digit,"") + @"($digit)";
47 }
48
49 Pid pid;

Subscribers

People subscribed via source and target branches

to all changes: