@@ -44,8 +44,10 @@ public class IPSocket
4444 /// </summary>
4545 public static string GetSocketString ( IPEndPoint endPoint )
4646 {
47- string format = ( endPoint . Address . AddressFamily == AddressFamily . InterNetworkV6 ) ? "[{0}]:{1}" : "{0}:{1}" ;
48- return string . Format ( format , endPoint . Address . ToString ( ) , endPoint . Port . ToString ( NumberFormatInfo . InvariantInfo ) ) ;
47+ var address = endPoint . Address . ToString ( ) ;
48+ var port = endPoint . Port . ToString ( NumberFormatInfo . InvariantInfo ) ;
49+
50+ return endPoint . Address . AddressFamily == AddressFamily . InterNetworkV6 ? $ "[{ address } ]:{ port } " : $ "{ address } :{ port } ";
4951 }
5052
5153 /// <summary>
@@ -123,7 +125,7 @@ public static string ParseHostFromSocket(string socket)
123125 {
124126 string host = socket ;
125127
126- if ( socket != null && socket . Trim ( ) . Length > 0 && socket . IndexOf ( ':' ) != - 1 )
128+ if ( ! string . IsNullOrWhiteSpace ( socket ) && socket . IndexOf ( ':' ) != - 1 )
127129 {
128130 host = socket . Substring ( 0 , socket . LastIndexOf ( ':' ) ) . Trim ( ) ;
129131 }
@@ -154,14 +156,14 @@ public static int ParsePortFromSocket(string socket)
154156 }
155157 // Look to see if this is IPv4 with a port (IPv6 will have another colon)
156158 // If it's a host name there will also not be another ':'.
157- else if ( socket . Substring ( 0 , lastColonPos ) . LastIndexOf ( ':' ) != - 1 )
159+ else if ( socket . AsSpan ( 0 , lastColonPos ) . LastIndexOf ( ':' ) != - 1 )
158160 {
159161 // This is an IPv6 address WITHOUT a port.
160162 lastColonPos = - 1 ;
161163 }
162164 }
163165
164- if ( socket != null && socket . Trim ( ) . Length > 0 && lastColonPos != - 1 )
166+ if ( ! string . IsNullOrWhiteSpace ( socket ) && lastColonPos != - 1 )
165167 {
166168 port = Convert . ToInt32 ( socket . Substring ( lastColonPos + 1 ) . Trim ( ) ) ;
167169 }
@@ -176,7 +178,7 @@ public static int ParsePortFromSocket(string socket)
176178 /// <returns>true/false</returns>
177179 public static bool IsIPAddress ( string socket )
178180 {
179- if ( socket == null || socket . Trim ( ) . Length == 0 )
181+ if ( string . IsNullOrWhiteSpace ( socket ) )
180182 {
181183 return false ;
182184 }
@@ -310,7 +312,7 @@ public static bool Parse(string endpointstring, out string host, out int port)
310312 }
311313 else
312314 {
313- throw new FormatException ( string . Format ( "Invalid endpoint ipaddress '{0 }'" , endpointstring ) ) ;
315+ throw new FormatException ( $ "Invalid endpoint ipaddress '{ endpointstring } '") ;
314316 }
315317
316318 return rc ;
@@ -327,7 +329,7 @@ public static IPEndPoint Parse(string endpointstring, int defaultport = -1)
327329 ( defaultport < IPEndPoint . MinPort
328330 || defaultport > IPEndPoint . MaxPort ) )
329331 {
330- throw new ArgumentException ( string . Format ( "Invalid default port '{0 }'" , defaultport ) ) ;
332+ throw new ArgumentException ( $ "Invalid default port '{ defaultport } '") ;
331333 }
332334
333335 string [ ] values = endpointstring . Split ( new char [ ] { ':' } ) ;
@@ -356,7 +358,7 @@ public static IPEndPoint Parse(string endpointstring, int defaultport = -1)
356358 }
357359 catch
358360 {
359- throw new FormatException ( string . Format ( "Invalid endpoint ipaddress '{0 }'" , endpointstring ) ) ;
361+ throw new FormatException ( $ "Invalid endpoint ipaddress '{ endpointstring } '") ;
360362 }
361363 }
362364 }
@@ -377,7 +379,7 @@ public static IPEndPoint Parse(string endpointstring, int defaultport = -1)
377379 }
378380 else
379381 {
380- throw new FormatException ( string . Format ( "Invalid endpoint ipaddress '{0 }'" , endpointstring ) ) ;
382+ throw new FormatException ( $ "Invalid endpoint ipaddress '{ endpointstring } '") ;
381383 }
382384
383385 if ( port == - 1 )
@@ -396,7 +398,7 @@ private static int getPort(string p)
396398 || port < IPEndPoint . MinPort
397399 || port > IPEndPoint . MaxPort )
398400 {
399- throw new FormatException ( string . Format ( "Invalid end point port '{0 }'" , p ) ) ;
401+ throw new FormatException ( $ "Invalid end point port '{ p } '") ;
400402 }
401403
402404 return port ;
@@ -410,13 +412,13 @@ private static IPAddress getIPfromHost(string p)
410412
411413 if ( hosts == null || hosts . Length == 0 )
412414 {
413- throw new ArgumentException ( string . Format ( "Host not found: {0}" , p ) ) ;
415+ throw new ArgumentException ( $ "Host not found: { p } " ) ;
414416 }
415417 return hosts [ 0 ] ;
416418 }
417419 catch
418420 {
419- throw new ArgumentException ( string . Format ( "Host not found: {0}" , p ) ) ;
421+ throw new ArgumentException ( $ "Host not found: { p } " ) ;
420422 }
421423 }
422424
0 commit comments