Skip to content

Commit 4e0993c

Browse files
committed
- removed redundant null checks
- updated null check syntax
1 parent 4682138 commit 4e0993c

12 files changed

Lines changed: 37 additions & 75 deletions

Asn1Parser/Asn1Builder.cs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public Asn1Builder AddInteger(BigInteger value) {
5656
/// </param>
5757
/// <returns>Current instance with added value.</returns>
5858
public Asn1Builder AddBitString(ReadOnlySpan<Byte> value, Byte unusedBits) {
59-
if (value == null) {
60-
throw new ArgumentNullException(nameof(value));
61-
}
6259
_rawData.Add(new Asn1BitString(value, unusedBits).GetRawDataAsMemory());
6360
return this;
6461
}
@@ -73,9 +70,6 @@ public Asn1Builder AddBitString(ReadOnlySpan<Byte> value, Byte unusedBits) {
7370
/// </param>
7471
/// <returns>Current instance with added value.</returns>
7572
public Asn1Builder AddBitString(ReadOnlySpan<Byte> value, Boolean calculateUnusedBits = false) {
76-
if (value == null) {
77-
throw new ArgumentNullException(nameof(value));
78-
}
7973
_rawData.Add(new Asn1BitString(value, calculateUnusedBits).GetRawDataAsMemory());
8074
return this;
8175
}
@@ -91,7 +85,6 @@ public Asn1Builder AddBitString(ReadOnlySpan<Byte> value, Boolean calculateUnuse
9185
/// <returns>Current instance with added value.</returns>
9286
public Asn1Builder AddOctetString(ReadOnlyMemory<Byte> value) {
9387
_rawData.Add(new Asn1OctetString(value, false).GetRawDataAsMemory());
94-
9588
return this;
9689
}
9790
/// <summary>
@@ -113,7 +106,7 @@ public Asn1Builder AddNull() {
113106
/// </exception>
114107
/// <returns>Current instance with added value.</returns>
115108
public Asn1Builder AddObjectIdentifier(Oid value) {
116-
if (value == null) {
109+
if (value is null) {
117110
throw new ArgumentNullException(nameof(value));
118111
}
119112
_rawData.Add(new Asn1ObjectIdentifier(value).GetRawDataAsMemory());
@@ -131,7 +124,7 @@ public Asn1Builder AddObjectIdentifier(Oid value) {
131124
/// <exception cref="FormatException">Specified value doesn't represent valid decimal-dot format.</exception>
132125
/// <returns>Current instance with added value.</returns>
133126
public Asn1Builder AddRelativeOid(String value) {
134-
if (value == null) {
127+
if (value is null) {
135128
throw new ArgumentNullException(nameof(value));
136129
}
137130
_rawData.Add(new Asn1RelativeOid(value).GetRawDataAsMemory());
@@ -159,7 +152,7 @@ public Asn1Builder AddEnumerated(UInt64 value) {
159152
/// </exception>
160153
/// <returns>Current instance with added value.</returns>
161154
public Asn1Builder AddUTF8String(String value) {
162-
if (value == null) {
155+
if (value is null) {
163156
throw new ArgumentNullException(nameof(value));
164157
}
165158
_rawData.Add(new Asn1UTF8String(value).GetRawDataAsMemory());
@@ -212,7 +205,7 @@ public Asn1Builder AddSet(ReadOnlySpan<Byte> value) {
212205
/// </exception>
213206
/// <returns>Current instance with added value.</returns>
214207
public Asn1Builder AddNumericString(String value) {
215-
if (value == null) {
208+
if (value is null) {
216209
throw new ArgumentNullException(nameof(value));
217210
}
218211
_rawData.Add(new Asn1NumericString(value).GetRawDataAsMemory());
@@ -229,7 +222,7 @@ public Asn1Builder AddNumericString(String value) {
229222
/// </exception>
230223
/// <returns>Current instance with added value.</returns>
231224
public Asn1Builder AddPrintableString(String value) {
232-
if (value == null) {
225+
if (value is null) {
233226
throw new ArgumentNullException(nameof(value));
234227
}
235228
_rawData.Add(new Asn1PrintableString(value).GetRawDataAsMemory());
@@ -246,7 +239,7 @@ public Asn1Builder AddPrintableString(String value) {
246239
/// </exception>
247240
/// <returns>Current instance with added value.</returns>
248241
public Asn1Builder AddTeletexString(String value) {
249-
if (value == null) {
242+
if (value is null) {
250243
throw new ArgumentNullException(nameof(value));
251244
}
252245
_rawData.Add(new Asn1TeletexString(value).GetRawDataAsMemory());
@@ -263,7 +256,7 @@ public Asn1Builder AddTeletexString(String value) {
263256
/// </exception>
264257
/// <returns>Current instance with added value.</returns>
265258
public Asn1Builder AddVideotexString(String value) {
266-
if (value == null) {
259+
if (value is null) {
267260
throw new ArgumentNullException(nameof(value));
268261
}
269262
_rawData.Add(Asn1Utils.Encode(Encoding.ASCII.GetBytes(value).AsSpan(), Asn1Type.VideotexString));
@@ -280,7 +273,7 @@ public Asn1Builder AddVideotexString(String value) {
280273
/// </exception>
281274
/// <returns>Current instance with added value.</returns>
282275
public Asn1Builder AddIA5String(String value) {
283-
if (value == null) {
276+
if (value is null) {
284277
throw new ArgumentNullException(nameof(value));
285278
}
286279
_rawData.Add(new Asn1IA5String(value).GetRawDataAsMemory());
@@ -337,7 +330,7 @@ public Asn1Builder AddRfcDateTime(DateTime value) {
337330
/// </exception>
338331
/// <returns>Current instance with added value.</returns>
339332
public Asn1Builder AddVisibleString(String value) {
340-
if (value == null) {
333+
if (value is null) {
341334
throw new ArgumentNullException(nameof(value));
342335
}
343336
_rawData.Add(new Asn1VisibleString(value).GetRawDataAsMemory());
@@ -354,7 +347,7 @@ public Asn1Builder AddVisibleString(String value) {
354347
/// </exception>
355348
/// <returns>Current instance with added value.</returns>
356349
public Asn1Builder AddUniversalString(String value) {
357-
if (value == null) {
350+
if (value is null) {
358351
throw new ArgumentNullException(nameof(value));
359352
}
360353
_rawData.Add(new Asn1UniversalString(value).GetRawDataAsMemory());
@@ -371,7 +364,7 @@ public Asn1Builder AddUniversalString(String value) {
371364
/// </exception>
372365
/// <returns>Current instance with added value.</returns>
373366
public Asn1Builder AddBMPString(String value) {
374-
if (value == null) {
367+
if (value is null) {
375368
throw new ArgumentNullException(nameof(value));
376369
}
377370
_rawData.Add(new Asn1BMPString(value).GetRawDataAsMemory());
@@ -424,9 +417,6 @@ public Asn1Builder AddDerData(ReadOnlyMemory<Byte> value, Byte outerTag) {
424417
/// <param name="mustEncode">
425418
/// Specifies if data in <strong>value</strong> parameter must be encoded or not. See Remarks for more details.
426419
/// </param>
427-
/// <exception cref="ArgumentNullException">
428-
/// <strong>value</strong> parameter is null.
429-
/// </exception>
430420
/// <exception cref="InvalidDataException">
431421
/// <strong>value</strong> is not encoded.
432422
/// </exception>
@@ -440,9 +430,6 @@ public Asn1Builder AddDerData(ReadOnlyMemory<Byte> value, Byte outerTag) {
440430
/// <strong>value</strong> parameter is untagged, an exception will be thrown.
441431
/// </remarks>
442432
public Asn1Builder AddImplicit(Byte implicitTag, ReadOnlySpan<Byte> value, Boolean mustEncode) {
443-
if (value == null) {
444-
throw new ArgumentNullException(nameof(value));
445-
}
446433
if (mustEncode) {
447434
_rawData.Add(Asn1Utils.Encode(value, (Byte)(0x80 + implicitTag)));
448435
} else {
@@ -469,9 +456,6 @@ public Asn1Builder AddImplicit(Byte implicitTag, ReadOnlySpan<Byte> value, Boole
469456
/// <param name="mustEncode">
470457
/// Specifies if data in <strong>value</strong> parameter must be encoded or not. See Remarks for more details.
471458
/// </param>
472-
/// <exception cref="ArgumentNullException">
473-
/// <strong>value</strong> parameter is null.
474-
/// </exception>
475459
/// <exception cref="InvalidDataException">
476460
/// <strong>value</strong> is not encoded.
477461
/// </exception>
@@ -484,9 +468,6 @@ public Asn1Builder AddImplicit(Byte implicitTag, ReadOnlySpan<Byte> value, Boole
484468
/// is untagged, invalid type will be produced.
485469
/// </remarks>
486470
public Asn1Builder AddExplicit(Byte explicitTag, ReadOnlySpan<Byte> value, Boolean mustEncode) {
487-
if (value == null) {
488-
throw new ArgumentNullException(nameof(value));
489-
}
490471
if (mustEncode) {
491472
_rawData.Add(Asn1Utils.Encode(value, (Byte)(0xa0 + explicitTag)));
492473
} else {
@@ -510,7 +491,7 @@ public Asn1Builder AddExplicit(Byte explicitTag, ReadOnlySpan<Byte> value, Boole
510491
/// In the current implementation, constructed BIT_STRING is encoded using primitive form.
511492
/// </remarks>
512493
public Asn1Builder AddBitString(Func<Asn1Builder, Asn1Builder> selector) {
513-
if (selector == null) {
494+
if (selector is null) {
514495
throw new ArgumentNullException(nameof(selector));
515496
}
516497
Asn1Builder b = selector(new Asn1Builder());
@@ -529,7 +510,7 @@ public Asn1Builder AddBitString(Func<Asn1Builder, Asn1Builder> selector) {
529510
/// In the current implementation, constructed OCTET_STRING is encoded using primitive form.
530511
/// </remarks>
531512
public Asn1Builder AddOctetString(Func<Asn1Builder, Asn1Builder> selector) {
532-
if (selector == null) {
513+
if (selector is null) {
533514
throw new ArgumentNullException(nameof(selector));
534515
}
535516
Asn1Builder b = selector(new Asn1Builder());
@@ -545,7 +526,7 @@ public Asn1Builder AddOctetString(Func<Asn1Builder, Asn1Builder> selector) {
545526
/// </exception>
546527
/// <returns>Current instance with added value.</returns>
547528
public Asn1Builder AddSequence(Func<Asn1Builder, Asn1Builder> selector) {
548-
if (selector == null) {
529+
if (selector is null) {
549530
throw new ArgumentNullException(nameof(selector));
550531
}
551532
Asn1Builder b = selector(new Asn1Builder());
@@ -561,7 +542,7 @@ public Asn1Builder AddSequence(Func<Asn1Builder, Asn1Builder> selector) {
561542
/// </exception>
562543
/// <returns>Current instance with added value.</returns>
563544
public Asn1Builder AddSet(Func<Asn1Builder, Asn1Builder> selector) {
564-
if (selector == null) {
545+
if (selector is null) {
565546
throw new ArgumentNullException(nameof(selector));
566547
}
567548
Asn1Builder b = selector(new Asn1Builder());
@@ -580,7 +561,7 @@ public Asn1Builder AddSet(Func<Asn1Builder, Asn1Builder> selector) {
580561
/// </exception>
581562
/// <returns>Current instance with added value.</returns>
582563
public Asn1Builder AddExplicit(Byte explicitTag, Func<Asn1Builder, Asn1Builder> selector) {
583-
if (selector == null) {
564+
if (selector is null) {
584565
throw new ArgumentNullException(nameof(selector));
585566
}
586567
Asn1Builder b = selector(new Asn1Builder());

Asn1Parser/Asn1Reader.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Int64 calculatePredictLength(Int64 offset) {
297297
return pPayloadLength + lengthBytes + 2;
298298
}
299299
void moveAndExpectTypes(Func<Boolean> action, params Byte[] expectedTypes) {
300-
if (expectedTypes == null) {
300+
if (expectedTypes is null) {
301301
throw new ArgumentNullException(nameof(expectedTypes));
302302
}
303303
var set = new HashSet<Byte>();
@@ -537,9 +537,6 @@ public void MoveNextSiblingAndExpectTags(params Asn1Type[] expectedTags) {
537537
/// method calls are not necessary.
538538
/// </remarks>
539539
public Boolean Seek(Int32 newPosition) {
540-
if (_offsetMap == null) {
541-
throw new InvalidOperationException();
542-
}
543540
if (!_offsetMap.TryGetValue(newPosition, out AsnInternalMap value)) {
544541
return false;
545542
}

Asn1Parser/Asn1Utils.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,11 @@ public static Byte[] GetLengthBytes(Int32 payloadLength) {
5151
/// Calculates the ASN.1 payload length from a given ASN.1 length header.
5252
/// </summary>
5353
/// <param name="asnHeader">A byte array that represents ASN.1 length header</param>
54-
/// <exception cref="ArgumentNullException">
55-
/// <strong>asnHeader</strong> parameter is null.
56-
/// </exception>
5754
/// <exception cref="OverflowException">
5855
/// <strong>asnHeader</strong> parameter length is more than 4 bytes or is invalid value.
5956
/// </exception>
6057
/// <returns>ASN.1 payload length in bytes.</returns>
6158
public static Int64 CalculatePayloadLength(ReadOnlySpan<Byte> asnHeader) {
62-
if (asnHeader == null) {
63-
throw new ArgumentNullException(nameof(asnHeader));
64-
}
6559
if (asnHeader.Length == 0) {
6660
return 0;
6761
}
@@ -113,7 +107,7 @@ public static Byte[] Encode(Byte[] rawData, Byte enclosingTag) {
113107
/// <remarks>If <strong>rawData</strong> is null, an empty tag is encoded.</remarks>
114108
public static ReadOnlyMemory<Byte> Encode(ReadOnlySpan<Byte> rawData, Byte enclosingTag) {
115109
Byte[] retValue;
116-
if (rawData == null) {
110+
if (rawData.Length == 0) {
117111
retValue = [enclosingTag, 0];
118112

119113
return retValue;

Asn1Parser/AsnFormatter.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static String BinaryToString(ReadOnlySpan<Byte> rawData, EncodingType enc
8181
/// </list>
8282
/// </remarks>
8383
public static String BinaryToString(Asn1Reader asn, EncodingType encoding = EncodingType.HexRaw, EncodingFormat format = EncodingFormat.CRLF, Boolean forceUpperCase = false) {
84-
if (asn == null) {
84+
if (asn is null) {
8585
throw new ArgumentNullException(nameof(asn));
8686
}
8787

@@ -127,7 +127,7 @@ public static Byte[] StringToBinary(String input, EncodingType encoding = Encodi
127127
}
128128

129129

130-
if (rawData == null) {
130+
if (rawData is null) {
131131
throw new InvalidDataException("The data is invalid.");
132132
}
133133
return rawData;
@@ -143,31 +143,31 @@ public static EncodingType TestInputString(String input) {
143143
Byte[]? rawBytes;
144144
foreach (PemHeader pemHeader in PemHeader.GetPemHeaders()) {
145145
rawBytes = StringToBinaryFormatter.FromBase64Header(input, pemHeader.GetHeader(), pemHeader.GetFooter());
146-
if (rawBytes != null) {
146+
if (rawBytes is not null) {
147147
return pemHeader.Encoding;
148148
}
149149
}
150150
rawBytes = StringToBinaryFormatter.FromBase64Header(input);
151-
if (rawBytes != null) {
151+
if (rawBytes is not null) {
152152
return EncodingType.Base64Header;
153153
}
154154
rawBytes = StringToBinaryFormatter.FromBase64(input);
155-
if (rawBytes != null) {
155+
if (rawBytes is not null) {
156156
return EncodingType.Base64;
157157
}
158158
rawBytes = StringToBinaryFormatter.FromHexAddr(input);
159-
if (rawBytes != null) {
159+
if (rawBytes is not null) {
160160
return EncodingType.HexAddress;
161161
}
162162
rawBytes = StringToBinaryFormatter.FromHexAddrAscii(input);
163-
if (rawBytes != null) {
163+
if (rawBytes is not null) {
164164
return EncodingType.HexAsciiAddress;
165165
}
166166
rawBytes = StringToBinaryFormatter.FromHex(input);
167-
if (rawBytes != null) {
167+
if (rawBytes is not null) {
168168
return EncodingType.Hex;
169169
}
170170
rawBytes = StringToBinaryFormatter.FromHexAscii(input);
171-
return rawBytes != null ? EncodingType.HexAscii : EncodingType.Binary;
171+
return rawBytes is not null ? EncodingType.HexAscii : EncodingType.Binary;
172172
}
173173
}

Asn1Parser/Universal/Asn1BitString.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ public Asn1BitString(ReadOnlyMemory<Byte> rawData) : this(new Asn1Reader(rawData
3838
/// <param name="calculateUnusedBits">
3939
/// <strong>True</strong> if the bit length is decremented to exclude trailing zero bits. Otherwise <strong>False</strong>.
4040
/// </param>
41-
/// <exception cref="ArgumentNullException"><strong>valueToEncode</strong> parameter is null reference.</exception>
4241
public Asn1BitString(ReadOnlySpan<Byte> valueToEncode, Boolean calculateUnusedBits) : base(TYPE) {
43-
if (valueToEncode == null) {
44-
throw new ArgumentNullException(nameof(valueToEncode));
45-
}
4642
m_encode(valueToEncode, calculateUnusedBits, 0);
4743
}
4844
/// <summary>
@@ -51,9 +47,7 @@ public Asn1BitString(ReadOnlySpan<Byte> valueToEncode, Boolean calculateUnusedBi
5147
/// </summary>
5248
/// <param name="valueToEncode">Raw value to encode.</param>
5349
/// <param name="unusedBits">A number of unused bits in bit string.</param>
54-
/// <exception cref="ArgumentNullException"><strong>valueToEncode</strong> parameter is null reference.</exception>
5550
public Asn1BitString(ReadOnlySpan<Byte> valueToEncode, Byte unusedBits) : base(TYPE) {
56-
if (valueToEncode == null) { throw new ArgumentNullException(nameof(valueToEncode)); }
5751
m_encode(valueToEncode, false, unusedBits);
5852
}
5953

@@ -105,10 +99,6 @@ public ReadOnlyMemory<Byte> GetValue() {
10599
/// <returns>The number of unused bits.</returns>
106100
/// <exception cref="ArgumentNullException"><strong>bytes</strong> parameter is null reference.</exception>
107101
public static Byte CalculateUnusedBits(ReadOnlySpan<Byte> bytes) {
108-
if (bytes == null) {
109-
throw new ArgumentNullException(nameof(bytes));
110-
}
111-
112102
return CalculateUnusedBits(bytes[bytes.Length - 1]); // calculate unused bits based on last byte.
113103
}
114104
/// <summary>

Asn1Parser/Universal/Asn1DateTime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected Asn1DateTime(Asn1Type type, DateTime time, TimeZoneInfo? zone = null,
4343

4444
void m_encode(Asn1Type type, DateTime time, TimeZoneInfo? zone, Boolean preciseTime) {
4545
zone = DateTimeUtils.CoerceTimeZone(zone);
46-
time = zone == null
46+
time = zone is null
4747
? DateTime.SpecifyKind(time, DateTimeKind.Local)
4848
: TimeZoneInfo.ConvertTimeToUtc(time, zone).ToLocalTime();
4949
Value = time;

Asn1Parser/Universal/Asn1ObjectIdentifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Asn1ObjectIdentifier(String oid) : this(new Oid(oid)) { }
5050
/// <exception cref="InvalidDataException">The string is not valid object identifier.</exception>
5151
/// <exception cref="OverflowException">The string is too large.</exception>
5252
public Asn1ObjectIdentifier(Oid oid) : base(TYPE) {
53-
if (oid == null) {
53+
if (oid is null) {
5454
throw new ArgumentNullException(nameof(oid));
5555
}
5656
m_encode(oid);

Asn1Parser/Universal/Asn1RelativeOid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Asn1RelativeOid(ReadOnlyMemory<Byte> rawData) : this(new Asn1Reader(rawDa
4141
/// <exception cref="OverflowException">The string is too large.</exception>
4242
/// <remarks>Maximum relative object identifier string is 8kb.</remarks>
4343
public Asn1RelativeOid(String relativeOid) : base(TYPE) {
44-
if (relativeOid == null) {
44+
if (relativeOid is null) {
4545
throw new ArgumentNullException(nameof(relativeOid));
4646
}
4747
m_encode(relativeOid);

Asn1Parser/Universal/Asn1String.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static Asn1String DecodeAnyString(ReadOnlyMemory<Byte> rawData, IEnumerab
8989
}
9090

9191
IEnumerable<Asn1Type> asn1Types = allowedStringTypes?.ToList();
92-
if (asn1Types != null && !asn1Types.Contains((Asn1Type)rawData.Span[0])) {
92+
if (asn1Types is not null && !asn1Types.Contains((Asn1Type)rawData.Span[0])) {
9393
throw new ArgumentException("Input string is not permitted by restriction.");
9494
}
9595
var tag = (Asn1Type)(rawData.Span[0] & (Int32)Asn1Type.TAG_MASK);

0 commit comments

Comments
 (0)