This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 9042d573110f camel-util: fix StringHelper.splitOnCharacter for
multi-character separators (#29851)
9042d573110f is described below
commit 9042d573110f88aa4136dc6abb43a57db441f006
Author: Amal Saidov <[email protected]>
AuthorDate: Mon Nov 16 20:33:17 2026 +0700
camel-util: fix StringHelper.splitOnCharacter for multi-character
separators (#29851)
`StringHelper.splitOnCharacter(String value, String needle, int count)`
accepts a `String` separator but advanced past each match with `substring(p +
1)`, which only steps over a single character. When the separator is longer
than one character, the leftover separator characters leaked into the following
value.
For example:
returned `["foo", ":bar"]` instead of `["foo", "bar"]`.
The fix advances by `needle.length()` instead of a fixed `1`. All current
callers pass a single-character separator (`:`, `,`, `}`), so their behaviour
is unchanged because `needle.length()` is `1` in those cases.
Co-authored-by: Claude Opus 4.8 <[email protected]>
---
.../java/org/apache/camel/util/StringHelper.java | 14 ++++++++++-
.../org/apache/camel/util/StringHelperTest.java | 28 ++++++++++++++++++++++
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git
a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
index 2ae906042a22..861d9bedaecf 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
@@ -399,6 +399,18 @@ public final class StringHelper {
return value;
}
+ /**
+ * Splits the value on the given separator, up to the given number of
parts.
+ * <p>
+ * The separator may be more than one character long, such as
<code>::</code>. The returned array always has
+ * <code>count</code> elements, and any trailing elements are left as
<code>null</code> when the value contains
+ * fewer separators than requested.
+ *
+ * @param value the value to split
+ * @param needle the separator to split on (may be multiple characters)
+ * @param count the maximum number of parts to split into
+ * @return an array of count elements, with null for the parts that
are not present
+ */
public static String[] splitOnCharacter(String value, String needle, int
count) {
String[] rc = new String[count];
rc[0] = value;
@@ -409,7 +421,7 @@ public final class StringHelper {
return rc;
}
rc[i - 1] = v.substring(0, p);
- rc[i] = v.substring(p + 1);
+ rc[i] = v.substring(p + needle.length());
}
return rc;
}
diff --git
a/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
b/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
index 18809ec57ac5..9d2cc596b057 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/StringHelperTest.java
@@ -693,6 +693,34 @@ public class StringHelperTest {
assertEquals("99", list[99]);
}
+ @Test
+ public void testSplitOnCharacterMultiChar() {
+ String[] list = splitOnCharacter("foo::bar", "::", 2);
+ assertEquals(2, list.length);
+ assertEquals("foo", list[0]);
+ assertEquals("bar", list[1]);
+
+ list = splitOnCharacter("foo::bar::baz", "::", 3);
+ assertEquals(3, list.length);
+ assertEquals("foo", list[0]);
+ assertEquals("bar", list[1]);
+ assertEquals("baz", list[2]);
+
+ // the separator characters must not leak into the following value
+ list = splitOnCharacter("a=>b=>c", "=>", 3);
+ assertEquals(3, list.length);
+ assertEquals("a", list[0]);
+ assertEquals("b", list[1]);
+ assertEquals("c", list[2]);
+
+ // when count is larger than the number of separators, the trailing
elements are left as null
+ list = splitOnCharacter("foo::bar", "::", 3);
+ assertEquals(3, list.length);
+ assertEquals("foo", list[0]);
+ assertEquals("bar", list[1]);
+ assertNull(list[2]);
+ }
+
@Test
public void testLimitLength() {
String s = "ABCDEFGHIJKLMNOPQRST";