From fc6215eeb1751311ebece6dc42570261d0674b8f Mon Sep 17 00:00:00 2001 From: Leandro Fernandes Date: Wed, 5 Jul 2023 10:10:35 -0300 Subject: [PATCH] change current impl (#6) --- src/CNPJ.cs | 53 +++++++++++++++++++++++++++------------------------- src/CPF.cs | 48 ++++++++++++++++++++++++----------------------- src/Utils.cs | 2 -- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/CNPJ.cs b/src/CNPJ.cs index 7ff9c81..4663654 100644 --- a/src/CNPJ.cs +++ b/src/CNPJ.cs @@ -16,32 +16,31 @@ public CNPJ(string value) [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static bool Validate(string value) { - Span digits = stackalloc int[14]; + Span 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 cnpj, bool skipFirst) + internal static int CriaDigitoVerificador(ReadOnlySpan 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; @@ -49,14 +48,16 @@ internal static int CriaDigitoVerificador(ReadOnlySpan cnpj, bool skipFirst public static string Generate() { - Span digits = stackalloc int[14]; + Span 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 chars = stackalloc char[14]; Utils.Cast(digits, chars); return new string(chars); @@ -64,14 +65,16 @@ public static string Generate() public static string GenerateFormatted() { - Span digits = stackalloc int[14]; + Span 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 chars = stackalloc char[18]; Utils.Cast(digits.Slice(0, 2), chars.Slice(0, 2)); diff --git a/src/CPF.cs b/src/CPF.cs index a3a0efc..783961c 100644 --- a/src/CPF.cs +++ b/src/CPF.cs @@ -16,29 +16,27 @@ public CPF(string value) [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] public static bool Validate(string value) { - Span digits = stackalloc int[11]; - + Span 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 cpf, bool skipFirst) + internal static int CriaDigitoVerificador(ReadOnlySpan 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; @@ -46,10 +44,12 @@ internal static int CriaDigitoVerificador(ReadOnlySpan cpf, bool skipFirst) public static string Generate() { - Span digits = stackalloc int[11]; + Span 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 chars = stackalloc char[11]; Utils.Cast(digits, chars); return new string(chars); @@ -57,10 +57,12 @@ public static string Generate() public static string GenerateFormatted() { - Span digits = stackalloc int[11]; + Span 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 chars = stackalloc char[14]; Utils.Cast(digits.Slice(0, 3), chars.Slice(0, 3)); diff --git a/src/Utils.cs b/src/Utils.cs index d3d9ab7..27a0e89 100644 --- a/src/Utils.cs +++ b/src/Utils.cs @@ -2,8 +2,6 @@ [assembly: InternalsVisibleTo("benchmark")] -internal delegate int SpanInt(ReadOnlySpan span, bool skipFirst); - internal class Utils { private static readonly Random _random = new();