fix: Use TryWrite for Serial formatting (#1205)

This commit is contained in:
Kamron Batman 2022-10-23 18:17:08 -07:00 committed by GitHub
parent 27e8f37294
commit bae017c45b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -118,36 +118,15 @@ public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatab
public static Serial operator --(Serial l) => (Serial)(l.Value - 1); public static Serial operator --(Serial l) => (Serial)(l.Value - 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => $"0x{Value:X8}"; public override string ToString() => $"{this}";
public string ToString(string format, IFormatProvider formatProvider) => ToString(); public string ToString(string format, IFormatProvider formatProvider) => ToString();
public bool TryFormat( public bool TryFormat(
Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider
) ) => format != null
{ ? Value.TryFormat(destination, out charsWritten, format, provider)
if (format != null) : destination.TryWrite(provider, $"0x{Value:X8}", out charsWritten);
{
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;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator uint(Serial a) => a.Value; public static explicit operator uint(Serial a) => a.Value;