KovtunV / NoStringEvaluating

Fast low memory consuming mathematical evaluation without endless string parsing! Parses string formula once and uses its object sequence in each evaluation. Moreover, provides user defined functions and variables.
MIT License
28 stars 10 forks source link

How to implement String.Format() Functionality #12

Closed prakashboston closed 10 months ago

prakashboston commented 10 months ago

String.Format() manages formatting including the position, alignment, and format type

KovtunV commented 10 months ago

Hello @prakashboston, add a function as described in a documentation

It could be like this:

public class MyFunction : IFunction
{
    public string Name { get; } = "Format";

    public bool CanHandleNullArguments { get; }

    public InternalEvaluatorValue Execute(List<InternalEvaluatorValue> args, ValueFactory factory)
    {
        var format = args[0].Word;
        var formatArguments = args.Skip(1).Cast<object>().ToArray();

        var result = string.Format(format, formatArguments);

        return factory.Word.Create(result);
    }
}
class Program
{
    static void Main()
    {
        var sss = string.Format("{0} hello {0}, {1}", 8, "hey");

        var ns = CreateNoString();

        var res = ns.CalcWord("Format('{0} hello {0}, {1}', 8, 'hey')");
    }

    static NoStringEvaluator CreateNoString()
    {
        static void Configure(NoStringEvaluatorOptions opt)
        {
            opt.WithFunctionsFrom(typeof(Program))
        }

        return NoStringEvaluator.CreateFacade(Configure).Evaluator;
    }

}
prakashboston commented 10 months ago

Thank you so much for your response.Its working fine and good to use this library

KovtunV commented 10 months ago

But keep in mind this snippet is not optimized, if you need a performance improvement change the row var formatArguments = args.Skip(1).Cast<object>().ToArray();