You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've approved and added a bunch of TryFormat methods for doing the equivalent of ToString, but rendering into a supplied Span<char> instead of allocating a string. Many of these TryFormat methods accept a string format argument, just as does ToString. @justinvp raised the question of whether these should instead be ReadOnlySpan<char> format.
Example scenario:
His scenario was in looking at changing the implementation of string.Format, StringBuilder.AppendFormat, etc.: today they allocate substrings when parsing out formats from holes, e.g. "blah {0:D4} blah {1:[##-##-##]}", in order to supply that format string to the formatting API. We can achieve this internally for these methods; the question is whether we should expose that publicly so that other scenarios can benefit from it as well, if there are other scenarios.
The main downside to this change is it makes the methods a bit harder to use, in particular because there's no implicit cast from string to ReadOnlySpan<char>, so instead of passing in a format like "D4", you need to do "D4".AsReadOnlySpan(). TryFormat methods on the following types would be affected:
BigInteger// TryFormat not yet implementedByte
DateTime
DateTimeOffset
Decimal
Double // TryFormat not yet implemented
Guid
Int16
Int32
Int64
SByte
Single // TryFormat not yet implemented
TimeSpan
UInt16
UInt32
UInt64
Options
Leave the design as-is. We could using string format, we can address string.Format/StringBuilder.AppendFormat internally, and rare public uses can allocate strings when needed.
Change the methods to use ReadOnlySpan<char> format instead of string format. Callers use AsReadOnlySpan() when they have a string.
Change the methods to use ReadOnlySpan<char> format instead of string format, and add an implicit cast on string from string to ReadOnlySpan<char>.
Keep the existing string format methods, and add overloads that take ReadOnlySpan<char>.
The text was updated successfully, but these errors were encountered:
We seem to gravitate to (2) because it doesn't require an implicit conversion, but also doesn't prevent us from adding it in the future.
@stephentoub has filed a separate issue to discuss (3) dotnet/corefx#25413.
karelz
changed the title
TryFormat with format strings as ReadOnlySpan<char> or string
TryFormat with format strings as ReadOnlySpan<char> or string
Nov 21, 2017
We've approved and added a bunch of
TryFormat
methods for doing the equivalent ofToString
, but rendering into a suppliedSpan<char>
instead of allocating astring
. Many of theseTryFormat
methods accept astring format
argument, just as doesToString
. @justinvp raised the question of whether these should instead beReadOnlySpan<char> format
.Example scenario:
His scenario was in looking at changing the implementation of
string.Format
,StringBuilder.AppendFormat
, etc.: today they allocate substrings when parsing out formats from holes, e.g."blah {0:D4} blah {1:[##-##-##]}"
, in order to supply that format string to the formatting API. We can achieve this internally for these methods; the question is whether we should expose that publicly so that other scenarios can benefit from it as well, if there are other scenarios.Another scenario:
https://github.com/dotnet/corefx/blob/master/src/System.Runtime.Numerics/src/System/Numerics/BigNumber.cs#L578
BigInteger creates a format string to use when formatting its _sign value, in order to specify the number of digits. If format supported
ReadOnlySpan<char>
, we could use stack space instead of allocating a string.The main downside to this change is it makes the methods a bit harder to use, in particular because there's no implicit cast from
string
toReadOnlySpan<char>
, so instead of passing in a format like"D4"
, you need to do"D4".AsReadOnlySpan()
.TryFormat
methods on the following types would be affected:Options
string format
, we can addressstring.Format
/StringBuilder.AppendFormat
internally, and rare public uses can allocate strings when needed.ReadOnlySpan<char> format
instead ofstring format
. Callers useAsReadOnlySpan()
when they have a string.ReadOnlySpan<char> format
instead ofstring format
, and add an implicit cast onstring
fromstring
toReadOnlySpan<char>
.string format
methods, and add overloads that takeReadOnlySpan<char>
.The text was updated successfully, but these errors were encountered: