Migrate to dotnet8 and fix warnings

This commit is contained in:
2024-02-18 00:46:45 +01:00
parent a688f2a692
commit 66536657e2
15 changed files with 492 additions and 470 deletions

11
.idea/.idea.CsvView/.idea/avalonia.xml generated Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AvaloniaProject">
<option name="projectPerEditor">
<map>
<entry key="CsvView/App.axaml" value="CsvView/CsvView.csproj" />
<entry key="CsvView/MainWindow.axaml" value="CsvView/CsvView.csproj" />
</map>
</option>
</component>
</project>

View File

@@ -7,60 +7,75 @@ public class ByteArraySearcherTests
[Fact] [Fact]
public void Contains__EmptyNeedle_ReturnsTrue() public void Contains__EmptyNeedle_ReturnsTrue()
{ {
byte[] haystack = { 1, 2, 3, 4, 5 }; // --- Arrange
byte[] haystack = [1, 2, 3, 4, 5,];
byte[] needle = Array.Empty<byte>(); byte[] needle = Array.Empty<byte>();
ByteArraySearcher searcher = new(needle); ByteArraySearcher searcher = new(needle);
// --- Act
bool result = searcher.Contains(haystack); bool result = searcher.Contains(haystack);
// --- Assert
Assert.True(result); Assert.True(result);
} }
[Fact] [Fact]
public void Contains__NeedleAtBeginning_ReturnsTrue() public void Contains__NeedleAtBeginning_ReturnsTrue()
{ {
byte[] haystack = { 1, 2, 3, 4, 5 }; // --- Arrange
byte[] needle = { 1, 2, 3 }; byte[] haystack = [1, 2, 3, 4, 5,];
byte[] needle = [1, 2, 3,];
ByteArraySearcher searcher = new(needle); ByteArraySearcher searcher = new(needle);
// --- Act
bool result = searcher.Contains(haystack); bool result = searcher.Contains(haystack);
// --- Assert
Assert.True(result); Assert.True(result);
} }
[Fact] [Fact]
public void Contains__NeedleInMiddle_ReturnsTrue() public void Contains__NeedleInMiddle_ReturnsTrue()
{ {
byte[] haystack = { 1, 2, 3, 4, 5 }; // --- Arrange
byte[] needle = { 3, 4 }; byte[] haystack = [1, 2, 3, 4, 5,];
byte[] needle = [3, 4,];
ByteArraySearcher searcher = new(needle); ByteArraySearcher searcher = new(needle);
// --- Act
bool result = searcher.Contains(haystack); bool result = searcher.Contains(haystack);
// --- Assert
Assert.True(result); Assert.True(result);
} }
[Fact] [Fact]
public void Contains__NeedleAtEnd_ReturnsTrue() public void Contains__NeedleAtEnd_ReturnsTrue()
{ {
byte[] haystack = { 1, 2, 3, 4, 5 }; // --- Arrange
byte[] needle = { 4, 5 }; byte[] haystack = [1, 2, 3, 4, 5,];
byte[] needle = [4, 5,];
ByteArraySearcher searcher = new(needle); ByteArraySearcher searcher = new(needle);
// --- Act
bool result = searcher.Contains(haystack); bool result = searcher.Contains(haystack);
// --- Assert
Assert.True(result); Assert.True(result);
} }
[Fact] [Fact]
public void Contains__NeedleNotPresent_ReturnsFalse() public void Contains__NeedleNotPresent_ReturnsFalse()
{ {
byte[] haystack = { 1, 2, 3, 4, 5 }; // --- Arrange
byte[] needle = { 5, 6, 7 }; byte[] haystack = [1, 2, 3, 4, 5,];
byte[] needle = [5, 6, 7,];
ByteArraySearcher searcher = new(needle); ByteArraySearcher searcher = new(needle);
// --- Act
bool result = searcher.Contains(haystack); bool result = searcher.Contains(haystack);
// --- Assert
Assert.False(result); Assert.False(result);
} }
} }

