crozone / FormatWith

String extensions for named parameterized string formatting.
MIT License
73 stars 13 forks source link

[Feature Request] Add a type that tokenizes the string once, and reuse it for processing parameters. #34

Open mohummedibrahim opened 8 months ago

mohummedibrahim commented 8 months ago

Hi @crozone, thanks for this very useful and easy-to-use library.

I'm wondering if it would be possible to add support for something that can parse the parametrized string once and then just reuse the parsed tokens for all replacement iterations?

I just gave it a try here https://github.com/mohummedibrahim/FormatWith, please let me know if that's something you're willing to accept and I can make an initial PR?

Thanks again!

crozone commented 8 months ago

Hi, thanks for the suggestion! Could you please elaborate on the usecase for pre-tokenizing a string? Currently we already have .FormattableWith() for creating FormattableStrings.

Is this new ParameterizedString for more performance on very large string documents?

mohummedibrahim commented 8 months ago

Thanks for the quick response!

Yes, FormattableWith() seems to be helpful when we need to inspect the parameters or need some custom formatting, but I don't see if we can input another replacement object to FormattableString and resolve the string with new values.

My use case is, I'm building a Mass Email Sender app, that has some simple templated strings as follows: Email Subject: [CompanyName] Label [FromState] to [ToState], Dated [Date] Email Message: ........... a large message with a lot of parameters ......... Attachment Names (Comma Separated): [CompanyName] Label Page1, [CompanyName] Label Page2 .....

The data source is DataTable that mostly contain a few hundred rows, the FormatWith is super helpful for me especially it accepts a custom handler Func parameter which I can create (and cache) and reuse for all the string templates.

The only issue here is that FormatWith parses the string every time even the string template is the same and was already parsed/tokenized. So yes ParameterizedString is mostly for performance and some convenience, as it accepts FormatOptions, so I can create one instance of it and customize the behaviours of all my string templates i.e. 1) Instead of curly brackes "{}", I have "[]". 2) The MissingKeyBehaviour I need to use is ReplaceWithFallback (with empty string value)

Hope it clarifies the use case I'm having. Please feel free to let me know if you have any other better alternatives or suggestions.