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

CUI-1532 remove side effects on provided object varargs of MoreString… #37

Merged
merged 1 commit into from
Jul 29, 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
35 changes: 19 additions & 16 deletions src/main/java/de/cuioss/tools/string/MoreStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static de.cuioss.tools.base.Preconditions.checkArgument;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

Expand Down Expand Up @@ -863,39 +865,40 @@ public static String trimOrNull(final String string) {
public static String lenientFormat(String template, Object... args) {
template = String.valueOf(template); // null -> "null"

final List<Object> lenientArgs;

if (args == null) {
args = new Object[] { "(Object[])null" };
lenientArgs= new ArrayList<>(1);
lenientArgs.add("(Object[])null");
} else {
for (var i = 0; i < args.length; i++) {
args[i] = lenientToString(args[i]);
lenientArgs = new ArrayList<>(args.length);
for (Object arg : args) {
lenientArgs.add(lenientToString(arg));
}
}

// start substituting the arguments into the '%s' placeholders
var builder = new StringBuilder(template.length() + 16 * args.length);
var templateStart = 0;
var i = 0;
while (i < args.length) {
var placeholderStart = template.indexOf("%s", templateStart);
StringBuilder builder = new StringBuilder(template.length() + 16 * lenientArgs.size());
int templateStart = 0;
int i = 0;
while (i < lenientArgs.size()) {
int placeholderStart = template.indexOf("%s", templateStart);
if (placeholderStart == -1) {
break;
}
builder.append(template, templateStart, placeholderStart);
builder.append(args[i]);
i++;
builder.append(lenientArgs.get(i++));
templateStart = placeholderStart + 2;
}
builder.append(template, templateStart, template.length());

// if we run out of placeholders, append the extra args in square braces
if (i < args.length) {
if (i < lenientArgs.size()) {
builder.append(" [");
builder.append(args[i]);
i++;
while (i < args.length) {
builder.append(lenientArgs.get(i++));
while (i < lenientArgs.size()) {
builder.append(", ");
builder.append(args[i]);
i++;
builder.append(lenientArgs.get(i++));
}
builder.append(']');
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/de/cuioss/tools/string/MoreStringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ void testLenientFormat() {
assertEquals("null [5, 6]", MoreStrings.lenientFormat(null, 5, 6));
assertEquals("null", MoreStrings.lenientFormat("%s", (Object) null));
assertEquals("(Object[])null", MoreStrings.lenientFormat("%s", (Object[]) null));
assertEquals("missing param 1%s", MoreStrings.lenientFormat("missing param %s%s", "1"));
}

@Test
Expand Down
Loading