This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_5_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_5_0_X by this push:
new 1906c8fa58 Avoid `String` concatenation in loop
1906c8fa58 is described below
commit 1906c8fa58cd5048fc3b0a033616bff82a631c16
Author: Daniel Sun <[email protected]>
AuthorDate: Sun Sep 7 05:13:09 2025 +0900
Avoid `String` concatenation in loop
(cherry picked from commit c0da6b4943aebd8468df386cbc08ff03a9c21bb8)
---
.../groovy/groovysh/jline/GroovyPosixCommands.java | 20 ++++++++++----------
.../groovy/groovysh/jline/SystemRegistryImpl.java | 20 +++++++++-----------
2 files changed, 19 insertions(+), 21 deletions(-)
diff --git
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java
index 512bfd1f1b..dcf69ffeaf 100644
---
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java
+++
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyPosixCommands.java
@@ -514,30 +514,30 @@ public class GroovyPosixCommands extends PosixCommands {
}
String longDisplay() {
- String username;
+ StringBuilder username;
if (attributes.containsKey("owner")) {
- username = Objects.toString(attributes.get("owner"), null);
+ username = new
StringBuilder(Objects.toString(attributes.get("owner"), null));
} else {
- username = "owner";
+ username = new StringBuilder("owner");
}
if (username.length() > 8) {
- username = username.substring(0, 8);
+ username = new StringBuilder(username.substring(0, 8));
} else {
for (int i = username.length(); i < 8; i++) {
- username = username + " ";
+ username.append(" ");
}
}
- String group;
+ StringBuilder group;
if (attributes.containsKey("group")) {
- group = Objects.toString(attributes.get("group"), null);
+ group = new
StringBuilder(Objects.toString(attributes.get("group"), null));
} else {
- group = "group";
+ group = new StringBuilder("group");
}
if (group.length() > 8) {
- group = group.substring(0, 8);
+ group = new StringBuilder(group.substring(0, 8));
} else {
for (int i = group.length(); i < 8; i++) {
- group = group + " ";
+ group.append(" ");
}
}
Number length = (Number) attributes.get("size");
diff --git
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/SystemRegistryImpl.java
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/SystemRegistryImpl.java
index a39a6fa378..6a3849aa4c 100644
---
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/SystemRegistryImpl.java
+++
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/SystemRegistryImpl.java
@@ -656,7 +656,7 @@ public class SystemRegistryImpl implements SystemRegistry {
int last;
List<String> pipes = new ArrayList<>();
String pipeSource = null;
- String rawLine = null;
+ StringBuilder rawLine = null;
String pipeResult = null;
if (isCommandAlias(ap.command())) {
ap.parse(replaceCommandAlias(ap.variable(), ap.command(),
nextRawLine));
@@ -783,7 +783,7 @@ public class SystemRegistryImpl implements SystemRegistry {
if (rawLine != null || (pipes.size() > 1 &&
customPipes.containsKey(pipes.get(pipes.size() - 2)))) {
done = false;
if (rawLine == null) {
- rawLine = pipeSource;
+ rawLine = new StringBuilder(pipeSource);
}
if (customPipes.containsKey(pipes.get(pipes.size() - 2))) {
List<String> fixes =
customPipes.get(pipes.get(pipes.size() - 2));
@@ -791,9 +791,7 @@ public class SystemRegistryImpl implements SystemRegistry {
int idx = subLine.indexOf(" ");
subLine = idx > 0 ? subLine.substring(idx + 1) :
"";
}
- rawLine += fixes.get(0)
- + (consoleId != null ?
consoleEngine().expandCommandLine(subLine) : subLine)
- + fixes.get(1);
+ rawLine.append(fixes.get(0)).append(consoleId != null
? consoleEngine().expandCommandLine(subLine) : subLine).append(fixes.get(1));
statement = true;
}
if (pipes.get(pipes.size() -
1).equals(pipeName.get(Pipe.FLIP))
@@ -802,31 +800,31 @@ public class SystemRegistryImpl implements SystemRegistry
{
done = true;
pipeSource = null;
if (variable != null) {
- rawLine = variable + " = " + rawLine;
+ rawLine.insert(0, variable + " = ");
}
}
if (last + 1 >= words.size() || file != null) {
done = true;
pipeSource = null;
if (pipeResult != null) {
- rawLine = pipeResult + " = " + rawLine;
+ rawLine.insert(0, pipeResult + " = ");
}
}
} else if (pipes.get(pipes.size() -
1).equals(pipeName.get(Pipe.FLIP)) || pipeStart) {
if (pipeStart && pipeResult != null) {
subLine = subLine.substring(subLine.indexOf("=") + 1);
}
- rawLine = flipArgument(command, subLine, pipes, arglist);
- rawLine = variable + "=" + rawLine;
+ rawLine = new StringBuilder(flipArgument(command, subLine,
pipes, arglist));
+ rawLine.insert(0, variable + "=");
} else {
- rawLine = flipArgument(command, subLine, pipes, arglist);
+ rawLine = new StringBuilder(flipArgument(command, subLine,
pipes, arglist));
}
if (done) {
//
// add composed command to return list
//
out.add(new CommandData(
- ap, statement, rawLine, variable, file, append,
pipes.get(pipes.size() - 1)));
+ ap, statement, rawLine.toString(), variable, file,
append, pipes.get(pipes.size() - 1)));
if (pipes.get(pipes.size() -
1).equals(pipeName.get(Pipe.AND))
|| pipes.get(pipes.size() -
1).equals(pipeName.get(Pipe.OR))) {
pipeSource = null;