Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExcelUtilities/NumericExpressionEvaluator.cs @ 17578

Last change on this file since 17578 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 4.6 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Mats Alm                       Added                   2013-03-01 (Prior file history on https://github.com/swmal/ExcelFormulaParser)
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using OfficeOpenXml.FormulaParsing.ExcelUtilities;
36using System.Text.RegularExpressions;
37using OfficeOpenXml.FormulaParsing.Excel.Operators;
38using OfficeOpenXml.FormulaParsing.ExpressionGraph;
39
40namespace OfficeOpenXml.FormulaParsing.ExcelUtilities
41{
42    public class NumericExpressionEvaluator
43    {
44        private ValueMatcher _valueMatcher;
45        private CompileResultFactory _compileResultFactory;
46
47        public NumericExpressionEvaluator()
48            : this(new ValueMatcher(), new CompileResultFactory())
49        {
50
51        }
52
53        public NumericExpressionEvaluator(ValueMatcher valueMatcher, CompileResultFactory compileResultFactory)
54        {
55            _valueMatcher = valueMatcher;
56            _compileResultFactory = compileResultFactory;
57        }
58
59        private string GetNonNumericStartChars(string expression)
60        {
61            if (!string.IsNullOrEmpty(expression))
62            {
63                if (Regex.IsMatch(expression, @"^([^\d]{2})")) return expression.Substring(0, 2);
64                if (Regex.IsMatch(expression, @"^([^\d]{1})")) return expression.Substring(0, 1);
65            }
66            return null;
67        }
68
69        public double? OperandAsDouble(object op)
70        {
71            if (op is double || op is int)
72            {
73                return Convert.ToDouble(op);
74            }
75            if (op != null)
76            {
77                double output;
78                if (double.TryParse(op.ToString(), out output))
79                {
80                    return output;
81                }
82            }
83            return null;
84        }
85
86        public bool Evaluate(object left, string expression)
87        {
88            var operatorCandidate = GetNonNumericStartChars(expression);
89            var leftNum = OperandAsDouble(left);
90            if (!string.IsNullOrEmpty(operatorCandidate) && leftNum != null)
91            {
92                IOperator op;
93                if (OperatorsDict.Instance.TryGetValue(operatorCandidate, out op))
94                {
95                    var numericCandidate = expression.Replace(operatorCandidate, string.Empty);
96                    double d;
97                    if (double.TryParse(numericCandidate, out d))
98                    {
99                        var leftResult = _compileResultFactory.Create(leftNum);
100                        var rightResult = _compileResultFactory.Create(d);
101                        var result = op.Apply(leftResult, rightResult);
102                        if (result.DataType != DataType.Boolean)
103                        {
104                            throw new ArgumentException("Illegal operator in expression");
105                        }
106                        return (bool)result.Result;
107                    }
108                }
109            }
110            return _valueMatcher.IsMatch(left, expression) == 0;
111        }
112    }
113}
Note: See TracBrowser for help on using the repository browser.