/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace dnSpy.Contracts.Debugger.DotNet.Code { /// /// IL span /// public readonly struct DbgILSpan : IEquatable { readonly uint start, end; /// /// Start offset /// public uint Start => start; /// /// End offset, exclusive /// public uint End => end; /// /// Length ( - ) /// public uint Length => end - start; /// /// true if it's empty ( is 0) /// public bool IsEmpty => end == start; /// /// Constructor /// /// Start offset /// Length public DbgILSpan(uint start, uint length) { this.start = start; end = start + length; } /// /// Creates a new instance /// /// Start offset /// End offset /// public static DbgILSpan FromBounds(uint start, uint end) { if (end < start) throw new ArgumentOutOfRangeException(nameof(end)); return new DbgILSpan(start, end - start); } internal static List OrderAndCompactList(List input) { if (input.Count <= 1) return input; input.Sort(DbgILSpanComparer.Instance); var res = new List(); var curr = input[0]; res.Add(curr); for (int i = 1; i < input.Count; i++) { var next = input[i]; if (curr.End == next.Start) res[res.Count - 1] = curr = new DbgILSpan(curr.Start, next.End - curr.Start); else if (next.Start > curr.End) { res.Add(next); curr = next; } else if (next.End > curr.End) res[res.Count - 1] = curr = new DbgILSpan(curr.Start, next.End - curr.Start); } return res; } sealed class DbgILSpanComparer : IComparer { public static readonly DbgILSpanComparer Instance = new DbgILSpanComparer(); public int Compare([AllowNull] DbgILSpan x, [AllowNull] DbgILSpan y) { int c = unchecked((int)x.Start - (int)y.Start); if (c != 0) return c; return unchecked((int)y.End - (int)x.End); } } /// /// operator ==() /// /// /// /// public static bool operator ==(DbgILSpan left, DbgILSpan right) => left.Equals(right); /// /// operator !=() /// /// /// /// public static bool operator !=(DbgILSpan left, DbgILSpan right) => !left.Equals(right); /// /// Equals() /// /// /// public bool Equals(DbgILSpan other) => start == other.start && end == other.end; /// /// Equals() /// /// /// public override bool Equals(object? obj) => obj is DbgILSpan && Equals((DbgILSpan)obj); /// /// GetHashCode() /// /// public override int GetHashCode() => (int)(start ^ ((end << 16) | (end >> 16))); /// /// ToString() /// /// public override string ToString() => "[" + start.ToString("X4") + "," + end.ToString("X4") + ")"; } }