View File

@@ -5,7 +5,6 @@ namespace CvsLib;
public class CsvFieldIndexerTests public class CsvFieldIndexerTests
{ {
#region GenerateIndex #region GenerateIndex
[Fact] [Fact]
@@ -51,7 +50,8 @@ public class CsvFieldIndexerTests
public void GenerateIndex__TwoLinesOfPainText__TwoRows() public void GenerateIndex__TwoLinesOfPainText__TwoRows()
{ {
// --- Arrange // --- Arrange
StringReader sr = new(""" StringReader sr = new(
"""
Hello World Hello World
Hello World Hello World
"""); """);
@@ -80,7 +80,8 @@ public class CsvFieldIndexerTests
public void GenerateIndex__TwoLinesOfQuotedText__TwoRows() public void GenerateIndex__TwoLinesOfQuotedText__TwoRows()
{ {
// --- Arrange // --- Arrange
StringReader sr = new(""" StringReader sr = new(
"""
"Hello World" "Hello World"
"Hello World" "Hello World"
"""); """);
@@ -109,7 +110,8 @@ public class CsvFieldIndexerTests
public void GenerateIndex__TwoLinesWithTwoQuotedColumns__TwoRowsTwoFields() public void GenerateIndex__TwoLinesWithTwoQuotedColumns__TwoRowsTwoFields()
{ {
// --- Arrange // --- Arrange
StringReader sr = new(""" StringReader sr = new(
"""
"Hello","World" "Hello","World"
"Hello","World" "Hello","World"
"""); """);
@@ -142,7 +144,8 @@ public class CsvFieldIndexerTests
public void GenerateIndex__TwoLinesWithTwoQuotedColumnsWithUnicode__TwoRowsTwoFields() public void GenerateIndex__TwoLinesWithTwoQuotedColumnsWithUnicode__TwoRowsTwoFields()
{ {
// --- Arrange // --- Arrange
StringReader sr = new(""" StringReader sr = new(
"""
"Hélló","Wórld" "Hélló","Wórld"
"Hélló","Wórld" "Hélló","Wórld"
"""); """);
@@ -179,7 +182,8 @@ public class CsvFieldIndexerTests
public void Search__TwoLinesWithTwoQuotedColumns__OneIndexFirstRow() public void Search__TwoLinesWithTwoQuotedColumns__OneIndexFirstRow()
{ {
// --- Arrange // --- Arrange
string strText = """ const string strText =
"""
"Hello","test" "Hello","test"
"Hello","World" "Hello","World"
"""; """;
@@ -202,7 +206,8 @@ public class CsvFieldIndexerTests
public void Search__TwoLinesWithTwoQuotedColumns__OneIndexSecondRow() public void Search__TwoLinesWithTwoQuotedColumns__OneIndexSecondRow()
{ {
// --- Arrange // --- Arrange
string strText = """ const string strText =
"""
"Hello","World" "Hello","World"
"Hello","test" "Hello","test"
"""; """;
@@ -225,7 +230,8 @@ public class CsvFieldIndexerTests
public void Search__TwoLinesWithTwoQuotedColumnsTwoMatches__OneIndexSecondRow() public void Search__TwoLinesWithTwoQuotedColumnsTwoMatches__OneIndexSecondRow()
{ {
// --- Arrange // --- Arrange
string strText = """ const string strText =
"""
"Hello","World" "Hello","World"
"test","test" "test","test"
"""; """;

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>CvsLib</RootNamespace> <RootNamespace>CvsLib</RootNamespace>

View File

@@ -1,12 +1,11 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
namespace CsvLib namespace CsvLib;
public class BufferedTextReader : TextReader
{ {
public class BufferedTextReader : TextReader
{
private readonly TextReader _baseReader; private readonly TextReader _baseReader;
private int _position;
private readonly StringBuilder _sbBuffer = new(); private readonly StringBuilder _sbBuffer = new();
private readonly Encoding _currentEncoding = Encoding.Default; private readonly Encoding _currentEncoding = Encoding.Default;
@@ -26,11 +25,11 @@ namespace CsvLib
if (read > 127) if (read > 127)
{ {
int count = _currentEncoding.GetByteCount(((char)read).ToString()); int count = _currentEncoding.GetByteCount(((char)read).ToString());
_position += count; Position += count;
} }
else else
{ {
_position++; Position++;
} }
if (read != -1) if (read != -1)
{ {
@@ -39,10 +38,7 @@ namespace CsvLib
return read; return read;
} }
public int Position public int Position { get; private set; }
{
get { return _position; }
}
public string GetBuffer() public string GetBuffer()
{ {
@@ -53,5 +49,4 @@ namespace CsvLib
{ {
_sbBuffer.Clear(); _sbBuffer.Clear();
} }
}
} }

View File

@@ -1,5 +1,3 @@
#nullable enable
namespace CsvLib; namespace CsvLib;
public class ByteArraySearcher public class ByteArraySearcher

View File

@@ -3,10 +3,10 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
namespace CsvLib namespace CsvLib;
public class CsvFieldIndexer
{ {
public class CsvFieldIndexer
{
private bool _insideString; private bool _insideString;
private Encoding _currentEncoding = Encoding.Default; private Encoding _currentEncoding = Encoding.Default;
@@ -67,7 +67,7 @@ namespace CsvLib
char c = line[i]; char c = line[i];
if (c == _separator && _insideString == false) if (c == _separator && _insideString == false)
{ {
if (fieldStartPosition != null) if (fieldStartPosition != null && fieldEndPosition != null)
{ {
fieldPositions.Add((long)fieldStartPosition); fieldPositions.Add((long)fieldStartPosition);
fieldPositions.Add((long)fieldEndPosition); fieldPositions.Add((long)fieldEndPosition);
@@ -105,7 +105,7 @@ namespace CsvLib
} }
if (_insideString == false) if (_insideString == false)
{ {
if (fieldStartPosition != null) if (fieldStartPosition != null && fieldEndPosition != null)
{ {
fieldPositions.Add((long)fieldStartPosition); fieldPositions.Add((long)fieldStartPosition);
fieldPositions.Add((long)fieldEndPosition); fieldPositions.Add((long)fieldEndPosition);
@@ -133,8 +133,7 @@ namespace CsvLib
_currentEncoding = streamReader.CurrentEncoding; _currentEncoding = streamReader.CurrentEncoding;
} }
using BufferedTextReader reader = new(textReader); using BufferedTextReader reader = new(textReader);
string currentLine; while (reader.ReadLine() is { } currentLine)
while ((currentLine = reader.ReadLine()) != null)
{ {
DummyParser(currentLine); DummyParser(currentLine);
if (_insideString) { continue; } if (_insideString) { continue; }
@@ -154,7 +153,6 @@ namespace CsvLib
private void SaveFile(string indexFile) private void SaveFile(string indexFile)
{ {
if (indexFile == null) { return; }
if (File.Exists(indexFile)) if (File.Exists(indexFile))
{ {
File.Delete(indexFile); File.Delete(indexFile);
@@ -178,9 +176,9 @@ namespace CsvLib
foreach (List<long> currentFieldIndex in _fieldIndex) foreach (List<long> currentFieldIndex in _fieldIndex)
{ {
binWriter.Write(currentFieldIndex.Count); binWriter.Write(currentFieldIndex.Count);
for (int i = 0; i < currentFieldIndex.Count; i++) foreach (long fieldIndex in currentFieldIndex)
{ {
binWriter.Write(currentFieldIndex[i]); binWriter.Write(fieldIndex);
} }
} }
} }
@@ -265,7 +263,7 @@ namespace CsvLib
} }
} }
public List<long> Search(string fileName, string textToSearch, Action<float> notifyProgress = null) public List<long> Search(string fileName, string textToSearch, Action<float>? notifyProgress = null)
{ {
List<long> index; List<long> index;
using FileStream streamIn = new(fileName, FileMode.Open); using FileStream streamIn = new(fileName, FileMode.Open);
@@ -277,10 +275,10 @@ namespace CsvLib
{ {
streamIn.Close(); streamIn.Close();
} }
return index ?? new List<long>(); return index;
} }
public List<long> Search(Stream streamIn, string textToSearch, Action<float> notifyProgress = null) public List<long> Search(Stream streamIn, string textToSearch, Action<float>? notifyProgress = null)
{ {
// TODO: Use MemoryMappedFile for better IO performance // TODO: Use MemoryMappedFile for better IO performance
DateTime datePrevious = DateTime.UtcNow; DateTime datePrevious = DateTime.UtcNow;
@@ -320,5 +318,4 @@ namespace CsvLib
return newIndexes; return newIndexes;
} }
}
} }

View File

@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<LangVersion>11</LangVersion> <LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -2,10 +2,10 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
namespace CsvLib namespace CsvLib;
public class CsvParser
{ {
public class CsvParser
{
private bool _insideString; private bool _insideString;
private readonly char _separator; private readonly char _separator;
@@ -21,8 +21,8 @@ namespace CsvLib
private List<List<string>> _data = new(); private List<List<string>> _data = new();
private List<string> _currentReg; private List<string>? _currentReg;
StringBuilder _currentCell; private StringBuilder? _currentCell;
public List<List<string>> Data public List<List<string>> Data
{ {
@@ -86,8 +86,7 @@ namespace CsvLib
stream.Seek(offset, SeekOrigin.Begin); stream.Seek(offset, SeekOrigin.Begin);
using (StreamReader reader = new(stream, Encoding.Default, true, 4096)) using (StreamReader reader = new(stream, Encoding.Default, true, 4096))
{ {
string currentLine; while (reader.ReadLine() is { } currentLine)
while ((currentLine = reader.ReadLine()) != null)
{ {
ParseLine(currentLine); ParseLine(currentLine);
if (count > 0 && Data.Count == count) if (count > 0 && Data.Count == count)
@@ -99,5 +98,4 @@ namespace CsvLib
stream.Close(); stream.Close();
} }
}
} }

View File

@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">/usr/share/dotnet/sdk/7.0.107/MSBuild.dll</s:String> <s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">/usr/share/dotnet/sdk/7.0.107/MSBuild.dll</s:String>
<s:Int64 x:Key="/Default/Environment/Hierarchy/Build/BuildTool/MsbuildVersion/@EntryValue">4294967293</s:Int64></wpf:ResourceDictionary> <s:Int64 x:Key="/Default/Environment/Hierarchy/Build/BuildTool/MsbuildVersion/@EntryValue">4294967293</s:Int64>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Avalonia/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -1,7 +1,7 @@
<Application xmlns="https://github.com/avaloniaui" <Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="CsvView.App" x:Class="CsvView.App"
RequestedThemeVariant="Default"> RequestedThemeVariant="Dark">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> <!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.Styles> <Application.Styles>

View File

@@ -1,14 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport> <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault> <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.2"/> <PackageReference Include="Avalonia" Version="11.0.2"/>
<PackageReference Include="Avalonia.Desktop" Version="11.0.2"/> <PackageReference Include="Avalonia.Desktop" Version="11.0.2"/>
@@ -18,7 +17,6 @@
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.2"/> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.2"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\CsvLib\CsvLib.csproj" /> <ProjectReference Include="..\CsvLib\CsvLib.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -9,12 +9,12 @@
Width="800" Width="800"
Height="600" Height="600"
Title="CsvView"> Title="CsvView">
<Grid RowDefinitions="Auto,Auto,Auto,*"> <Grid RowDefinitions="Auto,Auto,Auto,*" Margin="5">
<Grid ColumnDefinitions="Auto,*" Grid.Row="0"> <Grid ColumnDefinitions="Auto,*" Grid.Row="0" Margin="0 0 0 5">
<Button Grid.Column="0" Name="BtnLoad" Click="BtnLoad_OnClick">...</Button> <Button Grid.Column="0" Name="BtnLoad" Click="BtnLoad_OnClick">...</Button>
<TextBox Grid.Column="1" Name="TxtFileName" IsReadOnly="true" /> <TextBox Grid.Column="1" Name="TxtFileName" IsReadOnly="true" />
</Grid> </Grid>
<Grid ColumnDefinitions="*,Auto" Grid.Row="1"> <Grid ColumnDefinitions="*,Auto" Grid.Row="1" Margin="0 0 0 5">
<TextBox Grid.Column="0" Name="TxtSearch" /> <TextBox Grid.Column="0" Name="TxtSearch" />
<Button Grid.Column="1" Name="BtnSearch" Click="BtnSearch_OnClick">🔍</Button> <Button Grid.Column="1" Name="BtnSearch" Click="BtnSearch_OnClick">🔍</Button>
</Grid> </Grid>
@@ -22,7 +22,7 @@
<Button Name="BtnFirst" Click="BtnFirst_OnClick">|◁</Button> <Button Name="BtnFirst" Click="BtnFirst_OnClick">|◁</Button>
<Button Name="BtnPrevious" Click="BtnPrevious_OnClick">◁</Button> <Button Name="BtnPrevious" Click="BtnPrevious_OnClick">◁</Button>
<TextBox Name="TxtIndex" Text="{Binding Index}" TextChanged="TxtIndex_OnTextChanged"></TextBox> <TextBox Name="TxtIndex" Text="{Binding Index}" TextChanged="TxtIndex_OnTextChanged"></TextBox>
<TextBlock>/</TextBlock> <TextBlock VerticalAlignment="Center">/</TextBlock>
<TextBox Name="TxtMaxIndex" Text="{Binding MaxIndex}" IsReadOnly="true"></TextBox> <TextBox Name="TxtMaxIndex" Text="{Binding MaxIndex}" IsReadOnly="true"></TextBox>
<Button Name="BtnNext" Click="BtnNext_OnClick">▷</Button> <Button Name="BtnNext" Click="BtnNext_OnClick">▷</Button>
<Button Name="BtnLast" Click="BtnLast_OnClick">▷|</Button> <Button Name="BtnLast" Click="BtnLast_OnClick">▷|</Button>

View File

@@ -28,8 +28,8 @@ public partial class MainWindow : Window
AllowMultiple = false, AllowMultiple = false,
FileTypeFilter = new List<FilePickerFileType> FileTypeFilter = new List<FilePickerFileType>
{ {
new("CSV Files") { Patterns = new[] { "*.csv" } }, new("CSV Files") { Patterns = new[] { "*.csv", }, },
new("Any File") { Patterns = new[] { "*" } }, new("Any File") { Patterns = new[] { "*", }, },
}, },
}); });
@@ -89,6 +89,8 @@ public partial class MainWindow : Window
private void Search(string? textToSearch) private void Search(string? textToSearch)
{ {
if (textToSearch == null) { return; }
// TODO: Loading animation // TODO: Loading animation
CsvFieldIndexer csvIndexer = new(); CsvFieldIndexer csvIndexer = new();
csvIndexer.LoadIndexOfFile(_loadedFile); csvIndexer.LoadIndexOfFile(_loadedFile);

View File

@@ -3,7 +3,7 @@ using System;
namespace CsvView; namespace CsvView;
class Program static class Program
{ {
// Initialization code. Don't use any Avalonia, third-party APIs or any // Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
@@ -13,7 +13,7 @@ class Program
.StartWithClassicDesktopLifetime(args); .StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer. // Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp() private static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>() => AppBuilder.Configure<App>()
.UsePlatformDetect() .UsePlatformDetect()
.WithInterFont() .WithInterFont()