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

Add support for .NET 8 #26

Merged
merged 4 commits into from
Dec 1, 2023
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/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x

- name: Generate the package
# NOTE: As for the `${GITHUB_REF_NAME#v}` bit below, see https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables:~:text=branch%2D1.-,GITHUB_REF_NAME,-The%20short%20ref. We could've also done https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable but since we're using this value in a single place, we don't need to make it an environment variable.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x # NOTE: The `windows-latest` container already comes pre-installed with the .NET versions that we need, but we do need to use `setup-dotnet` (and pass it one of the versions — e.g. 7.0.x), so that the relevant binaries (e.g. `dotnet`) are actually added to PATH so we can use them. See https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software and https://github.com/actions/setup-dotnet
dotnet-version: 8.0.x # NOTE: The `windows-latest` container already comes pre-installed with the .NET versions that we need, but we do need to use `setup-dotnet` (and pass it one of the versions — e.g. 7.0.x), so that the relevant binaries (e.g. `dotnet`) are actually added to PATH so we can use them. See https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software and https://github.com/actions/setup-dotnet

# TODO: Some .NET codebases with GitHub Actions workflows seem to run `dotnet restore`, `dotnet build` and `dotnet test` as separate steps. Why?
- name: Run the tests
Expand Down
4 changes: 2 additions & 2 deletions src/Sqids/Sqids.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<LangVersion>11.0</LangVersion>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
Expand Down
22 changes: 17 additions & 5 deletions src/Sqids/SqidsEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public SqidsEncoder(SqidsOptions options)
w.Any(c => !options.Alphabet.Contains(c, StringComparison.OrdinalIgnoreCase))
#endif
);
_blockList = options.BlockList.ToArray(); // NOTE: Arrays are faster to iterate than HashSets, so we construct an array here.
_blockList = [.. options.BlockList]; // NOTE: Arrays are faster to iterate than HashSets, so we construct an array here.

_alphabet = options.Alphabet.ToCharArray();
ConsistentShuffle(_alphabet);
Expand All @@ -129,7 +129,10 @@ public string Encode(T number)
public string Encode(int number)
#endif
{
#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfLessThan(number, T.Zero, nameof(number));
#else
LeaFrock marked this conversation as resolved.
Show resolved Hide resolved
#if NET7_0
if (number < T.Zero)
#else
if (number < 0)
Expand All @@ -140,6 +143,11 @@ public string Encode(int number)
);

return Encode(stackalloc[] { number }); // NOTE: We use `stackalloc` here in order not to incur the cost of allocating an array on the heap, since we know the array will only have one element, we can use `stackalloc` safely.
#endif

#if NET8_0_OR_GREATER
return Encode([number]);
#endif
}

/// <summary>
Expand All @@ -159,7 +167,10 @@ public string Encode(params int[] numbers)
return string.Empty;

foreach (var number in numbers)
#if NET7_0_OR_GREATER
#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfLessThan(number, T.Zero, nameof(numbers));
#else
LeaFrock marked this conversation as resolved.
Show resolved Hide resolved
#if NET7_0
if (number < T.Zero)
#else
if (number < 0)
Expand All @@ -168,6 +179,7 @@ public string Encode(params int[] numbers)
nameof(numbers),
"Encoding is only supported for zero and positive numbers."
);
#endif

return Encode(numbers.AsSpan());
}
Expand Down Expand Up @@ -387,13 +399,13 @@ private static ReadOnlySpan<char> ToId(int num, ReadOnlySpan<char> alphabet)
do
{
id.Insert(0, alphabet[int.CreateChecked(result % T.CreateChecked(alphabet.Length))]);
result = result / T.CreateChecked(alphabet.Length);
result /= T.CreateChecked(alphabet.Length);
} while (result > T.Zero);
#else
do
{
id.Insert(0, alphabet[result % alphabet.Length]);
result = result / alphabet.Length;
result /= alphabet.Length;
} while (result > 0);
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/Sqids/SqidsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public sealed class SqidsOptions
/// <summary>
/// List of blocked words that must not appear in the IDs.
/// </summary>
public HashSet<string> BlockList { get; set; } = new()
{
public HashSet<string> BlockList { get; set; } =
[
"0rgasm",
"1d10t",
"1d1ot",
Expand Down Expand Up @@ -594,5 +594,5 @@ public sealed class SqidsOptions
"zocc0la",
"zocco1a",
"zoccola"
};
];
}
20 changes: 13 additions & 7 deletions test/Sqids.Tests/Sqids.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

<PropertyGroup>
<!-- NOTE: `net472` is used for testing `netstandard2.0` -->
<TargetFrameworks>net472;net6.0;net7.0</TargetFrameworks>
<LangVersion>11.0</LangVersion>
<TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

Expand Down