Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #320

Merged
merged 10 commits into from
Feb 13, 2025
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 .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ root = true
[*]
indent_style = space
charset = utf-8
insert_final_newline = true

# Xml files
[*.xml]
Expand All @@ -18,6 +17,7 @@ indent_size = 2
# Indentation and spacing
indent_size = 4
tab_width = 4
insert_final_newline = true

#### .NET Coding Conventions ####
[*.{cs,vb}]
Expand Down
20 changes: 10 additions & 10 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@
<PackageReference Include="MSTest.TestFramework" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="DotMake.CommandLine" Version="1.8.8" />
<PackageVersion Include="DotMake.CommandLine" Version="1.9.0" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
<PackageVersion Include="Microsoft.Build.Framework" Version="17.12.6" />
<PackageVersion Include="Microsoft.Build.Tasks.Core" Version="17.12.6" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.3" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.7.3" />
<PackageVersion Include="MSTest.TestFramework" Version="3.7.3" />
<PackageVersion Include="Polyfill" Version="2.1.0" />
<PackageVersion Include="Sharprompt" Version="2.4.5" />
<PackageVersion Include="Sharprompt" Version="3.0.0" />
<PackageVersion Include="ShellProgressBar" Version="5.2.0" />
<PackageVersion Include="SixLabors.ImageSharp" Version="3.1.6" />
<PackageVersion Include="System.Collections.Immutable" Version="9.0.0" />
<PackageVersion Include="System.IO.Hashing" Version="9.0.0" />
<PackageVersion Include="System.IO.Hashing" Version="9.0.1" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="YamlDotNet" Version="16.2.0" />
<PackageVersion Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>
</Project>
29 changes: 11 additions & 18 deletions app/Shimakaze.Sdk.Csf.Converter/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

using Sharprompt;

using Shimakaze.Sdk.Csf.IO;
using Shimakaze.Sdk.Csf.Json;
using Shimakaze.Sdk.Csf.Json.IO;
using Shimakaze.Sdk.Csf.Xml;
using Shimakaze.Sdk.Csf.Yaml;

Expand All @@ -30,15 +32,6 @@ internal sealed class RootCommand
[CliOption(Description = "不要启用交互模式", Required = false)]
public bool Quiet { get; set; }


