Skip to content

Commit 7c194a2

Browse files
committed
Simplify Uri's CreateHelper
1 parent 09e0e72 commit 7c194a2

File tree

1 file changed

+23
-30
lines changed
  • src/libraries/System.Private.Uri/src/System

1 file changed

+23
-30
lines changed

src/libraries/System.Private.Uri/src/System/UriExt.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,9 @@ private static bool CheckForUnicodeOrEscapedUnreserved(string data)
262262
//
263263
public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri, "uriKind")] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
264264
{
265-
if (uriString is null)
266-
{
267-
result = null;
268-
return false;
269-
}
270-
UriFormatException? e = null;
271-
result = CreateHelper(uriString, false, uriKind, ref e);
265+
result = CreateHelper(uriString, false, uriKind);
272266
result?.DebugSetLeftCtor();
273-
return e is null && result != null;
267+
return result is not null;
274268
}
275269

276270
/// <summary>
@@ -282,15 +276,9 @@ public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttrib
282276
/// <returns><see langword="true"/> if the <see cref="Uri"/> was successfully created; otherwise, <see langword="false"/>.</returns>
283277
public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri)] string? uriString, in UriCreationOptions creationOptions, [NotNullWhen(true)] out Uri? result)
284278
{
285-
if (uriString is null)
286-
{
287-
result = null;
288-
return false;
289-
}
290-
UriFormatException? e = null;
291-
result = CreateHelper(uriString, false, UriKind.Absolute, ref e, in creationOptions);
279+
result = CreateHelper(uriString, false, UriKind.Absolute, in creationOptions);
292280
result?.DebugSetLeftCtor();
293-
return e is null && result != null;
281+
return result is not null;
294282
}
295283

296284
public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(true)] out Uri? result)
@@ -303,6 +291,7 @@ public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(tru
303291
result = relativeLink;
304292
return true;
305293
}
294+
306295
result = null;
307296
return false;
308297
}
@@ -317,7 +306,6 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)]
317306
if (baseUri.IsNotAbsoluteUri)
318307
return false;
319308

320-
UriFormatException? e = null;
321309
string? newUriString = null;
322310

323311
bool dontEscape;
@@ -329,16 +317,17 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)]
329317
else
330318
{
331319
dontEscape = false;
332-
newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out e);
320+
newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out UriFormatException? e);
333321

334322
if (e != null)
335323
return false;
336324
}
337325

338-
result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e);
326+
result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute);
327+
Debug.Assert(result is null || result.IsAbsoluteUri);
339328

340329
result?.DebugSetLeftCtor();
341-
return e is null && result != null && result.IsAbsoluteUri;
330+
return result is not null;
342331
}
343332

344333
public string GetComponents(UriComponents components, UriFormat format)
@@ -708,9 +697,14 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
708697
//
709698
// a Uri.TryCreate() method goes through here.
710699
//
711-
internal static Uri? CreateHelper(string uriString, bool dontEscape, UriKind uriKind, ref UriFormatException? e, in UriCreationOptions creationOptions = default)
700+
internal static Uri? CreateHelper(string? uriString, bool dontEscape, UriKind uriKind, in UriCreationOptions creationOptions = default)
712701
{
713-
if ((int)uriKind < (int)UriKind.RelativeOrAbsolute || (int)uriKind > (int)UriKind.Relative)
702+
if (uriString is null)
703+
{
704+
return null;
705+
}
706+
707+
if (uriKind is < UriKind.RelativeOrAbsolute or > UriKind.Relative)
714708
{
715709
throw new ArgumentException(SR.Format(SR.net_uri_InvalidUriKind, uriKind));
716710
}
@@ -742,7 +736,7 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
742736
// Validate instance using ether built in or a user Parser
743737
try
744738
{
745-
result.InitializeUri(err, uriKind, out e);
739+
result.InitializeUri(err, uriKind, out UriFormatException? e);
746740

747741
if (e == null)
748742
{
@@ -752,10 +746,9 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
752746

753747
return null;
754748
}
755-
catch (UriFormatException ee)
749+
catch (UriFormatException)
756750
{
757-
Debug.Assert(!syntax!.IsSimple, "A UriPraser threw on InitializeAndValidate.");
758-
e = ee;
751+
Debug.Assert(!syntax.IsSimple, "A UriPraser threw on InitializeAndValidate.");
759752
// A precaution since custom Parser should never throw in this case.
760753
return null;
761754
}
@@ -973,12 +966,12 @@ internal bool IsBaseOfHelper(Uri uriLink)
973966

974967
if (uriLink is null)
975968
{
976-
UriFormatException? e = null;
969+
uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute)!;
977970

978-
uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e)!;
979-
980-
if (e != null)
971+
if (uriLink is null)
972+
{
981973
return false;
974+
}
982975
}
983976
}
984977

0 commit comments

Comments
 (0)