@@ -16,6 +16,8 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider
16
16
private static readonly Regex ThousandGroupRegex = MainRegexHelper . GetThousandGroupRegex ( ) ;
17
17
private static readonly Regex NumberRegex = MainRegexHelper . GetNumberRegex ( ) ;
18
18
private static readonly Regex PowRegex = MainRegexHelper . GetPowRegex ( ) ;
19
+ private static readonly Regex LogRegex = MainRegexHelper . GetLogRegex ( ) ;
20
+ private static readonly Regex LnRegex = MainRegexHelper . GetLnRegex ( ) ;
19
21
private static readonly Regex FunctionRegex = MainRegexHelper . GetFunctionRegex ( ) ;
20
22
21
23
private static Engine MagesEngine ;
@@ -67,12 +69,36 @@ public List<Result> Query(Query query)
67
69
// https://github.com/FlorianRappl/Mages/issues/132
68
70
// We bypass it by rewriting any pow(x,y) expression to the equivalent (x^y) expression
69
71
// before the engine sees it. This loop handles nested calls.
70
- string previous ;
71
- do
72
72
{
73
- previous = expression ;
74
- expression = PowRegex . Replace ( previous , PowMatchEvaluator ) ;
75
- } while ( previous != expression ) ;
73
+ string previous ;
74
+ do
75
+ {
76
+ previous = expression ;
77
+ expression = PowRegex . Replace ( previous , PowMatchEvaluator ) ;
78
+ } while ( previous != expression ) ;
79
+ }
80
+ // WORKAROUND END
81
+
82
+ // WORKAROUND START: The 'log' & 'ln' function in Mages v3.0.0 are broken.
83
+ // https://github.com/FlorianRappl/Mages/issues/137
84
+ // We bypass it by rewriting any log & ln expression to the equivalent (log10 & log) expression
85
+ // before the engine sees it. This loop handles nested calls.
86
+ {
87
+ string previous ;
88
+ do
89
+ {
90
+ previous = expression ;
91
+ expression = LogRegex . Replace ( previous , LogMatchEvaluator ) ;
92
+ } while ( previous != expression ) ;
93
+ }
94
+ {
95
+ string previous ;
96
+ do
97
+ {
98
+ previous = expression ;
99
+ expression = LnRegex . Replace ( previous , LnMatchEvaluator ) ;
100
+ } while ( previous != expression ) ;
101
+ }
76
102
// WORKAROUND END
77
103
78
104
var result = MagesEngine . Interpret ( expression ) ;
@@ -200,6 +226,33 @@ private static string PowMatchEvaluator(Match m)
200
226
return $ "({ arg1 } ^{ arg2 } )";
201
227
}
202
228
229
+ private static string LogMatchEvaluator ( Match m )
230
+ {
231
+ // m.Groups[1].Value will be `(...)` with parens
232
+ var contentWithParen = m . Groups [ 1 ] . Value ;
233
+ var argsContent = contentWithParen [ 1 ..^ 1 ] ;
234
+
235
+ // log is unary — if malformed, return original to let Mages handle it
236
+ var arg = argsContent . Trim ( ) ;
237
+ if ( string . IsNullOrEmpty ( arg ) ) return m . Value ;
238
+
239
+ // log(x) -> log10(x) (natural log)
240
+ return $ "(log10({ arg } ))";
241
+ }
242
+
243
+ private static string LnMatchEvaluator ( Match m )
244
+ {
245
+ // m.Groups[1].Value will be `(...)` with parens
246
+ var contentWithParen = m . Groups [ 1 ] . Value ;
247
+ var argsContent = contentWithParen [ 1 ..^ 1 ] ;
248
+
249
+ // ln is unary — if malformed, return original to let Mages handle it
250
+ var arg = argsContent . Trim ( ) ;
251
+ if ( string . IsNullOrEmpty ( arg ) ) return m . Value ;
252
+
253
+ // ln(x) -> log(x) (natural log)
254
+ return $ "(log({ arg } ))";
255
+ }
203
256
private static string NormalizeNumber ( string numberStr , bool isFunctionPresent , string decimalSep , string groupSep )
204
257
{
205
258
if ( isFunctionPresent )
0 commit comments