11using System ;
2- using System . Collections . Generic ;
3- using System . Diagnostics ;
42using System . Diagnostics . CodeAnalysis ;
53using System . Globalization ;
64using System . Linq ;
75using System . Runtime . Serialization ;
86using System . Text . RegularExpressions ;
97using GitHub . Extensions ;
8+ using static System . String ;
109
1110namespace GitHub . Primitives
1211{
@@ -31,6 +30,7 @@ public class UriString : StringEquivalent<UriString>, IEquatable<UriString>
3130
3231 public UriString ( string uriString ) : base ( NormalizePath ( uriString ) )
3332 {
33+ if ( uriString == null ) throw new ArgumentNullException ( nameof ( uriString ) , "Cannot create a null UriString" ) ;
3434 if ( uriString . Length == 0 ) return ;
3535 if ( Uri . TryCreate ( uriString , UriKind . Absolute , out url ) )
3636 {
@@ -47,7 +47,7 @@ public UriString(string uriString) : base(NormalizePath(uriString))
4747 if ( RepositoryName != null )
4848 {
4949 NameWithOwner = Owner != null
50- ? string . Format ( CultureInfo . InvariantCulture , "{0}/{1}" , Owner , RepositoryName )
50+ ? Format ( CultureInfo . InvariantCulture , "{0}/{1}" , Owner , RepositoryName )
5151 : RepositoryName ;
5252 }
5353 }
@@ -71,43 +71,6 @@ void SetUri(Uri uri)
7171 }
7272
7373 IsHypertextTransferProtocol = uri . IsHypertextTransferProtocol ( ) ;
74-
75- if ( String . IsNullOrEmpty ( uri . Query ) ) return ;
76-
77- try
78- {
79- var query = ParseQueryString ( uri ) ;
80-
81- if ( query . ContainsKey ( "branch" ) && ! String . IsNullOrEmpty ( query [ "branch" ] ) )
82- {
83- Branch = query [ "branch" ] . Replace ( "%2F" , "/" ) ;
84- }
85-
86- if ( query . ContainsKey ( "pr" ) && ! String . IsNullOrEmpty ( query [ "pr" ] ) )
87- {
88- PullRequest = query [ "pr" ] . Replace ( "%2F" , "/" ) ;
89- }
90-
91- if ( query . ContainsKey ( "filepath" ) && ! String . IsNullOrEmpty ( query [ "filepath" ] ) )
92- {
93- RelativePathToOpen = query [ "filepath" ] . Replace ( "%2F" , "/" ) . Replace ( '/' , '\\ ' ) ;
94- }
95- }
96- catch //(Exception ex)
97- {
98- //log.WarnException("Failed to read URI query", ex);
99- }
100- }
101-
102- // This is not a complete query string parsing algorithm, but it's good enough for our needs.
103- static IDictionary < string , string > ParseQueryString ( Uri uri )
104- {
105- Debug . Assert ( uri . Query . StartsWith ( '?' ) ,
106- String . Format ( CultureInfo . InvariantCulture , "Uri.Query doesn't start with '?': '{0}'" , uri . Query ) ) ;
107- return uri . Query . Substring ( 1 ) . Split ( new [ ] { '&' } , StringSplitOptions . RemoveEmptyEntries )
108- . Select ( pair => pair . Split ( '=' ) )
109- . ToDictionary ( pair => pair . First ( ) , pair => pair . Length > 1 ? pair [ 1 ] : null ,
110- StringComparer . OrdinalIgnoreCase ) ;
11174 }
11275
11376 void SetFilePath ( Uri uri )
@@ -144,8 +107,6 @@ bool ParseScpSyntax(string scpString)
144107 return false ;
145108 }
146109
147- public string Branch { get ; private set ; }
148- public string PullRequest { get ; set ; }
149110 public string Host { get ; private set ; }
150111
151112 public string Owner { get ; private set ; }
@@ -154,19 +115,24 @@ bool ParseScpSyntax(string scpString)
154115
155116 public string NameWithOwner { get ; private set ; }
156117
157- public string RelativePathToOpen { get ; private set ; }
158-
159118 public bool IsFileUri { get ; private set ; }
160119
161- public bool IsValidUri
162- {
163- get { return url != null ; }
164- }
120+ public bool IsValidUri => url != null ;
165121
166- public Uri ToUri ( )
122+ public Uri ToWebUri ( )
167123 {
168- if ( url == null )
169- throw new InvalidOperationException ( "This Uri String is not a valid Uri" ) ;
124+ if ( url == null || ( url . Scheme != Uri . UriSchemeHttp && url . Scheme != Uri . UriSchemeHttps ) )
125+ {
126+ return new UriBuilder
127+ {
128+ Scheme = Uri . UriSchemeHttps ,
129+ Host = Host ,
130+ Path = NameWithOwner ,
131+ Port = url ? . Port == 80
132+ ? - 1
133+ : ( url ? . Port ?? - 1 )
134+ } . Uri ;
135+ }
170136 return url ;
171137 }
172138
@@ -186,9 +152,7 @@ public static implicit operator UriString(string value)
186152 [ SuppressMessage ( "Microsoft.Usage" , "CA2225:OperatorOverloadsHaveNamedAlternates" ) ]
187153 public static implicit operator string ( UriString uriString )
188154 {
189- if ( uriString == null ) return null ;
190-
191- return uriString . Value ;
155+ return uriString ? . Value ;
192156 }
193157
194158 [ SuppressMessage ( "Microsoft.Usage" , "CA2234:PassSystemUriObjectsInsteadOfStrings" , Justification = "No." ) ]
@@ -197,7 +161,7 @@ public override UriString Combine(string addition)
197161 if ( url != null )
198162 {
199163 var urlBuilder = new UriBuilder ( url ) ;
200- if ( ! String . IsNullOrEmpty ( urlBuilder . Query ) )
164+ if ( ! IsNullOrEmpty ( urlBuilder . Query ) )
201165 {
202166 var query = urlBuilder . Query ;
203167 if ( query . StartsWith ( "?" , StringComparison . Ordinal ) )
@@ -221,7 +185,7 @@ public override UriString Combine(string addition)
221185 }
222186 return ToUriString ( urlBuilder . Uri ) ;
223187 }
224- return String . Concat ( Value , addition ) ;
188+ return Concat ( Value , addition ) ;
225189 }
226190
227191 public override string ToString ( )
@@ -253,12 +217,12 @@ static string GetSerializedValue(SerializationInfo info)
253217
254218 static string NormalizePath ( string path )
255219 {
256- return path == null ? null : path . Replace ( '\\ ' , '/' ) ;
220+ return path ? . Replace ( '\\ ' , '/' ) ;
257221 }
258222
259223 static string GetRepositoryName ( string repositoryNameSegment )
260224 {
261- if ( String . IsNullOrEmpty ( repositoryNameSegment )
225+ if ( IsNullOrEmpty ( repositoryNameSegment )
262226 || repositoryNameSegment . Equals ( "/" , StringComparison . Ordinal ) )
263227 {
264228 return null ;
0 commit comments