Skip to content

Commit

Permalink
Add bank account number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Aug 7, 2023
1 parent f322083 commit 9ea42ba
Show file tree
Hide file tree
Showing 6 changed files with 1,080 additions and 1,013 deletions.
36 changes: 36 additions & 0 deletions src/Redskap/BankAccountNumber.Formatting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using static Redskap.FormattingHelpers;

namespace Redskap;

public readonly partial struct BankAccountNumber
{
/// <inheritdoc />
public override string ToString()
{
const int length = Length + 2;

#if NETSTANDARD
var buffer = new char[length];

Format(this, buffer);

return new string(buffer);
#else
return string.Create(length, this, (span, number) =>
{
Format(number, span);
});
#endif
}

private static void Format(BankAccountNumber number, Span<char> buffer)
{
WriteDigit((uint) number.CheckDigit, buffer, 12);
WriteFourDecimalDigits((uint) number.CustomerNumber, buffer, 8);
buffer[7] = ' ';
WriteTwoDecimalDigits((uint) number.AccountGroup, buffer, 5);
buffer[4] = ' ';
WriteFourDecimalDigits((uint) number.RegisterNumber, buffer, 0);
}
}
30 changes: 29 additions & 1 deletion src/Redskap/FormattingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ internal static class FormattingHelpers
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static char GetChar(uint digit) => (char) ('0' + digit);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteDigit(uint value, Span<char> destination, int offset)
{
Debug.Assert(value <= 9);

destination[offset] = GetChar(value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteTwoDecimalDigits(uint value, Span<char> destination, int offset)
{
Expand Down Expand Up @@ -36,4 +44,24 @@ public static void WriteThreeDecimalDigits(uint value, Span<char> destination, i

destination[offset] = GetChar(value);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteFourDecimalDigits(uint value, Span<char> destination, int offset)
{
Debug.Assert(value <= 9999);

var temp = value;
value /= 10;
destination[offset + 3] = GetChar(temp - (value * 10));

temp = value;
value /= 10;
destination[offset + 2] = GetChar(temp - (value * 10));

temp = value;
value /= 10;
destination[offset + 1] = GetChar(temp - (value * 10));

destination[offset] = GetChar(value);
}
}
8 changes: 4 additions & 4 deletions src/Redskap/IdentificationNumber.Formatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public readonly partial struct IdentificationNumber
public override string ToString()
{
#if NETSTANDARD
var buffer = new char[Length];
var buffer = new char[Length];

Format(this, buffer);
Format(this, buffer);

return new string(buffer);
return new string(buffer);
#else
return string.Create(Length, this, (span, number) =>
{
Expand Down Expand Up @@ -64,4 +64,4 @@ private static void WriteYear(Span<char> destination, DateTime dateOfBirth)
{
WriteTwoDecimalDigits((uint) dateOfBirth.Year % 100, destination, 4);
}
}
}
11 changes: 7 additions & 4 deletions src/Redskap/Redskap.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>

<PackageProjectUrl>https://github.com/khellang/Redskap</PackageProjectUrl>
<RepositoryUrl>https://github.com/khellang/Redskap</RepositoryUrl>

Expand All @@ -26,10 +26,10 @@
<PackageTags>norway;norwegian;fødselsnummer;personnummer;parser;validator</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Authors>Kristian Hellang</Authors>

<PolySharpExcludeGeneratedTypes>System.Range</PolySharpExcludeGeneratedTypes>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
<None Include="../../icon.png" Pack="true" PackagePath="/" />
Expand Down Expand Up @@ -86,14 +86,17 @@
<DesignTime>True</DesignTime>
<DependentUpon>FamilyNames.tt</DependentUpon>
</Compile>
<Compile Update="BankAccountNumber.Formatting.cs">
<DependentUpon>BankAccountNumber.Parsing.cs</DependentUpon>
</Compile>
<Compile Update="BankAccountNumber.Parsing.cs">
<DependentUpon>BankAccountNumber.cs</DependentUpon>
</Compile>
<Compile Update="BankAccountNumber.Parsing.netstandard.cs">
<DependentUpon>BankAccountNumber.Parsing.cs</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageReference Include="System.Memory" Version="4.5.5" />
Expand Down
8 changes: 4 additions & 4 deletions test/Redskap.Tests/BankAccountNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void TryParse_Valid_Returns_True(string path)
Assert.Equal(testData.AccountGroup, result.AccountGroup);
Assert.Equal(testData.CustomerNumber, result.CustomerNumber);
Assert.Equal(testData.CheckDigit, result.CheckDigit);
// Assert.Equal(testData.Value, result.ToString());
Assert.Equal(testData.Value, result.ToString());
}
}

Expand Down Expand Up @@ -60,9 +60,9 @@ public static IEnumerable<TestData> ReadFrom(string fileName)
foreach (var line in File.ReadLines(fileName))
{
var registerNumber = int.Parse(line.AsSpan().Slice(0, 4));
var accountGroup = int.Parse(line.AsSpan().Slice(4, 2));
var customerNumber = int.Parse(line.AsSpan().Slice(6, 4));
var checkDigit = int.Parse(line.AsSpan().Slice(10, 1));
var accountGroup = int.Parse(line.AsSpan().Slice(5, 2));
var customerNumber = int.Parse(line.AsSpan().Slice(8, 4));
var checkDigit = int.Parse(line.AsSpan().Slice(12, 1));

yield return new TestData(line, registerNumber, accountGroup, customerNumber, checkDigit);
}
Expand Down
Loading

0 comments on commit 9ea42ba

Please sign in to comment.