/// <summary>
        /// Formulate string.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="argument"></param>
        /// <param name="format">string that contains the formatting, e.g. "X8"</param>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool TryEvaluate(ref FunctionEvaluationContext ctx, object argument, object format, out object result)
        {
            if (argument == null)
            {
                result = null; return(false);
            }
            string formatStr = format?.ToString();

            // Try custom format provider
            if (ctx.FormatProvider != null)
            {
                ICustomFormatter customFormatter__ = ctx.FormatProvider.GetFormat(typeof(ICustomFormatter)) as ICustomFormatter;
                if (customFormatter__ != null)
                {
                    string custom_formatter_result = customFormatter__.Format(formatStr, argument, ctx.Culture);
                    if (custom_formatter_result != null)
                    {
                        result = custom_formatter_result; return(true);
                    }
                }
            }

            if (argument is Enum @enum)
            {
                string separator = null;
                if (formatStr == null || formatStr == "" || formatStr == "g" || formatStr == "G" || formatStr == "f" || formatStr == "F")
                {
                    separator = ", ";
                }
                else if (formatStr == "|")
                {
                    separator = formatStr;
                }
                else if (formatStr == " |")
                {
                    separator = " | ";
                }

                if (separator != null)
                {
                    LineString enum_string = ctx.EvaluateEnum(@enum, separator);
                    if (enum_string.Value != null)
                    {
                        result = enum_string.Value; return(true);
                    }
                }
            }

            if (formatStr != null && argument is IFormattable formattable)
            {
                result = formattable.ToString(formatStr, ctx.Culture); return(true);
            }
            if (ctx.Culture.GetFormat(typeof(ICustomFormatter)) is ICustomFormatter customFormatter_)
            {
                result = customFormatter_.Format(formatStr, argument, ctx.Culture); return(true);
            }
            result = ctx.Culture == null?String.Format("{0:" + formatStr + "}", argument) : String.Format(ctx.Culture, "{0:" + formatStr + "}", argument);

            return(true);
        }
Example #2
0
 /// <summary>
 /// Call 3-argument function.
 /// </summary>
 /// <param name="function"></param>
 /// <param name="ctx"></param>
 /// <param name="arg0">Function argument</param>
 /// <param name="arg1"></param>
 /// <param name="arg2"></param>
 /// <param name="result">Object, String, Long, Double or null</param>
 /// <returns>true if evaluated, false if not</returns>
 /// <exception cref="Exception">On unexpected situation</exception>
 public static bool TryEvaluate(this IFunction function, ref FunctionEvaluationContext ctx, object arg0, object arg1, object arg2, out object result)
 {
     if (function is IFunction3 func3 && func3.TryEvaluate(ref ctx, arg0, arg1, arg2, out result))
     {
         return(true);
     }
     if (function is IFunctionN funcN && funcN.TryEvaluate(ref ctx, new object[] { arg0, arg1, arg2 }, out result))
     {
         return(true);
     }
     result = null; return(false);
 }
Example #3
0
 /// <summary>
 /// Call 0-arguments function.
 /// </summary>
 /// <param name="function"></param>
 /// <param name="ctx"></param>
 /// <param name="result">Object, String, Long, Double or null</param>
 /// <returns>true if evaluated, false if not</returns>
 /// <exception cref="Exception">On unexpected situation</exception>
 public static bool TryEvaluate(this IFunction function, ref FunctionEvaluationContext ctx, out object result)
 {
     if (function is IFunction0 func0 && func0.TryEvaluate(ref ctx, out result))
     {
         return(true);
     }
     if (function is IFunctionN funcN && funcN.TryEvaluate(ref ctx, no_args, out result))
     {
         return(true);
     }
     result = null; return(false);
 }
Example #4
0
 /// <summary>
 /// Call n-arguments function.
 /// </summary>
 /// <param name="function"></param>
 /// <param name="ctx"></param>
 /// <param name="args">Function arguments</param>
 /// <param name="result">Object, String, Long, Double or null</param>
 /// <returns>true if evaluated, false if not</returns>
 /// <exception cref="Exception">On unexpected error</exception>
 public static bool TryEvaluate(this IFunction function, ref FunctionEvaluationContext ctx, object[] args, out object result)
 {
     if (args == null || args.Length == 0)
     {
         if (function is IFunction0 func0 && func0.TryEvaluate(ref ctx, out result))
         {
             return(true);
         }
         if (function is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args ?? no_args, out result))
         {
             return(true);
         }
     }
     if (args.Length == 1)
     {
         if (function is IFunction1 func1 && func1.TryEvaluate(ref ctx, args[0], out result))
         {
             return(true);
         }
         if (function is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args, out result))
         {
             return(true);
         }
     }
     if (args.Length == 2)
     {
         if (function is IFunction2 func2 && func2.TryEvaluate(ref ctx, args[0], args[1], out result))
         {
             return(true);
         }
         if (function is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args, out result))
         {
             return(true);
         }
     }
     if (args.Length == 3)
     {
         if (function is IFunction3 func3 && func3.TryEvaluate(ref ctx, args[0], args[1], args[2], out result))
         {
             return(true);
         }
         if (function is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args, out result))
         {
             return(true);
         }
     }
     if (function is IFunctionN funcN_ && funcN_.TryEvaluate(ref ctx, args, out result))
     {
         return(true);
     }
     result = null; return(false);
 }
        /// <summary>
        /// Add padding.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="argument"></param>
        /// <param name="alignment">Int32 or Int64, negative value is padding to left of <paramref name="argument"/>, positive value is padding to right of <paramref name="argument"/></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool TryEvaluate(ref FunctionEvaluationContext ctx, object argument, object alignment, out object result)
        {
            String s = argument?.ToString();

            if (s == null)
            {
                result = null; return(false);
            }
            int a;

            if (alignment is Int32 i)
            {
                a = i;
            }
            else if (alignment is Int64 ii)
            {
                a = (Int32)ii;
            }
            else
            {
                result = s; return(true);
            }

            if (a == 0)
            {
                result = s; return(true);
            }
            if (a < 0)
            {
                a = -a;
                if (s.Length >= a)
                {
                    result = s; return(true);
                }
                result = s + new string(' ', a - s.Length);
                return(true);
            }
            {
                if (s.Length >= a)
                {
                    result = s; return(true);
                }
                result = new string(' ', a - s.Length) + s;
                return(true);
            }
        }
