Skip to content

Commit

Permalink
Merge pull request #37 from cuioss/feature/fix-lenientFormat-sideeffects
Browse files Browse the repository at this point in the history
CUI-1532 remove side effects on provided object varargs of MoreString…
  • Loading branch information
cuioss authored Jul 29, 2024
2 parents dbfc5e7 + 4626da5 commit a651f0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
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

0 comments on commit a651f0b

Please sign in to comment.