Esempio n. 1
0
        public SubExpression[] GetSubExpressions()
        {
            if (string.IsNullOrWhiteSpace(ExpressionString))
            {
                return(null);
            }

            if (!ExpressionString.Contains('(') && !ExpressionString.Contains(')'))
            {
                return(GetSubs());
            }

            var subs = 0;
            var currentExpression = string.Empty;
            var result            = new List <SubExpression>();

            for (var i = 0; i < ExpressionString.Length; i++)
            {
                var c = ExpressionString[i];

                if (c == '(')
                {
                    subs++;

                    if (subs == 1)
                    {
                        if (!string.IsNullOrWhiteSpace(currentExpression))
                        {
                            result.Add(new SubExpression(currentExpression, this));
                        }
                        currentExpression = string.Empty;
                    }
                    else
                    {
                        currentExpression += c;
                    }
                }
                else if (c == ')')
                {
                    subs--;


                    if (subs == 0)
                    {
                        if (!string.IsNullOrWhiteSpace(currentExpression))
                        {
                            result.Add(new SubExpression(currentExpression, this));
                        }
                        currentExpression = string.Empty;
                    }
                    else
                    {
                        currentExpression += c;
                    }
                }
                else if (ExpressionValues.IsMathSymbol(c) && subs == 0)
                {
                    if (!string.IsNullOrWhiteSpace(currentExpression))
                    {
                        result.Add(new SubExpression(currentExpression, this));
                    }
                    currentExpression = string.Empty;
                    result.Add(new SubExpression(c.ToString(), this));
                }
                else
                {
                    currentExpression += c;
                }
            }

            if (!string.IsNullOrWhiteSpace(currentExpression))
            {
                result.Add(new SubExpression(currentExpression, this));
            }

            if (result.Count == 1 && result[0].ExpressionString == ExpressionString)
            {
                return(null);
            }

            //var newExpressions = new List<(int, SubExpression)>();
            //for (var i = 0; i < result.Count; i++)
            //{
            //    var se = result[i];
            //    if (se.ExpressionString.Length > 1)
            //    {
            //        if (se.ExpressionString.HasMathSymbol(0))
            //        {
            //            newExpressions.Add((i, new SubExpression(se.ExpressionString[0].ToString(), this)));
            //            se.ExpressionString = se.ExpressionString.Substring(1);
            //        }

            //        if (se.ExpressionString.HasMathSymbol(se.ExpressionString.Length - 1))
            //        {
            //            newExpressions.Add((i+1, new SubExpression(ExpressionString[se.ExpressionString.Length - 1].ToString(), this)));
            //            se.ExpressionString = se.ExpressionString.Substring(0, se.ExpressionString.Length - 1);
            //        }
            //    }
            //}

            //var offset = 0;
            //foreach (var t in newExpressions)
            //{
            //   result.Insert(t.Item1+offset++, t.Item2);
            //}

            return(result.ToArray());
        }