Skip to content

Instantly share code, notes, and snippets.

@Aaronontheweb
Last active June 2, 2021 16:46
Show Gist options
  • Save Aaronontheweb/ca4ac2a86a880aa0618ecac0cbbb1784 to your computer and use it in GitHub Desktop.
Save Aaronontheweb/ca4ac2a86a880aa0618ecac0cbbb1784 to your computer and use it in GitHub Desktop.
Span<T> Hacks
/// <summary>
/// INTERNAL API.
///
/// <see cref="Span{T}"/> polyfills that should be deleted once we drop .NET Standard 2.0 support.
/// </summary>
internal static class SpanHacks
{
public static bool IsNumeric(char x)
{
return (x >= '0' && x <= '9');
}
/// <summary>
/// Parses an integer from a string.
/// </summary>
/// <remarks>
/// PERFORMS NO INPUT VALIDATION.
/// </remarks>
/// <param name="str">The span of input characters.</param>
/// <returns>An <see cref="int"/>.</returns>
public static int Parse(ReadOnlySpan<char> str)
{
if (TryParse(str, out var i))
return i;
throw new FormatException($"[{str.ToString()}] is now a valid numeric format");
}
/// <summary>
/// Parses an integer from a string.
/// </summary>
/// <remarks>
/// PERFORMS NO INPUT VALIDATION.
/// </remarks>
/// <param name="str">The span of input characters.</param>
/// <param name="returnValue">The parsed integer, if any.</param>
/// <returns>An <see cref="int"/>.</returns>
public static bool TryParse(ReadOnlySpan<char> str, out int returnValue)
{
var pos = 0;
returnValue = 0;
var sign = 1;
if (str[0] == '-')
{
sign = -1;
pos++;
}
for (; pos < str.Length; pos++)
{
if (!IsNumeric(str[pos]))
return false;
returnValue = returnValue * 10 + str[pos] - '0';
}
returnValue = sign * returnValue;
return true;
}
/// <summary>
/// Performs <see cref="string.ToLowerInvariant"/> without having to
/// allocate a new <see cref="string"/> first.
/// </summary>
/// <param name="input">The set of characters to be lower-cased</param>
/// <returns>A new string.</returns>
public static string ToLowerInvariant(ReadOnlySpan<char> input)
{
Span<char> output = stackalloc char[input.Length];
for (var i = 0; i < input.Length; i++)
{
output[i] = char.ToLowerInvariant(input[i]);
}
return output.ToString();
}
}
@Aaronontheweb
Copy link
Author

Added TryParse method in addition to the original Parse routine for int extraction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment