Git Product home page Git Product logo

Comments (2)

JeremySkinner avatar JeremySkinner commented on July 23, 2024

I've put together a PR with an initial implementation for your review (#20)

from formatwith.

crozone avatar crozone commented on July 23, 2024

@JeremySkinner I've finally got your PR merged, apologies it took so long!

It's still a work in progress, but I've overhauled the public FormatWith API to enable ReadOnlySpan<char> based scenarios. All of the existing FormatWith overloads are also now wrapping the lower level Span based overload.

The work is happening in the release-4.0 branch.

The main Span API usage looks like this:

HandlerAction handlerAction = static (key, format, result) =>
{
    if (key.Equals("KeyNumber1".AsSpan(), StringComparison.Ordinal))
    {
        result("A Nice Value".AsSpan());
        return true;
    }
    return false;
};

FallbackAction fallbackAction = static (result) =>
{
    result("A good fallback value".AsSpan());
};

string replacement = "abc {KeyNumber1} {DoesntExist}".AsSpan().FormatWith(handlerAction, MissingKeyBehaviour.ReplaceWithFallback, fallbackAction);
Assert.Equal("abc A Nice Value A good fallback value", replacement);

Currently there aren't any more "convenient" Span overloads (that take a dictionary or object lookup, for example), but I think they'll probably be needed. Also, the string returning overloads use a cache of ThreadLocal WeakReference'd StringBuilders which should provide a balance of performance and memory consumption.

There is also a lower level overload that accepts a callback delegate to write the result, and doesn't use StringBuilder at all:

public static void FormatWith(
           ReadOnlySpan<char> formatString,
           HandlerAction handlerAction,
           ResultAction destinationWriterAction,
           MissingKeyBehaviour missingKeyBehaviour,
           FallbackAction fallbackReplacementAction = null,
           char openBraceChar = '{',
           char closeBraceChar = '}')

Please let me know if you have any thoughts on the shape of these APIs, I'd love to get some feedback on their practicality and usability.

from formatwith.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.