Ejemplo n.º 1
0
        // Attempts to parse a number from the beginning of the given input string. Returns the character size of the matched string.
        static (double, int) GetNextNumber(string input, INumberParser numberParser)
        {
            // Attempt to parse anything before an operator or space as a number
            Regex regex = new Regex("^-?([^-+/*\\(\\)\\^\\s]+)");
            var   match = regex.Match(input);

            if (match.Success)
            {
                // Might be a number
                var matchLength = match.Groups[0].Length;
                var parsedNum   = ApiInformation.IsTypePresent(numberParser?.GetType().FullName)
                                        ? numberParser.ParseDouble(input.Substring(0, matchLength))
                                        : double.TryParse(input.Substring(0, matchLength), out var d)
                                                ? (double?)d
                                                : null;

                if (parsedNum != null)
                {
                    // Parsing was successful
                    return(parsedNum.Value, matchLength);
                }
            }

            return(double.NaN, 0);
        }
Ejemplo n.º 2
0
        // Attempts to parse a number from the beginning of the given input string. Returns the character size of the matched string.
        private static Tuple <double, int> GetNextNumber(string input, INumberParser numberParser)
        {
            // Attempt to parse anything before an operator or space as a number
            string regex = "^-?([^-+/*\\(\\)\\^\\s]+)";
            Match  match = Regex.Match(input, regex);

            if (match.Success)
            {
                // Might be a number
                var parsedNum = numberParser.ParseDouble(match.Value);
                if (parsedNum.HasValue)
                {
                    // Parsing was successful
                    return(new Tuple <double, int>(parsedNum.Value, match.Length));
                }
            }

            return(new Tuple <double, int>(double.NaN, 0));
        }