Skip to content

Commit

Permalink
Align WriteSha1AsHex
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCraver committed Jan 11, 2024
1 parent 408a009 commit af70390
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/StackExchange.Redis/PhysicalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,40 +1126,42 @@ private static int AppendToSpan(Span<byte> span, ReadOnlySpan<byte> value, int o

internal void WriteSha1AsHex(byte[] value)
{
if (_ioPipe?.Output is PipeWriter writer)
if (_ioPipe?.Output is not PipeWriter writer)
{
if (value == null)
{
writer.Write(NullBulkString.Span);
}
else if (value.Length == ResultProcessor.ScriptLoadProcessor.Sha1HashLength)
{
// $40\r\n = 5
// {40 bytes}\r\n = 42
return; // Prevent null refs during disposal
}

var span = writer.GetSpan(47);
span[0] = (byte)'$';
span[1] = (byte)'4';
span[2] = (byte)'0';
span[3] = (byte)'\r';
span[4] = (byte)'\n';
if (value == null)
{
writer.Write(NullBulkString.Span);
}
else if (value.Length == ResultProcessor.ScriptLoadProcessor.Sha1HashLength)
{
// $40\r\n = 5
// {40 bytes}\r\n = 42

int offset = 5;
for (int i = 0; i < value.Length; i++)
{
var b = value[i];
span[offset++] = ToHexNibble(b >> 4);
span[offset++] = ToHexNibble(b & 15);
}
span[offset++] = (byte)'\r';
span[offset++] = (byte)'\n';
var span = writer.GetSpan(47);
span[0] = (byte)'$';
span[1] = (byte)'4';
span[2] = (byte)'0';
span[3] = (byte)'\r';
span[4] = (byte)'\n';

writer.Advance(offset);
}
else
int offset = 5;
for (int i = 0; i < value.Length; i++)
{
throw new InvalidOperationException("Invalid SHA1 length: " + value.Length);
var b = value[i];
span[offset++] = ToHexNibble(b >> 4);
span[offset++] = ToHexNibble(b & 15);
}
span[offset++] = (byte)'\r';
span[offset++] = (byte)'\n';

writer.Advance(offset);
}
else
{
throw new InvalidOperationException("Invalid SHA1 length: " + value.Length);
}
}

Expand Down

0 comments on commit af70390

Please sign in to comment.