/* 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; namespace dnSpy.Contracts.Hex { /// /// Line position info /// public readonly struct HexLinePositionInfo { /// /// Gets the type /// public HexLinePositionInfoType Type { get; } /// /// true if it's a position within the offset column /// public bool IsOffset => Type == HexLinePositionInfoType.Offset; /// /// true if it's a position within a value cell /// public bool IsValueCell => Type == HexLinePositionInfoType.ValueCell; /// /// true if it's a position within an ASCII cell /// public bool IsAsciiCell => Type == HexLinePositionInfoType.AsciiCell; /// /// true if it's a value cell separator position /// public bool IsValueCellSeparator => Type == HexLinePositionInfoType.ValueCellSeparator; /// /// true if it's a position between two columns /// public bool IsColumnSeparator => Type == HexLinePositionInfoType.ColumnSeparator; /// /// true if it's a position greater than or equal to the line length /// public bool IsVirtualSpace => Type == HexLinePositionInfoType.VirtualSpace; /// /// Gets the line position /// public int Position { get; } /// /// Gets the position within the cell, offset column /// public int CellPosition { get; } /// /// Gets the cell if any /// public HexCell? Cell { get; } HexLinePositionInfo(HexLinePositionInfoType type, int position, int cellPosition) { Type = type; Position = position; CellPosition = cellPosition; Cell = null; } HexLinePositionInfo(HexLinePositionInfoType type, int position, HexCell cell) { Type = type; Position = position; CellPosition = position - cell.CellSpan.Start; if (CellPosition < 0) throw new ArgumentOutOfRangeException(nameof(position)); Cell = cell; } /// /// Creates a position within the offset column /// /// Line position /// Offset character index /// public static HexLinePositionInfo CreateOffset(int linePosition, int offsetIndex) { if (linePosition < 0) throw new ArgumentOutOfRangeException(nameof(linePosition)); if (offsetIndex < 0) throw new ArgumentOutOfRangeException(nameof(offsetIndex)); if (linePosition < offsetIndex) throw new ArgumentOutOfRangeException(nameof(linePosition)); return new HexLinePositionInfo(HexLinePositionInfoType.Offset, linePosition, offsetIndex); } /// /// Creates a position within a value cell /// /// Line position /// Cell /// public static HexLinePositionInfo CreateValue(int linePosition, HexCell cell) { if (linePosition < 0) throw new ArgumentOutOfRangeException(nameof(linePosition)); if (cell is null) throw new ArgumentNullException(nameof(cell)); return new HexLinePositionInfo(HexLinePositionInfoType.ValueCell, linePosition, cell); } /// /// Creates a value cell separator position /// /// Line position /// Cell /// public static HexLinePositionInfo CreateValueCellSeparator(int linePosition, HexCell cell) { if (linePosition < 0) throw new ArgumentOutOfRangeException(nameof(linePosition)); if (cell is null) throw new ArgumentNullException(nameof(cell)); return new HexLinePositionInfo(HexLinePositionInfoType.ValueCellSeparator, linePosition, cell); } /// /// Creates a position within an ASCII cell /// /// Line position /// Cell /// public static HexLinePositionInfo CreateAscii(int linePosition, HexCell cell) { if (linePosition < 0) throw new ArgumentOutOfRangeException(nameof(linePosition)); if (cell is null) throw new ArgumentNullException(nameof(cell)); return new HexLinePositionInfo(HexLinePositionInfoType.AsciiCell, linePosition, cell); } /// /// Creates a column separator position /// /// Line position /// public static HexLinePositionInfo CreateColumnSeparator(int linePosition) { if (linePosition < 0) throw new ArgumentOutOfRangeException(nameof(linePosition)); return new HexLinePositionInfo(HexLinePositionInfoType.ColumnSeparator, linePosition, 0); } /// /// Creates a virtual space position /// /// Line position /// Length of line /// public static HexLinePositionInfo CreateVirtualSpace(int linePosition, int lineLength) { if (linePosition < 0) throw new ArgumentOutOfRangeException(nameof(linePosition)); if (lineLength < 0) throw new ArgumentOutOfRangeException(nameof(lineLength)); if (linePosition < lineLength) throw new ArgumentOutOfRangeException(nameof(linePosition)); return new HexLinePositionInfo(HexLinePositionInfoType.VirtualSpace, linePosition, linePosition - lineLength); } } }