Skip to content

Commit

Permalink
Address all IDE0057, use range operator - simplify slice. (#8618)
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist authored Sep 1, 2023
1 parent 163b935 commit 27e71a3
Show file tree
Hide file tree
Showing 37 changed files with 108 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ internal static SiloAddress UnpackRowKey(string rowKey)
int idx1 = rowKey.IndexOf(Seperator);
int idx2 = rowKey.LastIndexOf(Seperator);
ReadOnlySpan<char> rowKeySpan = rowKey.AsSpan();
ReadOnlySpan<char> addressStr = rowKeySpan.Slice(0, idx1);
ReadOnlySpan<char> addressStr = rowKeySpan[..idx1];
ReadOnlySpan<char> portStr = rowKeySpan.Slice(idx1 + 1, idx2 - idx1 - 1);
ReadOnlySpan<char> genStr = rowKeySpan.Slice(idx2 + 1);
ReadOnlySpan<char> genStr = rowKeySpan[(idx2 + 1)..];
IPAddress address = IPAddress.Parse(addressStr);
int port = int.Parse(portStr);
int generation = int.Parse(genStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ string WithoutAssemblyVersion(string input)
if (asmNameIndex >= 0)
{
var asmVersionIndex = input.IndexOf(',', asmNameIndex + 1);
if (asmVersionIndex >= 0) return input.Substring(0, asmVersionIndex);
return input.Substring(0, asmNameIndex);
if (asmVersionIndex >= 0) return input[..asmVersionIndex];
return input[..asmNameIndex];
}

return input;
Expand All @@ -538,7 +538,7 @@ int GetGenericArity(string input)
var arityIndex = input.IndexOf("`", StringComparison.OrdinalIgnoreCase);
if (arityIndex != -1)
{
return int.Parse(input.AsSpan().Slice(arityIndex + 1));
return int.Parse(input.AsSpan()[(arityIndex + 1)..]);
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ internal static SiloAddress UnpackRowKey(string rowKey)
debugInfo = string.Format("UnpackRowKey: RowKey={0} Idx1={1} Idx2={2}", rowKey, idx1, idx2);
#endif
ReadOnlySpan<char> rowKeySpan = rowKey.AsSpan();
ReadOnlySpan<char> addressStr = rowKeySpan.Slice(0, idx1);
ReadOnlySpan<char> addressStr = rowKeySpan[..idx1];
ReadOnlySpan<char> portStr = rowKeySpan.Slice(idx1 + 1, idx2 - idx1 - 1);
ReadOnlySpan<char> genStr = rowKeySpan.Slice(idx2 + 1);
ReadOnlySpan<char> genStr = rowKeySpan[(idx2 + 1)..];
#if DEBUG
debugInfo = string.Format("UnpackRowKey: RowKey={0} -> Address={1} Port={2} Generation={3}",
rowKey, addressStr.ToString(), portStr.ToString(), genStr.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private void Initialize()
private static string ExtractInstanceName(string instanceId, string deploymentId)
{
return instanceId.Length > deploymentId.Length && instanceId.StartsWith(deploymentId, StringComparison.Ordinal)
? instanceId.Substring(deploymentId.Length + 1)
? instanceId[(deploymentId.Length + 1)..]
: instanceId;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string MakeRowKey(long sequenceId)
return $"{RK_PREFIX}{sequenceId.ToString("x16")}";
}

public long SequenceId => long.Parse(this.RowKey.Substring(RK_PREFIX.Length), NumberStyles.AllowHexSpecifier);
public long SequenceId => long.Parse(this.RowKey[RK_PREFIX.Length..], NumberStyles.AllowHexSpecifier);

// Row keys range from s0000000000000001 to s7fffffffffffffff
public const string RK_PREFIX = "s_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public bool TryGetPredicate(string predicatePattern, out IChannelNamespacePredic
predicate = new AllStreamNamespacesPredicate();
return true;
case var regex when regex.StartsWith(RegexChannelNamespacePredicate.Prefix, StringComparison.Ordinal):
predicate = new RegexChannelNamespacePredicate(regex.Substring(RegexChannelNamespacePredicate.Prefix.Length));
predicate = new RegexChannelNamespacePredicate(regex[RegexChannelNamespacePredicate.Prefix.Length..]);
return true;
case var ns when ns.StartsWith(ExactMatchChannelNamespacePredicate.Prefix, StringComparison.Ordinal):
predicate = new ExactMatchChannelNamespacePredicate(ns.Substring(ExactMatchChannelNamespacePredicate.Prefix.Length));
predicate = new ExactMatchChannelNamespacePredicate(ns[ExactMatchChannelNamespacePredicate.Prefix.Length..]);
return true;
}

Expand Down Expand Up @@ -67,13 +67,13 @@ public bool TryGetPredicate(string predicatePattern, out IChannelNamespacePredic
var index = predicatePattern.IndexOf(':', start);
if (index < 0)
{
typeName = predicatePattern.Substring(start);
typeName = predicatePattern[start..];
arg = null;
}
else
{
typeName = predicatePattern.Substring(start, index - start);
arg = predicatePattern.Substring(index + 1);
typeName = predicatePattern[start..index];
arg = predicatePattern[(index + 1)..];
}

var type = Type.GetType(typeName, throwOnError: true);
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Core.Abstractions/IDs/ClientGrainId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static bool TryParse(GrainId grainId, out ClientGrainId clientId)
var key = grainId.Key.AsSpan();
if (key.IndexOf((byte)ObserverGrainId.SegmentSeparator) is int index && index >= 0)
{
key = key.Slice(0, index);
key = key[..index];
grainId = new GrainId(grainId.Type, new IdSpan(key.ToArray()));
}

Expand Down
16 changes: 8 additions & 8 deletions src/Orleans.Core.Abstractions/IDs/GrainIdKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IdSpan CreateIntegerKey(long key)
Span<byte> buf = stackalloc byte[sizeof(long) * 2];
Utf8Formatter.TryFormat(key, buf, out var len, 'X');
Debug.Assert(len > 0, "Unable to format the provided value as a UTF8 string");
return new IdSpan(buf.Slice(0, len).ToArray());
return new IdSpan(buf[..len].ToArray());
}

/// <summary>
Expand Down Expand Up @@ -81,7 +81,7 @@ public static IdSpan CreateIntegerKey(long key, string? keyExtension)

var extLen = Encoding.UTF8.GetByteCount(keyExtension);
var buf = new byte[len + 1 + extLen];
tmp.Slice(0, len).CopyTo(buf);
tmp[..len].CopyTo(buf);
buf[len] = (byte)'+';
Encoding.UTF8.GetBytes(keyExtension, 0, keyExtension.Length, buf, len + 1);

Expand Down Expand Up @@ -180,8 +180,8 @@ public static bool TryGetIntegerKey(this GrainId grainId, out long key, out stri
var keyString = grainId.Key.AsSpan();
if (keyString.IndexOf((byte)'+') is int index && index >= 0)
{
keyExt = Encoding.UTF8.GetString(keyString.Slice(index + 1));
keyString = keyString.Slice(0, index);
keyExt = Encoding.UTF8.GetString(keyString[(index + 1)..]);
keyString = keyString[..index];
}

return Utf8Parser.TryParse(keyString, out key, out var len, 'X') && len == keyString.Length;
Expand All @@ -203,7 +203,7 @@ internal static bool TryGetIntegerKey(this GrainId grainId, out long key)
{
var keyString = grainId.Key.AsSpan();
if (keyString.IndexOf((byte)'+') is int index && index >= 0)
keyString = keyString.Slice(0, index);
keyString = keyString[..index];

return Utf8Parser.TryParse(keyString, out key, out var len, 'X') && len == keyString.Length;
}
Expand Down Expand Up @@ -260,8 +260,8 @@ public static bool TryGetGuidKey(this GrainId grainId, out Guid key, out string?
var keyString = grainId.Key.AsSpan();
if (keyString.Length > 32 && keyString[32] == (byte)'+')
{
keyExt = Encoding.UTF8.GetString(keyString.Slice(33));
keyString = keyString.Slice(0, 32);
keyExt = Encoding.UTF8.GetString(keyString[33..]);
keyString = keyString[..32];
}
else if (keyString.Length != 32)
{
Expand Down Expand Up @@ -289,7 +289,7 @@ internal static bool TryGetGuidKey(this GrainId grainId, out Guid key)
var keyString = grainId.Key.AsSpan();
if (keyString.Length > 32 && keyString[32] == (byte)'+')
{
keyString = keyString.Slice(0, 32);
keyString = keyString[..32];
}
else if (keyString.Length != 32)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Orleans.Core.Abstractions/IDs/Legacy/LegacyGrainId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private static LegacyGrainId FromGrainIdInternal(GrainId id)
var keySpan = id.Key.Value.Span;
if (keySpan.Length < 32) return null;

if (!Utf8Parser.TryParse(keySpan.Slice(0, 16), out ulong n0, out len, 'X') || len < 16)
if (!Utf8Parser.TryParse(keySpan[..16], out ulong n0, out len, 'X') || len < 16)
return null;

if (!Utf8Parser.TryParse(keySpan.Slice(16, 16), out ulong n1, out len, 'X') || len < 16)
Expand All @@ -236,7 +236,7 @@ private static LegacyGrainId FromGrainIdInternal(GrainId id)
if (keySpan.Length > 32)
{
if (keySpan[32] != '+') return null;
keyExt = Encoding.UTF8.GetString(keySpan.Slice(33));
keyExt = Encoding.UTF8.GetString(keySpan[33..]);
}

return FindOrCreateGrainId(UniqueKey.NewKey(n0, n1, typeCodeData, keyExt));
Expand Down Expand Up @@ -301,7 +301,7 @@ private string ToStringImpl(bool detailed)
if (!detailed)
{
if (keyString.Length >= 48)
idString = keyString.Substring(24, 8) + keyString.Substring(48);
idString = keyString.Substring(24, 8) + keyString[48..];
else
idString = keyString.Substring(24, 8);
}
Expand All @@ -312,7 +312,7 @@ private string ToStringImpl(bool detailed)
case UniqueKey.Category.Grain:
case UniqueKey.Category.KeyExtGrain:
var typeString = TypeCode.ToString("X");
if (!detailed) typeString = typeString.Substring(Math.Max(0, typeString.Length - 8));
if (!detailed) typeString = typeString[Math.Max(0, typeString.Length - 8)..];
fullString = $"*grn/{typeString}/{idString}";
break;
case UniqueKey.Category.Client:
Expand Down
8 changes: 4 additions & 4 deletions src/Orleans.Core.Abstractions/IDs/Legacy/UniqueKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ internal static UniqueKey Parse(ReadOnlySpan<char> input)
input = input.Trim();
if (input.Length >= minimumValidKeyLength)
{
var n0 = ulong.Parse(input.Slice(0, 16).ToString(), NumberStyles.AllowHexSpecifier);
var n0 = ulong.Parse(input[..16].ToString(), NumberStyles.AllowHexSpecifier);
var n1 = ulong.Parse(input.Slice(16, 16).ToString(), NumberStyles.AllowHexSpecifier);
var typeCodeData = ulong.Parse(input.Slice(32, 16).ToString(), NumberStyles.AllowHexSpecifier);
string keyExt = null;
if (input.Length > minimumValidKeyLength)
{
if (input[48] != '+') throw new InvalidDataException("UniqueKey hex string missing + separator.");
keyExt = input.Slice(49).ToString();
keyExt = input[49..].ToString();
}

return NewKey(n0, n1, typeCodeData, keyExt);
Expand Down Expand Up @@ -265,7 +265,7 @@ internal ReadOnlySpan<byte> ToByteArray()
if (extBytes != null)
{
BinaryPrimitives.WriteInt32LittleEndian(spanBytes.Slice(offset, sizeof(int)), extBytesLength);
extBytes.CopyTo(spanBytes.Slice(offset + sizeof(int)));
extBytes.CopyTo(spanBytes[(offset + sizeof(int))..]);
}
else
{
Expand Down Expand Up @@ -299,7 +299,7 @@ private unsafe Guid Guid
{
var guid = value.ToByteArray().AsSpan();
N0 = BinaryPrimitives.ReadUInt64LittleEndian(guid);
N1 = BinaryPrimitives.ReadUInt64LittleEndian(guid.Slice(8));
N1 = BinaryPrimitives.ReadUInt64LittleEndian(guid[8..]);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/Orleans.Core.Abstractions/IDs/SiloAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@ internal byte[] ToUtf8String()
size = Encoding.UTF8.GetBytes(addr, buf);

buf[size++] = (byte)':';
var success = Utf8Formatter.TryFormat(Endpoint.Port, buf.Slice(size), out len);
var success = Utf8Formatter.TryFormat(Endpoint.Port, buf[size..], out len);
Debug.Assert(success);
Debug.Assert(len > 0);
Debug.Assert(len <= 11);
size += len;

buf[size++] = (byte)SEPARATOR;
success = Utf8Formatter.TryFormat(Generation, buf.Slice(size), out len);
success = Utf8Formatter.TryFormat(Generation, buf[size..], out len);
Debug.Assert(success);
Debug.Assert(len > 0);
Debug.Assert(len <= 11);
size += len;

utf8 = buf.Slice(0, size).ToArray();
utf8 = buf[..size].ToArray();
}

return utf8;
Expand Down Expand Up @@ -193,7 +193,7 @@ public static SiloAddress FromUtf8String(ReadOnlySpan<byte> addr)
if (atSign < 0) ThrowInvalidUtf8SiloAddress(addr);

// IPEndpoint is the host, then ':', then the port
var endpointSlice = addr.Slice(0, atSign);
var endpointSlice = addr[..atSign];
int lastColon = endpointSlice.LastIndexOf((byte)':');
if (lastColon < 0) ThrowInvalidUtf8SiloAddress(addr);

Expand All @@ -205,11 +205,11 @@ public static SiloAddress FromUtf8String(ReadOnlySpan<byte> addr)
if (!IPAddress.TryParse(hostString, out var host))
ThrowInvalidUtf8SiloAddress(addr);

var portSlice = endpointSlice.Slice(lastColon + 1);
var portSlice = endpointSlice[(lastColon + 1)..];
if (!Utf8Parser.TryParse(portSlice, out int port, out len) || len < portSlice.Length)
ThrowInvalidUtf8SiloAddress(addr);

var genSlice = addr.Slice(atSign + 1);
var genSlice = addr[(atSign + 1)..];
if (!Utf8Parser.TryParse(genSlice, out int generation, out len) || len < genSlice.Length)
ThrowInvalidUtf8SiloAddress(addr);

Expand Down Expand Up @@ -304,9 +304,9 @@ private uint[] GetUniformHashCodesImpl(int numHashes)
if (address.AddressFamily == AddressFamily.InterNetwork) // IPv4
{
#pragma warning disable CS0618 // Type or member is obsolete
BinaryPrimitives.WriteInt32LittleEndian(bytes.Slice(12), (int)address.Address);
BinaryPrimitives.WriteInt32LittleEndian(bytes[12..], (int)address.Address);
#pragma warning restore CS0618
bytes.Slice(0, 12).Clear();
bytes[..12].Clear();
}
else // IPv6
{
Expand All @@ -315,16 +315,16 @@ private uint[] GetUniformHashCodesImpl(int numHashes)
}
var offset = 16;
// Port
BinaryPrimitives.WriteInt32LittleEndian(bytes.Slice(offset), Endpoint.Port);
BinaryPrimitives.WriteInt32LittleEndian(bytes[offset..], Endpoint.Port);
offset += sizeof(int);
// Generation
BinaryPrimitives.WriteInt32LittleEndian(bytes.Slice(offset), Generation);
BinaryPrimitives.WriteInt32LittleEndian(bytes[offset..], Generation);
offset += sizeof(int);

var hashes = new uint[numHashes];
for (int extraBit = 0; extraBit < numHashes; extraBit++)
{
BinaryPrimitives.WriteInt32LittleEndian(bytes.Slice(offset), extraBit);
BinaryPrimitives.WriteInt32LittleEndian(bytes[offset..], extraBit);
hashes[extraBit] = StableHash.ComputeHash(bytes);
}
return hashes;
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Core.Abstractions/IDs/SystemTargetGrainId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public SystemTargetGrainId WithSiloAddress(SiloAddress siloAddress)
var key = _grainId.Key.AsSpan();
if (key.IndexOf((byte)SegmentSeparator) is int index && index >= 0)
{
var extraIdentifier = key.Slice(index + 1);
var extraIdentifier = key[(index + 1)..];

var buf = new byte[addr.Length + 1 + extraIdentifier.Length];
addr.CopyTo(buf.AsSpan());
Expand All @@ -147,7 +147,7 @@ public SiloAddress GetSiloAddress()
var key = _grainId.Key.AsSpan();
if (key.IndexOf((byte)SegmentSeparator) is int index && index >= 0)
{
key = key.Slice(0, index);
key = key[..index];
}

return SiloAddress.FromUtf8String(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static MajorMinorVersion Parse(string value)

var i = value.IndexOf('.');
if (i < 0) throw new ArgumentException(nameof(value));
return new MajorMinorVersion(long.Parse(value.Substring(0, i)), long.Parse(value.Substring(i + 1)));
return new MajorMinorVersion(long.Parse(value[..i]), long.Parse(value[(i + 1)..]));
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private static Cache BuildCache(ClusterManifest clusterManifest)
// A specified default grain type trumps others.
primaryImplementation = GrainType.Create(defaultTypeString);
}
else if (string.Equals(interfaceTypeName?.Substring(1), typeName, StringComparison.Ordinal))
else if (string.Equals(interfaceTypeName?[1..], typeName, StringComparison.Ordinal))
{
// Otherwise, a substring match on the interface name, dropping the 'I', is used.
primaryImplementation = id;
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Core/Manifest/GrainBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ bool TryExtractBindingProperty(KeyValuePair<string, string> property, out (strin
return false;
}

var bindingIndex = property.Key.Substring(BindingPrefix.Length, indexEndIndex - BindingPrefix.Length);
var bindingKey = property.Key.Substring(indexEndIndex + 1);
var bindingIndex = property.Key[BindingPrefix.Length..indexEndIndex];
var bindingKey = property.Key[(indexEndIndex + 1)..];

if (string.IsNullOrWhiteSpace(bindingIndex) || string.IsNullOrWhiteSpace(bindingKey))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Orleans.Core/Manifest/GrainTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ private GrainType GetGrainTypeByConvention(Type type)
var index = name.IndexOf('`');
if (index > 0)
{
name = name.Substring(0, index);
name = name[..index];
}

// Trim "Grain" suffix
index = name.LastIndexOf(GrainSuffix);
if (index > 0 && name.Length - index == GrainSuffix.Length)
{
name = name.Substring(0, index);
name = name[..index];
}

// Append the generic arity, eg typeof(MyListGrain<T>) would eventually become mylist`1
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Core/Messaging/MessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public MessageSerializer(
Span<byte> lengthBytes = stackalloc byte[FramingLength];
input.Slice(input.Start, FramingLength).CopyTo(lengthBytes);
var headerLength = BinaryPrimitives.ReadInt32LittleEndian(lengthBytes);
var bodyLength = BinaryPrimitives.ReadInt32LittleEndian(lengthBytes.Slice(4));
var bodyLength = BinaryPrimitives.ReadInt32LittleEndian(lengthBytes[4..]);

// Check lengths
ThrowIfLengthsInvalid(headerLength, bodyLength);
Expand Down
Loading

0 comments on commit 27e71a3

Please sign in to comment.