Skip to content

Commit

Permalink
public Item.JIS8Encoding settable property.
Browse files Browse the repository at this point in the history
  • Loading branch information
westjeffho committed Jan 3, 2024
1 parent ffe91bb commit cb68fda
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.3.2</Version>
<Version>2.4.1</Version>
<RepositoryUrl>https://github.com/mkjeff/secs4net</RepositoryUrl>
<Authors>mkjeff</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -14,7 +14,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<EnablePackageValidation>true</EnablePackageValidation>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<Copyright>Copyright © secs4net 2023</Copyright>
<Copyright>Copyright © secs4net 2024</Copyright>
<PackageProjectUrl>https://mkjeff.github.io/secs4net/</PackageProjectUrl>
</PropertyGroup>

Expand Down
6 changes: 4 additions & 2 deletions samples/SecsDevice/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Windows.Forms;

namespace SecsDevice;
Expand All @@ -11,9 +12,10 @@ static class Program
[STAThread]
static void Main()
{

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Secs4Net.Item.JIS8Encoding = Encoding.GetEncoding(50222);
#if NET
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
#endif
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Expand Down
35 changes: 18 additions & 17 deletions samples/SecsDevice/SecsDevice.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net8.0-windows;net472</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net8.0-windows;net472</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Secs4Net\Secs4Net.csproj" />
<ProjectReference Include="..\..\src\Secs4Net.Sml\Secs4Net.Sml.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Secs4Net\Secs4Net.csproj" />
<ProjectReference Include="..\..\src\Secs4Net.Sml\Secs4Net.Sml.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageReference Update="PolySharp" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Update="PolySharp" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Secs4Net.Json/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void WriteTo(this Item item, Utf8JsonWriter writer)
writer.WriteEndObject();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void WriteArrayValue<T>(Utf8JsonWriter writer, Item item, Action<Utf8JsonWriter, T> write) where T : unmanaged
static void WriteArrayValue<T>(Utf8JsonWriter writer, Item item, Action<Utf8JsonWriter, T> write) where T : unmanaged, IEquatable<T>
{
var span = item.GetMemory<T>().Span;
foreach (ref readonly var value in span)
Expand Down
16 changes: 9 additions & 7 deletions src/Secs4Net.Sml/MemoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.HighPerformance;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System;

Expand All @@ -21,18 +22,19 @@ internal static partial class MemoryExtensions
public static SpanSplitEnumerator<char> Split(in this ReadOnlySpan<char> span, char separator, StringSplitOptions options = StringSplitOptions.None)
=> new(span, separator, options == StringSplitOptions.RemoveEmptyEntries);

public static bool IsEmpty<T>(this SpanSplitEnumerator<T> source) where T : IEquatable<T>
public static bool IsEmpty<T>(this SpanSplitEnumerator<T> source) where T : unmanaged, IEquatable<T>
=> !source.MoveNext();

public static TResult[] ToArray<TResult>(ref this SpanSplitEnumerator<char> source, SpanParser<TResult> seelctor, int? size)
public static TResult[] ToArray<TResult>(ref this SpanSplitEnumerator<char> source, SpanParser<TResult> selector, int? size)
{
if (size.HasValue)
{
var list = new TResult[size.GetValueOrDefault()];
int i = 0;
ref var r0 = ref MemoryMarshal.GetReference(list.AsSpan());
uint i = 0;
foreach (var span in source)
{
list.DangerousGetReferenceAt(i++) = seelctor.Invoke(span);
Unsafe.Add(ref r0, i++) = selector.Invoke(span);
if (i == list.Length)
{
break;
Expand All @@ -46,15 +48,15 @@ public static TResult[] ToArray<TResult>(ref this SpanSplitEnumerator<char> sour
var list = new List<TResult>();
foreach (var span in source)
{
list.Add(seelctor.Invoke(span));
list.Add(selector.Invoke(span));
}

return list.ToArray();
}
}
}

internal ref struct SpanSplitEnumerator<T> where T : IEquatable<T>
internal ref struct SpanSplitEnumerator<T> where T : unmanaged, IEquatable<T>
{
private ReadOnlySpan<T> _sequence;
private readonly T _separator;
Expand Down
2 changes: 2 additions & 0 deletions src/Secs4Net/Extensions/ExtensionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal static unsafe Span<byte> AsBytes<T>(this ref T value) where T : unmanag
=> new(Unsafe.AsPointer(ref value), sizeof(T));
#endif

#if !NET
internal static async Task WithCancellation(this Task task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down Expand Up @@ -77,4 +78,5 @@ internal static async Task<T> WithCancellation<T>(this Task<T> task, Cancellatio
return await task.ConfigureAwait(false);
}
}
#endif
}
2 changes: 1 addition & 1 deletion src/Secs4Net/Item.Decode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal static Item DecodeDataItem(SecsFormat format, in ReadOnlySequence<byte>

(SecsFormat.JIS8, 0) => J(),
(SecsFormat.JIS8, >= 512) => DecodeLazyStringItem(format, length, bytes),
(SecsFormat.JIS8, _) => DecodeStringItem(format, length, bytes, Jis8Encoding),
(SecsFormat.JIS8, _) => DecodeStringItem(format, length, bytes, JIS8Encoding),

(SecsFormat.Binary, 0) => B(),
(SecsFormat.Binary, >= 1024) => DecodeMemoryOwnerItem<byte>(SecsFormat.Binary, length, bytes),
Expand Down
2 changes: 1 addition & 1 deletion src/Secs4Net/Item.LazyString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal LazyStringItem(SecsFormat format, IMemoryOwner<byte> owner)
_owner = owner;
_value = new Lazy<string>(() =>
{
var encoding = Format == SecsFormat.ASCII ? Encoding.ASCII : Jis8Encoding;
var encoding = Format == SecsFormat.ASCII ? Encoding.ASCII : JIS8Encoding;
return encoding.GetString(_owner.Memory.Span);
}, isThreadSafe: false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Secs4Net/Item.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void EncodeTo(IBufferWriter<byte> buffer)
return;
}

var encoder = Format == SecsFormat.ASCII ? Encoding.ASCII : Jis8Encoding;
var encoder = Format == SecsFormat.ASCII ? Encoding.ASCII : JIS8Encoding;
var byteLength = encoder.GetByteCount(_value);
EncodeItemHeader(Format, byteLength, buffer);
var length = encoder.GetBytes(_value, buffer.GetSpan(byteLength));
Expand Down
16 changes: 12 additions & 4 deletions src/Secs4Net/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public abstract partial class Item : IEquatable<Item>, IDisposable
private const nuint ElementOffset1 = 1;
private const nuint ElementOffset2 = 2;
private const nuint ElementOffset3 = 3;
public static readonly Encoding Jis8Encoding = Encoding.GetEncoding(50222);
private static readonly Item EmptyL = new ListItem(SecsFormat.List, []);
private static readonly Item EmptyA = new StringItem(SecsFormat.ASCII, string.Empty);
private static readonly Item EmptyJ = new StringItem(SecsFormat.JIS8, string.Empty);
Expand All @@ -27,9 +26,18 @@ public abstract partial class Item : IEquatable<Item>, IDisposable
private static readonly Item EmptyI8 = new MemoryItem<long>(SecsFormat.I8);
private static readonly Item EmptyF4 = new MemoryItem<float>(SecsFormat.F4);
private static readonly Item EmptyF8 = new MemoryItem<double>(SecsFormat.F8);
public static Encoding JIS8Encoding { get; set; } = Encoding.UTF8;

public SecsFormat Format { get; }

static Item()
{
if (!BitConverter.IsLittleEndian)
{
throw new PlatformNotSupportedException("This version is only work on little endian hardware.");
}
}

private protected Item(SecsFormat format)
{
Format = format;
Expand Down Expand Up @@ -70,7 +78,7 @@ public virtual Item[] Items
/// <returns></returns>
/// <exception cref="IndexOutOfRangeException">When item is empty or data length less than sizeof(<typeparamref name="T"/>)</exception>
/// <exception cref="NotSupportedException">when the item's <see cref="Format"/> is <see cref="SecsFormat.List"/> or <see cref="SecsFormat.ASCII"/> or <see cref="SecsFormat.JIS8"/></exception>
public virtual ref T FirstValue<T>() where T : unmanaged
public virtual ref T FirstValue<T>() where T : unmanaged, IEquatable<T>
=> throw ThrowNotSupportException(Format);

/// <summary>
Expand All @@ -79,14 +87,14 @@ public virtual ref T FirstValue<T>() where T : unmanaged
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="NotSupportedException">when <see cref="Format"/> is <see cref="SecsFormat.List"/> or <see cref="SecsFormat.ASCII"/> or <see cref="SecsFormat.JIS8"/></exception>
public virtual T FirstValueOrDefault<T>(T defaultValue = default) where T : unmanaged
public virtual T FirstValueOrDefault<T>(T defaultValue = default) where T : unmanaged, IEquatable<T>
=> throw ThrowNotSupportException(Format);

/// <summary>
/// Get item array as <see cref="Memory{T}"/>
/// </summary>
/// <exception cref="NotSupportedException">when <see cref="Format"/> is <see cref="SecsFormat.List"/> or <see cref="SecsFormat.ASCII"/> or <see cref="SecsFormat.JIS8"/></exception>
public virtual Memory<T> GetMemory<T>() where T : unmanaged
public virtual Memory<T> GetMemory<T>() where T : unmanaged, IEquatable<T>
=> throw ThrowNotSupportException(Format);

/// <summary>
Expand Down
20 changes: 0 additions & 20 deletions src/Secs4Net/ModuleInitializer.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/Secs4Net/Secs4Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="PooledAwait" Version="1.0.49" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion test/Benchmarks/ItemEncodeDecode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void GlobalSetup()
SecsFormat.Binary => B(CreateArray<byte>(Size)),
SecsFormat.Boolean => Boolean(CreateArray<bool>(Size)),
SecsFormat.ASCII => A(CreateString(Size, Encoding.ASCII)),
SecsFormat.JIS8 => J(CreateString(Size, Item.Jis8Encoding)),
SecsFormat.JIS8 => J(CreateString(Size, Item.JIS8Encoding)),
SecsFormat.I8 => I8(CreateArray<long>(Size)),
SecsFormat.I1 => I1(CreateArray<sbyte>(Size)),
SecsFormat.I2 => I2(CreateArray<short>(Size)),
Expand Down
6 changes: 3 additions & 3 deletions test/Benchmarks/JsonSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Setup()
U4(MemoryOwner<uint>.Allocate(ItemCount)),
F4(MemoryOwner<float>.Allocate(ItemCount)),
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)), //JIS encoding cost more memory in coreclr
J(CreateString(ItemCount, Item.JIS8Encoding)), //JIS encoding cost more memory in coreclr
F8(MemoryOwner<double>.Allocate(ItemCount)),
L(
I1(MemoryOwner<sbyte>.Allocate(ItemCount)),
Expand All @@ -52,15 +52,15 @@ public void Setup()
B(MemoryOwner<byte>.Allocate(ItemCount)),
L(
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount))),
F8(MemoryOwner<double>.Allocate(ItemCount))),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount)),
L(
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount))),
F8(MemoryOwner<double>.Allocate(ItemCount))),
Expand Down
6 changes: 3 additions & 3 deletions test/Benchmarks/PipeDecoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Setup()
U4(MemoryOwner<uint>.Allocate(ItemCount)),
F4(MemoryOwner<float>.Allocate(ItemCount)),
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
F8(MemoryOwner<double>.Allocate(ItemCount)),
L(
I1(MemoryOwner<sbyte>.Allocate(ItemCount)),
Expand All @@ -51,15 +51,15 @@ public void Setup()
B(MemoryOwner<byte>.Allocate(ItemCount)),
L(
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount))),
F8(MemoryOwner<double>.Allocate(ItemCount))),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount)),
L(
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount))),
F8(MemoryOwner<double>.Allocate(ItemCount))),
Expand Down
6 changes: 3 additions & 3 deletions test/Benchmarks/SmlSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Setup()
U4(MemoryOwner<uint>.Allocate(ItemCount)),
F4(MemoryOwner<float>.Allocate(ItemCount)),
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)), //JIS encoding cost more memory in coreclr
J(CreateString(ItemCount, Item.JIS8Encoding)), //JIS encoding cost more memory in coreclr
F8(MemoryOwner<double>.Allocate(ItemCount)),
L(
I1(MemoryOwner<sbyte>.Allocate(ItemCount)),
Expand All @@ -47,15 +47,15 @@ public void Setup()
B(MemoryOwner<byte>.Allocate(ItemCount)),
L(
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount))),
F8(MemoryOwner<double>.Allocate(ItemCount))),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount)),
L(
A(CreateString(ItemCount, Encoding.ASCII)),
J(CreateString(ItemCount, Item.Jis8Encoding)),
J(CreateString(ItemCount, Item.JIS8Encoding)),
Boolean(MemoryOwner<bool>.Allocate(ItemCount)),
B(MemoryOwner<byte>.Allocate(ItemCount))),
F8(MemoryOwner<double>.Allocate(ItemCount))),
Expand Down

0 comments on commit cb68fda

Please sign in to comment.