Skip to content

Commit

Permalink
String Method FxCop Error Mop Up (#1337)
Browse files Browse the repository at this point in the history
* Fix a few more FxCop Warnings about String Methods

**Bug**
Various FxCop warnings about strings, such as using the implicit overloads vs explicit ones.

**Fix**

- Make `Parse` methods use invarient culture.
- Fix a few more `StartsWith` calls
- Fix a few more format calls.

* A bit more mopup
  • Loading branch information
mjbvz committed Oct 13, 2016
1 parent 7166537 commit daffc33
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Nodejs/Product/LogConverter/LogParsing/LogConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ record = record.Replace("\"\"", "\"");
// To cover them all, we just repeatedly strip the tail of the string after the last ':',
// so long as it can be parsed as an integer.
int colon;
while ((colon = location.LastIndexOf(':')) != -1) {
while ((colon = location.LastIndexOf(":", StringComparison.Ordinal)) != -1) {
string tail = location.Substring(colon + 1, location.Length - (colon + 1));
int number;
if (!Int32.TryParse(tail, out number)) {
Expand Down
3 changes: 2 additions & 1 deletion Nodejs/Product/Nodejs/Commands/ImportWizardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//*********************************************************//

using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
Expand Down Expand Up @@ -48,7 +49,7 @@ class ImportWizardCommand : Command {
".njsproj"
);
dlg.ImportSettings.SourcePath = argItems[1];
commandIdToRaise = int.Parse(argItems[2]);
commandIdToRaise = int.Parse(argItems[2], CultureInfo.InvariantCulture);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Nodejs/Product/Nodejs/Debugger/NodeEvaluationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//*********************************************************//

using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -113,7 +114,7 @@ class NodeEvaluationResult {
return stringValue.Length;
}

return int.Parse(match.Groups[1].Value);
return int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
}
}
}
3 changes: 2 additions & 1 deletion Nodejs/Product/Nodejs/JsonListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Net.Sockets;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -81,7 +82,7 @@ abstract class JsonListener {
string body = String.Empty;
string contentLen;
if (headers.TryGetValue("Content-Length", out contentLen)) {
int lengthRemaining = Int32.Parse(contentLen);
int lengthRemaining = int.Parse(contentLen, CultureInfo.InvariantCulture);
if (lengthRemaining != 0) {
StringBuilder bodyBuilder = new StringBuilder();

Expand Down
3 changes: 2 additions & 1 deletion Nodejs/Product/Npm/PackageComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//
//*********************************************************//

using System;
using System.Collections.Generic;

namespace Microsoft.NodejsTools.Npm {
Expand All @@ -27,7 +28,7 @@ public class PackageComparer : IComparer<IPackage> {
return 1;
}
// TODO: should take into account versions!
return x.Name.CompareTo(y.Name);
return string.Compare(x.Name, y.Name, StringComparison.Ordinal);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Nodejs/Product/Npm/SPI/DatabasePackageCatalogFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class CatalogEntryComparer : IComparer<CatalogEntry> {
}

private int? CompareEntryStrings(string x, string y) {
var xIndex = string.IsNullOrEmpty(x) ? -1 : x.ToLower().IndexOf(_filterText, StringComparison.OrdinalIgnoreCase);
var yIndex = string.IsNullOrEmpty(y) ? -1 : y.ToLower().IndexOf(_filterText, StringComparison.OrdinalIgnoreCase);
var xIndex = string.IsNullOrEmpty(x) ? -1 : x.ToLower(CultureInfo.InvariantCulture).IndexOf(_filterText, StringComparison.OrdinalIgnoreCase);
var yIndex = string.IsNullOrEmpty(y) ? -1 : y.ToLower(CultureInfo.InvariantCulture).IndexOf(_filterText, StringComparison.OrdinalIgnoreCase);

int? xEqualsY = null;
const int xBeforeY = -1;
Expand Down
4 changes: 3 additions & 1 deletion Nodejs/Product/Npm/SPI/DependencyUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//
//*********************************************************//

using System;

namespace Microsoft.NodejsTools.Npm.SPI {
internal class DependencyUrl : IDependencyUrl {
public DependencyUrl(string address) {
Expand All @@ -24,7 +26,7 @@ internal class DependencyUrl : IDependencyUrl {

public DependencyUrlType Type {
get {
var index = Address.IndexOf("://");
var index = Address.IndexOf("://", StringComparison.Ordinal);
if (index < 0) {
return DependencyUrlType.GitHub;
} else {
Expand Down
6 changes: 3 additions & 3 deletions Nodejs/Product/Npm/SPI/NodeModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class NodeModules : AbstractNodeModules {
break;
}

var parentNodeModulesIndex = moduleDir.LastIndexOf(NodejsConstants.NodeModulesFolder, Math.Max(0, moduleDir.Length - NodejsConstants.NodeModulesFolder.Length - dependency.Name.Length - 1));
var parentNodeModulesIndex = moduleDir.LastIndexOf(NodejsConstants.NodeModulesFolder, Math.Max(0, moduleDir.Length - NodejsConstants.NodeModulesFolder.Length - dependency.Name.Length - 1), StringComparison.Ordinal);
moduleDir = moduleDir.Substring(0, parentNodeModulesIndex + NodejsConstants.NodeModulesFolder.Length);
} while (moduleDir.Contains(NodejsConstants.NodeModulesFolder));
}
Expand Down Expand Up @@ -140,8 +140,8 @@ internal class NodeModules : AbstractNodeModules {
}

public override int GetDepth(string filepath) {
var lastNodeModules = filepath.LastIndexOf(NodejsConstants.NodeModulesFolder + "\\");
var directoryToSearch = filepath.IndexOf("\\", lastNodeModules + NodejsConstants.NodeModulesFolder.Length + 1);
var lastNodeModules = filepath.LastIndexOf(NodejsConstants.NodeModulesFolder + "\\", StringComparison.Ordinal);
var directoryToSearch = filepath.IndexOf("\\", lastNodeModules + NodejsConstants.NodeModulesFolder.Length + 1, StringComparison.Ordinal);
var directorySubString = directoryToSearch == -1 ? filepath : filepath.Substring(0, directoryToSearch);

ModuleInfo value = null;
Expand Down
7 changes: 4 additions & 3 deletions Nodejs/Product/Npm/SPI/NpmSearchFilterStringComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//*********************************************************//

using System;
using System.Globalization;
using System.Linq;

namespace Microsoft.NodejsTools.Npm.SPI {
Expand All @@ -26,15 +27,15 @@ internal class NpmSearchFilterStringComparer : AbstractNpmSearchComparer {
}

private int GetExactKeywordMatchCount(IPackage source) {
return source.Keywords == null ? 0 : source.Keywords.Count(keyword => keyword.ToLower() == _filterString);
return source.Keywords == null ? 0 : source.Keywords.Count(keyword => keyword.ToLower(CultureInfo.InvariantCulture) == _filterString);
}

private int GetStartsWithMatchCount(IPackage source) {
return source.Keywords == null ? 0 : source.Keywords.Count(keyword => keyword.ToLower().StartsWith(_filterString));
return source.Keywords == null ? 0 : source.Keywords.Count(keyword => keyword.ToLower(CultureInfo.InvariantCulture).StartsWith(_filterString, StringComparison.Ordinal));
}

private int GetPartialKeywordMatchCount(IPackage source) {
return source.Keywords == null ? 0 : source.Keywords.Count(keyword => keyword.ToLower().Contains(_filterString));
return source.Keywords == null ? 0 : source.Keywords.Count(keyword => keyword.ToLower(CultureInfo.InvariantCulture).Contains(_filterString));
}

private new int CompareBasedOnKeywords(IPackage x, IPackage y) {
Expand Down
6 changes: 3 additions & 3 deletions Nodejs/Product/Npm/SemverVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public struct SemverVersion {
// /Hack

return new SemverVersion(
ulong.Parse(match.Groups["major"].Value),
ulong.Parse(match.Groups["minor"].Value),
ulong.Parse(patch),
ulong.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture),
ulong.Parse(match.Groups["minor"].Value, CultureInfo.InvariantCulture),
ulong.Parse(patch, CultureInfo.InvariantCulture),
preRelease.Success ? preRelease.Value : null,
buildMetadata.Success ? buildMetadata.Value : null);
} catch (OverflowException oe) {
Expand Down
3 changes: 2 additions & 1 deletion Nodejs/Product/Npm/SemverVersionComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;

namespace Microsoft.NodejsTools.Npm {

Expand Down Expand Up @@ -63,7 +64,7 @@ public class SemverVersionComparer : IComparer<SemverVersion> {
if (xn) {
if (yn) {
// Compare numeric identifiers in the expected way
var result = int.Parse(xs[index]).CompareTo(int.Parse(ys[index]));
var result = int.Parse(xs[index], CultureInfo.InvariantCulture).CompareTo(int.Parse(ys[index], CultureInfo.InvariantCulture));
if (0 != result) {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion Nodejs/Product/Profiling/NodejsProfilingPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class UpdateSolutionEvents : IVsUpdateSolutionEvents {
var process = new ProfiledProcess(interpreter, interpreterArgs, script, scriptArgs, workingDir, env, arch, launchUrl, port, startBrowser, jmc);

string baseName = Path.GetFileNameWithoutExtension(session.Filename);
string date = DateTime.Now.ToString("yyyyMMdd");
string date = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
string outPath = Path.Combine(Path.GetDirectoryName(session.Filename), baseName + "_" + date + ".vspx");

int count = 1;
Expand Down
6 changes: 3 additions & 3 deletions Nodejs/Product/Profiling/Profiling/ProfiledProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ select connection.LocalEndPoint.Port
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"Microsoft.NodejsTools.NodeLogConverter.exe"
),
(_justMyCode ? "/jmc " : String.Empty) +
(is012 ? "/v:0.12 " : String.Empty) +
(_justMyCode ? "/jmc " : string.Empty) +
(is012 ? "/v:0.12 " : string.Empty) +
"\"" + v8log + "\" " +
"\"" + filename + "\" " +
"\"" + _process.StartTime.ToString() + "\" " +
"\"" + _process.StartTime.ToString(CultureInfo.InvariantCulture) + "\" " +
"\"" + executionTime.ToString() + "\""
);
Expand Down
2 changes: 1 addition & 1 deletion Nodejs/Product/Profiling/Profiling/SessionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SessionNode : BaseHierarchyNode, IVsHierarchyDeleteHandler, IVsPersistHier
switch (prop) {
case __VSHPROPID.VSHPROPID_Expanded:
if (itemid == ReportsItemId) {
_isReportsExpanded = Convert.ToBoolean(var);
_isReportsExpanded = Convert.ToBoolean(var, CultureInfo.InvariantCulture);
break;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using Microsoft.NodejsTools.Debugger.Commands;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;

namespace NodejsTests.Debugger.Commands {
[TestClass]
Expand Down Expand Up @@ -55,7 +56,7 @@ public class ChangeBreakpointCommandTests {
Assert.AreEqual(
string.Format(
"{{\"command\":\"changebreakpoint\",\"seq\":{0},\"type\":\"request\",\"arguments\":{{\"breakpoint\":{1},\"enabled\":{2},\"condition\":\"{3}\",\"ignoreCount\":{4}}}}}",
commandId, breakpointId, enabled.ToString().ToLower(), condition, ignoreCount),
commandId, breakpointId, enabled.ToString().ToLower(CultureInfo.InvariantCulture), condition, ignoreCount),
changeBreakpointCommand.ToString());
}
}
Expand Down
5 changes: 3 additions & 2 deletions Nodejs/Tests/Core/Debugger/Commands/ContinueCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.NodejsTools.Debugger;
using Microsoft.NodejsTools.Debugger.Commands;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;

namespace NodejsTests.Debugger.Commands {
[TestClass]
Expand All @@ -35,7 +36,7 @@ public class ContinueCommandTests {
Assert.AreEqual(
string.Format(
"{{\"command\":\"continue\",\"seq\":{0},\"type\":\"request\",\"arguments\":{{\"stepaction\":\"{1}\",\"stepcount\":1}}}}",
commandId, stepping.ToString().ToLower()),
commandId, stepping.ToString().ToLower(CultureInfo.InvariantCulture)),
continueCommand.ToString());
}

Expand All @@ -54,7 +55,7 @@ public class ContinueCommandTests {
Assert.AreEqual(
string.Format(
"{{\"command\":\"continue\",\"seq\":{0},\"type\":\"request\",\"arguments\":{{\"stepaction\":\"{1}\",\"stepcount\":{2}}}}}",
commandId, stepping.ToString().ToLower(), stepCount),
commandId, stepping.ToString().ToLower(CultureInfo.InvariantCulture), stepCount),
continueCommand.ToString());
}
}
Expand Down
3 changes: 2 additions & 1 deletion Nodejs/Tests/Core/Debugger/Commands/ScriptsCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//
//*********************************************************//

using System.Globalization;
using Microsoft.NodejsTools.Debugger;
using Microsoft.NodejsTools.Debugger.Commands;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class ScriptsCommandTests {
Assert.AreEqual(
string.Format(
"{{\"command\":\"scripts\",\"seq\":{0},\"type\":\"request\",\"arguments\":{{\"includeSource\":{1},\"ids\":[{2}]}}}}",
commandId, includeSource.ToString().ToLower(), moduleId),
commandId, includeSource.ToString().ToLower(CultureInfo.InvariantCulture), moduleId),
scriptsCommand.ToString());
}

Expand Down

0 comments on commit daffc33

Please sign in to comment.