Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed May 21, 2024
1 parent d0b6e3b commit 1cd0c50
Show file tree
Hide file tree
Showing 74 changed files with 2,595 additions and 1,267 deletions.
6 changes: 3 additions & 3 deletions src/CsvHelper/Compatibility/AsyncExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace CsvHelper;
#if !(NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER)
namespace System.IO;

internal static class AsyncExtensions
{
#if !(NETSTANDARD2_1_OR_GREATER || NET)
public static ValueTask DisposeAsync(this TextWriter textWriter)
{
if (textWriter != null)
Expand All @@ -12,5 +12,5 @@ public static ValueTask DisposeAsync(this TextWriter textWriter)

return default;
}
#endif
}
#endif
23 changes: 23 additions & 0 deletions src/CsvHelper/Compatibility/BitOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if !NET6_0_OR_GREATER
namespace System.Numerics;

internal static class BitOperations
{
public static int TrailingZeroCount(int value)
{
if (value == 0)
{
return 32;
}

var result = 0;
while ((value & 1) == 0)
{
value >>= 1;
result++;
}

return result;
}
}
#endif
5 changes: 5 additions & 0 deletions src/CsvHelper/Compatibility/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#if !NET6_0_OR_GREATER
namespace System.Runtime.CompilerServices;

internal static class IsExternalInit { }
#endif
12 changes: 12 additions & 0 deletions src/CsvHelper/Compatibility/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if !NET48_OR_GREATER
namespace System.Linq;

internal static class ListExtensions
{
public static List<T> Append<T>(this List<T> list, T item)
{
list.Add(item);
return list;
}
}
#endif
28 changes: 28 additions & 0 deletions src/CsvHelper/Compatibility/TextReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
using System.Buffers;

namespace System.IO;

internal static class TextReaderExtensions
{
public static int ReadBlock(this TextReader textReader, Span<char> buffer)
{
char[] array = ArrayPool<char>.Shared.Rent(buffer.Length);

try
{
int numRead = textReader.ReadBlock(array, 0, buffer.Length);
if ((uint)numRead > (uint)buffer.Length)
{
throw new IOException("Invalid read length.");
}
new Span<char>(array, 0, numRead).CopyTo(buffer);
return numRead;
}
finally
{
ArrayPool<char>.Shared.Return(array);
}
}
}
#endif
23 changes: 23 additions & 0 deletions src/CsvHelper/Compatibility/TextWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if !NET6_0_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
using System.Buffers;

namespace System.IO;

internal static class TextWriterExtensions
{
public static void Write(this TextWriter textWriter, ReadOnlySpan<char> buffer)
{
char[] array = ArrayPool<char>.Shared.Rent(buffer.Length);

try
{
buffer.CopyTo(new Span<char>(array));
textWriter.Write(array, 0, buffer.Length);
}
finally
{
ArrayPool<char>.Shared.Return(array);
}
}
}
#endif
File renamed without changes.
45 changes: 45 additions & 0 deletions src/CsvHelper/Configuration/CsvOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace CsvHelper.Configuration;

public abstract record CsvOptions
{
public char Delimiter { get; init; } = ',';

public char Escape { get; init; } = '\"';

public char? NewLine { get; init; }

internal int BufferSize = 0x1000;

internal void Validate()
{
if (Delimiter == Escape)
{
throw new ConfigurationException($"{nameof(Delimiter)} and {nameof(Escape)} cannot be the same.");
}

if (Delimiter == NewLine)
{
throw new ConfigurationException($"{nameof(Delimiter)} and {nameof(NewLine)} cannot be the same.");
}

if (Escape == NewLine)
{
throw new ConfigurationException($"{nameof(Escape)} and {nameof(NewLine)} cannot be the same.");
}

if (Delimiter < 1)
{
throw new ConfigurationException($"{nameof(Delimiter)} must be greater than 0.");
}

if (Escape < 1)
{
throw new ConfigurationException($"{nameof(Escape)} must be greater than 0.");
}

if (NewLine < 1)
{
throw new ConfigurationException($"{nameof(NewLine)} must be greater than 0.");
}
}
}
34 changes: 34 additions & 0 deletions src/CsvHelper/Configuration/CsvParserOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace CsvHelper.Configuration;

public record CsvParserOptions : CsvOptions
{
public CsvMode Mode { get; init; }

public bool CacheFields { get; init; }

public ParsingStrategy? ParsingStrategy { get; init; }

internal StringCreator StringCreator = (chars, i)
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
=> new string(chars);
#else
=> chars.ToString();
#endif

internal int ParsedRowsSize = 8;

internal int ParsedFieldsSize = 8 * 32;

internal CsvModeParse ModeParse = ModeRfc4180.Parse;

internal ICsvParsingStrategy ParsingStrategyImplementation = new ThrowParsingStrategy();

internal char[] ValidSpecialCharsForIntrinsics = Enumerable
.Range(32, 127 - 32)
.Select(i => (char)i)
.ToList()
.Append('\t')
.Append('\r')
.Append('\n')
.ToArray();
}
12 changes: 12 additions & 0 deletions src/CsvHelper/Configuration/CsvSerializerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CsvHelper.Configuration;

public delegate bool ShouldEscape(ReadOnlySpan<char> field);

public record CsvSerializerOptions : CsvOptions
{
public CsvMode Mode { get; init; }

public ShouldEscape? ShouldEscape { get; init; }

internal CsvModeEscape ModeEscape = ModeRfc4180.Escape;
}
12 changes: 12 additions & 0 deletions src/CsvHelper/Configuration/ParsingStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CsvHelper.Configuration;

public enum ParsingStrategy
{
IndexOfAny = 0,
#if NET7_0_OR_GREATER
Vector256 = 1,
#endif
#if NET6_0_OR_GREATER
Avx2 = 2,
#endif
}
3 changes: 3 additions & 0 deletions src/CsvHelper/Configuration/StringCreatorDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace CsvHelper.Configuration;

public delegate string StringCreator(ReadOnlySpan<char> chars, int columnIndex);
14 changes: 14 additions & 0 deletions src/CsvHelper/CsvField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics;

namespace CsvHelper;

[DebuggerDisplay("Start = {Start}, Length = {Length}, EscapeMask = {EscapeMask}")]
internal record struct CsvField
(
int Start,
int Length,
int EscapeMask,
char[] EscapeBuffer,
int EscapeLength,
bool IsInvalid
);
15 changes: 15 additions & 0 deletions src/CsvHelper/CsvHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,51 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<!-- .NET 4.7 -->
<ItemGroup Condition="'$(TargetFramework)' == 'net47'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<!-- .NET 4.8 -->
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<!-- .NET Standard 2.0 -->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<!-- .NET Standard 2.1 -->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 1cd0c50

Please sign in to comment.