未验证 提交 6f6e2482 编写于 作者: M Miha Zupan 提交者: GitHub

Remove unsafe code and make scheme parsing faster in Uri (#90087)

* Remove unsafe code and make scheme parsing faster in Uri

* Reduce the number of range checks

* Switch loop to IndexOf
上级 d5b4ab2f
......@@ -77,11 +77,7 @@ internal static string ParseCanonicalName(string str, int start, int end, ref bo
if (index >= 0)
{
// We saw uppercase letters. Avoid allocating both the substring and the lower-cased variant.
return string.Create(end - start, (str, start), static (buffer, state) =>
{
int newLength = state.str.AsSpan(state.start, buffer.Length).ToLowerInvariant(buffer);
Debug.Assert(newLength == buffer.Length);
});
return UriHelper.SpanToLowerInvariantString(str.AsSpan(start, end - start));
}
if (str.AsSpan(start, end - start) is Localhost or Loopback)
......
......@@ -10,6 +10,17 @@ namespace System
{
internal static class UriHelper
{
public static unsafe string SpanToLowerInvariantString(ReadOnlySpan<char> span)
{
#pragma warning disable CS8500 // takes address of managed type
return string.Create(span.Length, (IntPtr)(&span), static (buffer, spanPtr) =>
{
int charsWritten = (*(ReadOnlySpan<char>*)spanPtr).ToLowerInvariant(buffer);
Debug.Assert(charsWritten == buffer.Length);
});
#pragma warning restore CS8500
}
// http://host/Path/Path/File?Query is the base of
// - http://host/Path/Path/File/ ... (those "File" words may be different in semantic but anyway)
// - http://host/Path/Path/#Fragment
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册