private static readonly JsonSerializerOptions Options = new()
{
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

public async Task RunAsync()
{
InitInputFormat();
Expand All @@ -48,16 +41,16 @@ public async Task RunAsync()
await using FileStream ifs = Input.OpenRead();
await using FileStream ofs = Output.Create();

Task<CsfDocument> reader = InputFormat switch
Task<CsfData> reader = InputFormat switch
{
SupportedFormat.Csf => Task.Run(() => CsfReader.Read(ifs)),
SupportedFormat.Csf => Task.Run(() => CsfReader.ReadAllData(ifs)),
SupportedFormat.Yaml => Task.Run(() =>
{
using StreamReader sr = new(ifs);
return CsfYamlV1Reader.Read(sr);
}),
SupportedFormat.JsonV2 => CsfJsonV2Reader.ReadAsync(ifs, Options),
SupportedFormat.JsonV1 => CsfJsonV1Reader.ReadAsync(ifs, Options),
SupportedFormat.JsonV2 => CsfJsonV2.ReadAllDataAsync(ifs),
SupportedFormat.JsonV1 => CsfJsonV1.ReadAllDataAsync(ifs),
SupportedFormat.Xml => Task.Run(() =>
{
using StreamReader sr = new(ifs);
Expand All @@ -66,22 +59,22 @@ public async Task RunAsync()
_ => throw new NotSupportedException(),
};

Func<CsfDocument, Task> writer = OutputFormat switch
Func<CsfData, Task> writer = OutputFormat switch
{
SupportedFormat.Yaml => async csf => await Task.Run(async () =>
{
await using StreamWriter sw = new(ofs);
CsfYamlV1Writer.Write(sw, csf);
}),
SupportedFormat.JsonV2 => async csf => await CsfJsonV2Writer.WriteAsync(ofs, csf, Options),
SupportedFormat.JsonV1 => async csf => await CsfJsonV1Writer.WriteAsync(ofs, csf, Options),
SupportedFormat.JsonV2 => async csf => await CsfJsonV2.WriteAllDataAsync(ofs, csf),
SupportedFormat.JsonV1 => async csf => await CsfJsonV1.WriteAllDataAsync(ofs, csf),
SupportedFormat.Xml => async csf =>
{
await using StreamWriter sw = new(ofs);
CsfXmlV1Writer.Write(sw, csf, new() { Indent = true });
CsfXmlV1Writer.Write(sw, csf);
}
,
SupportedFormat.Csf => async csf => await Task.Run(() => CsfWriter.Write(ofs, csf)),
SupportedFormat.Csf => async csf => await Task.Run(() => CsfWriter.WriteAllData(ofs, csf)),
_ => throw new NotSupportedException()
};

Expand Down
89 changes: 13 additions & 76 deletions lib/Shimakaze.Sdk.Common/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Shimakaze.Sdk.Common;

/// <summary>
/// 流实用工具
/// </summary>
public static unsafe class StreamExtensions
public static class StreamExtensions
{
/// <summary>
/// 断言流可以Seek
Expand All @@ -23,13 +26,12 @@ public static Stream CanSeek(this Stream stream)
/// <param name="stream"> 流 </param>
/// <param name="destination"> 目标结构体 </param>
/// <exception cref="OverflowException"> </exception>
public static int Read<T>(this Stream stream, out T destination)
public static void Read<T>(this Stream stream, out T destination)
where T : unmanaged
{
fixed (T* ptr = &destination)
{
return stream.Read(new Span<byte>(ptr, sizeof(T)));
}
Span<byte> buffer = stackalloc byte[Unsafe.SizeOf<T>()];
stream.ReadExactly(buffer);
destination = MemoryMarshal.Read<T>(buffer);
}

/// <summary>
Expand All @@ -39,38 +41,10 @@ public static int Read<T>(this Stream stream, out T destination)
/// <param name="stream"> 流 </param>
/// <param name="destination"> 目标数组 </param>
/// <exception cref="OverflowException"> </exception>
public static int Read<T>(this Stream stream, in T[] destination)
public static void Read<T>(this Stream stream, in Span<T> destination)
where T : unmanaged
{
fixed (T* ptr = destination)
{
return stream.Read(new Span<byte>(ptr, destination.Length * sizeof(T)));
}
}

/// <summary>
/// 读取字符串
/// </summary>
/// <param name="stream"> 流 </param>
/// <param name="value"> 读出来的字符串 </param>
/// <param name="length"> 要读取的长度 </param>
/// <param name="isUnicode"> 是否是wchar </param>
public static int Read(this Stream stream, out string value, int length, bool isUnicode = false)
{
int result;
if (isUnicode)
{
char* buffer = stackalloc char[length];
result = stream.Read(new Span<byte>(buffer, length * sizeof(char)));
value = new(buffer, 0, length);
}
else
{
sbyte* buffer = stackalloc sbyte[length];
result = stream.Read(new Span<byte>(buffer, length));
value = new(buffer, 0, length);
}
return result;
stream.ReadExactly(MemoryMarshal.Cast<T, byte>(destination));
}

/// <summary>
Expand All @@ -83,10 +57,7 @@ public static int Read(this Stream stream, out string value, int length, bool is
public static void Write<T>(this Stream stream, in T value)
where T : unmanaged
{
fixed (T* ptr = &value)
{
stream.Write(new Span<byte>(ptr, sizeof(T)));
}
stream.Write(MemoryMarshal.AsBytes([value]));
}

/// <summary>
Expand All @@ -96,44 +67,10 @@ public static void Write<T>(this Stream stream, in T value)
/// <param name="stream"> 流 </param>
/// <param name="value"> 结构体数组 </param>
/// <exception cref="OverflowException"> </exception>
public static void Write<T>(this Stream stream, in T[] value)
public static void Write<T>(this Stream stream, in ReadOnlySpan<T> value)
where T : unmanaged
{
fixed (T* ptr = value)
{
stream.Write(new Span<byte>(ptr, value.Length * sizeof(T)));
}
}

/// <summary>
/// 写入一个字符串到流
/// </summary>
/// <param name="stream"> 流 </param>
/// <param name="value"> 字符串 </param>
/// <param name="length"> 字符串长度 </param>
/// <param name="isUnicode"> 是否是wchar </param>
public static void Write(this Stream stream, string value, int length, bool isUnicode = false)
{
if (isUnicode)
{
fixed (char* ptr = value)
{
stream.Write(new Span<byte>(ptr, length * sizeof(char)));
}
}
else
{
byte* ptr = stackalloc byte[length];
fixed (char* p = value)
{
for (int i = 0; i < length; i++)
{
ptr[i] = (byte)p[i];
}
}

stream.Write(new Span<byte>(ptr, length));
}
stream.Write(MemoryMarshal.AsBytes(value));
}

/// <summary>
Expand Down

This file was deleted.

Loading
Loading