Comments (2)
I've put together a PR with an initial implementation for your review (#20)
from formatwith.
@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)
- Can you create a licence for FormatWith HOT 3
- Just a thank you. HOT 1
- Formatting with SubProperties HOT 1
- Problem with GetFormatParameters() HOT 1
- Updated release? HOT 4
- Assembly signing / strong naming HOT 4
- Breaking changes for 2.0 release HOT 4
- Suggest using IReadOnlyDictionary<> instead of IDictionary<> HOT 3
- Nested arguments HOT 4
- custom braces as string, not char - {{ }} << >> HOT 2
- Add Support for generic value dictionaries HOT 5
- Add support for indexers (support for Arrays/Lists/Dictionaries/etc) HOT 1
- Additional formatting options.
- Vectorize the tokenizer HOT 1
- Handle whitespace padding around parameters
- Allow passing in StringComparer or StringComparison for object based lookups
- Define the CultureInfo of the format HOT 2
- Q: Is this project still alive? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from formatwith.