Fix UriUtilityCore's handling of anchors and querystrings, also optimized with Span (#15678)
This commit is contained in:
@@ -4,8 +4,6 @@ namespace Umbraco.Cms.Core;
|
||||
|
||||
public static class UriUtilityCore
|
||||
{
|
||||
#region Uri string utilities
|
||||
|
||||
public static bool HasScheme(string uri) => uri.IndexOf("://", StringComparison.InvariantCulture) > 0;
|
||||
|
||||
public static string StartWithScheme(string uri) => StartWithScheme(uri, null);
|
||||
@@ -15,16 +13,15 @@ public static class UriUtilityCore
|
||||
|
||||
public static string EndPathWithSlash(string uri)
|
||||
{
|
||||
var pos1 = Math.Max(0, uri.IndexOf('?'));
|
||||
var pos2 = Math.Max(0, uri.IndexOf('#'));
|
||||
var pos = Math.Min(pos1, pos2);
|
||||
ReadOnlySpan<char> uriSpan = uri.AsSpan();
|
||||
var pos = IndexOfPathEnd(uriSpan);
|
||||
|
||||
var path = pos > 0 ? uri.Substring(0, pos) : uri;
|
||||
var path = (pos > 0 ? uriSpan[..pos] : uriSpan).ToString();
|
||||
path = path.EnsureEndsWith('/');
|
||||
|
||||
if (pos > 0)
|
||||
{
|
||||
path += uri.Substring(pos);
|
||||
return string.Concat(path, uriSpan[pos..]);
|
||||
}
|
||||
|
||||
return path;
|
||||
@@ -32,20 +29,27 @@ public static class UriUtilityCore
|
||||
|
||||
public static string TrimPathEndSlash(string uri)
|
||||
{
|
||||
var pos1 = Math.Max(0, uri.IndexOf('?'));
|
||||
var pos2 = Math.Max(0, uri.IndexOf('#'));
|
||||
var pos = Math.Min(pos1, pos2);
|
||||
ReadOnlySpan<char> uriSpan = uri.AsSpan();
|
||||
var pos = IndexOfPathEnd(uriSpan);
|
||||
|
||||
var path = pos > 0 ? uri[..pos] : uri;
|
||||
var path = (pos > 0 ? uriSpan[..pos] : uriSpan).ToString();
|
||||
path = path.TrimEnd(Constants.CharArrays.ForwardSlash);
|
||||
|
||||
if (pos > 0)
|
||||
{
|
||||
path += uri.Substring(pos);
|
||||
return string.Concat(path, uriSpan[pos..]);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
#endregion
|
||||
private static int IndexOfPathEnd(ReadOnlySpan<char> uri)
|
||||
{
|
||||
var pos1 = Math.Max(0, uri.IndexOf('?'));
|
||||
var pos2 = Math.Max(0, uri.IndexOf('#'));
|
||||
return pos1 == 0 && pos2 == 0 ? 0
|
||||
: pos1 == 0 ? pos2
|
||||
: pos2 == 0 ? pos1
|
||||
: Math.Min(pos1, pos2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user