Skip to content

Commit

Permalink
Use string.IsNullOrEmpty and ArgumentException.ThrowIfNullOrEmpty in …
Browse files Browse the repository at this point in the history
…many more places (#85858)
  • Loading branch information
stephentoub committed May 9, 2023
1 parent 7f35658 commit c0bb818
Show file tree
Hide file tree
Showing 150 changed files with 344 additions and 479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,7 @@ public bool VerifyString(string input, out int testPosition, out MaskedTextResul
{
testPosition = 0;

if (input == null || input.Length == 0) // nothing to verify.
if (string.IsNullOrEmpty(input)) // nothing to verify.
{
resultHint = MaskedTextResultHint.NoEffect;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public virtual PropertyDescriptorCollection GetChildProperties(object? instance,
protected Type? GetTypeFromName(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] string? typeName)
{
if (typeName == null || typeName.Length == 0)
if (string.IsNullOrEmpty(typeName))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ public static EventDescriptorCollection GetEvents(object component, Attribute[]?
name = component.Site.Name;
}

if (name == null || name.Length == 0)
if (string.IsNullOrEmpty(name))
{
int ci = System.Threading.Interlocked.Increment(ref s_collisionIndex) - 1;
name = ci.ToString(CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute?
{ // this means type implements IXmlSerializable
Type? type = null;
string? typeName = xmlReader.GetAttribute(Keywords.MSD_INSTANCETYPE, Keywords.MSDNS);
if (typeName == null || typeName.Length == 0)
if (string.IsNullOrEmpty(typeName))
{ // No CDT polumorphism
string? xsdTypeName = xmlReader.GetAttribute(Keywords.TYPE, Keywords.XSINS); // this xsd type: Base type polymorphism
if (null != xsdTypeName && xsdTypeName.Length > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal void SetColumnError(DataColumn column, string? error)
{
Debug.Assert(column != null, "Invalid (null) argument");
Debug.Assert(column.Table != null, "Invalid (loose) column");
if (error == null || error.Length == 0)
if (string.IsNullOrEmpty(error))
{
// remove error from the collection
Clear(column);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public string DataSetName
DataCommonEventSource.Log.Trace("<ds.DataSet.set_DataSetName|API> {0}, '{1}'", ObjectID, value);
if (value != _dataSetName)
{
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
{
throw ExceptionBuilder.SetDataSetNameToEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ internal XmlNode ToNode(XmlDocument dc, Hashtable prefixes, bool inRemoting)
{
XmlElement typeNode = dc.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_SIMPLETYPE, Keywords.XSDNS);

if (_name != null && _name.Length != 0)
if (!string.IsNullOrEmpty(_name))
{
// this is a global type
typeNode.SetAttribute(Keywords.NAME, _name);
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/System.Data.Common/src/System/Data/XDRSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal void LoadSchema(XmlElement schemaRoot, DataSet ds)

// Get Locale and CaseSensitive properties

if (_schemaName == null || _schemaName.Length == 0)
if (string.IsNullOrEmpty(_schemaName))
_schemaName = "NewDataSet";

ds.Namespace = _schemaUri;
Expand Down Expand Up @@ -85,7 +85,7 @@ internal void LoadSchema(XmlElement schemaRoot, DataSet ds)
if (FEqualIdentity(node, Keywords.XDR_ELEMENT, Keywords.XDRNS) ||
FEqualIdentity(node, Keywords.XDR_ATTRIBUTE, Keywords.XDRNS))
{
if (strType == null || strType.Length == 0)
if (string.IsNullOrEmpty(strType))
return null;

// Find an ELEMENTTYPE or ATTRIBUTETYPE with name=strType
Expand Down Expand Up @@ -133,7 +133,7 @@ internal static bool IsTextOnlyContent(XmlElement node)
Debug.Assert(FEqualIdentity(node, Keywords.XDR_ELEMENTTYPE, Keywords.XDRNS), $"Invalid node type {node.LocalName}");

string value = node.GetAttribute(Keywords.CONTENT);
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
{
string type = node.GetAttribute(Keywords.DT_TYPE, Keywords.DTNS);
return !string.IsNullOrEmpty(type);
Expand Down Expand Up @@ -311,7 +311,7 @@ private static Type ParseDataType(string dt, string dtValues)
}

NameType nt = FindNameType(strType);
if (nt == s_enumerationNameType && (dtValues == null || dtValues.Length == 0))
if (nt == s_enumerationNameType && string.IsNullOrEmpty(dtValues))
throw ExceptionBuilder.MissingAttribute("type", Keywords.DT_VALUES);
return nt.type;
}
Expand All @@ -332,7 +332,7 @@ internal static string GetInstanceName(XmlElement node)
else
{
instanceName = node.GetAttribute(Keywords.TYPE);
if (instanceName == null || instanceName.Length == 0)
if (string.IsNullOrEmpty(instanceName))
throw ExceptionBuilder.MissingAttribute("Element", Keywords.TYPE);
}

Expand Down Expand Up @@ -400,7 +400,7 @@ internal void HandleColumn(XmlElement node, DataTable table)

strType = typeNode.GetAttribute(Keywords.DT_TYPE, Keywords.DTNS);
strValues = typeNode.GetAttribute(Keywords.DT_VALUES, Keywords.DTNS);
if (strType == null || strType.Length == 0)
if (string.IsNullOrEmpty(strType))
{
strType = string.Empty;
type = typeof(string);
Expand Down Expand Up @@ -480,7 +480,7 @@ internal void HandleColumn(XmlElement node, DataTable table)
column.Namespace = targetNamespace;

table.Columns.Add(column);
if (strDefault != null && strDefault.Length != 0)
if (!string.IsNullOrEmpty(strDefault))
try
{
column.DefaultValue = SqlConvert.ChangeTypeForXML(strDefault, type);
Expand Down
38 changes: 19 additions & 19 deletions src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal static bool FEqualIdentity(XmlNode? node, string name, string ns)
internal static bool GetBooleanAttribute(XmlElement element, string attrName, string attrNS, bool defVal)
{
string value = element.GetAttribute(attrName, attrNS);
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
{
return defVal;
}
Expand Down Expand Up @@ -447,22 +447,22 @@ internal void HandleRelation(XmlElement node, bool fNested)
}

parentName = node.GetAttribute(Keywords.MSD_PARENT, Keywords.MSDNS);
if (parentName == null || parentName.Length == 0)
if (string.IsNullOrEmpty(parentName))
throw ExceptionBuilder.RelationParentNameMissing(strName);
parentName = XmlConvert.DecodeName(parentName);

childName = node.GetAttribute(Keywords.MSD_CHILD, Keywords.MSDNS);
if (childName == null || childName.Length == 0)
if (string.IsNullOrEmpty(childName))
throw ExceptionBuilder.RelationChildNameMissing(strName);
childName = XmlConvert.DecodeName(childName);

value = node.GetAttribute(Keywords.MSD_PARENTKEY, Keywords.MSDNS);
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
throw ExceptionBuilder.RelationTableKeyMissing(strName);

parentNames = value.TrimEnd(null).Split(new char[] { Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP });
value = node.GetAttribute(Keywords.MSD_CHILDKEY, Keywords.MSDNS);
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
throw ExceptionBuilder.RelationChildKeyMissing(strName);

childNames = value.TrimEnd(null).Split(new char[] { Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP });
Expand Down Expand Up @@ -666,7 +666,7 @@ public void LoadSchema(XmlSchemaSet schemaSet, DataSet ds)
{
_schemaName = schemaRoot.Id;

if (_schemaName == null || _schemaName.Length == 0)
if (string.IsNullOrEmpty(_schemaName))
{
_schemaName = "NewDataSet";
}
Expand Down Expand Up @@ -1208,7 +1208,7 @@ internal static DataColumn[] BuildKey(XmlSchemaIdentityConstraint keyNode, DataT
internal static bool GetBooleanAttribute(XmlSchemaAnnotated element, string attrName, bool defVal)
{
string? value = GetMsdataAttribute(element, attrName);
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
{
return defVal;
}
Expand All @@ -1227,7 +1227,7 @@ internal static bool GetBooleanAttribute(XmlSchemaAnnotated element, string attr
internal static string GetStringAttribute(XmlSchemaAnnotated element, string attrName, string defVal)
{
string? value = GetMsdataAttribute(element, attrName);
if (value == null || value.Length == 0)
if (string.IsNullOrEmpty(value))
{
return defVal;
}
Expand Down Expand Up @@ -1287,7 +1287,7 @@ internal void HandleKeyref(XmlSchemaKeyref keyref)
if (table == null)
return;

if (refer == null || refer.Length == 0)
if (string.IsNullOrEmpty(refer))
throw ExceptionBuilder.MissingRefer(name);

ConstraintTable? key = (ConstraintTable?)_constraintNodes![refer];
Expand Down Expand Up @@ -1321,7 +1321,7 @@ internal void HandleKeyref(XmlSchemaKeyref keyref)
{
string relName = XmlConvert.DecodeName(GetStringAttribute(keyref, Keywords.MSD_RELATIONNAME, keyref.Name!));

if (relName == null || relName.Length == 0)
if (string.IsNullOrEmpty(relName))
relName = name;

int iExisting = fKey[0].Table!.DataSet!.Relations.InternalIndexOf(relName);
Expand Down Expand Up @@ -1383,7 +1383,7 @@ internal void HandleConstraint(XmlSchemaIdentityConstraint keyNode)
string? name;

name = XmlConvert.DecodeName(keyNode.Name);
if (name == null || name.Length == 0)
if (string.IsNullOrEmpty(name))
throw ExceptionBuilder.MissingAttribute(Keywords.NAME);

if (_constraintNodes!.ContainsKey(name))
Expand Down Expand Up @@ -1930,10 +1930,10 @@ internal static bool IsXsdType(string name)
if (_typeNs == Keywords.XSDNS)
return null;
XmlSchemaAnnotated? typeNode;
if (_type == null || _type.Length == 0)
if (string.IsNullOrEmpty(_type))
{
_type = isAttr ? attr!.RefName.Name : el!.RefName.Name;
if (_type == null || _type.Length == 0)
if (string.IsNullOrEmpty(_type))
typeNode = isAttr ? attr!.SchemaType : el!.SchemaType;
else
typeNode = isAttr ? FindTypeNode((XmlSchemaAnnotated)_attributes![attr!.RefName]!) : FindTypeNode((XmlSchemaAnnotated)_elementsTable![el!.RefName]!);
Expand All @@ -1956,7 +1956,7 @@ internal void HandleSimpleTypeSimpleContentColumn(XmlSchemaSimpleType typeNode,
SimpleType? xsdType = null;

// if (typeNode.QualifiedName.Namespace != Keywords.XSDNS) { // this means UDSimpleType
if (typeNode.QualifiedName.Name != null && typeNode.QualifiedName.Name.Length != 0 && typeNode.QualifiedName.Namespace != Keywords.XSDNS)
if (!string.IsNullOrEmpty(typeNode.QualifiedName.Name) && typeNode.QualifiedName.Namespace != Keywords.XSDNS)
{ // this means UDSimpleType
xsdType = new SimpleType(typeNode);
strType = typeNode.QualifiedName.ToString(); // use qualified name
Expand Down Expand Up @@ -2212,7 +2212,7 @@ internal void HandleAttributeColumn(XmlSchemaAttribute attrib, DataTable table,
{
XmlSchemaSimpleType node = (typeNode as XmlSchemaSimpleType)!;
xsdType = new SimpleType(node);
if (node.QualifiedName.Name != null && node.QualifiedName.Name.Length != 0 && node.QualifiedName.Namespace != Keywords.XSDNS)
if (!string.IsNullOrEmpty(node.QualifiedName.Name) && node.QualifiedName.Namespace != Keywords.XSDNS)
{
// this means UDSimpleType
strType = node.QualifiedName.ToString(); // use qualified name
Expand Down Expand Up @@ -2376,7 +2376,7 @@ internal void HandleElementColumn(XmlSchemaElement elem, DataTable table, bool i
xsdType = new SimpleType(simpleTypeNode!);
// ((XmlSchemaSimpleType)typeNode).Name != null && ((XmlSchemaSimpleType)typeNode).Name.Length != 0 check is for annonymos simple type,
// it should be user defined Named simple type
if (((XmlSchemaSimpleType)typeNode).Name != null && ((XmlSchemaSimpleType)typeNode).Name!.Length != 0 && ((XmlSchemaSimpleType)typeNode).QualifiedName.Namespace != Keywords.XSDNS)
if (!string.IsNullOrEmpty(((XmlSchemaSimpleType)typeNode).Name) && ((XmlSchemaSimpleType)typeNode).QualifiedName.Namespace != Keywords.XSDNS)
{
strType = ((XmlSchemaSimpleType)typeNode).QualifiedName.ToString(); // use qualified name
type = ParseDataType(strType);
Expand Down Expand Up @@ -2582,13 +2582,13 @@ internal void HandleDataSet(XmlSchemaElement node, bool isNewDataSet)

// reuse variable
value = GetMsdataAttribute(node, Keywords.MSD_DATASETNAME);
if (value != null && value.Length != 0)
if (!string.IsNullOrEmpty(value))
{
dsName = value;
}

value = GetMsdataAttribute(node, Keywords.MSD_DATASETNAMESPACE);
if (value != null && value.Length != 0)
if (!string.IsNullOrEmpty(value))
{
dsNamespace = value;
}
Expand All @@ -2597,7 +2597,7 @@ internal void HandleDataSet(XmlSchemaElement node, bool isNewDataSet)
SetExtProperties(_ds, node.UnhandledAttributes);


if (dsName != null && dsName.Length != 0)
if (!string.IsNullOrEmpty(dsName))
_ds.DataSetName = XmlConvert.DecodeName(dsName);

// _ds.Namespace = node.QualifiedName.Namespace;
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/System.Data.Common/src/System/Data/xmlsaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ internal void HandleColumnType(DataColumn col, XmlDocument dc, XmlElement root,
XmlNode type;
string? name = stNode.Name;

if (name != null && name.Length != 0)
if (!string.IsNullOrEmpty(name))
{
// For remoting, always need to work with root schema's namespace
string nSpace = (_schFormat != SchemaFormat.Remoting) ? stNode.Namespace :
Expand Down Expand Up @@ -1314,7 +1314,7 @@ internal void HandleColumnType(DataColumn col, XmlDocument dc, XmlElement root,
else
{
string typeName = XmlDataTypeName(col.DataType); // do not update the hashtable, as it will not write msdata:DataType
if (typeName == null || typeName.Length == 0)
if (string.IsNullOrEmpty(typeName))
{
if (col.DataType == typeof(Guid) || col.DataType == typeof(Type))
{
Expand Down Expand Up @@ -1812,7 +1812,7 @@ internal XmlElement HandleTable(DataTable table, XmlDocument dc, XmlElement sche
// the type for this element
DataColumn col = table.Columns[0];
string _typeName = XmlDataTypeName(col.DataType);
if (_typeName == null || _typeName.Length == 0)
if (string.IsNullOrEmpty(_typeName))
{
_typeName = Keywords.XSD_ANYTYPE;
}
Expand Down Expand Up @@ -2445,7 +2445,7 @@ private void GenerateTableErrors(DataTable table)
DataColumn column = table.Columns[colNum];
string error = row.GetColumnError(column);
string columnPrefix = (column.Namespace.Length != 0) ? column.Prefix : string.Empty;
if (error == null || error.Length == 0)
if (string.IsNullOrEmpty(error))
{
continue;
}
Expand Down Expand Up @@ -2770,7 +2770,7 @@ internal static bool RowHasErrors(DataRow row)
{
DataColumn column = row.Table.Columns[colNum];
string error = row.GetColumnError(column);
if (error == null || error.Length == 0)
if (string.IsNullOrEmpty(error))
{
continue;
}
Expand All @@ -2794,7 +2794,7 @@ internal void SaveDiffgramData(XmlWriter xw, Hashtable rowsOrder)

string prefix = (_ds != null) ? ((_ds.Namespace.Length == 0) ? "" : _ds.Prefix) : ((_dt!.Namespace.Length == 0) ? "" : _dt.Prefix);

if (_ds == null || _ds.DataSetName == null || _ds.DataSetName.Length == 0)
if (_ds == null || string.IsNullOrEmpty(_ds.DataSetName))
_xmlw.WriteStartElement(prefix, Keywords.DOCUMENTELEMENT, (_dt!.Namespace == null) ? "" : _dt.Namespace);
else
_xmlw.WriteStartElement(prefix, XmlConvert.EncodeLocalName(_ds.DataSetName), _ds.Namespace);
Expand Down Expand Up @@ -2852,7 +2852,7 @@ internal void Save(XmlWriter xw, bool writeSchema)
}
else
{
if (_ds.DataSetName == null || _ds.DataSetName.Length == 0)
if (string.IsNullOrEmpty(_ds.DataSetName))
_xmlw.WriteStartElement(prefix, Keywords.DOCUMENTELEMENT, _ds.Namespace);
else
_xmlw.WriteStartElement(prefix, XmlConvert.EncodeLocalName(_ds.DataSetName), _ds.Namespace);
Expand Down

0 comments on commit c0bb818

Please sign in to comment.