Skip to content

Commit 3b63772

Browse files
authored
Simplify Uri's CreateHelper (#122002)
1 parent a3b39f5 commit 3b63772

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
@@ -223,15 +223,9 @@ private static bool CheckForUnicodeOrEscapedUnreserved(string data)
223223
//
224224
public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri, "uriKind")] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
225225
{
226-
if (uriString is null)
227-
{
228-
result = null;
229-
return false;
230-
}
231-
UriFormatException? e = null;
232-
result = CreateHelper(uriString, false, uriKind, ref e);
226+
result = CreateHelper(uriString, false, uriKind);
233227
result?.DebugSetLeftCtor();
234-
return e is null && result != null;
228+
return result is not null;
235229
}
236230

237231
/// <summary>
@@ -243,15 +237,9 @@ public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttrib
243237
/// <returns><see langword="true"/> if the <see cref="Uri"/> was successfully created; otherwise, <see langword="false"/>.</returns>
244238
public static bool TryCreate([NotNullWhen(true), StringSyntax(StringSyntaxAttribute.Uri)] string? uriString, in UriCreationOptions creationOptions, [NotNullWhen(true)] out Uri? result)
245239
{
246-
if (uriString is null)
247-
{
248-
result = null;
249-
return false;
250-
}
251-
UriFormatException? e = null;
252-
result = CreateHelper(uriString, false, UriKind.Absolute, ref e, in creationOptions);
240+
result = CreateHelper(uriString, false, UriKind.Absolute, in creationOptions);
253241
result?.DebugSetLeftCtor();
254-
return e is null && result != null;
242+
return result is not null;
255243
}
256244

257245
public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(true)] out Uri? result)
@@ -264,6 +252,7 @@ public static bool TryCreate(Uri? baseUri, string? relativeUri, [NotNullWhen(tru
264252
result = relativeLink;
265253
return true;
266254
}
255+
267256
result = null;
268257
return false;
269258
}
@@ -278,7 +267,6 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)]
278267
if (baseUri.IsNotAbsoluteUri)
279268
return false;
280269

281-
UriFormatException? e = null;
282270
string? newUriString = null;
283271

284272
bool dontEscape;
@@ -290,16 +278,17 @@ public static bool TryCreate(Uri? baseUri, Uri? relativeUri, [NotNullWhen(true)]
290278
else
291279
{
292280
dontEscape = false;
293-
newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out e);
281+
newUriString = baseUri.Syntax.InternalResolve(baseUri, relativeUri, out UriFormatException? e);
294282

295283
if (e != null)
296284
return false;
297285
}
298286

299-
result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e);
287+
result ??= CreateHelper(newUriString!, dontEscape, UriKind.Absolute);
288+
Debug.Assert(result is null || result.IsAbsoluteUri);
300289

301290
result?.DebugSetLeftCtor();
302-
return e is null && result != null && result.IsAbsoluteUri;
291+
return result is not null;
303292
}
304293

305294
public string GetComponents(UriComponents components, UriFormat format)
@@ -669,9 +658,14 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
669658
//
670659
// a Uri.TryCreate() method goes through here.
671660
//
672-
internal static Uri? CreateHelper(string uriString, bool dontEscape, UriKind uriKind, ref UriFormatException? e, in UriCreationOptions creationOptions = default)
661+
internal static Uri? CreateHelper(string? uriString, bool dontEscape, UriKind uriKind, in UriCreationOptions creationOptions = default)
673662
{
674-
if ((int)uriKind < (int)UriKind.RelativeOrAbsolute || (int)uriKind > (int)UriKind.Relative)
663+
if (uriString is null)
664+
{
665+
return null;
666+
}
667+
668+
if (uriKind is < UriKind.RelativeOrAbsolute or > UriKind.Relative)
675669
{
676670
throw new ArgumentException(SR.Format(SR.net_uri_InvalidUriKind, uriKind));
677671
}
@@ -703,7 +697,7 @@ private Uri(Flags flags, UriParser? uriParser, string uri)
703697
// Validate instance using ether built in or a user Parser
704698
try
705699
{
706-
e = result.InitializeUri(err, uriKind);
700+
UriFormatException? e = result.InitializeUri(err, uriKind);
707701

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

714708
return null;
715709
}
716-
catch (UriFormatException ee)
710+
catch (UriFormatException)
717711
{
718-
Debug.Assert(!syntax!.IsSimple, "A UriPraser threw on InitializeAndValidate.");
719-
e = ee;
712+
Debug.Assert(!syntax.IsSimple, "A UriPraser threw on InitializeAndValidate.");
720713
// A precaution since custom Parser should never throw in this case.
721714
return null;
722715
}
@@ -934,12 +927,12 @@ internal bool IsBaseOfHelper(Uri uriLink)
934927

935928
if (uriLink is null)
936929
{
937-
UriFormatException? e = null;
938-
939-
uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute, ref e)!;
930+
uriLink = CreateHelper(newUriString!, dontEscape, UriKind.Absolute)!;
940931

941-
if (e != null)
932+
if (uriLink is null)
933+
{
942934
return false;
935+
}
943936
}
944937
}
945938

0 commit comments

Comments
 (0)