Skip to content

Commit

Permalink
Use array of chars instead of String builder in DocumentFormatterUtil…
Browse files Browse the repository at this point in the history
…#cpf where length is predicted
  • Loading branch information
snuyanzin committed Oct 1, 2023
1 parent 8d79729 commit 9ad4dc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
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);
}
}

0 comments on commit 9ad4dc6

Please sign in to comment.