@@ -448,9 +448,7 @@ public readonly record struct NpgsqlInet
448448
449449 public NpgsqlInet ( IPAddress address , byte netmask )
450450 {
451- if ( address . AddressFamily != AddressFamily . InterNetwork && address . AddressFamily != AddressFamily . InterNetworkV6 )
452- throw new ArgumentException ( "Only IPAddress of InterNetwork or InterNetworkV6 address families are accepted" , nameof ( address ) ) ;
453-
451+ CheckAddressFamily ( address ) ;
454452 Address = address ;
455453 Netmask = netmask ;
456454 }
@@ -461,12 +459,23 @@ public NpgsqlInet(IPAddress address)
461459 }
462460
463461 public NpgsqlInet ( string addr )
464- => ( Address , Netmask ) = addr . Split ( '/' ) switch
462+ {
463+ switch ( addr . Split ( '/' ) )
465464 {
466- { Length : 2 } segments => ( IPAddress . Parse ( segments [ 0 ] ) , byte . Parse ( segments [ 1 ] ) ) ,
467- { Length : 1 } segments => ( IPAddress . Parse ( segments [ 0 ] ) , ( byte ) 32 ) ,
468- _ => throw new FormatException ( "Invalid number of parts in CIDR specification" )
469- } ;
465+ case { Length : 2 } segments :
466+ ( Address , Netmask ) = ( IPAddress . Parse ( segments [ 0 ] ) , byte . Parse ( segments [ 1 ] ) ) ;
467+ break ;
468+ case { Length : 1 } segments :
469+ var ipAddr = IPAddress . Parse ( segments [ 0 ] ) ;
470+ CheckAddressFamily ( ipAddr ) ;
471+ ( Address , Netmask ) = (
472+ ipAddr ,
473+ ipAddr . AddressFamily == AddressFamily . InterNetworkV6 ? ( byte ) 128 : ( byte ) 32 ) ;
474+ break ;
475+ default :
476+ throw new FormatException ( "Invalid number of parts in CIDR specification" ) ;
477+ }
478+ }
470479
471480 public override string ToString ( )
472481 => ( Address . AddressFamily == AddressFamily . InterNetwork && Netmask == 32 ) ||
@@ -485,6 +494,12 @@ public void Deconstruct(out IPAddress address, out byte netmask)
485494 address = Address ;
486495 netmask = Netmask ;
487496 }
497+
498+ static void CheckAddressFamily ( IPAddress address )
499+ {
500+ if ( address . AddressFamily != AddressFamily . InterNetwork && address . AddressFamily != AddressFamily . InterNetworkV6 )
501+ throw new ArgumentException ( "Only IPAddress of InterNetwork or InterNetworkV6 address families are accepted" , nameof ( address ) ) ;
502+ }
488503}
489504
490505/// <summary>
0 commit comments