Skip to content

Commit

Permalink
Add ArgumentException.ThrowIfNullOrWhiteSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed May 10, 2023
1 parent 52b81af commit 6a1af9e
Show file tree
Hide file tree
Showing 33 changed files with 105 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDis

internal static void ValidateSubprotocol(string subProtocol)
{
if (string.IsNullOrWhiteSpace(subProtocol))
{
throw new ArgumentException(SR.net_WebSockets_InvalidEmptySubProtocol, nameof(subProtocol));
}
ArgumentException.ThrowIfNullOrWhiteSpace(subProtocol);

int indexOfInvalidChar = subProtocol.AsSpan().IndexOfAnyExcept(s_validSubprotocolChars);
if (indexOfInvalidChar >= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
<data name="AllowedValuesAttribute_Invalid" xml:space="preserve">
<value>The {0} field does not equal any of the values specified in AllowedValuesAttribute.</value>
</data>
<data name="ArgumentIsNullOrWhitespace" xml:space="preserve">
<value>The argument '{0}' cannot be null, empty or contain only whitespace.</value>
</data>
<data name="AssociatedMetadataTypeTypeDescriptor_MetadataTypeContainsUnknownProperties" xml:space="preserve">
<value>The associated metadata type for type '{0}' contains the following unknown properties or fields: {1}. Please make sure that the names of these members match the names of the properties on the main type.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public ColumnAttribute()
/// <param name="name">The name of the column the property is mapped to.</param>
public ColumnAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name));
}

ArgumentException.ThrowIfNullOrWhiteSpace(name);
Name = name;
}

Expand Down Expand Up @@ -64,11 +60,7 @@ public string? TypeName
get => _typeName;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(value)), nameof(value));
}

ArgumentException.ThrowIfNullOrWhiteSpace(value);
_typeName = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public class ForeignKeyAttribute : Attribute
/// </param>
public ForeignKeyAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name));
}

ArgumentException.ThrowIfNullOrWhiteSpace(name);
Name = name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public class InversePropertyAttribute : Attribute
/// <param name="property">The navigation property representing the other end of the same relationship.</param>
public InversePropertyAttribute(string property)
{
if (string.IsNullOrWhiteSpace(property))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(property)), nameof(property));
}

ArgumentException.ThrowIfNullOrWhiteSpace(property);
Property = property;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ public class TableAttribute : Attribute
/// <param name="name">The name of the table the class is mapped to.</param>
public TableAttribute(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name));
}

ArgumentException.ThrowIfNullOrWhiteSpace(name);
Name = name;
}

Expand All @@ -42,11 +38,7 @@ public string? Schema
get => _schema;
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(value)), nameof(value));
}

ArgumentException.ThrowIfNullOrWhiteSpace(value);
_schema = value;
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Net.Http/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@
<data name="net_http_content_read_as_stream_has_task" xml:space="preserve">
<value>The content's stream has already been retrieved via async ReadAsStreamAsync and cannot be subsequently accessed synchronously.</value>
</data>
<data name="net_http_argument_empty_string" xml:space="preserve">
<value>The value cannot be null or empty.</value>
</data>
<data name="net_http_client_request_already_sent" xml:space="preserve">
<value>The request message was already sent. Cannot send the same request message multiple times.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AuthenticationHeaderValue(string scheme)

