Skip to content

Commit d5fe7a9

Browse files
authored
Merge pull request #37 from pfpack/release/v4.0.1-rc.1
release/v4.0.1-rc.1
2 parents 851356f + 2624984 commit d5fe7a9

File tree

7 files changed

+115
-46
lines changed

7 files changed

+115
-46
lines changed

src/core-unit/Unit.Tests/Unit.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<PrivateAssets>all</PrivateAssets>
3737
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3838
</PackageReference>
39-
<PackageReference Include="xunit.v3" Version="3.2.0" />
39+
<PackageReference Include="xunit.v3" Version="3.2.1" />
4040
</ItemGroup>
4141

4242
<ItemGroup>

src/core-unit/Unit/Unit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<Description>PrimeFuncPack Core.Unit is a core library for .NET consisting of Unit type targeted for use in functional programming.</Description>
2020
<RootNamespace>System</RootNamespace>
2121
<AssemblyName>PrimeFuncPack.Core.Unit</AssemblyName>
22-
<Version>4.0.0</Version>
22+
<Version>4.0.1-rc.1</Version>
2323
</PropertyGroup>
2424

2525
<ItemGroup>
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
namespace System;
1+
#pragma warning disable IDE0060 // Remove unused parameter
2+
3+
using System.Runtime.CompilerServices;
4+
5+
namespace System;
26

37
partial class UnitExtensions
48
{
59
// We maintain that Unit can be derived from any input value.
6-
// Unit.From is being called instead of returning default(Unit) for the sake of clarity.
710
//
8-
// Don't apply AggressiveInlining to this extension since it's already applied to Unit.From
9-
// and redundant use of this attribute can reduce performance.
11+
// This extension is for more convenient syntax when getting Unit from a result value.
1012
//
11-
// This extension is for more convenient syntax when converting to Unit from a result value.
13+
// (For better performance, default(Unit) is returned instead of calling symmetric Unit.From factory.)
1214

13-
public static Unit ToUnit<TResult>(this TResult result)
14-
=>
15-
Unit.From(result);
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16+
public static Unit ToUnit<TResult>(this TResult result) => default;
1617
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Diagnostics;
2+
using System.Runtime.CompilerServices;
3+
using System.Text;
4+
5+
namespace System;
6+
7+
partial class UnitFormUtf8
8+
{
9+
private static Encoding Encoding => Encoding.UTF8;
10+
11+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12+
private static byte[]? InnerGetBytes(string s)
13+
{
14+
var bytesCount = Encoding.GetByteCount(s);
15+
if (bytesCount == default)
16+
{
17+
return null;
18+
}
19+
20+
var bytes = new byte[bytesCount];
21+
22+
var sourceSpan = s.AsSpan();
23+
var destSpan = new Span<byte>(bytes);
24+
var bytesWritten = Encoding.GetBytes(sourceSpan, destSpan);
25+
Debug.Assert(bytesWritten == bytesCount);
26+
27+
return bytes;
28+
}
29+
30+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31+
private static ReadOnlySpan<byte> InnerAsSpan(byte[]? bytes)
32+
{
33+
Debug.Assert(bytes is null || bytes.Length != default);
34+
35+
if (bytes is null)
36+
{
37+
#pragma warning disable IDE0301 // Simplify collection initialization
38+
return ReadOnlySpan<byte>.Empty;
39+
#pragma warning restore IDE0301 // Simplify collection initialization
40+
}
41+
42+
return new(bytes);
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace System;
2+
3+
partial class UnitFormUtf8
4+
{
5+
// We initialize the byte arrays inline in separate classes without static ctors
6+
// to obtain the values lazily and at the same time independently of each other.
7+
8+
// General (default) form
9+
private static class InnerGeneral
10+
{
11+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.General);
12+
}
13+
14+
// Basic forms
15+
private static class InnerCanonical
16+
{
17+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.Canonical);
18+
}
19+
private static class InnerJsonObj
20+
{
21+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.JsonObj);
22+
}
23+
private static class InnerEmpty
24+
{
25+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.Empty);
26+
}
27+
28+
// Extended forms
29+
private static class InnerCanonicalExtended
30+
{
31+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.CanonicalExtended);
32+
}
33+
private static class InnerJsonObjExtended
34+
{
35+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.JsonObjExtended);
36+
}
37+
private static class InnerEmptyExtended
38+
{
39+
internal static readonly byte[]? Value = InnerGetBytes(UnitForm.EmptyExtended);
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace System;
2+
3+
partial class UnitFormUtf8
4+
{
5+
// General (default) form
6+
internal static ReadOnlySpan<byte> General => InnerAsSpan(InnerGeneral.Value);
7+
8+
// Basic forms
9+
internal static ReadOnlySpan<byte> Canonical => InnerAsSpan(InnerCanonical.Value);
10+
internal static ReadOnlySpan<byte> JsonObj => InnerAsSpan(InnerJsonObj.Value);
11+
internal static ReadOnlySpan<byte> Empty => InnerAsSpan(InnerEmpty.Value);
12+
13+
// Extended forms
14+
internal static ReadOnlySpan<byte> CanonicalExtended => InnerAsSpan(InnerCanonicalExtended.Value);
15+
internal static ReadOnlySpan<byte> JsonObjExtended => InnerAsSpan(InnerJsonObjExtended.Value);
16+
internal static ReadOnlySpan<byte> EmptyExtended => InnerAsSpan(InnerEmptyExtended.Value);
17+
}
Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
1-
using System.Text;
1+
namespace System;
22

3-
namespace System;
4-
5-
internal static class UnitFormUtf8
3+
internal static partial class UnitFormUtf8
64
{
7-
// General (default) form
8-
internal static ReadOnlySpan<byte> General => new(InnerBytes.General);
9-
10-
// Basic forms
11-
internal static ReadOnlySpan<byte> Canonical => new(InnerBytes.Canonical);
12-
internal static ReadOnlySpan<byte> JsonObj => new(InnerBytes.JsonObj);
13-
internal static ReadOnlySpan<byte> Empty => new(InnerBytes.Empty);
14-
15-
// Extended forms
16-
internal static ReadOnlySpan<byte> CanonicalExtended => new(InnerBytes.CanonicalExtended);
17-
internal static ReadOnlySpan<byte> JsonObjExtended => new(InnerBytes.JsonObjExtended);
18-
internal static ReadOnlySpan<byte> EmptyExtended => new(InnerBytes.EmptyExtended);
19-
20-
// We initialize the byte arrays inline with no static ctor to obtain and cache them
21-
// lazily only on demand, and also isolate them in a nested class for thread-safety.
22-
private static class InnerBytes
23-
{
24-
internal static readonly byte[] General = InnerGetBytes(UnitForm.General);
25-
26-
internal static readonly byte[] Canonical = InnerGetBytes(UnitForm.Canonical);
27-
internal static readonly byte[] JsonObj = InnerGetBytes(UnitForm.JsonObj);
28-
internal static readonly byte[] Empty = InnerGetBytes(UnitForm.Empty);
29-
30-
internal static readonly byte[] CanonicalExtended = InnerGetBytes(UnitForm.CanonicalExtended);
31-
internal static readonly byte[] JsonObjExtended = InnerGetBytes(UnitForm.JsonObjExtended);
32-
internal static readonly byte[] EmptyExtended = InnerGetBytes(UnitForm.EmptyExtended);
33-
34-
// We use the most proven method to obtain UTF-8 bytes.
35-
private static byte[] InnerGetBytes(string s)
36-
=>
37-
Encoding.UTF8.GetBytes(s);
38-
}
395
}

0 commit comments

Comments
 (0)