Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Display dotnet version
run: dotnet --version
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Display dotnet version
run: dotnet --version
Expand Down
59 changes: 0 additions & 59 deletions CsvHelper.sln

This file was deleted.

14 changes: 14 additions & 0 deletions CsvHelper.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Solution>
<Folder Name="/Solution Items/">
<File Path=".editorconfig" />
<File Path=".gitignore" />
<File Path="README.markdown" />
</Folder>
<Project Path="performance/CsvHelper.Performance/CsvHelper.Performance.csproj" />
<Project Path="src/CsvHelper/CsvHelper.csproj" />
<Project Path="TestApp/TestApp.csproj" />
<Project Path="tests/CsvHelper.Tests/CsvHelper.Tests.csproj" />
<Properties Name="Performance">
<Property Name="HasPerformanceSessions" Value="true" />
</Properties>
</Solution>
2 changes: 1 addition & 1 deletion TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishTrimmed>true</PublishTrimmed>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.5" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions src/CsvHelper/Configuration/ConfigurationFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace CsvHelper.Configuration;
/// <summary>Holds the default callback methods for delegate members of <c>CsvHelper.Configuration.Configuration</c>.</summary>
public static class ConfigurationFunctions
{
private static readonly char[] lineEndingChars = ['\r', '\n'];

/// <summary>
/// Throws a <c>MissingFieldException</c>.
/// </summary>
Expand Down
20 changes: 1 addition & 19 deletions src/CsvHelper/Configuration/CsvConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public record CsvConfiguration : IReaderConfiguration, IWriterConfiguration
/// <inheritdoc/>
public virtual bool DetectColumnCountChanges { get; set; }

/// <inheritdoc/>
public virtual IComparer<string> DynamicPropertySort { get; set; }

/// <inheritdoc/>
public virtual Encoding Encoding { get; set; } = Encoding.UTF8;

Expand All @@ -65,18 +62,6 @@ public record CsvConfiguration : IReaderConfiguration, IWriterConfiguration
/// <inheritdoc/>
public virtual bool IgnoreBlankLines { get; set; } = true;

/// <inheritdoc/>
public virtual bool IgnoreReferences { get; set; }

/// <inheritdoc/>
public virtual bool IncludePrivateMembers { get; set; }

/// <inheritdoc/>
public virtual char[] InjectionCharacters { get; set; } = ['=', '@', '+', '-'];

/// <inheritdoc/>
public virtual char InjectionEscapeCharacter { get; set; } = '\t';

/// <inheritdoc/>
public bool IsNewLineSet { get; private set; }

Expand Down Expand Up @@ -122,10 +107,7 @@ public virtual string NewLine
public virtual TrimOptions TrimOptions { get; set; }

/// <inheritdoc/>
public virtual bool UseNewObjectForNullReferenceMembers { get; set; } = true;

/// <inheritdoc/>
public virtual char[] WhiteSpaceChars { get; set; } = new char[] { ' ' };
public virtual char[] WhiteSpaceChars { get; set; } = [' '];

/// <summary>
/// Initializes a new instance of the <see cref="CsvConfiguration"/> class
Expand Down
7 changes: 0 additions & 7 deletions src/CsvHelper/Configuration/IReaderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ public interface IReaderConfiguration : IParserConfiguration
/// </summary>
ShouldSkipRecord ShouldSkipRecord { get; }

/// <summary>
/// Gets a value indicating if private
/// member should be read from and written to.
/// <c>true</c> to include private member, otherwise <c>false</c>. Default is false.
/// </summary>
bool IncludePrivateMembers { get; }

/// <summary>
/// Gets a value indicating whether changes in the column
/// count should be detected. If true, a <see cref="BadDataException"/>
Expand Down
4 changes: 2 additions & 2 deletions src/CsvHelper/CsvHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!-- Build -->
<AssemblyName>CsvHelper</AssemblyName>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net9.0</TargetFrameworks>
<RootNamespace>CsvHelper</RootNamespace>
<DefaultLanguage>en-US</DefaultLanguage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.133" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.8.118" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
</Project>
21 changes: 7 additions & 14 deletions src/CsvHelper/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class CsvReader : IReader
private readonly bool hasHeaderRecord;
private readonly ShouldSkipRecord shouldSkipRecord;
private readonly ReadingExceptionOccurred readingExceptionOccurred;
private readonly CultureInfo cultureInfo;
private readonly bool ignoreBlankLines;
private readonly MissingFieldFound missingFieldFound;
private readonly bool includePrivateMembers;
private readonly PrepareHeaderForMatch prepareHeaderForMatch;

private CsvContext context;
Expand Down Expand Up @@ -79,11 +77,9 @@ public CsvReader(IParser parser)
context = parser.Context ?? throw new InvalidOperationException($"For {nameof(IParser)} to be used in {nameof(CsvReader)}, {nameof(IParser.Context)} must also implement {nameof(CsvContext)}.");
context.Reader = this;

cultureInfo = Configuration.CultureInfo;
detectColumnCountChanges = Configuration.DetectColumnCountChanges;
hasHeaderRecord = Configuration.HasHeaderRecord;
ignoreBlankLines = Configuration.IgnoreBlankLines;
includePrivateMembers = Configuration.IncludePrivateMembers;
missingFieldFound = Configuration.MissingFieldFound;
prepareHeaderForMatch = Configuration.PrepareHeaderForMatch;
readingExceptionOccurred = Configuration.ReadingExceptionOccurred;
Expand Down Expand Up @@ -261,16 +257,13 @@ public virtual string GetField(string name, int index)
/// <inheritdoc/>
public virtual int GetFieldIndex(string name, int index = 0, bool isTryGet = false)
{
return GetFieldIndex(new[] { name }, index, isTryGet);
return GetFieldIndex([name], index, isTryGet);
}

/// <inheritdoc/>
public virtual int GetFieldIndex(string[] names, int index = 0, bool isTryGet = false, bool isOptional = false)
{
if (names == null)
{
throw new ArgumentNullException(nameof(names));
}
ArgumentNullException.ThrowIfNull(names);

if (!hasHeaderRecord)
{
Expand All @@ -284,9 +277,9 @@ public virtual int GetFieldIndex(string[] names, int index = 0, bool isTryGet =

// Caching the named index speeds up mappings that use ConvertUsing tremendously.
var nameKey = string.Join("_", names) + index;
if (namedIndexCache.ContainsKey(nameKey))
if (namedIndexCache.TryGetValue(nameKey, out (string, int) value))
{
(var cachedName, var cachedIndex) = namedIndexCache[nameKey];
(var cachedName, var cachedIndex) = value;
return namedIndexes[cachedName][cachedIndex];
}

Expand Down Expand Up @@ -376,13 +369,13 @@ protected virtual void ParseNamedIndexes()
{
var args = new PrepareHeaderForMatchArgs(headerRecord[i], i);
var name = prepareHeaderForMatch(args);
if (namedIndexes.ContainsKey(name))
if (namedIndexes.TryGetValue(name, out List<int> value))
{
namedIndexes[name].Add(i);
value.Add(i);
}
else
{
namedIndexes[name] = new List<int> { i };
namedIndexes[name] = [i];
}
}
}
Expand Down
51 changes: 9 additions & 42 deletions src/CsvHelper/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ namespace CsvHelper;
public class CsvWriter : IDisposable
{
private readonly TextWriter writer;
private string newLine = "\r\n";
private string quoteString = "\"";
private string delimiter = ";";
private readonly bool leaveOpen;
private char quote = '"';
private readonly string newLine = "\r\n";
private readonly string quoteString = "\"";
private readonly string delimiter = ";";
private readonly char quote = '"';
private readonly char comment = '#';
private readonly string escapeQuoteString;
private readonly char[] injectionCharacters;
private readonly char injectionEscapeCharacter;

private int row = 1;
private int index;
Expand Down Expand Up @@ -47,7 +44,7 @@ public class CsvWriter : IDisposable
public CsvWriter(TextWriter writer)
{
this.writer = writer;
escapeQuoteString = new string(new[] { '"' });
escapeQuoteString = new string(['"']);
buffer = new char[bufferSize];
}

Expand Down Expand Up @@ -82,7 +79,7 @@ public virtual void WriteHeader(string[] headers)
headerRecord.Add(header);
}

HeaderRecord = headerRecord.ToArray();
HeaderRecord = [.. headerRecord];
}

/// <summary>
Expand Down Expand Up @@ -139,36 +136,14 @@ private bool ShouldQuote(string field)
(
field.Contains(quote) // Contains quote
|| field[0] == ' ' // Starts with a space
|| field[field.Length - 1] == ' ' // Ends with a space
|| field[^1] == ' ' // Ends with a space
|| (delimiter.Length > 0 && field.Contains(delimiter)) // Contains delimiter
|| field.Contains(newLine) // Contains newline
);

return shouldQuote;
}

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual string SanitizeForInjection(string field)
{
if (string.IsNullOrEmpty(field))
{
return field;
}

if (ArrayHelper.Contains(injectionCharacters, field[0]))
{
return injectionEscapeCharacter + field;
}

if (field[0] == quote && ArrayHelper.Contains(injectionCharacters, field[1]))
{
return field[0].ToString() + injectionEscapeCharacter.ToString() + field.Substring(1);
}

return field;
}

/// <inheritdoc/>
public virtual void NextRecord()
{
Expand Down Expand Up @@ -266,11 +241,7 @@ protected virtual void Dispose(bool disposing)
if (disposing)
{
// Dispose managed state (managed objects)

if (!leaveOpen)
{
writer.Dispose();
}
writer.Dispose();
}

// Free unmanaged resources (unmanaged objects) and override finalizer
Expand Down Expand Up @@ -301,11 +272,7 @@ protected virtual async ValueTask DisposeAsync(bool disposing)
if (disposing)
{
// Dispose managed state (managed objects)

if (!leaveOpen)
{
await writer.DisposeAsync().ConfigureAwait(false);
}
await writer.DisposeAsync().ConfigureAwait(false);
}

// Free unmanaged resources (unmanaged objects) and override finalizer
Expand Down
Loading