Skip to content

Replaced comparison with -1 w/ equivalent comparison to 0 #61428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Components/Endpoints/src/FormMapping/FormDataReader.cs
Original file line number Diff line number Diff line change
@@ -138,7 +138,7 @@ internal IReadOnlyDictionary<FormKey, HashSet<FormKey>> ProcessFormKeys()
while (startIndex >= 0)
{
var endIndex = key.Value.Span[startIndex..].IndexOf(']') + startIndex;
if (endIndex == -1)
if (endIndex < 0)
{
// Ignore malformed keys
break;
@@ -157,7 +157,7 @@ internal IReadOnlyDictionary<FormKey, HashSet<FormKey>> ProcessFormKeys()

var nextOpenBracket = key.Value.Span[(endIndex + 1)..].IndexOf('[');

startIndex = nextOpenBracket != -1 ? endIndex + 1 + nextOpenBracket : -1;
startIndex = nextOpenBracket >= 0 ? endIndex + 1 + nextOpenBracket : -1;
}
}

@@ -264,7 +264,7 @@ internal readonly bool TryGetValues(out StringValues values) =>
internal string GetLastPrefixSegment()
{
var index = _currentPrefixBuffer.Span.LastIndexOfAny(".[");
if (index == -1)
if (index < 0)
{
return _currentPrefixBuffer.ToString();
}
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ public bool TryDeserializeComponentDescriptorCollection(string serializedCompone
// check covers that the sequence starts by 0.
if (lastSequence != serverComponent.Sequence - 1)
{
if (lastSequence == -1)
if (lastSequence < 0)
{
Log.DescriptorSequenceMustStartAtZero(_logger, serverComponent.Sequence);
}
@@ -125,7 +125,7 @@ public bool TryDeserializeComponentDescriptorCollection(string serializedCompone
return false;
}

if (lastSequence != -1 && !previousInstance.InvocationId.Equals(serverComponent.InvocationId))
if (lastSequence >= 0 && !previousInstance.InvocationId.Equals(serverComponent.InvocationId))
{
Log.MismatchedInvocationId(_logger, previousInstance.InvocationId.ToString("N"), serverComponent.InvocationId.ToString("N"));
descriptors.Clear();
Original file line number Diff line number Diff line change
@@ -79,11 +79,11 @@ protected static string RemoveVersionFromAssemblyName(string forwardedTypeName)
// Type, Assembly, Version={Version}, Culture={Culture}, PublicKeyToken={Token}

var versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal);
while (versionStartIndex != -1)
while (versionStartIndex >= 0)
{
var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + ", Version=".Length);

if (versionEndIndex == -1)
if (versionEndIndex < 0)
{
// No end index, so are done and can remove the rest
return forwardedTypeName.Substring(0, versionStartIndex);
2 changes: 1 addition & 1 deletion src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
@@ -501,7 +501,7 @@ sourceValue is List<string> sourceDescriptions &&
destinationValue is List<string> destinationDescriptions)
{
var wireUpIndex = destinationDescriptions.IndexOf(typeof(WireSourcePipeline).FullName!);
if (wireUpIndex != -1)
if (wireUpIndex >= 0)
{
destinationDescriptions.RemoveAt(wireUpIndex);
destinationDescriptions.InsertRange(wireUpIndex, sourceDescriptions);
2 changes: 1 addition & 1 deletion src/FileProviders/Embedded/src/EmbeddedFileProvider.cs
Original file line number Diff line number Diff line change
@@ -180,7 +180,7 @@ public IChangeToken Watch(string pattern)

private static bool HasInvalidPathChars(string path)
{
return path.IndexOfAny(_invalidFileNameChars) != -1;
return path.IndexOfAny(_invalidFileNameChars) >= 0;
}

#region Helper methods
Original file line number Diff line number Diff line change
@@ -63,13 +63,13 @@ internal EmbeddedFilesManifest(ManifestDirectory rootDirectory)
private static StringSegment RemoveLeadingAndTrailingDirectorySeparators(string path)
{
Debug.Assert(path.Length > 0);
var start = Array.IndexOf(_separators, path[0]) == -1 ? 0 : 1;
var start = Array.IndexOf(_separators, path[0]) < 0 ? 0 : 1;
if (start == path.Length)
{
return StringSegment.Empty;
}

var end = Array.IndexOf(_separators, path[path.Length - 1]) == -1 ? path.Length : path.Length - 1;
var end = Array.IndexOf(_separators, path[path.Length - 1]) < 0 ? path.Length : path.Length - 1;
var trimmed = new StringSegment(path, start, end - start);
return trimmed;
}
@@ -84,5 +84,5 @@ internal EmbeddedFilesManifest Scope(string path)
throw new InvalidOperationException($"Invalid path: '{path}'");
}

private static bool HasInvalidPathChars(string path) => path.IndexOfAny(_invalidFileNameChars) != -1;
private static bool HasInvalidPathChars(string path) => path.IndexOfAny(_invalidFileNameChars) >= 0;
}
2 changes: 1 addition & 1 deletion src/Http/Headers/src/ContentDispositionHeaderValue.cs
Original file line number Diff line number Diff line change
@@ -487,7 +487,7 @@ private StringSegment EncodeAndQuoteMime(StringSegment input)

if (needsQuotes)
{
if (result.IndexOfAny(EscapeChars) != -1)
if (result.IndexOfAny(EscapeChars) >= 0)
{
// '\' and '"' must be escaped in a quoted string
result = result.ToString().Replace(@"\", @"\\").Replace(@"""", @"\""");
2 changes: 1 addition & 1 deletion src/Http/Headers/src/HeaderUtilities.cs
Original file line number Diff line number Diff line change
@@ -196,7 +196,7 @@ private static int AdvanceCacheDirectiveIndex(int current, string headerValue)
// Find the next delimiter
current = headerValue.IndexOf(',', current);

if (current == -1)
if (current < 0)
{
// If no delimiter found, skip to the end
return headerValue.Length;
8 changes: 4 additions & 4 deletions src/Http/Headers/src/MediaTypeHeaderValue.cs
Original file line number Diff line number Diff line change
@@ -278,7 +278,7 @@ public StringSegment SubTypeWithoutSuffix
{
var subType = SubType;
var startOfSuffix = subType.LastIndexOf(PlusCharacter);
if (startOfSuffix == -1)
if (startOfSuffix < 0)
{
return subType;
}
@@ -303,7 +303,7 @@ public StringSegment Suffix
{
var subType = SubType;
var startOfSuffix = subType.LastIndexOf(PlusCharacter);
if (startOfSuffix == -1)
if (startOfSuffix < 0)
{
return default(StringSegment);
}
@@ -718,7 +718,7 @@ private bool MatchesSubtype(StringSegment mediaType)

StringSegment suffix;
var startOfSuffix = subType.LastIndexOf(PlusCharacter);
if (startOfSuffix == -1)
if (startOfSuffix < 0)
{
suffix = default(StringSegment);
}
@@ -756,7 +756,7 @@ private bool MatchesSubtypeWithoutSuffix(MediaTypeHeaderValue set)
private bool MatchesSubtypeWithoutSuffix(StringSegment subType, int startOfSuffix)
{
StringSegment subTypeWithoutSuffix;
if (startOfSuffix == -1)
if (startOfSuffix < 0)
{
subTypeWithoutSuffix = subType;
}
Original file line number Diff line number Diff line change
@@ -141,7 +141,7 @@ public bool MoveNext()
_trailingStart = -1;

if (_offset == _headerLength &&
_leadingStart != -1 &&
_leadingStart >= 0 &&
_leadingStart != _offset)
{
// Also produce trailing whitespace
@@ -186,9 +186,9 @@ public bool MoveNext()
switch (attr)
{
case Attr.Delimiter:
_valueStart = _valueStart == -1 ? _offset : _valueStart;
_valueEnd = _valueEnd == -1 ? _offset : _valueEnd;
_trailingStart = _trailingStart == -1 ? _offset : _trailingStart;
_valueStart = _valueStart < 0 ? _offset : _valueStart;
_valueEnd = _valueEnd < 0 ? _offset : _valueEnd;
_trailingStart = _trailingStart < 0 ? _offset : _trailingStart;
_leadingEnd = _offset;
_mode = Mode.Produce;
break;
2 changes: 1 addition & 1 deletion src/Http/Http.Abstractions/src/PathString.cs
Original file line number Diff line number Diff line change
@@ -180,7 +180,7 @@ private static string ToEscapedUriComponent(string value, int i)
public static PathString FromUriComponent(string uriComponent)
{
int position = uriComponent.IndexOf('%');
if (position == -1)
if (position < 0)
{
return new PathString(uriComponent);
}
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ internal static bool IsFileName(ReadOnlySpan<char> value)
}

var dotIndex = value.IndexOf('.');
if (dotIndex == -1)
if (dotIndex < 0)
{
// No dot.
return false;
2 changes: 1 addition & 1 deletion src/Http/Routing/src/InlineRouteParameterParser.cs
Original file line number Diff line number Diff line change
@@ -167,7 +167,7 @@ private static ConstraintParseResults ParseConstraints(
// determine if the terminator needs to be consumed as part of the current constraint
// specification.
var indexOfClosingParantheses = routeParameter.IndexOf(')', currentIndex + 1);
if (indexOfClosingParantheses == -1)
if (indexOfClosingParantheses < 0)
{
constraintText = routeParameter.Substring(startIndex, currentIndex - startIndex);
inlineConstraints.Add(new InlineConstraint(constraintText));
4 changes: 2 additions & 2 deletions src/Http/Routing/src/Matching/NegotiationMatcherPolicy.cs
Original file line number Diff line number Diff line change
@@ -431,7 +431,7 @@ public override int GetDestination(HttpContext httpContext)
if (valueQuality > currentQuality)
{
var newDestination = GetDestination(valueToken);
if (newDestination != -1)
if (newDestination >= 0)
{
currentSelectedValue = valueToken;
selectedDestination = newDestination;
@@ -447,7 +447,7 @@ public override int GetDestination(HttpContext httpContext)
if (newServerQuality > currentServerQuality)
{
var newDestination = GetDestination(valueToken);
if (newDestination != -1)
if (newDestination >= 0)
{
currentSelectedValue = valueToken;
selectedDestination = newDestination;
8 changes: 4 additions & 4 deletions src/Http/Routing/src/PathTokenizer.cs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public int Count
{
get
{
if (_count == -1)
if (_count < 0)
{
// We haven't computed the real count of segments yet.
if (_path.Length == 0)
@@ -79,7 +79,7 @@ public StringSegment this[int index]

// Skip the first `/`.
var delimiterIndex = 1;
while ((delimiterIndex = _path.IndexOf('/', delimiterIndex)) != -1)
while ((delimiterIndex = _path.IndexOf('/', delimiterIndex)) >= 0)
{
if (currentSegmentIndex++ == index)
{
@@ -158,7 +158,7 @@ public bool MoveNext()
return false;
}

if (_index == -1)
if (_index < 0)
{
// Skip the first `/`.
_index = 1;
@@ -176,7 +176,7 @@ public bool MoveNext()
}

var delimiterIndex = _path.IndexOf('/', _index);
if (delimiterIndex != -1)
if (delimiterIndex >= 0)
{
_length = delimiterIndex - _index;
return true;
2 changes: 1 addition & 1 deletion src/Http/Routing/src/Patterns/RoutePatternMatcher.cs
Original file line number Diff line number Diff line change
@@ -390,7 +390,7 @@ private static bool MatchComplexSegmentCore(
StringComparison.OrdinalIgnoreCase);
}

if (indexOfLiteral == -1)
if (indexOfLiteral < 0)
{
// If we couldn't find this literal index, this segment cannot match
return false;
2 changes: 1 addition & 1 deletion src/Http/Routing/src/Patterns/RoutePatternParser.cs
Original file line number Diff line number Diff line change
@@ -545,7 +545,7 @@ public string Capture()

private string DebuggerToString()
{
if (_index == -1)
if (_index < 0)
{
return _template;
}
2 changes: 1 addition & 1 deletion src/Http/Routing/src/RouteCollection.cs
Original file line number Diff line number Diff line change
@@ -157,7 +157,7 @@ public virtual async Task RouteAsync(RouteContext context)
var urlWithoutQueryString = url;
var queryString = string.Empty;

if (indexOfSeparator != -1)
if (indexOfSeparator >= 0)
{
urlWithoutQueryString = url.Substring(0, indexOfSeparator);
queryString = url.Substring(indexOfSeparator);
4 changes: 2 additions & 2 deletions src/Http/WebUtilities/src/FormPipeReader.cs
Original file line number Diff line number Diff line change
@@ -167,7 +167,7 @@ private void ParseFormValuesFast(ReadOnlySpan<byte> span,
var ampersand = span.IndexOf(andDelimiter);
ReadOnlySpan<byte> keyValuePair;
int equals;
var foundAmpersand = ampersand != -1;
var foundAmpersand = ampersand >= 0;

if (foundAmpersand)
{
@@ -196,7 +196,7 @@ private void ParseFormValuesFast(ReadOnlySpan<byte> span,

equals = keyValuePair.IndexOf(equalsDelimiter);

if (equals == -1)
if (equals < 0)
{
// Too long for the whole segment to be a key.
if (keyValuePair.Length > KeyLengthLimit)
2 changes: 1 addition & 1 deletion src/Http/WebUtilities/src/HttpRequestStreamReader.cs
Original file line number Diff line number Diff line change
@@ -397,7 +397,7 @@ private ReadLineStepResult ReadLineStep(ref StringBuilder? sb, ref bool consumeL

var index = span.IndexOfAny(carriageReturn, lineFeed);

if (index != -1)
if (index >= 0)
{
if (span[index] == carriageReturn)
{
4 changes: 2 additions & 2 deletions src/Http/WebUtilities/src/QueryHelpers.cs
Original file line number Diff line number Diff line change
@@ -84,14 +84,14 @@ public static string AddQueryString(
var uriToBeAppended = uri.AsSpan();
var anchorText = ReadOnlySpan<char>.Empty;
// If there is an anchor, then the query string must be inserted before its first occurrence.
if (anchorIndex != -1)
if (anchorIndex >= 0)
{
anchorText = uriToBeAppended.Slice(anchorIndex);
uriToBeAppended = uriToBeAppended.Slice(0, anchorIndex);
}

var queryIndex = uriToBeAppended.IndexOf('?');
var hasQuery = queryIndex != -1;
var hasQuery = queryIndex >= 0;

var sb = new StringBuilder();
sb.Append(uriToBeAppended);
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public DefaultPersonalDataProtector(ILookupProtectorKeyRing keyRing, ILookupProt
{
Debug.Assert(data != null);
var split = data.IndexOf(':');
if (split == -1 || split == data.Length - 1)
if (split < 0 || split == data.Length - 1)
{
throw new InvalidOperationException("Malformed data.");
}
Original file line number Diff line number Diff line change
@@ -434,7 +434,7 @@ private static bool TryValidateHost(string host)
}

var firstNonHostCharIdx = host.AsSpan().IndexOfAnyExcept(HostChars);
if (firstNonHostCharIdx == -1)
if (firstNonHostCharIdx < 0)
{
// no port
return true;
2 changes: 1 addition & 1 deletion src/Middleware/Rewrite/src/RedirectRule.cs
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ public void ApplyRule(RewriteContext context)
schemeSplit += Uri.SchemeDelimiter.Length;
var pathSplit = newPath.IndexOf('/', schemeSplit);

if (pathSplit == -1)
if (pathSplit < 0)
{
host = new HostString(newPath.Substring(schemeSplit));
newPath = "/";
2 changes: 1 addition & 1 deletion src/Mvc/Mvc.Core/src/Formatters/AcceptHeaderParser.cs
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ private static bool TryParseValue(string value, ref int index, out MediaTypeSegm
{
// The parsing failed.
currentIndex = value.IndexOf(',', currentIndex);
if (currentIndex == -1)
if (currentIndex < 0)
{
index = value.Length;
return false;
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ private static string NormalizeJQueryToMvc(StringBuilder builder, string key)
}

var indexOpen = key.IndexOf('[');
if (indexOpen == -1)
if (indexOpen < 0)
{
// Fast path, no normalization needed.
// This skips string conversion and allocating the string builder.
@@ -55,7 +55,7 @@ private static string NormalizeJQueryToMvc(StringBuilder builder, string key)
var position = 0;
while (position < key.Length)
{
if (indexOpen == -1)
if (indexOpen < 0)
{
// No more brackets.
builder.Append(key, position, key.Length - position);
@@ -66,7 +66,7 @@ private static string NormalizeJQueryToMvc(StringBuilder builder, string key)

// Find closing bracket.
var indexClose = key.IndexOf(']', indexOpen);
if (indexClose == -1)
if (indexClose < 0)
{
throw new ArgumentException(
message: Resources.FormatJQueryFormValueProviderFactory_MissingClosingBracket(key),
Loading