public AuthenticationHeaderValue(string scheme, string? parameter)
{
HeaderUtilities.CheckValidToken(scheme, nameof(scheme));
HeaderUtilities.CheckValidToken(scheme);
HttpHeaders.CheckContainsNewLine(parameter);
_scheme = scheme;
_parameter = parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ object ICloneable.Clone()

private sealed class TokenObjectCollection : ObjectCollection<string>
{
public override void Validate(string item) => HeaderUtilities.CheckValidToken(item, nameof(item));
public override void Validate(string item) => HeaderUtilities.CheckValidToken(item);

public int GetHashCode(StringComparer comparer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Net.Http.Headers
Expand Down Expand Up @@ -34,7 +35,7 @@ public string DispositionType
get { return _dispositionType; }
set
{
CheckDispositionTypeFormat(value, nameof(value));
CheckDispositionTypeFormat(value);
_dispositionType = value;
}
}
Expand Down Expand Up @@ -139,7 +140,7 @@ protected ContentDispositionHeaderValue(ContentDispositionHeaderValue source)

public ContentDispositionHeaderValue(string dispositionType)
{
CheckDispositionTypeFormat(dispositionType, nameof(dispositionType));
CheckDispositionTypeFormat(dispositionType);
_dispositionType = dispositionType;
}

Expand Down Expand Up @@ -270,12 +271,9 @@ private static int GetDispositionTypeExpressionLength(string input, int startInd
return typeLength;
}

private static void CheckDispositionTypeFormat(string dispositionType, string parameterName)
private static void CheckDispositionTypeFormat(string dispositionType, [CallerArgumentExpression(nameof(dispositionType))] string? parameterName = null)
{
if (string.IsNullOrEmpty(dispositionType))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(dispositionType, parameterName);

// When adding values using strongly typed objects, no leading/trailing LWS (whitespace) are allowed.
int dispositionTypeLength = GetDispositionTypeExpressionLength(dispositionType, 0, out string? tempDispositionType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public string Unit
get { return _unit; }
set
{
HeaderUtilities.CheckValidToken(value, nameof(value));
HeaderUtilities.CheckValidToken(value);
_unit = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public EntityTagHeaderValue(string tag)

public EntityTagHeaderValue(string tag, bool isWeak)
{
if (string.IsNullOrEmpty(tag))
{
throw new ArgumentException(SR.net_http_argument_empty_string, nameof(tag));
}
ArgumentException.ThrowIfNullOrWhiteSpace(tag);

int length;
if ((HttpRuleParser.GetQuotedStringLength(tag, 0, out length) != HttpParseResult.Parsed) ||
(length != tag.Length))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Net.Http.Headers
Expand Down Expand Up @@ -139,25 +140,19 @@ private static void AddHexEscaped(byte c, ref ValueStringBuilder destination)
return null;
}

internal static void CheckValidToken(string value, string parameterName)
internal static void CheckValidToken(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName);

if (HttpRuleParser.GetTokenLength(value, 0) != value.Length)
{
throw new FormatException(SR.Format(CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, value));
}
}

internal static void CheckValidComment(string value, string parameterName)
internal static void CheckValidComment(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName);

int length;
if ((HttpRuleParser.GetCommentLength(value, 0, out length) != HttpParseResult.Parsed) ||
Expand All @@ -167,12 +162,9 @@ internal static void CheckValidComment(string value, string parameterName)
}
}

internal static void CheckValidQuotedString(string value, string parameterName)
internal static void CheckValidQuotedString(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName);

int length;
if ((HttpRuleParser.GetQuotedStringLength(value, 0, out length) != HttpParseResult.Parsed) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,7 @@ private static void ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreIte

private HeaderDescriptor GetHeaderDescriptor(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(SR.net_http_argument_empty_string, nameof(name));
}
ArgumentException.ThrowIfNullOrWhiteSpace(name);

if (!HeaderDescriptor.TryGet(name, out HeaderDescriptor descriptor))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
using static System.HexConverter;

Expand Down Expand Up @@ -68,7 +69,7 @@ public string? MediaType
get { return _mediaType; }
set
{
CheckMediaTypeFormat(value, nameof(value));
CheckMediaTypeFormat(value);
_mediaType = value;
}
}
Expand Down Expand Up @@ -100,7 +101,7 @@ public MediaTypeHeaderValue(string mediaType)
/// <param name="charSet">The value to use for the character set.</param>
public MediaTypeHeaderValue(string mediaType, string? charSet)
{
CheckMediaTypeFormat(mediaType, nameof(mediaType));
CheckMediaTypeFormat(mediaType);
_mediaType = mediaType;

if (!string.IsNullOrEmpty(charSet))
Expand Down Expand Up @@ -272,12 +273,9 @@ private static int GetMediaTypeExpressionLength(string input, int startIndex, ou
return mediaTypeLength;
}

private static void CheckMediaTypeFormat(string mediaType, string parameterName)
private static void CheckMediaTypeFormat(string mediaType, [CallerArgumentExpression(nameof(mediaType))] string? parameterName = null)
{
if (string.IsNullOrEmpty(mediaType))
{
throw new ArgumentException(SR.net_http_argument_empty_string, parameterName);
}
ArgumentException.ThrowIfNullOrWhiteSpace(mediaType, parameterName);

// When adding values using strongly typed objects, no leading/trailing LWS (whitespace) are allowed.
// Also no LWS between type and subtype are allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ internal static int GetValueLength(string input, int startIndex)

private static void CheckNameValueFormat(string name, string? value)
{
HeaderUtilities.CheckValidToken(name, nameof(name));
HeaderUtilities.CheckValidToken(name);
CheckValueFormat(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public ProductHeaderValue(string name)

public ProductHeaderValue(string name, string? version)
{
HeaderUtilities.CheckValidToken(name, nameof(name));
HeaderUtilities.CheckValidToken(name);

if (!string.IsNullOrEmpty(version))
{
HeaderUtilities.CheckValidToken(version, nameof(version));
HeaderUtilities.CheckValidToken(version);
_version = version;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ProductInfoHeaderValue(ProductHeaderValue product)

public ProductInfoHeaderValue(string comment)
{
HeaderUtilities.CheckValidComment(comment, nameof(comment));
HeaderUtilities.CheckValidComment(comment);
_comment = comment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public string Unit
get { return _unit; }
set
{
HeaderUtilities.CheckValidToken(value, nameof(value));
HeaderUtilities.CheckValidToken(value);
_unit = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class StringWithQualityHeaderValue : ICloneable

public StringWithQualityHeaderValue(string value)
{
HeaderUtilities.CheckValidToken(value, nameof(value));
HeaderUtilities.CheckValidToken(value);

_value = value;
_quality = NotSetSentinel;
}

public StringWithQualityHeaderValue(string value, double quality)
{
HeaderUtilities.CheckValidToken(value, nameof(value));
HeaderUtilities.CheckValidToken(value);

ArgumentOutOfRangeException.ThrowIfNegative(quality);
ArgumentOutOfRangeException.ThrowIfGreaterThan(quality, 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected TransferCodingHeaderValue(TransferCodingHeaderValue source)

public TransferCodingHeaderValue(string value)
{
HeaderUtilities.CheckValidToken(value, nameof(value));
HeaderUtilities.CheckValidToken(value);
_value = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ public ViaHeaderValue(string protocolVersion, string receivedBy, string? protoco

public ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName, string? comment)
{
HeaderUtilities.CheckValidToken(protocolVersion, nameof(protocolVersion));
HeaderUtilities.CheckValidToken(protocolVersion);
CheckReceivedBy(receivedBy);

if (!string.IsNullOrEmpty(protocolName))
{
HeaderUtilities.CheckValidToken(protocolName, nameof(protocolName));
HeaderUtilities.CheckValidToken(protocolName);
_protocolName = protocolName;
}

if (!string.IsNullOrEmpty(comment))
{
HeaderUtilities.CheckValidComment(comment, nameof(comment));
HeaderUtilities.CheckValidComment(comment);
_comment = comment;
}

Expand Down Expand Up @@ -275,10 +275,7 @@ object ICloneable.Clone()

private static void CheckReceivedBy(string receivedBy)
{
if (string.IsNullOrEmpty(receivedBy))
{
throw new ArgumentException(SR.net_http_argument_empty_string, nameof(receivedBy));
}
ArgumentException.ThrowIfNullOrWhiteSpace(receivedBy);

// 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value
// is a valid host.;
Expand Down
Loading

0 comments on commit 6a1af9e

Please sign in to comment.