SmallestCSVParser/SmallestCSVParser.cs
(开头部分) 3KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
// SmallestCSVParser version 1.1.1 - Copyright (C) 2024 Karl Pickett
using System.Text;
namespace SmallestCSV;
public class SmallestCSVParser
{
/// <summary>
/// This is thrown if the CSV has invalid syntax.
/// </summary>
public class Error: Exception {
public Error(string message): base(message) { }
}
/// <summary>
/// This class does not Close/Dispose the `stream`.
/// </summary>
public SmallestCSVParser(StreamReader stream) {
_stream = stream;
_sb = new();
}
/// <summary>
/// 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.
/// </summary>
public List<string>? ReadNextRow(bool removeEnclosingQuotes=true) {
List<string> ret = new();
while (true) {
var (column, hasMore) = ReadNextColumn(removeEnclosingQuotes);
if (column != null) {
ret.Add(column);
}
if (!hasMore) {
return ret.Any() ? ret : null;
}
}
}
private (string? Column, bool RowHasMoreColumns) ReadNextColumn(bool removeEnclosingQuotes) {
_sb.Clear();
switch (_stream.Peek()) {
case -1:
return (null, false);
case '"':
ReadQuotedColumn(removeEnclosingQuotes);
return (_sb.ToString(), !TryFinishLine());
default:
ReadNonQuotedColumn();
return (_sb.ToString(), !TryFinishLine());
}
}
private void ReadQuotedColumn(bool removeEnclosingQuotes) {
后面还有 68 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。