Skip to content

Commit

Permalink
Use String#replace whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Dec 22, 2023
1 parent 7cb59b3 commit 9b7db13
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/main/java/net/datafaker/providers/base/Code.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.datafaker.providers.base;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand All @@ -18,8 +17,6 @@ public class Code extends AbstractProvider<BaseProviders> {
private static final String[] REPORTING_BODY_IDENTIFIERS
= {"01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};

private static final Pattern HYPHEN = Pattern.compile("-");

protected Code(BaseProviders faker) {
super(faker);
}
Expand Down Expand Up @@ -147,7 +144,7 @@ private int isbn13CheckDigit(CharSequence t) {
}

private String stripIsbnSeparator(CharSequence t) {
return HYPHEN.matcher(t.toString()).replaceAll("");
return t.toString().replace("-", "");
}

public String asin() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/datafaker/providers/base/Internet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @since 0.8.0
*/
public class Internet extends AbstractProvider<BaseProviders> {
private static final Pattern SINGLE_QUOTE = Pattern.compile("'");
private static final Pattern COLON = Pattern.compile(":");
private static final List<String> HTTP_SCHEMES = List.of("http://", "https://");

Expand All @@ -41,7 +40,8 @@ protected Internet(BaseProviders faker) {
*/
public String username() {
StringBuilder result = new StringBuilder();
final String firstName = faker.name().firstName().toLowerCase(faker.getContext().getLocale()) + "." + faker.name().lastName().toLowerCase(faker.getContext().getLocale());
final Name name = faker.name();
final String firstName = name.firstName().toLowerCase(faker.getContext().getLocale()) + "." + name.lastName().toLowerCase(faker.getContext().getLocale());
for (int i = 0; i < firstName.length(); i++) {
final char c = firstName.charAt(i);
if (c == '\'' || Character.isWhitespace(c)) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public String domainName() {
}

public String domainWord() {
return FakerIDN.toASCII(SINGLE_QUOTE.matcher(faker.name().lastName().toLowerCase()).replaceAll(""));
return FakerIDN.toASCII(faker.name().lastName().toLowerCase().replace("'", ""));
}

public String domainSuffix() {
Expand Down Expand Up @@ -145,7 +145,7 @@ public String webdomain() {
"www",
".",
FakerIDN.toASCII(
SINGLE_QUOTE.matcher(faker.name().firstName().toLowerCase()).replaceAll("") +
faker.name().firstName().toLowerCase().replace("'", "") +
"-" +
domainWord()
),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/datafaker/service/FakeValuesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ private String[] splitArguments(String arguments) {
i++;
}
if (cnt % 2 == 1) {
result.add(arguments.substring(start, i - 1).replaceAll("''", "'"));
result.add(arguments.substring(start, i - 1).replace("''", "'"));
argsStarted = false;
}
} else if (arguments.charAt(i) == '\'') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void creditCard() {
}

private void assertCardLuhnDigit(String creditCard) {
final String creditCardStripped = creditCard.replaceAll("-", "");
final String creditCardStripped = creditCard.replace("-", "");
assertThat(LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(creditCardStripped)).isTrue();
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/net/datafaker/script/ProviderGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ private void createTest(File file, Set<String> strings, ProviderType providerTyp
private String toJavaConvention(String baseName) {

// replace underscores with spaces
String withoutUnderscore = baseName.replaceAll("_", " ");
String withoutUnderscore = baseName.replace("_", " ");
// for every word in the name, capitalize the first letter
String capitalizedWords = WordUtils.capitalize(withoutUnderscore);
// remove all spaces
return capitalizedWords.replaceAll(" ", "");
return capitalizedWords.replace(" ", "");
}
}

Expand Down

0 comments on commit 9b7db13

Please sign in to comment.