Example #6
0
        /// <summary>
        /// Call n-arguments function.
        /// </summary>
        /// <param name="functions"></param>
        /// <param name="functionName"></param>
        /// <param name="ctx"></param>
        /// <param name="args">Function arguments</param>
        /// <param name="result">Object, String, Long, Double or null</param>
        /// <returns>true if evaluated, false if not</returns>
        /// <exception cref="Exception">On unexpected error</exception>
        public static bool TryEvaluate(this IFunctions functions, string functionName, ref FunctionEvaluationContext ctx, object[] args, out object result)
        {
            if (functions == null || functionName == null)
            {
                result = null; return(false);
            }
            IFunction func;

            if (functions is IFunctionsQueryable queryable && queryable.TryGetValue(functionName, out func))
            {
                if (args == null || args.Length == 0)
                {
                    if (func is IFunction0 func0 && func0.TryEvaluate(ref ctx, out result))
                    {
                        return(true);
                    }
                    if (func is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args ?? no_args, out result))
                    {
                        return(true);
                    }
                }
                if (args.Length == 1)
                {
                    if (func is IFunction1 func1 && func1.TryEvaluate(ref ctx, args[0], out result))
                    {
                        return(true);
                    }
                    if (func is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args, out result))
                    {
                        return(true);
                    }
                }
                if (args.Length == 2)
                {
                    if (func is IFunction2 func2 && func2.TryEvaluate(ref ctx, args[0], args[1], out result))
                    {
                        return(true);
                    }
                    if (func is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args, out result))
                    {
                        return(true);
                    }
                }
                if (args.Length == 3)
                {
                    if (func is IFunction3 func3 && func3.TryEvaluate(ref ctx, args[0], args[1], args[2], out result))
                    {
                        return(true);
                    }
                    if (func is IFunctionN funcN && funcN.TryEvaluate(ref ctx, args, out result))
                    {
                        return(true);
                    }
                }
                if (func is IFunctionN funcN_ && funcN_.TryEvaluate(ref ctx, args, out result))
                {
                    return(true);
                }
            }
            result = null; return(false);
        }
Example #7
0
        /// <summary>
        /// Call 3-argument function.
        /// </summary>
        /// <param name="functions"></param>
        /// <param name="functionName"></param>
        /// <param name="ctx"></param>
        /// <param name="arg0">Function argument</param>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <param name="result">Object, String, Long, Double or null</param>
        /// <returns>true if evaluated, false if not</returns>
        /// <exception cref="Exception">On unexpected error</exception>
        public static bool TryEvaluate(this IFunctions functions, string functionName, ref FunctionEvaluationContext ctx, object arg0, object arg1, object arg2, out object result)
        {
            if (functions == null || functionName == null)
            {
                result = null; return(false);
            }
            IFunction func;

            if (functions is IFunctionsQueryable queryable && queryable.TryGetValue(functionName, out func))
            {
                if (func is IFunction3 func3 && func3.TryEvaluate(ref ctx, arg0, arg1, arg2, out result))
                {
                    return(true);
                }
                if (func is IFunctionN funcN && funcN.TryEvaluate(ref ctx, new object[] { arg0, arg1, arg2 }, out result))
                {
                    return(true);
                }
            }
            result = null; return(false);
        }
Example #8
0
        /// <summary>
        /// Call 0-arguments function.
        /// </summary>
        /// <param name="functions"></param>
        /// <param name="functionName"></param>
        /// <param name="ctx"></param>
        /// <param name="result">Object, String, Long, Double or null</param>
        /// <returns>true if evaluated, false if not</returns>
        /// <exception cref="Exception">On unexpected error</exception>
        public static bool TryEvaluate(this IFunctions functions, string functionName, ref FunctionEvaluationContext ctx, out object result)
        {
            if (functions == null || functionName == null)
            {
                result = null; return(false);
            }
            IFunction func;

            if (functions is IFunctionsQueryable queryable && queryable.TryGetValue(functionName, out func))
            {
                if (func is IFunction0 func0 && func0.TryEvaluate(ref ctx, out result))
                {
                    return(true);
                }
                if (func is IFunctionN funcN && funcN.TryEvaluate(ref ctx, no_args, out result))
                {
                    return(true);
                }
            }
            result = null; return(false);
        }