66using System . Globalization ;
77using System . Net ;
88using System . Net . NetworkInformation ;
9+ using System . Numerics ;
910using System . Runtime . InteropServices ;
1011using System . Security . Cryptography ;
1112using System . Security . Principal ;
@@ -534,10 +535,10 @@ public struct Version : IComparable, IComparable<Version>, IEquatable<Version>
534535 {
535536 public static readonly Version Null = new ( - 1 , - 1 , - 1 , - 1 ) ;
536537
537- public readonly int Major ;
538- public readonly int Minor ;
539- public readonly int Patch ;
540- public readonly int Remainder ;
538+ public readonly BigInteger Major ;
539+ public readonly BigInteger Minor ;
540+ public readonly BigInteger Patch ;
541+ public readonly BigInteger Remainder ;
541542
542543 /// <summary>
543544 /// Segments past the fourth, with trailing zeroes trimmed so that equal versions
@@ -546,12 +547,23 @@ public struct Version : IComparable, IComparable<Version>, IEquatable<Version>
546547 /// (e.g. a build revision on a four-part version: 1.2.3.4_1 vs 1.2.3.4_2) order
547548 /// correctly instead of collapsing into Remainder.
548549 /// </summary>
549- private readonly int [ ] ? _extraSegments ;
550+ private readonly BigInteger [ ] ? _extraSegments ;
550551
551- public Version ( int major , int minor = 0 , int patch = 0 , int remainder = 0 )
552+ public Version (
553+ BigInteger major ,
554+ BigInteger minor = default ,
555+ BigInteger patch = default ,
556+ BigInteger remainder = default
557+ )
552558 : this ( major , minor , patch , remainder , null ) { }
553559
554- private Version ( int major , int minor , int patch , int remainder , int [ ] ? extraSegments )
560+ private Version (
561+ BigInteger major ,
562+ BigInteger minor ,
563+ BigInteger patch ,
564+ BigInteger remainder ,
565+ BigInteger [ ] ? extraSegments
566+ )
555567 {
556568 Major = major ;
557569 Minor = minor ;
@@ -564,16 +576,16 @@ private Version(int major, int minor, int patch, int remainder, int[]? extraSegm
564576 /// Builds a Version from an arbitrary number of segments. Segments past the fourth are
565577 /// retained for comparison rather than being folded into Remainder.
566578 /// </summary>
567- internal static Version FromSegments ( IReadOnlyList < int > segments )
579+ internal static Version FromSegments ( IReadOnlyList < BigInteger > segments )
568580 {
569581 int count = segments . Count ;
570582 while ( count > 4 && segments [ count - 1 ] == 0 )
571583 count -- ;
572584
573- int [ ] ? extra = null ;
585+ BigInteger [ ] ? extra = null ;
574586 if ( count > 4 )
575587 {
576- extra = new int [ count - 4 ] ;
588+ extra = new BigInteger [ count - 4 ] ;
577589 for ( int i = 4 ; i < count ; i ++ )
578590 extra [ i - 4 ] = segments [ i ] ;
579591 }
@@ -586,12 +598,14 @@ internal static Version FromSegments(IReadOnlyList<int> segments)
586598 extra
587599 ) ;
588600
589- static int Segment ( IReadOnlyList < int > values , int index ) =>
590- index < values . Count ? values [ index ] : 0 ;
601+ static BigInteger Segment ( IReadOnlyList < BigInteger > values , int index ) =>
602+ index < values . Count ? values [ index ] : BigInteger . Zero ;
591603 }
592604
593- private int ExtraSegment ( int index ) =>
594- _extraSegments is not null && index < _extraSegments . Length ? _extraSegments [ index ] : 0 ;
605+ private BigInteger ExtraSegment ( int index ) =>
606+ _extraSegments is not null && index < _extraSegments . Length
607+ ? _extraSegments [ index ]
608+ : BigInteger . Zero ;
595609
596610 public int CompareTo ( object ? other_ ) => other_ is Version other ? CompareTo ( other ) : 0 ;
597611
@@ -669,7 +683,7 @@ public override int GetHashCode()
669683 hash . Add ( Minor ) ;
670684 hash . Add ( Patch ) ;
671685 hash . Add ( Remainder ) ;
672- foreach ( int segment in _extraSegments ?? [ ] )
686+ foreach ( BigInteger segment in _extraSegments ?? [ ] )
673687 hash . Add ( segment ) ;
674688 return hash . ToHashCode ( ) ;
675689 }
@@ -732,25 +746,34 @@ public static Version VersionStringToStruct(string version)
732746 // Collect every segment rather than capping at four. Capping used to append the
733747 // surplus digits to the fourth segment, which inflated it: 1.2.3.4_1 became
734748 // 1.2.3.41 and so compared as greater than the newer 1.2.3.5, hiding the update.
735- List < string > segmentDigits = [ "" ] ;
749+ List < BigInteger > numbers = [ ] ;
750+ StringBuilder digits = new ( ) ;
751+ bool gapInSegment = false ;
752+ bool spliced = false ;
736753 bool first = true ;
737754
738755 foreach ( char c in version )
739756 {
740757 if ( char . IsDigit ( c ) )
741- segmentDigits [ ^ 1 ] += c ;
758+ {
759+ spliced |= gapInSegment ;
760+ digits . Append ( c ) ;
761+ }
742762 else if ( ! first && separators . Contains ( c ) )
743- segmentDigits . Add ( "" ) ;
763+ {
764+ numbers . Add ( ParseVersionSegment ( digits . ToString ( ) , spliced ) ) ;
765+ digits . Clear ( ) ;
766+ gapInSegment = false ;
767+ spliced = false ;
768+ }
769+ else if ( digits . Length > 0 )
770+ {
771+ gapInSegment = true ;
772+ }
744773 first = false ;
745774 }
746775
747- int [ ] numbers = new int [ segmentDigits . Count ] ;
748- for ( int i = 0 ; i < numbers . Length ; i ++ )
749- {
750- if ( int . TryParse ( segmentDigits [ i ] , out int val ) )
751- numbers [ i ] = val ;
752- }
753-
776+ numbers . Add ( ParseVersionSegment ( digits . ToString ( ) , spliced ) ) ;
754777 return Version . FromSegments ( numbers ) ;
755778 }
756779 catch
@@ -760,6 +783,24 @@ public static Version VersionStringToStruct(string version)
760783 }
761784 }
762785
786+ private static BigInteger ParseVersionSegment ( string digits , bool spliced )
787+ {
788+ if ( spliced )
789+ return int . TryParse ( digits , out int small ) ? small : BigInteger . Zero ;
790+
791+ if ( long . TryParse ( digits , out long value ) )
792+ return value ;
793+
794+ return BigInteger . TryParse (
795+ digits ,
796+ NumberStyles . None ,
797+ CultureInfo . InvariantCulture ,
798+ out BigInteger large
799+ )
800+ ? large
801+ : BigInteger . Zero ;
802+ }
803+
763804 public static System . Version NormalizeVersionForComparison ( System . Version version )
764805 {
765806 return version . Revision >= 0
0 commit comments