Change calls from string.IndexOf to string.Contains (CoreFX) (dotnet/corefx#32249)
authorGrant <grant@jesanna.com>
Thu, 13 Sep 2018 22:20:39 +0000 (15:20 -0700)
committerDan Moseley <danmose@microsoft.com>
Thu, 13 Sep 2018 22:20:39 +0000 (15:20 -0700)
* Update call sites

* More call sites

* Another callsite

* More call sites

* CR fixes - revert src/Common/src/CoreLib/*

* Spurious fix

* Revert string.Contains where platform may not be NetCore21

* nit

* More reverts

* More reverts

* More reverts

* Revert

* Remove comments

* Revert

Commit migrated from https://github.com/dotnet/corefx/commit/6ffca612323448f1488a6c63b86b69611b4286d9

36 files changed:
src/libraries/Common/src/Interop/Unix/System.Native/Interop.ReadDir.cs
src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs
src/libraries/Common/src/System/Data/Common/DbConnectionOptions.Common.cs
src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs
src/libraries/System.CodeDom/src/Microsoft/CSharp/CSharpCodeGenerator.cs
src/libraries/System.CodeDom/src/Microsoft/VisualBasic/VBCodeGenerator.cs
src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/EnumConverter.cs
src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/SyntaxCheck.cs
src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/BaseConfigurationRecord.cs
src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationProperty.cs
src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationSectionCollection.cs
src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationSectionGroupCollection.cs
src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/TypeUtil.cs
src/libraries/System.Data.Common/src/System/Data/Common/DBCommandBuilder.cs
src/libraries/System.Data.Common/src/System/Data/Common/DbConnectionOptions.cs
src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs
src/libraries/System.Data.Odbc/src/Common/System/Data/Common/DbConnectionOptions.cs
src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnectionString.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpSystemProxy.cs
src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpEndPointManager.cs
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/PhysicalAddress.cs
src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs
src/libraries/System.Net.WebHeaderCollection/src/System/Net/HeaderInfoTable.cs
src/libraries/System.Private.Uri/src/System/UriBuilder.cs
src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs
src/libraries/System.Private.Xml/src/System/Xml/Core/XsdCachingReader.cs
src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParser.cs
src/libraries/System.Private.Xml/src/System/Xml/Schema/DtdParserAsync.cs
src/libraries/System.Private.Xml/src/System/Xml/Schema/FacetChecker.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/SoapReflectionImporter.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlReflectionImporter.cs
src/libraries/System.Private.Xml/src/System/Xml/Serialization/Xmlcustomformatter.cs
src/libraries/System.Runtime.Extensions/src/System/Environment.Windows.cs
src/libraries/System.ServiceProcess.ServiceController/src/System/ServiceProcess/ServiceController.cs
src/libraries/System.Transactions.Local/src/System/Transactions/TransactionsEtwProvider.cs
src/libraries/System.Web.HttpUtility/src/System/Web/Util/HttpEncoderUtility.cs

index 695d50b..d64b804 100644 (file)
@@ -50,7 +50,7 @@ internal static partial class Interop
 
                 int charCount = Encoding.UTF8.GetChars(nameBytes, buffer);
                 ReadOnlySpan<char> value = buffer.Slice(0, charCount);
-                Debug.Assert(NameLength != -1 || value.IndexOf('\0') == -1, "should not have embedded nulls if we parsed the end of string");
+                Debug.Assert(NameLength != -1 || !value.Contains('\0'), "should not have embedded nulls if we parsed the end of string");
                 return value;
             }
         }
index 12b6173..3f43ebf 100644 (file)
@@ -268,7 +268,7 @@ namespace System.Runtime.Serialization
             }
 
             // Now see if we have some arity.  baseType could be null if this is an array type. 
-            if (_baseType != null && _baseType.IndexOf('`') != -1)
+            if (_baseType != null && _baseType.IndexOf('`') != -1) // string.Contains(char) is .NetCore2.1+ specific
             {
                 _needsFixup = false;
             }
index fd352bb..22eb17d 100644 (file)
@@ -407,6 +407,7 @@ namespace System.Data.Common
                 bool compValue = s_connectionStringValidValueRegex.IsMatch(keyvalue);
                 Debug.Assert((-1 == keyvalue.IndexOf('\u0000')) == compValue, "IsValueValid mismatch with regex");
 #endif
+                // string.Contains(char) is .NetCore2.1+ specific
                 return (-1 == keyvalue.IndexOf('\u0000'));
             }
             return true;
@@ -420,6 +421,7 @@ namespace System.Data.Common
                 bool compValue = s_connectionStringValidKeyRegex.IsMatch(keyname);
                 Debug.Assert(((0 < keyname.Length) && (';' != keyname[0]) && !Char.IsWhiteSpace(keyname[0]) && (-1 == keyname.IndexOf('\u0000'))) == compValue, "IsValueValid mismatch with regex");
 #endif
+                // string.Contains(char) is .NetCore2.1+ specific
                 return ((0 < keyname.Length) && (';' != keyname[0]) && !char.IsWhiteSpace(keyname[0]) && (-1 == keyname.IndexOf('\u0000')));
             }
             return false;
index bc14e4f..c0742a9 100644 (file)
@@ -634,6 +634,8 @@ namespace Microsoft.Win32
         private static string FixupName(string name)
         {
             Debug.Assert(name != null, "[FixupName]name!=null");
+
+            // string.Contains(char) is .NetCore2.1+ specific
             if (name.IndexOf('\\') == -1)
             {
                 return name;
index cc61543..93cbac4 100644 (file)
@@ -284,7 +284,7 @@ namespace Microsoft.CSharp
             // If the string is short, use C style quoting (e.g "\r\n")
             // Also do it if it is too long to fit in one line
             // If the string contains '\0', verbatim style won't work.
-            if (value.Length < 256 || value.Length > 1500 || (value.IndexOf('\0') != -1))
+            if (value.Length < 256 || value.Length > 1500 || (value.IndexOf('\0') != -1)) // string.Contains(char) is .NetCore2.1+ specific
                 return QuoteSnippetStringCStyle(value);
 
             // Otherwise, use 'verbatim' style quoting (e.g. @"foo")
index 1fa6d88..6d17b36 100644 (file)
@@ -866,7 +866,7 @@ namespace Microsoft.VisualBasic
                 string typeName = GetTypeOutput(e.CreateType);
                 Output.Write(typeName);
 
-                if (typeName.IndexOf('(') == -1)
+                if (typeName.IndexOf('(') == -1) // string.Contains(char) is .NetCore2.1+ specific
                 {
                     Output.Write("()");
                 }
index ead7c14..ec027e2 100644 (file)
@@ -78,7 +78,7 @@ namespace System.ComponentModel
             {
                 try
                 {
-                    if (strValue.IndexOf(',') != -1)
+                    if (strValue.Contains(','))
                     {
                         bool isUnderlyingTypeUInt64 = Enum.GetUnderlyingType(EnumType) == typeof(ulong);
                         long convertedValue = 0;
index 5de9aba..c0ed8f4 100644 (file)
@@ -25,7 +25,7 @@ namespace System.ComponentModel
                 return false;
 
             // Machine names shouldn't contain any "\"
-            return (value.IndexOf('\\') == -1);
+            return !value.Contains('\\');
         }
 
         /// <summary>
index 6784683..0d41f9f 100644 (file)
@@ -3218,7 +3218,7 @@ namespace System.Configuration
             if (trimmedConfigSource.Length != configSource.Length)
                 throw new ConfigurationErrorsException(SR.Config_source_invalid_format, errorInfo);
 
-            if (configSource.IndexOf('/') != -1)
+            if (configSource.IndexOf('/') != -1) // string.Contains(char) is .NetCore2.1+ specific
                 throw new ConfigurationErrorsException(SR.Config_source_invalid_chars, errorInfo);
 
             if (string.IsNullOrEmpty(configSource) || Path.IsPathRooted(configSource))
index 13222ce..84f228e 100644 (file)
@@ -134,7 +134,7 @@ namespace System.Configuration
 
                 if (collectionAttribute != null)
                 {
-                    if (collectionAttribute.AddItemName.IndexOf(',') == -1) AddElementName = collectionAttribute.AddItemName;
+                    if (collectionAttribute.AddItemName.IndexOf(',') == -1) AddElementName = collectionAttribute.AddItemName; // string.Contains(char) is .NetCore2.1+ specific
                     RemoveElementName = collectionAttribute.RemoveItemName;
                     ClearElementName = collectionAttribute.ClearItemsName;
                 }
@@ -402,4 +402,4 @@ namespace System.Configuration
             }
         }
     }
-}
\ No newline at end of file
+}
index 320eb43..532f6cc 100644 (file)
@@ -93,7 +93,7 @@ namespace System.Configuration
                 throw ExceptionUtil.ParameterNullOrEmpty(nameof(name));
 
             // prevent GetConfig from returning config not in this collection
-            if (name.IndexOf('/') >= 0)
+            if (name.IndexOf('/') >= 0) // string.Contains(char) is .NetCore2.1+ specific
                 return null;
 
             // get the section from the config record
@@ -141,4 +141,4 @@ namespace System.Configuration
             Remove(GetKey(index));
         }
     }
-}
\ No newline at end of file
+}
index 5059228..cd3076f 100644 (file)
@@ -90,7 +90,7 @@ namespace System.Configuration
                 throw ExceptionUtil.ParameterNullOrEmpty(nameof(name));
 
             // prevent GetConfig from returning config not in this collection
-            if (name.IndexOf('/') >= 0)
+            if (name.IndexOf('/') >= 0) // string.Contains(char) is .NetCore2.1+ specific
                 return null;
 
             // get the section group
index 67f16d2..e1f04f0 100644 (file)
@@ -50,7 +50,7 @@ namespace System.Configuration
 
             // Don't bother to look around if we've already got something that
             // is clearly not a simple type name.
-            if (string.IsNullOrEmpty(typeString) || typeString.IndexOf(',') != -1)
+            if (string.IsNullOrEmpty(typeString) || typeString.IndexOf(',') != -1) // string.Contains(char) is .NetCore2.1+ specific
                 return null;
 
             // Ignore all exceptions, otherwise callers will get unexpected
index 83719b5..1d54100 100644 (file)
@@ -80,7 +80,7 @@ namespace System.Data.Common
                     }
 
                     // Mutate name if it contains space(s)
-                    if (columnName.IndexOf(' ') >= 0)
+                    if (columnName.Contains(' '))
                     {
                         columnName = columnName.Replace(' ', '_');
                         isMutatedName = true;
index b15e7ba..e076990 100644 (file)
@@ -89,6 +89,7 @@ namespace System.Data.Common
                 if (useOdbcRules)
                 {
                     if ((0 < keyValue.Length) &&
+                        // string.Contains(char) is .NetCore2.1+ specific
                         (('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || string.Equals(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase)) &&
                         !s_connectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
                     {
@@ -106,6 +107,7 @@ namespace System.Data.Common
                     // <value> -> <value>
                     builder.Append(keyValue);
                 }
+                // string.Contains(char) is .NetCore2.1+ specific
                 else if ((-1 != keyValue.IndexOf('\"')) && (-1 == keyValue.IndexOf('\'')))
                 {
                     // <val"ue> -> <'val"ue'>
index 5800cc6..85aef39 100644 (file)
@@ -2756,7 +2756,7 @@ namespace System.Data
             if ((QualifiedTableName == null) || (QualifiedTableName.Length == 0))
                 throw ExceptionBuilder.InvalidSelector(xpath);
 
-            if (QualifiedTableName.IndexOf(':') != -1)
+            if (QualifiedTableName.Contains(':'))
                 prefix = QualifiedTableName.Substring(0, QualifiedTableName.IndexOf(':'));
             else
                 return GetMsdataAttribute(key, Keywords.MSD_TABLENS);
index 179d3c8..d9bebe0 100644 (file)
@@ -151,6 +151,7 @@ namespace System.Data.Common
                 if (useOdbcRules)
                 {
                     if ((0 < keyValue.Length) &&
+                        // string.Contains(char) is .NetCore2.1+ specific
                         (('{' == keyValue[0]) || (0 <= keyValue.IndexOf(';')) || (0 == string.Compare(DbConnectionStringKeywords.Driver, keyName, StringComparison.OrdinalIgnoreCase))) &&
                         !s_connectionStringQuoteOdbcValueRegex.IsMatch(keyValue))
                     {
@@ -168,6 +169,7 @@ namespace System.Data.Common
                     // <value> -> <value>
                     builder.Append(keyValue);
                 }
+                // string.Contains(char) is .NetCore2.1+ specific
                 else if ((-1 != keyValue.IndexOf('\"')) && (-1 == keyValue.IndexOf('\'')))
                 {
                     // <val"ue> -> <'val"ue'>
index 23f0c41..8cd2625 100644 (file)
@@ -317,6 +317,7 @@ namespace System.Data.SqlClient
                 }
             }
 
+            // string.Contains(char) is .NetCore2.1+ specific
             if (0 <= _attachDBFileName.IndexOf('|'))
             {
                 throw ADP.InvalidConnectionOptionValue(KEY.AttachDBFilename);
index 8fab9bc..d8a9d4f 100644 (file)
@@ -307,7 +307,7 @@ namespace System.Net.Http
                             }
                         }
                     }
-                    if (uri.HostNameType != UriHostNameType.IPv6 && uri.IdnHost.IndexOf('.') == -1)
+                    if (uri.HostNameType != UriHostNameType.IPv6 && !uri.IdnHost.Contains('.'))
                     {
                         // Not address and does not have a dot.
                         // Hosts without FQDN are considered local.
index 256fc2b..95b0c31 100644 (file)
@@ -95,7 +95,7 @@ namespace System.Net
             if (lp.Host != "*" && lp.Host != "+" && Uri.CheckHostName(lp.Host) == UriHostNameType.Unknown)
                 throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_listener_host);
 
-            if (lp.Path.IndexOf('%') != -1)
+            if (lp.Path.Contains('%'))
                 throw new HttpListenerException((int)HttpStatusCode.BadRequest, SR.net_invalid_path);
 
             if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
@@ -202,7 +202,7 @@ namespace System.Net
         private static void RemovePrefixInternal(string prefix, HttpListener listener)
         {
             ListenerPrefix lp = new ListenerPrefix(prefix);
-            if (lp.Path.IndexOf('%') != -1)
+            if (lp.Path.Contains('%'))
                 return;
 
             if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
index ded75b5..2a13739 100644 (file)
@@ -127,7 +127,7 @@ namespace System.Net.NetworkInformation
             }
 
             // Has dashes?
-            if (address.IndexOf('-') >= 0)
+            if (address.Contains('-'))
             {
                 hasDashes = true;
                 buffer = new byte[(address.Length + 1) / 3];
index a84831a..e1353ce 100644 (file)
@@ -299,7 +299,7 @@ namespace System.Net
                 }
 
                 Uri hostUri;
-                if ((value.IndexOf('/') != -1) || (!TryGetHostUri(value, out hostUri)))
+                if ((value.Contains('/')) || (!TryGetHostUri(value, out hostUri)))
                 {
                     throw new ArgumentException(SR.net_invalid_host, nameof(value));
                 }
@@ -311,7 +311,7 @@ namespace System.Net
                 {
                     _hostHasPort = true;
                 }
-                else if (value.IndexOf(':') == -1)
+                else if (!value.Contains(':'))
                 {
                     _hostHasPort = false;
                 }
index 6e283f9..ad3780e 100644 (file)
@@ -21,7 +21,7 @@ namespace System.Net
         {
             // RFC 6265: (for Set-Cookie header)
             // If the name-value-pair string lacks a %x3D ("=") character, ignore the set-cookie-string entirely.
-            if (isSetCookie && (value.IndexOf('=') < 0)) return Array.Empty<string>();
+            if (isSetCookie && (!value.Contains('='))) return Array.Empty<string>();
 
             var tempStringCollection = new List<string>();
 
@@ -65,10 +65,10 @@ namespace System.Net
         private static bool IsDuringExpiresAttributeParsing(string singleValue)
         {
             // Current cookie doesn't contain any attributes.
-            if (singleValue.IndexOf(';') < 0) return false;
+            if (!singleValue.Contains(';')) return false;
 
             string lastElement = singleValue.Split(';').Last();
-            bool noComma = lastElement.IndexOf(',') < 0;
+            bool noComma = !lastElement.Contains(',');
 
             string lastAttribute = lastElement.Split('=')[0].Trim();
             bool isExpires = string.Equals(lastAttribute, "Expires", StringComparison.OrdinalIgnoreCase);
index 0126fa4..389db7e 100644 (file)
@@ -202,7 +202,7 @@ namespace System
                 }
                 _host = value;
                 //probable ipv6 address - Note: this is only supported for cases where the authority is inet-based.
-                if (_host.IndexOf(':') >= 0)
+                if (_host.Contains(':'))
                 {
                     //set brackets
                     if (_host[0] != '[')
index 16e96cb..097b584 100644 (file)
@@ -1009,7 +1009,7 @@ namespace System.Xml
         public override string GetAttribute(string name)
         {
             int i;
-            if (name.IndexOf(':') == -1)
+            if (!name.Contains(':'))
             {
                 i = GetIndexOfAttributeWithoutPrefix(name);
             }
@@ -1049,7 +1049,7 @@ namespace System.Xml
         public override bool MoveToAttribute(string name)
         {
             int i;
-            if (name.IndexOf(':') == -1)
+            if (!name.Contains(':'))
             {
                 i = GetIndexOfAttributeWithoutPrefix(name);
             }
index 3e7e51e..d162fbf 100644 (file)
@@ -245,7 +245,7 @@ namespace System.Xml
         public override string GetAttribute(string name)
         {
             int i;
-            if (name.IndexOf(':') == -1)
+            if (!name.Contains(':'))
             {
                 i = GetAttributeIndexWithoutPrefix(name);
             }
@@ -305,7 +305,7 @@ namespace System.Xml
         public override bool MoveToAttribute(string name)
         {
             int i;
-            if (name.IndexOf(':') == -1)
+            if (!name.Contains(':'))
             {
                 i = GetAttributeIndexWithoutPrefix(name);
             }
index 0f5c0b5..cf1ef81 100644 (file)
@@ -1496,7 +1496,7 @@ namespace System.Xml
             {
                 systemId = GetValue();
 
-                if (systemId.IndexOf('#') >= 0)
+                if (systemId.Contains('#'))
                 {
                     Throw(_curPos - systemId.Length - 1, SR.Xml_FragmentId, new string[] { systemId.Substring(systemId.IndexOf('#')), systemId });
                 }
index 1badb5d..eef67cd 100644 (file)
@@ -1112,7 +1112,7 @@ namespace System.Xml
             {
                 systemId = GetValue();
 
-                if (systemId.IndexOf('#') >= 0)
+                if (systemId.Contains('#'))
                 {
                     Throw(_curPos - systemId.Length - 1, SR.Xml_FragmentId, new string[] { systemId.Substring(systemId.IndexOf('#')), systemId });
                 }
index 0fb11bd..0c5ed19 100644 (file)
@@ -360,7 +360,7 @@ namespace System.Xml.Schema
                     {
                         _regStr.Append(")");
                         string tempStr = _regStr.ToString();
-                        if (tempStr.IndexOf('|') != -1)
+                        if (tempStr.Contains('|'))
                         { // ordinal compare
                             _regStr.Insert(0, "(");
                             _regStr.Append(")");
index 4bab38a..252d4f9 100644 (file)
@@ -804,7 +804,7 @@ namespace System.Xml.Serialization
             if (a.SoapType != null && a.SoapType.TypeName.Length > 0)
                 typeName = a.SoapType.TypeName;
 
-            if (type.IsGenericType && typeName.IndexOf('{') >= 0)
+            if (type.IsGenericType && typeName.Contains('{'))
             {
                 Type genType = type.GetGenericTypeDefinition();
                 Type[] names = genType.GetGenericArguments();
@@ -816,7 +816,7 @@ namespace System.Xml.Serialization
                     if (typeName.Contains(argument))
                     {
                         typeName = typeName.Replace(argument, XsdTypeName(types[i]));
-                        if (typeName.IndexOf('{') < 0)
+                        if (!typeName.Contains('{'))
                         {
                             break;
                         }
index f6f7f65..421d7e7 100644 (file)
@@ -946,7 +946,7 @@ namespace System.Xml.Serialization
             if (a.XmlType != null && a.XmlType.TypeName.Length > 0)
                 typeName = a.XmlType.TypeName;
 
-            if (type.IsGenericType && typeName.IndexOf('{') >= 0)
+            if (type.IsGenericType && typeName.Contains('{'))
             {
                 Type genType = type.GetGenericTypeDefinition();
                 Type[] names = genType.GetGenericArguments();
@@ -958,7 +958,7 @@ namespace System.Xml.Serialization
                     if (typeName.Contains(argument))
                     {
                         typeName = typeName.Replace(argument, XsdTypeName(types[i]));
-                        if (typeName.IndexOf('{') < 0)
+                        if (!typeName.Contains('{'))
                         {
                             break;
                         }
index 9c95119..8b86cc7 100644 (file)
@@ -130,7 +130,7 @@ namespace System.Xml.Serialization
         {
             if (nmTokens == null)
                 return null;
-            if (nmTokens.IndexOf(' ') < 0)
+            if (!nmTokens.Contains(' '))
                 return FromXmlNmToken(nmTokens);
             else
             {
index 317ddd2..440dec8 100644 (file)
@@ -30,7 +30,7 @@ namespace System
                 builder.Length = (int)length;
 
                 // If we have a tilde in the path, make an attempt to expand 8.3 filenames
-                return builder.AsSpan().IndexOf('~') >= 0
+                return builder.AsSpan().Contains('~')
                     ? PathHelper.TryExpandShortFileName(ref builder, null)
                     : builder.ToString();
             }
index 35123d0..9372f66 100644 (file)
@@ -424,6 +424,7 @@ namespace System.ServiceProcess
 
         private static bool CheckMachineName(string value)
         {
+            // string.Contains(char) is .NetCore2.1+ specific
             return !string.IsNullOrWhiteSpace(value) && value.IndexOf('\\') == -1;
         }
 
index e031cf7..e8379e2 100644 (file)
@@ -1170,7 +1170,7 @@ namespace System.Transactions
         {
             Guid guid = Guid.Empty;
 
-            if (str.IndexOf('-') >= 0)
+            if (str.Contains('-'))
             { // GUID with dash
                 if (str.Length >= 36)
                 {
index 5582b1a..b6bb434 100644 (file)
@@ -48,6 +48,6 @@ namespace System.Web.Util
         }
 
         //  Helper to encode spaces only
-        internal static string UrlEncodeSpaces(string str) => str != null && str.IndexOf(' ') >= 0 ? str.Replace(" ", "%20") : str;
+        internal static string UrlEncodeSpaces(string str) => str != null && str.Contains(' ') ? str.Replace(" ", "%20") : str;
     }
 }