Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f953d74

Browse files
committed
Merge pull request #2018 from justinvp/stringequals
Use String.Equals instead of String.Compare for equality checks
2 parents 7ea9cd5 + 423d73e commit f953d74

27 files changed

+67
-64
lines changed

src/System.IO.FileSystem/src/System/IO/Directory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,12 @@ public static void Move(String sourceDirName, String destDirName)
578578

579579
StringComparison pathComparison = PathInternal.GetComparison();
580580

581-
if (String.Compare(sourcePath, destPath, pathComparison) == 0)
581+
if (String.Equals(sourcePath, destPath, pathComparison))
582582
throw new IOException(SR.IO_SourceDestMustBeDifferent);
583583

584584
String sourceRoot = Path.GetPathRoot(sourcePath);
585585
String destinationRoot = Path.GetPathRoot(destPath);
586-
if (String.Compare(sourceRoot, destinationRoot, pathComparison) != 0)
586+
if (!String.Equals(sourceRoot, destinationRoot, pathComparison))
587587
throw new IOException(SR.IO_SourceDestMustHaveSameRoot);
588588

589589
FileSystem.Current.MoveDirectory(fullsourceDirName, fulldestDirName);

src/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,13 @@ public void MoveTo(String destDirName)
418418
fullSourcePath = FullPath + PathHelpers.DirectorySeparatorCharAsString;
419419

420420
StringComparison pathComparison = PathInternal.GetComparison();
421-
if (String.Compare(fullSourcePath, fullDestDirName, pathComparison) == 0)
421+
if (String.Equals(fullSourcePath, fullDestDirName, pathComparison))
422422
throw new IOException(SR.IO_SourceDestMustBeDifferent);
423423

424424
String sourceRoot = Path.GetPathRoot(fullSourcePath);
425425
String destinationRoot = Path.GetPathRoot(fullDestDirName);
426426

427-
if (String.Compare(sourceRoot, destinationRoot, pathComparison) != 0)
427+
if (!String.Equals(sourceRoot, destinationRoot, pathComparison))
428428
throw new IOException(SR.IO_SourceDestMustHaveSameRoot);
429429

430430
FileSystem.Current.MoveDirectory(FullPath, fullDestDirName);

src/System.Net.Http/src/System/Net/Headers/AuthenticationHeaderValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ public override bool Equals(object obj)
7474

7575
if (string.IsNullOrEmpty(_parameter) && string.IsNullOrEmpty(other._parameter))
7676
{
77-
return (string.Compare(_scheme, other._scheme, StringComparison.OrdinalIgnoreCase) == 0);
77+
return (string.Equals(_scheme, other._scheme, StringComparison.OrdinalIgnoreCase));
7878
}
7979
else
8080
{
8181
// Since we can't parse the parameter, we use case-sensitive comparison.
82-
return (string.Compare(_scheme, other._scheme, StringComparison.OrdinalIgnoreCase) == 0) &&
83-
(string.CompareOrdinal(_parameter, other._parameter) == 0);
82+
return string.Equals(_scheme, other._scheme, StringComparison.OrdinalIgnoreCase) &&
83+
string.Equals(_parameter, other._parameter, StringComparison.Ordinal);
8484
}
8585
}
8686

8787
public override int GetHashCode()
8888
{
89-
int result = _scheme.ToLowerInvariant().GetHashCode();
89+
int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_scheme);
9090

9191
if (!string.IsNullOrEmpty(_parameter))
9292
{

src/System.Net.Http/src/System/Net/Headers/CacheControlHeaderValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ public override int GetHashCode()
333333
{
334334
foreach (var noCacheHeader in _noCacheHeaders)
335335
{
336-
result = result ^ noCacheHeader.ToLowerInvariant().GetHashCode();
336+
result = result ^ StringComparer.OrdinalIgnoreCase.GetHashCode(noCacheHeader);
337337
}
338338
}
339339

340340
if ((_privateHeaders != null) && (_privateHeaders.Count > 0))
341341
{
342342
foreach (var privateHeader in _privateHeaders)
343343
{
344-
result = result ^ privateHeader.ToLowerInvariant().GetHashCode();
344+
result = result ^ StringComparer.OrdinalIgnoreCase.GetHashCode(privateHeader);
345345
}
346346
}
347347

src/System.Net.Http/src/System/Net/Headers/ContentDispositionHeaderValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ public override bool Equals(object obj)
178178
return false;
179179
}
180180

181-
return (string.Compare(_dispositionType, other._dispositionType, StringComparison.OrdinalIgnoreCase) == 0) &&
181+
return string.Equals(_dispositionType, other._dispositionType, StringComparison.OrdinalIgnoreCase) &&
182182
HeaderUtilities.AreEqualCollections(_parameters, other._parameters);
183183
}
184184

185185
public override int GetHashCode()
186186
{
187187
// The dispositionType string is case-insensitive.
188-
return _dispositionType.ToLowerInvariant().GetHashCode() ^ NameValueHeaderValue.GetHashCode(_parameters);
188+
return StringComparer.OrdinalIgnoreCase.GetHashCode(_dispositionType) ^ NameValueHeaderValue.GetHashCode(_parameters);
189189
}
190190

191191
// Implement ICloneable explicitly to allow derived types to "override" the implementation.

