示例#1
0
    public string[][] Parse(TextReader reader)
    {
        var context = new ParserContext();

        ParserState currentState = ParserState.LineStartState;
        string      next;

        while ((next = reader.ReadLine()) != null)
        {
            foreach (char ch in next)
            {
                switch (ch)
                {
                case CommaCharacter:
                    currentState = currentState.Comma(context);
                    break;

                case QuoteCharacter:
                    currentState = currentState.Quote(context);
                    break;

                default:
                    currentState = currentState.AnyChar(ch, context);
                    break;
                }
            }
            currentState = currentState.EndOfLine(context);
        }
        List <string[]> allLines = context.GetAllLines();

        return(allLines.ToArray());
    }
        public string[][] Parse(string csvData)
        {
            var context = new ParserContext();

            string[] lines = csvData.Split('\n');

            ParserState currentState = ParserState.LineStartState;

            foreach (string next in lines)
            {
                foreach (char ch in next)
                {
                    switch (ch)
                    {
                    case CommaCharacter:
                        currentState = currentState.Comma(context);
                        break;

                    case QuoteCharacter:
                        currentState = currentState.Quote(context);
                        break;

                    default:
                        currentState = currentState.AnyChar(ch, context);
                        break;
                    }
                }
                currentState = currentState.EndOfLine(context);
            }
            List <string[]> allLines = context.GetAllLines();

            return(allLines.ToArray());
        }
        private string[] ParseLine(string data)
        {
            var         context      = new ParserContext();
            ParserState currentState = ParserState.LineStartState;

            foreach (var ch in data)
            {
                switch (ch)
                {
                case CommaCharacter:
                    currentState = currentState.Comma(context);
                    break;

                case QuoteCharacter:
                    currentState = currentState.Quote(context);
                    break;

                default:
                    currentState = currentState.AnyChar(ch, context);
                    break;
                }
            }

            currentState.EndOfLine(context);
            return(context.Values.ToArray());
        }
示例#4
0
        public string[][] Parse(string csvData)
        {
            var context = new ParserContext();

            // Handle both Windows and Mac line endings
            string[] lines = csvData.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

            ParserState currentState = ParserState.LineStartState;

            foreach (string next in lines)
            {
                foreach (char ch in next)
                {
                    switch (ch)
                    {
                    case CommaCharacter:
                        currentState = currentState.Comma(context);
                        break;

                    case QuoteCharacter:
                        currentState = currentState.Quote(context);
                        break;

                    default:
                        currentState = currentState.AnyChar(ch, context);
                        break;
                    }
                }
                currentState = currentState.EndOfLine(context);
            }
            List <string[]> allLines = context.GetAllLines();

            return(allLines.ToArray());
        }
示例#5
0
        public string[][] Parse(string csvData)
        {
            var context = new ParserContext();

            // Handle both Windows and Mac line endings
            var lines = Regex.Split(csvData, "\n|\r\n");

            ParserState currentState = ParserState.LineStartState;

            for (int i = 0; i < lines.Length; i++)
            {
                var next = lines [i];

                // Skip empty entries
                if (next.Length == 0)
                {
                    continue;
                }

                for (int j = 0; j < next.Length; j++)
                {
                    var ch = next [j];

                    switch (ch)
                    {
                    case CommaCharacter:
                        currentState = currentState.Comma(context);
                        break;

                    case QuoteCharacter:
                        currentState = currentState.Quote(context);
                        break;

                    default:
                        currentState = currentState.AnyChar(ch, context);
                        break;
                    }
                }
                currentState = currentState.EndOfLine(context);
            }

            List <string[]> allLines = context.GetAllLines();

            return(allLines.ToArray());
        }
示例#6
0
        public string[][] Parse(TextReader reader)
        {
            var context = new ParserContext();

            if (MaxColumnsToRead != 0)
            {
                context.MaxColumnsToRead = MaxColumnsToRead;
            }

            ParserState currentState = ParserState.LineStartState;
            string      next;

            while ((next = reader.ReadLine()) != null)
            {
                foreach (char ch in next)
                {
                    switch (ch)
                    {
                    case CommaCharacter:
                        currentState = currentState.Comma(context);
                        break;

                    case QuoteCharacter:
                        currentState = currentState.Quote(context);
                        break;

                    default:
                        currentState = currentState.AnyChar(ch, context);
                        break;
                    }
                }
                currentState = currentState.EndOfLine(context);
            }
            List <string[]> allLines = context.GetAllLines();

            if (TrimTrailingEmptyLines && allLines.Count > 0)
            {
                bool isEmpty = true;
                for (int i = allLines.Count - 1; i >= 0; i--)
                {
// ReSharper disable RedundantAssignment
                    isEmpty = true;
// ReSharper restore RedundantAssignment
                    for (int j = 0; j < allLines[i].Length; j++)
                    {
                        if (!String.IsNullOrEmpty(allLines[i][j]))
                        {
                            isEmpty = false;
                            break;
                        }
                    }
                    if (!isEmpty)
                    {
                        if (i < allLines.Count - 1)
                        {
                            allLines.RemoveRange(i + 1, allLines.Count - i - 1);
                        }
                        break;
                    }
                }
                if (isEmpty)
                {
                    allLines.RemoveRange(0, allLines.Count);
                }
            }
            return(allLines.ToArray());
        }
 public void AnyChar(char ch)
 {
     currState.AnyChar(ch);
 }