mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-18 03:51:19 -10:00
* Reduce allocation in Index/Range.ToString * Address PR feedback Signed-off-by: dotnet-bot <[email protected]>
73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Diagnostics;
|
|
|
|
namespace System
|
|
{
|
|
public readonly struct Range : IEquatable<Range>
|
|
{
|
|
public Index Start { get; }
|
|
public Index End { get; }
|
|
|
|
private Range(Index start, Index end)
|
|
{
|
|
Start = start;
|
|
End = end;
|
|
}
|
|
|
|
public override bool Equals(object value)
|
|
{
|
|
if (value is Range)
|
|
{
|
|
Range r = (Range)value;
|
|
return r.Start.Equals(Start) && r.End.Equals(End);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool Equals (Range other) => other.Start.Equals(Start) && other.End.Equals(End);
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(Start.GetHashCode(), End.GetHashCode());
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
Span<char> span = stackalloc char[2 + (2 * 11)]; // 2 for "..", then for each index 1 for '^' and 10 for longest possible uint
|
|
int charsWritten;
|
|
int pos = 0;
|
|
|
|
if (Start.FromEnd)
|
|
{
|
|
span[0] = '^';
|
|
pos = 1;
|
|
}
|
|
bool formatted = ((uint)Start.Value).TryFormat(span.Slice(pos), out charsWritten);
|
|
Debug.Assert(formatted);
|
|
pos += charsWritten;
|
|
|
|
span[pos++] = '.';
|
|
span[pos++] = '.';
|
|
|
|
if (End.FromEnd)
|
|
{
|
|
span[pos++] = '^';
|
|
}
|
|
formatted = ((uint)End.Value).TryFormat(span.Slice(pos), out charsWritten);
|
|
Debug.Assert(formatted);
|
|
pos += charsWritten;
|
|
|
|
return new string(span.Slice(0, pos));
|
|
}
|
|
|
|
public static Range Create(Index start, Index end) => new Range(start, end);
|
|
public static Range FromStart(Index start) => new Range(start, new Index(0, fromEnd: true));
|
|
public static Range ToEnd(Index end) => new Range(new Index(0, fromEnd: false), end);
|
|
public static Range All() => new Range(new Index(0, fromEnd: false), new Index(0, fromEnd: true));
|
|
}
|
|
}
|