Commit a0c0a7d7 authored by Nick Craver's avatar Nick Craver

CommandBytes: ToUpper instead of ToLower()

This is more consitent with our commands, enums, and redis docs themseleves.
parent 43c1df00
......@@ -13,8 +13,8 @@ public void Basic()
config.SetDefaultPorts();
Assert.Contains(new DnsEndPoint(".", 6379), config.EndPoints);
var map = config.CommandMap;
Assert.Equal("$PING=p", map.ToString());
Assert.Equal(".:6379,$PING=p", config.ToString());
Assert.Equal("$PING=P", map.ToString());
Assert.Equal(".:6379,$PING=P", config.ToString());
}
}
}
......@@ -111,7 +111,7 @@ public unsafe CommandBytes(string value)
{
len = Encoding.GetBytes(cPtr, value.Length, bPtr + 1, MaxLength);
}
*bPtr = (byte)LowerCasify(len, bPtr + 1);
*bPtr = (byte)UpperCasify(len, bPtr + 1);
}
}
......@@ -123,7 +123,7 @@ public unsafe CommandBytes(ReadOnlySpan<byte> value)
{
byte* bPtr = (byte*)uPtr;
value.CopyTo(new Span<byte>(bPtr + 1, value.Length));
*bPtr = (byte)LowerCasify(value.Length, bPtr + 1);
*bPtr = (byte)UpperCasify(value.Length, bPtr + 1);
}
}
public unsafe CommandBytes(ReadOnlySequence<byte> value)
......@@ -148,10 +148,10 @@ public unsafe CommandBytes(ReadOnlySequence<byte> value)
target = target.Slice(segment.Length);
}
}
*bPtr = (byte)LowerCasify(len, bPtr + 1);
*bPtr = (byte)UpperCasify(len, bPtr + 1);
}
}
private unsafe int LowerCasify(int len, byte* bPtr)
private unsafe int UpperCasify(int len, byte* bPtr)
{
const ulong HighBits = 0x8080808080808080;
if (((_0 | _1 | _2) & HighBits) == 0)
......@@ -159,30 +159,30 @@ private unsafe int LowerCasify(int len, byte* bPtr)
// no unicode; use ASCII bit bricks
for (int i = 0; i < len; i++)
{
*bPtr = ToLowerInvariantAscii(*bPtr++);
*bPtr = ToUpperInvariantAscii(*bPtr++);
}
return len;
}
else
{
return LowerCasifyUnicode(len, bPtr);
return UpperCasifyUnicode(len, bPtr);
}
}
private static unsafe int LowerCasifyUnicode(int oldLen, byte* bPtr)
private static unsafe int UpperCasifyUnicode(int oldLen, byte* bPtr)
{
const int MaxChars = ChunkLength * sizeof(ulong); // leave rounded up; helps stackalloc
char* workspace = stackalloc char[MaxChars];
int charCount = Encoding.GetChars(bPtr, oldLen, workspace, MaxChars);
char* c = workspace;
for (int i = 0; i < charCount; i++) *c = char.ToLowerInvariant((*c++));
for (int i = 0; i < charCount; i++) *c = char.ToUpperInvariant(*c++);
int newLen = Encoding.GetBytes(workspace, charCount, bPtr, MaxLength);
// don't forget to zero any shrink
for (int i = newLen; i < oldLen; i++) bPtr[i] = 0;
return newLen;
}
private static byte ToLowerInvariantAscii(byte b) => b >= 'A' && b <= 'Z' ? (byte)(b | 32) : b;
private static byte ToUpperInvariantAscii(byte b) => b >= 'a' && b <= 'z' ? (byte)(b - 32) : b;
internal unsafe byte[] ToArray()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment