Skip to content

Commit a6523bb

Browse files
committed
Simplify InitializeUri and remove some dead branches
1 parent 09e0e72 commit a6523bb

File tree

1 file changed

+29
-45
lines changed
  • src/libraries/System.Private.Uri/src/System

1 file changed

+29
-45
lines changed

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

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre
4646
private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatException? e)
4747
{
4848
DebugAssertInCtor();
49+
Debug.Assert((err is ParsingError.None) == (_syntax is not null));
50+
51+
bool hasUnicode = false;
52+
53+
if (IriParsing && CheckForUnicodeOrEscapedUnreserved(_string))
54+
{
55+
_flags |= Flags.HasUnicode;
56+
hasUnicode = true;
57+
_originalUnicodeString = _string; // original string location changed
58+
}
4959

5060
if (err == ParsingError.None)
5161
{
@@ -80,26 +90,32 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep
8090
}
8191
}
8292
}
83-
else if (err > ParsingError.LastErrorOkayForRelativeUris)
93+
else
94+
{
95+
// If we encountered any parsing errors that indicate this may be a relative Uri,
96+
// and we'll allow relative Uri's, then create one.
97+
if (err <= ParsingError.LastErrorOkayForRelativeUris)
98+
{
99+
e = null;
100+
_flags &= Flags.UserEscaped | Flags.HasUnicode; // the only flags that makes sense for a relative uri
101+
if (hasUnicode)
102+
{
103+
// Iri'ze and then normalize relative uris
104+
_string = EscapeUnescapeIri(_originalUnicodeString, 0, _originalUnicodeString.Length, isQuery: false);
105+
}
106+
}
107+
else
84108
{
85109
// This is a fatal error based solely on scheme name parsing
86110
_string = null!; // make it be invalid Uri
87111
e = GetException(err);
88-
return;
89112
}
90113

91-
bool hasUnicode = false;
92-
93-
if (IriParsing && CheckForUnicodeOrEscapedUnreserved(_string))
94-
{
95-
_flags |= Flags.HasUnicode;
96-
hasUnicode = true;
97-
// switch internal strings
98-
_originalUnicodeString = _string; // original string location changed
114+
return;
99115
}
100116

101-
if (_syntax != null)
102-
{
117+
Debug.Assert(err == ParsingError.None && _syntax is not null);
118+
103119
if (_syntax.IsSimple)
104120
{
105121
if ((err = PrivateParseMinimal()) != ParsingError.None)
@@ -122,7 +138,6 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep
122138
}
123139
else
124140
e = null;
125-
// will return from here
126141

127142
if (e is null && hasUnicode)
128143
{
@@ -148,18 +163,7 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep
148163
// Ask a registered type to validate this uri
149164
_syntax.InternalValidate(this, out e);
150165

151-
if (e != null)
152-
{
153-
// Can we still take it as a relative Uri?
154-
if (uriKind != UriKind.Absolute && err != ParsingError.None
155-
&& err <= ParsingError.LastErrorOkayForRelativeUris)
156-
{
157-
_syntax = null!; // convert it to relative
158-
e = null;
159-
_flags &= Flags.UserEscaped; // the only flag that makes sense for a relative uri
160-
}
161-
}
162-
else // e == null
166+
if (e is null)
163167
{
164168
if (err != ParsingError.None || InFact(Flags.ErrorOrParsingRecursion))
165169
{
@@ -187,26 +191,6 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep
187191
}
188192
}
189193
}
190-
// will return from here
191-
}
192-
}
193-
// If we encountered any parsing errors that indicate this may be a relative Uri,
194-
// and we'll allow relative Uri's, then create one.
195-
else if (err != ParsingError.None && uriKind != UriKind.Absolute
196-
&& err <= ParsingError.LastErrorOkayForRelativeUris)
197-
{
198-
e = null;
199-
_flags &= (Flags.UserEscaped | Flags.HasUnicode); // the only flags that makes sense for a relative uri
200-
if (hasUnicode)
201-
{
202-
// Iri'ze and then normalize relative uris
203-
_string = EscapeUnescapeIri(_originalUnicodeString, 0, _originalUnicodeString.Length, isQuery: false);
204-
}
205-
}
206-
else
207-
{
208-
_string = null!; // make it be invalid Uri
209-
e = GetException(err);
210194
}
211195
}
212196

0 commit comments

Comments
 (0)