src/System.Net.Http/src/System/Net/Headers/ContentRangeHeaderValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ public override bool Equals(object obj)
127127
}
128128

129129
return ((_from == other._from) && (_to == other._to) && (_length == other._length) &&
130-
(string.Compare(_unit, other._unit, StringComparison.OrdinalIgnoreCase) == 0));
130+
string.Equals(_unit, other._unit, StringComparison.OrdinalIgnoreCase));
131131
}
132132

133133
public override int GetHashCode()
134134
{
135-
int result = _unit.ToLowerInvariant().GetHashCode();
135+
int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_unit);
136136

137137
if (HasRange)
138138
{

src/System.Net.Http/src/System/Net/Headers/EntityTagHeaderValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public override bool Equals(object obj)
9292
}
9393

9494
// Since the tag is a quoted-string we treat it case-sensitive.
95-
return ((_isWeak == other._isWeak) && (string.CompareOrdinal(_tag, other._tag) == 0));
95+
return ((_isWeak == other._isWeak) && string.Equals(_tag, other._tag, StringComparison.Ordinal));
9696
}
9797

9898
public override int GetHashCode()

src/System.Net.Http/src/System/Net/Headers/MediaTypeHeaderValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ public override bool Equals(object obj)
113113
return false;
114114
}
115115

116-
return (string.Compare(_mediaType, other._mediaType, StringComparison.OrdinalIgnoreCase) == 0) &&
116+
return string.Equals(_mediaType, other._mediaType, StringComparison.OrdinalIgnoreCase) &&
117117
HeaderUtilities.AreEqualCollections(_parameters, other._parameters);
118118
}
119119

120120
public override int GetHashCode()
121121
{
122122
// The media-type string is case-insensitive.
123-
return _mediaType.ToLowerInvariant().GetHashCode() ^ NameValueHeaderValue.GetHashCode(_parameters);
123+
return StringComparer.OrdinalIgnoreCase.GetHashCode(_mediaType) ^ NameValueHeaderValue.GetHashCode(_parameters);
124124
}
125125

126126
public static MediaTypeHeaderValue Parse(string input)

src/System.Net.Http/src/System/Net/Headers/NameValueHeaderValue.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override int GetHashCode()
6262
{
6363
Debug.Assert(_name != null);
6464

65-
int nameHashCode = _name.ToLowerInvariant().GetHashCode();
65+
int nameHashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(_name);
6666

6767
if (!string.IsNullOrEmpty(_value))
6868
{
@@ -73,7 +73,7 @@ public override int GetHashCode()
7373
return nameHashCode ^ _value.GetHashCode();
7474
}
7575

76-
return nameHashCode ^ _value.ToLowerInvariant().GetHashCode();
76+
return nameHashCode ^ StringComparer.OrdinalIgnoreCase.GetHashCode(_value);
7777
}
7878

7979
return nameHashCode;
@@ -88,7 +88,7 @@ public override bool Equals(object obj)
8888
return false;
8989
}
9090

91-
if (string.Compare(_name, other._name, StringComparison.OrdinalIgnoreCase) != 0)
91+
if (!string.Equals(_name, other._name, StringComparison.OrdinalIgnoreCase))
9292
{
9393
return false;
9494
}
@@ -105,11 +105,11 @@ public override bool Equals(object obj)
105105
if (_value[0] == '"')
106106
{
107107
// We have a quoted string, so we need to do case-sensitive comparison.
108-
return (string.CompareOrdinal(_value, other._value) == 0);
108+
return string.Equals(_value, other._value, StringComparison.Ordinal);
109109
}
110110
else
111111
{
112-
return (string.Compare(_value, other._value, StringComparison.OrdinalIgnoreCase) == 0);
112+
return string.Equals(_value, other._value, StringComparison.OrdinalIgnoreCase);
113113
}
114114
}
115115

@@ -307,7 +307,7 @@ internal static NameValueHeaderValue Find(ICollection<NameValueHeaderValue> valu
307307

308308
foreach (var value in values)
309309
{
310-
if (string.Compare(value.Name, name, StringComparison.OrdinalIgnoreCase) == 0)
310+
if (string.Equals(value.Name, name, StringComparison.OrdinalIgnoreCase))
311311
{
312312
return value;
313313
}

src/System.Net.Http/src/System/Net/Headers/ProductHeaderValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,17 @@ public override bool Equals(object obj)
6969
return false;
7070
}
7171

72-
return (string.Compare(_name, other._name, StringComparison.OrdinalIgnoreCase) == 0) &&
73-
(string.Compare(_version, other._version, StringComparison.OrdinalIgnoreCase) == 0);
72+
return string.Equals(_name, other._name, StringComparison.OrdinalIgnoreCase) &&
73+
string.Equals(_version, other._version, StringComparison.OrdinalIgnoreCase);
7474
}
7575

7676
public override int GetHashCode()
7777
{
78-
int result = _name.ToLowerInvariant().GetHashCode();
78+
int result = StringComparer.OrdinalIgnoreCase.GetHashCode(_name);
7979

8080
if (!string.IsNullOrEmpty(_version))
8181
{
82-
result = result ^ _version.ToLowerInvariant().GetHashCode();
82+
result = result ^ StringComparer.OrdinalIgnoreCase.GetHashCode(_version);
8383
}
8484

8585
return result;

0 commit comments

Comments
 (0)