Merge pull request #66817 from raulsntos/dotnet/color-span

C#: Use Span in Color to avoid string allocations
This commit is contained in:
Rémi Verschelde 2022-10-03 14:52:29 +02:00
commit 208b440398

View File

@ -597,7 +597,7 @@ namespace Godot
/// <exception name="ArgumentOutOfRangeException"> /// <exception name="ArgumentOutOfRangeException">
/// <paramref name="rgba"/> color code is invalid. /// <paramref name="rgba"/> color code is invalid.
/// </exception> /// </exception>
private static Color FromHTML(string rgba) private static Color FromHTML(ReadOnlySpan<char> rgba)
{ {
Color c; Color c;
if (rgba.Length == 0) if (rgba.Length == 0)
@ -611,7 +611,7 @@ namespace Godot
if (rgba[0] == '#') if (rgba[0] == '#')
{ {
rgba = rgba.Substring(1); rgba = rgba.Slice(1);
} }
// If enabled, use 1 hex digit per channel instead of 2. // If enabled, use 1 hex digit per channel instead of 2.
@ -665,22 +665,22 @@ namespace Godot
if (c.r < 0) if (c.r < 0)
{ {
throw new ArgumentOutOfRangeException("Invalid color code. Red part is not valid hexadecimal: " + rgba); throw new ArgumentOutOfRangeException($"Invalid color code. Red part is not valid hexadecimal: {rgba}");
} }
if (c.g < 0) if (c.g < 0)
{ {
throw new ArgumentOutOfRangeException("Invalid color code. Green part is not valid hexadecimal: " + rgba); throw new ArgumentOutOfRangeException($"Invalid color code. Green part is not valid hexadecimal: {rgba}");
} }
if (c.b < 0) if (c.b < 0)
{ {
throw new ArgumentOutOfRangeException("Invalid color code. Blue part is not valid hexadecimal: " + rgba); throw new ArgumentOutOfRangeException($"Invalid color code. Blue part is not valid hexadecimal: {rgba}");
} }
if (c.a < 0) if (c.a < 0)
{ {
throw new ArgumentOutOfRangeException("Invalid color code. Alpha part is not valid hexadecimal: " + rgba); throw new ArgumentOutOfRangeException($"Invalid color code. Alpha part is not valid hexadecimal: {rgba}");
} }
return c; return c;
} }
@ -817,9 +817,9 @@ namespace Godot
value = max; value = max;
} }
private static int ParseCol4(string str, int ofs) private static int ParseCol4(ReadOnlySpan<char> str, int index)
{ {
char character = str[ofs]; char character = str[index];
if (character >= '0' && character <= '9') if (character >= '0' && character <= '9')
{ {
@ -836,9 +836,9 @@ namespace Godot
return -1; return -1;
} }
private static int ParseCol8(string str, int ofs) private static int ParseCol8(ReadOnlySpan<char> str, int index)
{ {
return ParseCol4(str, ofs) * 16 + ParseCol4(str, ofs + 1); return ParseCol4(str, index) * 16 + ParseCol4(str, index + 1);
} }
private static string ToHex32(float val) private static string ToHex32(float val)
@ -847,16 +847,16 @@ namespace Godot
return b.HexEncode(); return b.HexEncode();
} }
internal static bool HtmlIsValid(string color) internal static bool HtmlIsValid(ReadOnlySpan<char> color)
{ {
if (string.IsNullOrEmpty(color)) if (color.IsEmpty)
{ {
return false; return false;
} }
if (color[0] == '#') if (color[0] == '#')
{ {
color = color.Substring(1); color = color.Slice(1);
} }
// Check if the amount of hex digits is valid. // Check if the amount of hex digits is valid.