Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions src/libraries/System.Private.Uri/src/System/UriExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,9 @@ private static bool CheckForUnicodeOrEscapedUnreserved(string data)
//
public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri, "uriKind")] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
{
if (uriString is null)
{
result = null;
return false;
}
UriFormatException? e = null;
result = CreateHelper(uriString, false, uriKind, ref e);
result = CreateHelper(uriString, false, uriKind);
result?.DebugSetLeftCtor();
return e is null && result != null;
return result is not null;
}

/// <summary>
Expand All @@ -243,15 +237,9 @@ public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttrib
/// <returns><see langword="true"/> if the <see cref="Uri"/> was successfully created; otherwise, <see langword="false"/>.</returns>
public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri)] string? uriString, in UriCreationOptions creationOptions, [NotNullWhen(true)] out Uri? result)
{
if (uriString is null)
{
result = null;
return false;
}
UriFormatException? e = null;
result = CreateHelper(uriString, false, UriKind.Absolute, ref e, in creationOptions);
result = CreateHelper(uriString, false, UriKind.Absolute, in creationOptions);
result?.DebugSetLeftCtor();
return e is null && result != null;
return result is not null;
}

public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(true)] out Uri? result)
Expand All @@ -264,6 +252,7 @@ public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(tru
result = relativeLink;
return true;
}

result = null;
return false;
}
Expand All @@ -278,7 +267,6 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)]
if (baseUri.IsNotAbsoluteUri)
return false;

UriFormatException? e = null;
string? newUriString = null;

bool dontEscape;
Expand All @@ -290,16 +278,17 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)]
else
{
dontEscape = false;
newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out e);
newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out UriFormatException? e);

if (e != null)
return false;
}

result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e);
result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute);
Debug.Assert(result is null || result.IsAbsoluteUri);

result?.DebugSetLeftCtor();
return e is null && result != null && result.IsAbsoluteUri;
return result is not null;
}

public string GetComponents(UriComponents components, UriFormat format)
Expand Down Expand Up @@ -669,9 +658,14 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
//
// a Uri.TryCreate() method goes through here.
//
internal static Uri? CreateHelper(string uriString, bool dontEscape, UriKind uriKind, ref UriFormatException? e, in UriCreationOptions creationOptions = default)
internal static Uri? CreateHelper(string? uriString, bool dontEscape, UriKind uriKind, in UriCreationOptions creationOptions = default)
{
if ((int)uriKind < (int)UriKind.RelativeOrAbsolute || (int)uriKind > (int)UriKind.Relative)
if (uriString is null)
{
return null;
}

if (uriKind is < UriKind.RelativeOrAbsolute or > UriKind.Relative)
{
throw new ArgumentException(SR.Format(SR.net_uri_InvalidUriKind, uriKind));
}
Expand Down Expand Up @@ -703,7 +697,7 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
// Validate instance using ether built in or a user Parser
try
{
e = result.InitializeUri(err, uriKind);
UriFormatException? e = result.InitializeUri(err, uriKind);

if (e == null)
{
Expand All @@ -713,10 +707,9 @@ private Uri(Flags flags, UriParser? uriParser, string uri)

return null;
}
catch (UriFormatException ee)
catch (UriFormatException)
{
Debug.Assert(!syntax!.IsSimple, "A UriPraser threw on InitializeAndValidate.");
e = ee;
Debug.Assert(!syntax.IsSimple, "A UriPraser threw on InitializeAndValidate.");
// A precaution since custom Parser should never throw in this case.
return null;
}
Expand Down Expand Up @@ -934,12 +927,12 @@ internal bool IsBaseOfHelper(Uri uriLink)

if (uriLink is null)
{
UriFormatException? e = null;

uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e)!;
uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute)!;

if (e != null)
if (uriLink is null)
{
return false;
}
}
}

Expand Down