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

Use array of chars instead of String builder in DocumentFormatterUtil#cpf where length is predicted #977

Merged
merged 1 commit into from
Oct 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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/net/datafaker/providers/base/CPFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseFaker> {

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.
*/
Expand All @@ -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);
}
}