@@ -16,6 +16,8 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider
1616 private static readonly Regex ThousandGroupRegex = MainRegexHelper . GetThousandGroupRegex ( ) ;
1717 private static readonly Regex NumberRegex = MainRegexHelper . GetNumberRegex ( ) ;
1818 private static readonly Regex PowRegex = MainRegexHelper . GetPowRegex ( ) ;
19+ private static readonly Regex LogRegex = MainRegexHelper . GetLogRegex ( ) ;
20+ private static readonly Regex LnRegex = MainRegexHelper . GetLnRegex ( ) ;
1921 private static readonly Regex FunctionRegex = MainRegexHelper . GetFunctionRegex ( ) ;
2022
2123 private static Engine MagesEngine ;
@@ -67,12 +69,36 @@ public List<Result> Query(Query query)
6769 // https://github.com/FlorianRappl/Mages/issues/132
6870 // We bypass it by rewriting any pow(x,y) expression to the equivalent (x^y) expression
6971 // before the engine sees it. This loop handles nested calls.
70- string previous ;
71- do
7272 {
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+ }
76102 // WORKAROUND END
77103
78104 var result = MagesEngine . Interpret ( expression ) ;
@@ -200,6 +226,33 @@ private static string PowMatchEvaluator(Match m)
200226 return $ "({ arg1 } ^{ arg2 } )";
201227 }
202228
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+ }
203256 private static string NormalizeNumber ( string numberStr , bool isFunctionPresent , string decimalSep , string groupSep )
204257 {
205258 if ( isFunctionPresent )
0 commit comments