Example #1
0
        /// <summary>
        /// Gets the "line" as defined by the Positions array.  This assumes that the positions
        /// mark the beginning of the "lines".
        /// </summary>
        /// <param name="index">The index in question in the input.</param>
        /// <param name="lineNum">The 1-based line number in which the index is found.</param>
        /// <param name="lineOffset">The offset of the index from the beginning of the line.</param>
        /// <returns></returns>
        public IEnumerable <TInput> GetLine(int index, out int lineNum, out int lineOffset)
        {
            if (sortedPositions == null || sortedPositions.Length != Positions.Count)
            {
                if (Positions.Count == 0 || !Positions.Contains(0))
                {
                    Positions.Add(0);
                }

                sortedPositions = Positions.OrderBy(n => n).ToArray();
            }

            int start, next;
            var srch       = Array.BinarySearch(sortedPositions, index);
            var srchOffset = sortedPositions[0] == 0 ? 1 : 2;

            // we are on the the beginning of the line
            if (srch >= 0)
            {
                start = sortedPositions[srch];
                if (srch + 1 < sortedPositions.Length)
                {
                    next = sortedPositions[srch + 1];
                }
                else
                {
                    next = int.MaxValue;
                }

                lineNum    = srch + srchOffset;
                lineOffset = 0;
            }
            else
            {
                // srch is the index of the next largest position
                srch = ~srch;

                if (srch == 0)
                {
                    start = 0;
                    next  = sortedPositions[srch];
                }
                else if (srch < sortedPositions.Length)
                {
                    start = sortedPositions[srch - 1];
                    next  = sortedPositions[srch];
                }
                else
                {
                    start = sortedPositions[sortedPositions.Length - 1];
                    next  = int.MaxValue;
                }

                lineNum    = srch + (srchOffset - 1);
                lineOffset = index - start;
            }

            // get subsequence
            if (InputString != null)
            {
                int li = start;
                while (li < next && li < InputString.Length &&
                       InputString[li] != '\r' && InputString[li] != '\n')
                {
                    ++li;
                }
                return((IEnumerable <TInput>)(object) InputString.Substring(start, li - start));
            }
            else if (InputList != null)
            {
                next = Math.Min(InputList.Count, next - start);
                return(InputList.Skip(start).Take(next - start));
            }
            else
            {
                return(InputEnumerable
                       .Skip(start)
                       .TakeWhile((item, i) => i + start < next));
            }
        }
 private void UpdateDisplay()
 {
     CurrentDisplay = InputList.Skip((CurrentPage - 1) * ItemsPerPage).Take(ItemsPerPage).ToList();
 }