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

Introduce NewStringFromCharArray{,SubSequence} Refaster rules #1012

Merged
merged 2 commits into from
Feb 11, 2024
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 @@ -162,6 +162,39 @@ String after(Object object) {
}
}

/**
* Prefer direct invocation of {@link String#String(char[], int, int)} over the indirection
* introduced by alternatives.
*/
static final class NewStringFromCharArraySubSequence {
@BeforeTemplate
String before(char[] data, int offset, int count) {
return Refaster.anyOf(
String.valueOf(data, offset, count), String.copyValueOf(data, offset, count));
}

@AfterTemplate
String after(char[] data, int offset, int count) {
return new String(data, offset, count);
}
}

/**
* Prefer direct invocation of {@link String#String(char[])} over the indirection introduced by
* alternatives.
*/
static final class NewStringFromCharArray {
@BeforeTemplate
String before(char[] data) {
return Refaster.anyOf(String.valueOf(data), new String(data, 0, data.length));
}

@AfterTemplate
String after(char[] data) {
return new String(data);
}
}

/**
* Prefer direct delegation to {@link String#valueOf(Object)} over the indirection introduced by
* {@link Objects#toString(Object)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ String testStringValueOf() {
return Objects.toString("foo");
}

ImmutableSet<String> testNewStringFromCharArraySubSequence() {
return ImmutableSet.of(
String.valueOf(new char[] {'f', 'o', 'o'}, 0, 1),
String.copyValueOf(new char[] {'b', 'a', 'r'}, 2, 3));
}

ImmutableSet<String> testNewStringFromCharArray() {
return ImmutableSet.of(
String.valueOf(new char[] {'f', 'o', 'o'}),
new String(new char[] {'b', 'a', 'r'}, 0, new char[] {'b', 'a', 'r'}.length));
}

Function<Object, String> testStringValueOfMethodReference() {
return Objects::toString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ String testStringValueOf() {
return String.valueOf("foo");
}

ImmutableSet<String> testNewStringFromCharArraySubSequence() {
return ImmutableSet.of(
new String(new char[] {'f', 'o', 'o'}, 0, 1), new String(new char[] {'b', 'a', 'r'}, 2, 3));
}

ImmutableSet<String> testNewStringFromCharArray() {
return ImmutableSet.of(
new String(new char[] {'f', 'o', 'o'}), new String(new char[] {'b', 'a', 'r'}));
}

Function<Object, String> testStringValueOfMethodReference() {
return String::valueOf;
}
Expand Down
Loading