SmallestCSVParserBenchmark/OldVersions/SmallestCSVParser-1.0.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

// SmallestCSVParser version 1.0 - Copyright (C) 2024 Karl Pickett
using System.Diagnostics;
using System.Text;

namespace SmallestCSV;


public class SmallestCSVParser_1_0
{
    public class Error: Exception {
        public Error(string message): base(message) { }
    }

    public SmallestCSVParser_1_0(StreamReader stream) {
        _stream = stream;
        _sb = new();
    }

    /*
       Read all columns for the next row/line.
       If we are at end of file, this returns null.

       By default, columns that were quoted (") have their enclosing quotes
       removed.  Set `removeEnclosingQuotes` to false if you want to preserve
       the quotes, for example to distinguish between an empty quoted vs
       unquoted column.
    */
    public List<string>? ReadNextRow(bool removeEnclosingQuotes=true) {
        List<string> ret = new();
        while (true) {
            var (column, hasMore) = ReadNextColumn();
            if (column != null) {
                if (removeEnclosingQuotes && column.StartsWith('"')) {
                    Trace.Assert(column.EndsWith('"'));
                    column = column.Substring(1, column.Length - 2);
                }
                ret.Add(column);
            }
            if (!hasMore) {
                return ret.Any() ? ret : null;
            }
        }
    }

    private (string? Column, bool RowHasMoreColumns) ReadNextColumn() {
        _sb.Clear();
        switch (_stream.Peek()) {
            case -1:
                return (null, false);
            case '"':
                _sb.Append((char)_stream.Read());
                foreach (var ch in ReadQuotedColumn()) { _sb.Append(ch); }
                return (_sb.ToString(), !TryFinishLine());
            default:
                foreach (var ch in ReadNonQuotedColumn()) { _sb.Append(ch); }
                return (_sb.ToString(), !TryFinishLine());
        }
    }

    private IEnumerable<char> ReadQuotedColumn() {
后面还有 62 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。