textinput.cs

来自「csv解析类」· CS 代码 · 共 64 行

CS
64
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace QiHe.CodeLib.Csv
{
    public class TextInput : ParserInput<char>
    {
        string InputText;

        List<int> LineBreaks;

        public TextInput(string text)
        {
            InputText = text;

            LineBreaks = new List<int>();
            LineBreaks.Add(0);
            for (int index = 0; index < InputText.Length; index++)
            {
                if (InputText[index] == '\n')
                {
                    LineBreaks.Add(index);
                }
            }
        }

        #region ParserInput<char> Members

        public bool HasInput(int pos)
        {
            return pos < InputText.Length;
        }

        public char GetInputSymbol(int pos)
        {
            return InputText[pos];
        }

        public string GetSubString(int start, int length)
        {
            return InputText.Substring(start, length);
        }

        public string LocateError(int position, string message)
        {
            int line;
            int col = 1;
            for (line = 1; line < LineBreaks.Count; line++)
            {
                if (LineBreaks[line] > position)
                {
                    col = position - LineBreaks[line - 1];
                    break;
                }
            }
            string ch = HasInput(position) ? " '" + GetInputSymbol(position) + "'" : null;
            return String.Format("Line {0}, Col {1}: {2}{3}", line, col, message, ch);
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?