Skip to content

Commit

Permalink
Use string.TryCopyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
xtqqczze committed Oct 6, 2024
1 parent 1a4c249 commit 208d0fb
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/libraries/System.Private.CoreLib/src/System/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,17 @@ public bool TryFormat(Span<char> destination, out int charsWritten)
{
if (m_value)
{
if (destination.Length >= 4)
if (TrueLiteral.TryCopyTo(destination))
{
ulong true_val = BitConverter.IsLittleEndian ? 0x65007500720054ul : 0x54007200750065ul; // "True"
Unsafe.WriteUnaligned(ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(destination)), true_val);
charsWritten = 4;
charsWritten = TrueLiteral.Length;
return true;
}
}
else
{
if (destination.Length >= 5)
if (FalseLiteral.TryCopyTo(destination))
{
ulong fals_val = BitConverter.IsLittleEndian ? 0x73006C00610046ul : 0x460061006C0073ul; // "Fals"
Unsafe.WriteUnaligned(ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(destination)), fals_val);
destination[4] = 'e';
charsWritten = 5;
charsWritten = FalseLiteral.Length;
return true;
}
}
Expand Down Expand Up @@ -196,7 +191,7 @@ internal static bool IsTrueStringIgnoreCase(ReadOnlySpan<char> value)
// "true" as a ulong, each char |'d with 0x0020 for case-insensitivity
ulong true_val = BitConverter.IsLittleEndian ? 0x65007500720074ul : 0x74007200750065ul;
return value.Length == 4 &&
(MemoryMarshal.Read<ulong>(MemoryMarshal.AsBytes(value)) | 0x0020002000200020) == true_val;
(Unsafe.ReadUnaligned<ulong>(ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(value))) | 0x0020002000200020) == true_val;
}

internal static bool IsFalseStringIgnoreCase(ReadOnlySpan<char> value)
Expand Down

0 comments on commit 208d0fb

Please sign in to comment.