Skip to content

Commit

Permalink
change current impl (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromoh authored Jul 5, 2023
1 parent 9056be7 commit fc6215e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 50 deletions.
53 changes: 28 additions & 25 deletions src/CNPJ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,65 @@ public CNPJ(string value)
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static bool Validate(string value)
{
Span<int> digits = stackalloc int[14];
Span<int> digits2 = stackalloc int[15];
var digits = digits2.Slice(1);

return Utils.TryWriteNumbers(digits, value)
&& CriaDigitoVerificador(digits, true) == digits[12]
&& CriaDigitoVerificador(digits, false) == digits[13];
&& CriaDigitoVerificador(digits2) == digits[12]
&& CriaDigitoVerificador(digits) == digits[13];
}

[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
internal static int CriaDigitoVerificador(ReadOnlySpan<int> cnpj, bool skipFirst)
internal static int CriaDigitoVerificador(ReadOnlySpan<int> cnpj)
{
var i = 0;

var total =
(skipFirst ? 0 : cnpj[i++] * 6) +
cnpj[i++] * 5 +
cnpj[i++] * 4 +
cnpj[i++] * 3 +
cnpj[i++] * 2 +
cnpj[i++] * 9 +
cnpj[i++] * 8 +
cnpj[i++] * 7 +
cnpj[i++] * 6 +
cnpj[i++] * 5 +
cnpj[i++] * 4 +
cnpj[i++] * 3 +
cnpj[i++] * 2;
cnpj[0] * 6 +
cnpj[1] * 5 +
cnpj[2] * 4 +
cnpj[3] * 3 +
cnpj[4] * 2 +
cnpj[5] * 9 +
cnpj[6] * 8 +
cnpj[7] * 7 +
cnpj[8] * 6 +
cnpj[9] * 5 +
cnpj[10] * 4 +
cnpj[11] * 3 +
cnpj[12] * 2;

total %= 11;
return total < 2 ? 0 : 11 - total;
}

public static string Generate()
{
Span<int> digits = stackalloc int[14];
Span<int> digits2 = stackalloc int[15];
var digits = digits2.Slice(1);

Utils.GenerateImpl(digits, 8);
digits[8] = 0;
digits[9] = 0;
digits[10] = 0;
digits[11] = 1;
digits[12] = CriaDigitoVerificador(digits, true);
digits[13] = CriaDigitoVerificador(digits, false);
digits[12] = CriaDigitoVerificador(digits2);
digits[13] = CriaDigitoVerificador(digits);
Span<char> chars = stackalloc char[14];
Utils.Cast(digits, chars);
return new string(chars);
}

public static string GenerateFormatted()
{
Span<int> digits = stackalloc int[14];
Span<int> digits2 = stackalloc int[15];
var digits = digits2.Slice(1);

Utils.GenerateImpl(digits, 8);
digits[8] = 0;
digits[9] = 0;
digits[10] = 0;
digits[11] = 1;
digits[12] = CriaDigitoVerificador(digits, true);
digits[13] = CriaDigitoVerificador(digits, false);
digits[12] = CriaDigitoVerificador(digits2);
digits[13] = CriaDigitoVerificador(digits);
Span<char> chars = stackalloc char[18];

Utils.Cast(digits.Slice(0, 2), chars.Slice(0, 2));
Expand Down
48 changes: 25 additions & 23 deletions src/CPF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,53 @@ public CPF(string value)
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static bool Validate(string value)
{
Span<int> digits = stackalloc int[11];

Span<int> digits2 = stackalloc int[12];
var digits = digits2.Slice(1);
return Utils.TryWriteNumbers(digits, value)
&& CriaDigitoVerificador(digits, true) == digits[9]
&& CriaDigitoVerificador(digits, false) == digits[10];
&& CriaDigitoVerificador(digits2) == digits[9]
&& CriaDigitoVerificador(digits) == digits[10];
}

[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
internal static int CriaDigitoVerificador(ReadOnlySpan<int> cpf, bool skipFirst)
internal static int CriaDigitoVerificador(ReadOnlySpan<int> cpf)
{
var i = 0;

var total =
(skipFirst ? 0 : cpf[i++] * 11) +
cpf[i++] * 10 +
cpf[i++] * 9 +
cpf[i++] * 8 +
cpf[i++] * 7 +
cpf[i++] * 6 +
cpf[i++] * 5 +
cpf[i++] * 4 +
cpf[i++] * 3 +
cpf[i++] * 2;
cpf[0] * 11 +
cpf[1] * 10 +
cpf[2] * 9 +
cpf[3] * 8 +
cpf[4] * 7 +
cpf[5] * 6 +
cpf[6] * 5 +
cpf[7] * 4 +
cpf[8] * 3 +
cpf[9] * 2;

total %= 11;
return total < 2 ? 0 : 11 - total;
}

public static string Generate()
{
Span<int> digits = stackalloc int[11];
Span<int> digits2 = stackalloc int[12];
var digits = digits2.Slice(1);

Utils.GenerateImpl(digits, 9);
digits[9] = CriaDigitoVerificador(digits, true);
digits[10] = CriaDigitoVerificador(digits, false);
digits[9] = CriaDigitoVerificador(digits2);
digits[10] = CriaDigitoVerificador(digits);
Span<char> chars = stackalloc char[11];
Utils.Cast(digits, chars);
return new string(chars);
}

public static string GenerateFormatted()
{
Span<int> digits = stackalloc int[11];
Span<int> digits2 = stackalloc int[12];
var digits = digits2.Slice(1);

Utils.GenerateImpl(digits, 9);
digits[9] = CriaDigitoVerificador(digits, true);
digits[10] = CriaDigitoVerificador(digits, false);
digits[9] = CriaDigitoVerificador(digits2);
digits[10] = CriaDigitoVerificador(digits);
Span<char> chars = stackalloc char[14];

Utils.Cast(digits.Slice(0, 3), chars.Slice(0, 3));
Expand Down
2 changes: 0 additions & 2 deletions src/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

[assembly: InternalsVisibleTo("benchmark")]

internal delegate int SpanInt(ReadOnlySpan<int> span, bool skipFirst);

internal class Utils
{
private static readonly Random _random = new();
Expand Down

0 comments on commit fc6215e

Please sign in to comment.