fix: Use TryWrite for Serial formatting (#1205)
This commit is contained in:
parent
27e8f37294
commit
bae017c45b
1 changed files with 4 additions and 25 deletions
|
|
@ -118,36 +118,15 @@ public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatab
|
|||
public static Serial operator --(Serial l) => (Serial)(l.Value - 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override string ToString() => $"0x{Value:X8}";
|
||||
public override string ToString() => $"{this}";
|
||||
|
||||
public string ToString(string format, IFormatProvider formatProvider) => ToString();
|
||||
|
||||
public bool TryFormat(
|
||||
Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider
|
||||
)
|
||||
{
|
||||
if (format != null)
|
||||
{
|
||||
return Value.TryFormat(destination, out charsWritten, format, provider);
|
||||
}
|
||||
|
||||
if (destination.Length < 10)
|
||||
{
|
||||
charsWritten = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
destination[0] = '0';
|
||||
destination[1] = 'x';
|
||||
|
||||
var result = Value.TryFormat(destination[2..], out charsWritten, "X8", provider);
|
||||
if (result)
|
||||
{
|
||||
charsWritten += 2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
) => format != null
|
||||
? Value.TryFormat(destination, out charsWritten, format, provider)
|
||||
: destination.TryWrite(provider, $"0x{Value:X8}", out charsWritten);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator uint(Serial a) => a.Value;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue