示例#1
0
        /// <summary>
        /// Creates a <see cref="HexLinePositionInfo"/>
        /// </summary>
        /// <param name="linePosition">Line position</param>
        /// <returns></returns>
        public HexLinePositionInfo GetLinePositionInfo(int linePosition)
        {
            if (linePosition >= Text.Length)
            {
                return(HexLinePositionInfo.CreateVirtualSpace(linePosition, Text.Length));
            }

            if (IsOffsetColumnPresent)
            {
                var span = GetOffsetSpan();
                if (span.Contains(linePosition))
                {
                    return(HexLinePositionInfo.CreateOffset(linePosition, linePosition - span.Start));
                }
            }

            if (IsValuesColumnPresent)
            {
                var valuesSpan = GetValuesSpan(onlyVisibleCells: false);
                if (valuesSpan.Contains(linePosition))
                {
                    int cellIndex = (linePosition - valuesSpan.Start) / LineProvider.GetCharsPerCellIncludingSeparator(HexColumnType.Values);
                    var cell      = ValueCells[cellIndex];
                    if (cell.SeparatorSpan.Contains(linePosition))
                    {
                        return(HexLinePositionInfo.CreateValueCellSeparator(linePosition, cell));
                    }
                    return(HexLinePositionInfo.CreateValue(linePosition, cell));
                }
            }

            if (IsAsciiColumnPresent)
            {
                var asciiSpan = GetAsciiSpan(onlyVisibleCells: false);
                if (asciiSpan.Contains(linePosition))
                {
                    int cellIndex = linePosition - asciiSpan.Start;
                    var cell      = AsciiCells[cellIndex];
                    return(HexLinePositionInfo.CreateAscii(linePosition, cell));
                }
            }

            foreach (var column in ColumnOrder)
            {
                var span = GetSpan(column, onlyVisibleCells: false);
                if (span.End == linePosition)
                {
                    return(HexLinePositionInfo.CreateColumnSeparator(linePosition));
                }
            }

            throw new InvalidOperationException();
        }
示例#2
0
        HexLinePositionInfo?GetClosestCellPosition(int linePosition)
        {
            (HexColumnType columnType, HexCell cell)? closest = null;
            int cellPosition = -1;

            foreach (var info in GetCells())
            {
                var cell = info.cell;
                if (closest == null || Compare(linePosition, cell, closest.Value.cell) < 0)
                {
                    closest      = info;
                    cellPosition = linePosition - info.cell.CellSpan.Start;
                    if (cellPosition < 0)
                    {
                        cellPosition = 0;
                    }
                    else if (cellPosition >= info.cell.CellSpan.Length)
                    {
                        cellPosition = info.cell.CellSpan.Length - 1;
                    }
                }
            }
            if (closest == null)
            {
                return(null);
            }
            if (cellPosition < 0 || cellPosition >= closest.Value.cell.CellSpan.Length)
            {
                throw new InvalidOperationException();
            }
            int pos = closest.Value.cell.CellSpan.Start + cellPosition;

            if (closest.Value.columnType == HexColumnType.Values)
            {
                return(HexLinePositionInfo.CreateValue(pos, closest.Value.cell));
            }
            if (closest.Value.columnType == HexColumnType.Ascii)
            {
                return(HexLinePositionInfo.CreateAscii(pos, closest.Value.cell));
            }
            throw new InvalidOperationException();
        }