Skip to content

Commit edc76fa

Browse files
committed
Review feedback, case insensitive, consistent separators
1 parent e990e0f commit edc76fa

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ public List<Result> Query(Query query)
5252

5353
try
5454
{
55-
bool isFunctionPresent = FunctionRegex.IsMatch(query.Search);
56-
var expression = NumberRegex.Replace(query.Search, m => NormalizeNumber(m.Value, isFunctionPresent));
55+
var search = query.Search;
56+
bool isFunctionPresent = FunctionRegex.IsMatch(search);
57+
58+
// Mages is case sensitive, so we need to convert all function names to lower case.
59+
search = FunctionRegex.Replace(search, m => m.Value.ToLowerInvariant());
60+
61+
var decimalSep = GetDecimalSeparator();
62+
var groupSep = GetGroupSeparator(decimalSep);
63+
var expression = NumberRegex.Replace(search, m => NormalizeNumber(m.Value, isFunctionPresent, decimalSep, groupSep));
5764

5865
// WORKAROUND START: The 'pow' function in Mages v3.0.0 is broken.
5966
// https://github.com/FlorianRappl/Mages/issues/132
@@ -185,48 +192,56 @@ private static string PowMatchEvaluator(Match m)
185192
return $"({arg1}^{arg2})";
186193
}
187194

188-
private static string NormalizeNumber(string numberStr, bool isFunctionPresent)
195+
private static string NormalizeNumber(string numberStr, bool isFunctionPresent, string decimalSep, string groupSep)
189196
{
190-
var culture = CultureInfo.CurrentCulture;
191-
var groupSep = culture.NumberFormat.NumberGroupSeparator;
192-
var decimalSep = culture.NumberFormat.NumberDecimalSeparator;
193-
194197
if (isFunctionPresent)
195198
{
196199
// STRICT MODE: When functions are present, ',' is ALWAYS an argument separator.
197-
// It must not be normalized.
198200
if (numberStr.Contains(','))
199201
{
200202
return numberStr;
201203
}
202204

203-
// The string has no commas. It could have a '.' group separator (e.g. in de-DE)
204-
// or a '.' decimal separator (e.g. in en-US).
205-
// Since Mages' decimal separator is '.', we only need to strip the group separator.
206-
if (groupSep == ".")
205+
string processedStr = numberStr;
206+
207+
// Handle group separator, with special care for ambiguous dot.
208+
if (!string.IsNullOrEmpty(groupSep))
207209
{
208-
var parts = numberStr.Split('.');
209-
// A number with a dot group separator, e.g., "1.234"
210-
if (parts.Length > 1)
210+
if (groupSep == ".")
211211
{
212-
// Check if the parts after the first dot have the correct group length (usually 3).
213-
for (int i = 1; i < parts.Length; i++)
212+
var parts = processedStr.Split('.');
213+
if (parts.Length > 1)
214214
{
215-
if (parts[i].Length != 3)
215+
bool isGrouped = true;
216+
for (var i = 1; i < parts.Length; i++)
217+
{
218+
if (parts[i].Length != 3)
219+
{
220+
isGrouped = false;
221+
break;
222+
}
223+
}
224+
225+
if (isGrouped)
216226
{
217-
// Malformed grouping, e.g., "1.23". This is likely a decimal number.
218-
// Return as is and let Mages handle it.
219-
return numberStr;
227+
processedStr = processedStr.Replace(groupSep, "");
220228
}
229+
// If not grouped, it's likely a decimal number, so we don't strip dots.
221230
}
222-
// Correct grouping, e.g., "1.234" or "1.234.567". Strip separators.
223-
return numberStr.Replace(".", "");
224231
}
232+
else
233+
{
234+
processedStr = processedStr.Replace(groupSep, "");
235+
}
236+
}
237+
238+
// Handle decimal separator.
239+
if (decimalSep != ".")
240+
{
241+
processedStr = processedStr.Replace(decimalSep, ".");
225242
}
226243

227-
// For any other case (e.g. en-US culture where group sep is ',' which was already handled),
228-
// return the string as is.
229-
return numberStr;
244+
return processedStr;
230245
}
231246
else
232247
{
@@ -236,7 +251,10 @@ private static string NormalizeNumber(string numberStr, bool isFunctionPresent)
236251
{
237252
processedStr = processedStr.Replace(groupSep, "");
238253
}
239-
processedStr = processedStr.Replace(decimalSep, ".");
254+
if (decimalSep != ".")
255+
{
256+
processedStr = processedStr.Replace(decimalSep, ".");
257+
}
240258
return processedStr;
241259
}
242260
}

Plugins/Flow.Launcher.Plugin.Calculator/MainRegexHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static partial class MainRegexHelper
1010
[GeneratedRegex(@"\B(?=(\d{3})+(?!\d))", RegexOptions.Compiled)]
1111
public static partial Regex GetThousandGroupRegex();
1212

13-
[GeneratedRegex(@"\bpow(\((?:[^()\[\]]|\((?<Depth>)|\)(?<-Depth>)|\[(?<Depth>)|\](?<-Depth>))*(?(Depth)(?!))\))", RegexOptions.Compiled | RegexOptions.RightToLeft)]
13+
[GeneratedRegex(@"\bpow(\((?:[^()\[\]]|\((?<Depth>)|\)(?<-Depth>)|\[(?<Depth>)|\](?<-Depth>))*(?(Depth)(?!))\))", RegexOptions.Compiled | RegexOptions.RightToLeft | RegexOptions.IgnoreCase)]
1414
public static partial Regex GetPowRegex();
1515

1616
[GeneratedRegex(@"\b(sqrt|pow|factorial|abs|sign|ceil|floor|round|exp|log|log2|log10|min|max|lt|eq|gt|sin|cos|tan|arcsin|arccos|arctan|isnan|isint|isprime|isinfty|rand|randi|type|is|as|length|throw|catch|eval|map|clamp|lerp|regex|shuffle)\s*\(", RegexOptions.Compiled | RegexOptions.IgnoreCase)]

0 commit comments

Comments
 (0)