@@ -22,12 +22,13 @@ namespace TensionDev.UUID
22
22
{
23
23
public sealed class Uuid : IComparable < Uuid > , IEquatable < Uuid >
24
24
{
25
- private uint _time_low ;
26
- private ushort _time_mid ;
27
- private ushort _time_hi_and_version ;
25
+ private const string INVALID_FORMAT_STRING = "The format of s is invalid" ;
26
+ private readonly uint _time_low ;
27
+ private readonly ushort _time_mid ;
28
+ private readonly ushort _time_hi_and_version ;
28
29
private byte _clock_seq_hi_and_reserved ;
29
- private byte _clock_seq_low ;
30
- private byte [ ] _node ;
30
+ private readonly byte _clock_seq_low ;
31
+ private readonly byte [ ] _node ;
31
32
32
33
/// <summary>
33
34
/// A read-only instance of the Uuid object whose value is all zeros.
@@ -37,7 +38,7 @@ public sealed class Uuid : IComparable<Uuid>, IEquatable<Uuid>
37
38
/// <summary>
38
39
/// A read-only instance of the Uuid object whose value is all ones.
39
40
/// </summary>
40
- public static readonly Uuid Max = new Uuid ( uint . MaxValue , ushort . MaxValue , byte . MaxValue , byte . MaxValue , byte . MaxValue , new byte [ ] { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ) ;
41
+ public static readonly Uuid Max = new Uuid ( uint . MaxValue , ushort . MaxValue , byte . MaxValue , byte . MaxValue , byte . MaxValue , new byte [ ] { 0xff , 0xff , 0xff , 0xff , 0xff , 0xff } ) ;
41
42
42
43
public Uuid ( )
43
44
{
@@ -84,20 +85,17 @@ public Uuid(byte[] b) : this()
84
85
/// <exception cref="System.OverflowException">The format of s is invalid</exception>
85
86
public Uuid ( string s ) : this ( )
86
87
{
87
- if ( s == null )
88
- throw new ArgumentNullException ( nameof ( s ) ) ;
89
-
90
88
if ( String . IsNullOrEmpty ( s ) )
91
- throw new FormatException ( "The format of s is invalid" ) ;
89
+ throw new ArgumentNullException ( nameof ( s ) , INVALID_FORMAT_STRING ) ;
92
90
93
91
if ( s . Length != 32 && s . Length != 36 && s . Length != 38 )
94
- throw new FormatException ( "The format of s is invalid" ) ;
92
+ throw new FormatException ( INVALID_FORMAT_STRING ) ;
95
93
96
- if ( s . Length == 38 )
94
+ if ( s . Length == 38 &&
95
+ ! ( s . StartsWith ( "{" ) && s . EndsWith ( "}" ) ) &&
96
+ ! ( s . StartsWith ( "(" ) && s . EndsWith ( ")" ) ) )
97
97
{
98
- if ( ! ( s . StartsWith ( "{" ) && s . EndsWith ( "}" ) ) &&
99
- ! ( s . StartsWith ( "(" ) && s . EndsWith ( ")" ) ) )
100
- throw new FormatException ( "The format of s is invalid" ) ;
98
+ throw new FormatException ( INVALID_FORMAT_STRING ) ;
101
99
}
102
100
103
101
string vs = s . Replace ( "{" , "" ) ;
@@ -107,15 +105,15 @@ public Uuid(string s) : this()
107
105
108
106
if ( vs . Length == 36 )
109
107
{
110
- Regex regex = new Regex ( @"\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b" , RegexOptions . IgnoreCase ) ;
108
+ Regex regex = new Regex ( @"\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b" , RegexOptions . IgnoreCase , TimeSpan . FromMilliseconds ( 100 ) ) ;
111
109
MatchCollection matches = regex . Matches ( vs ) ;
112
110
if ( matches . Count == 0 )
113
- throw new FormatException ( "The format of s is invalid" ) ;
111
+ throw new FormatException ( INVALID_FORMAT_STRING ) ;
114
112
}
115
113
vs = vs . Replace ( "-" , "" ) ;
116
114
117
115
if ( vs . Length != 32 )
118
- throw new FormatException ( "The format of s is invalid" ) ;
116
+ throw new FormatException ( INVALID_FORMAT_STRING ) ;
119
117
120
118
Byte [ ] b = new Byte [ 16 ] ;
121
119
for ( Int32 i = 0 ; i < vs . Length ; i += 2 )
@@ -126,11 +124,11 @@ public Uuid(string s) : this()
126
124
}
127
125
catch ( FormatException )
128
126
{
129
- throw new FormatException ( "The format of s is invalid" ) ;
127
+ throw new FormatException ( INVALID_FORMAT_STRING ) ;
130
128
}
131
129
catch ( OverflowException )
132
130
{
133
- throw new OverflowException ( "The format of s is invalid" ) ;
131
+ throw new OverflowException ( INVALID_FORMAT_STRING ) ;
134
132
}
135
133
}
136
134
@@ -356,9 +354,10 @@ public Guid ToVariant2()
356
354
{
357
355
byte newClockSeq = ( byte ) ( _clock_seq_hi_and_reserved & 0x1F ) ;
358
356
newClockSeq = ( byte ) ( newClockSeq | 0xC0 ) ;
359
- Uuid variant2 = new Uuid ( this . ToByteArray ( ) ) ;
360
-
361
- variant2 . _clock_seq_hi_and_reserved = newClockSeq ;
357
+ Uuid variant2 = new Uuid ( this . ToByteArray ( ) )
358
+ {
359
+ _clock_seq_hi_and_reserved = newClockSeq
360
+ } ;
362
361
363
362
return variant2 . ToGuid ( ) ;
364
363
}
@@ -370,10 +369,13 @@ public Guid ToVariant2()
370
369
/// <returns>A TensionDev.UUID.Uuid object.</returns>
371
370
public static Uuid ToVariant1 ( Guid guid )
372
371
{
373
- Uuid variant1 = new Uuid ( guid . ToString ( ) ) ;
374
- byte newClockSeq = ( byte ) ( variant1 . _clock_seq_hi_and_reserved & 0x3F ) ;
372
+ Uuid variant2 = new Uuid ( guid . ToString ( ) ) ;
373
+ byte newClockSeq = ( byte ) ( variant2 . _clock_seq_hi_and_reserved & 0x3F ) ;
375
374
newClockSeq = ( byte ) ( newClockSeq | 0x80 ) ;
376
- variant1 . _clock_seq_hi_and_reserved = newClockSeq ;
375
+ Uuid variant1 = new Uuid ( variant2 . ToByteArray ( ) )
376
+ {
377
+ _clock_seq_hi_and_reserved = newClockSeq
378
+ } ;
377
379
378
380
return variant1 ;
379
381
}
0 commit comments