/// <summary> /// Evaluate basic math portion(s) of the given string. /// </summary> /// <typeparam name="T">Return type; supported: /// <see langword="int"/>, /// <see langword="long"/>, /// <see langword="float"/>, /// <see langword="double"/>, /// <see langword="decimal"/>. /// </typeparam> /// <param name="s">Some string w/ or w/o basic mathy things.</param> /// <param name="evaluateMethod">Some <see cref="Dice.RollEvaluateMethod">roll evaluate method</see>.</param> /// <returns>Something sensible?</returns> static public T Evaluate<T>(this string s, Dice.RollEvaluateMethod evaluateMethod = Dice.RollEvaluateMethod.Default) { if (typeof(T) == typeof(int)) return (T)(object)int.Parse(s.Evaluate(evaluateMethod)); if (typeof(T) == typeof(double)) return (T)(object)double.Parse(s.Evaluate(evaluateMethod)); if (typeof(T) == typeof(long)) return (T)(object)long.Parse(s.Evaluate(evaluateMethod)); if (typeof(T) == typeof(float)) return (T)(object)float.Parse(s.Evaluate(evaluateMethod)); if (typeof(T) == typeof(decimal)) return (T)(object)decimal.Parse(s.Evaluate(evaluateMethod)); throw new TypeAccessException("At the moment only these types are supported: int, long, float, double, decimal"); }
/// <summary> /// Evaluate basic math portion(s) of the given string. /// </summary> /// <param name="s">String.</param> /// <param name="evaluateMethod">Some <see cref="Dice.RollEvaluateMethod">roll evaluate method</see>.</param> /// <returns>Math-solved string.</returns> static public string Evaluate(this string s, Dice.RollEvaluateMethod evaluateMethod = Dice.RollEvaluateMethod.Default) { string res; // replace dice roll occurances res = Dice.Evaluate(s, evaluateMethod); // subeval within braces res = Regex.Replace(res, @"\(([^)(]*)\)", delegate (Match m) { return m.Groups[1].ToString().Evaluate(); }); // add, sub, mul, div, etc. in some sort of priority order. res = res.CalcDo(CalcMode.Mul); res = res.CalcDo(CalcMode.Div); res = res.CalcDo(CalcMode.Add); res = res.CalcDo(CalcMode.Sub); return res; }