Skip to content

Commit

Permalink
Use array of chars instead of StringBuilder for Company#domainName
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Oct 2, 2023
1 parent d7ade4f commit ec31786
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
23 changes: 10 additions & 13 deletions src/main/java/net/datafaker/providers/base/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,22 @@ public String logo() {
}

public String url() {
return String.join(".",
"www",
FakerIDN.toASCII(domainName()),
domainSuffix()
);
return "www."
+ FakerIDN.toASCII(domainName()) + "."
+ domainSuffix();
}

private String domainName() {
String value = name().toLowerCase(faker.getContext().getLocale());
StringBuilder sb = new StringBuilder(value.length());
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
final char[] res = name().toLowerCase(faker.getContext().getLocale()).toCharArray();
int offset = 0;
for (int i = 0; i < res.length; i++) {
final char c = res[i];
switch (c) {
case '.', ',', '\'', ' ', ']' -> {
}
default -> sb.append(c);
case '.', ',', '\'', ' ', ']' -> offset++;
default -> res[i - offset] = res[i];
}
}
return sb.toString();
return String.valueOf(res, 0, res.length - offset);
}

private String domainSuffix() {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/net/datafaker/providers/base/CompanyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

import java.util.List;
import java.util.Collection;
import java.util.regex.Pattern;

class CompanyTest extends BaseFakerTest<BaseFaker> {

public static final Pattern URL_PATTERN = Pattern.compile("(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])");
public static final Pattern PHRASE_PATTERN = Pattern.compile("(\\w+[ /-]?){1,9}");
private final Company company = faker.company();

@Test
Expand All @@ -31,12 +34,12 @@ void testBuzzword() {

@Test
void testCatchPhrase() {
assertThat(company.catchPhrase()).matches("(\\w+[ /-]?){1,9}");
assertThat(company.catchPhrase()).matches(PHRASE_PATTERN);
}

@Test
void testBs() {
assertThat(company.bs()).matches("(\\w+[ /-]?){1,9}");
assertThat(company.bs()).matches(PHRASE_PATTERN);
}

@Test
Expand All @@ -46,7 +49,6 @@ void testLogo() {

@RepeatedTest(100)
void testUrl() {
String regexp = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])";
assertThat(company.url()).matches(regexp);
assertThat(company.url()).matches(URL_PATTERN);
}
}

0 comments on commit ec31786

Please sign in to comment.