From 9ad4dc6e8029982f6d96f67ed27d6ff83712c621 Mon Sep 17 00:00:00 2001 From: Sergey Nuyanzin Date: Sun, 1 Oct 2023 22:14:34 +0200 Subject: [PATCH] Use array of chars instead of String builder in DocumentFormatterUtil#cpf where length is predicted --- .../idnumbers/pt/br/DocumentFormatterUtil.java | 16 ++++++++++------ .../pt/br/IdNumberGeneratorPtBrUtil.java | 6 +++--- .../net/datafaker/providers/base/CPFTest.java | 14 ++++++++------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/datafaker/idnumbers/pt/br/DocumentFormatterUtil.java b/src/main/java/net/datafaker/idnumbers/pt/br/DocumentFormatterUtil.java index 1df0642a4..30d38b2e3 100644 --- a/src/main/java/net/datafaker/idnumbers/pt/br/DocumentFormatterUtil.java +++ b/src/main/java/net/datafaker/idnumbers/pt/br/DocumentFormatterUtil.java @@ -22,12 +22,16 @@ public static String cnpj(String cnpj) { } public static String cpf(String cpf) { - StringBuilder sb = new StringBuilder(20); - sb.append(cpf, 0, 3) - .append('.').append(cpf, 3, 6) - .append('.').append(cpf, 6, 9) - .append('-').append(cpf, 9, cpf.length()); - return sb.toString(); + char[] input = cpf.toCharArray(); + char[] res = new char[input.length + 3]; + System.arraycopy(input, 0, res, 0, 3); + res[3] = '.'; + System.arraycopy(input, 3, res, 4, 3); + res[7] = '.'; + System.arraycopy(input, 6, res, 8, 3); + res[11] = '-'; + System.arraycopy(input, 9, res, 12, input.length - 9); + return String.valueOf(res); } public static String unmask(String doc) { diff --git a/src/main/java/net/datafaker/idnumbers/pt/br/IdNumberGeneratorPtBrUtil.java b/src/main/java/net/datafaker/idnumbers/pt/br/IdNumberGeneratorPtBrUtil.java index aa4beecf9..27ddef9a0 100644 --- a/src/main/java/net/datafaker/idnumbers/pt/br/IdNumberGeneratorPtBrUtil.java +++ b/src/main/java/net/datafaker/idnumbers/pt/br/IdNumberGeneratorPtBrUtil.java @@ -60,11 +60,11 @@ public static String cnpj(BaseProviders faker, boolean formatted, boolean valid, public static String cpf(BaseProviders faker, boolean formatted, boolean valid) { String cpf; if (valid) { - StringBuilder partial = new StringBuilder(); + char[] partial = new char[9]; for (int i = 0; i < 9; i++) { - partial.append(faker.random().nextInt(9)); + partial[i] = (char)('0' + faker.random().nextInt(9)); } - cpf = partial.toString(); + cpf = String.valueOf(partial); int d1 = digit(calculateWeight(cpf, 10, 0, cpf.length())); int d2 = digit((d1 * 2) + calculateWeight(cpf, 11, 0, cpf.length())); diff --git a/src/test/java/net/datafaker/providers/base/CPFTest.java b/src/test/java/net/datafaker/providers/base/CPFTest.java index 32fed1a9b..375ba22e3 100644 --- a/src/test/java/net/datafaker/providers/base/CPFTest.java +++ b/src/test/java/net/datafaker/providers/base/CPFTest.java @@ -2,12 +2,16 @@ import org.junit.jupiter.api.RepeatedTest; +import java.util.regex.Pattern; + import static net.datafaker.idnumbers.pt.br.IdNumberGeneratorPtBrUtil.isCPFValid; import static org.assertj.core.api.Assertions.assertThat; class CPFTest extends BaseFakerTest { + public static final Pattern CPF_EXPRESSION = Pattern.compile("(^\\d{3}\\x2E\\d{3}\\x2E\\d{3}\\x2D\\d{2}$)"); + /** * A valid CPF is either a real number or a generated valid number. */ @@ -30,11 +34,9 @@ void isInvalidCPF() { */ @RepeatedTest(100) void formattedCPF() { - final String cpfExpression = "(^\\d{3}\\x2E\\d{3}\\x2E\\d{3}\\x2D\\d{2}$)"; - - assertThat(faker.cpf().valid()).matches(cpfExpression); - assertThat(faker.cpf().valid(true)).matches(cpfExpression); - assertThat(faker.cpf().invalid()).matches(cpfExpression); - assertThat(faker.cpf().invalid(true)).matches(cpfExpression); + assertThat(faker.cpf().valid()).matches(CPF_EXPRESSION); + assertThat(faker.cpf().valid(true)).matches(CPF_EXPRESSION); + assertThat(faker.cpf().invalid()).matches(CPF_EXPRESSION); + assertThat(faker.cpf().invalid(true)).matches(CPF_EXPRESSION); } }