This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit a80ff680e445e2b9af0b365b1ee418504c6285c5 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Feb 27 08:57:59 2021 +0100 CAMEL-16250 - support more edge cases of dashToCamelCase conversion --- .../java/org/apache/camel/util/StringHelper.java | 25 ++++++++++++---------- .../org/apache/camel/util/StringHelperTest.java | 18 ++++++++-------- 2 files changed, 23 insertions(+), 20 deletions(-) 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 686af48..fa0c827 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 @@ -46,8 +46,7 @@ public final class StringHelper { * @throws NullPointerException if <code>s</code> is <code>null</code>. */ public static String sanitize(String s) { - return s - .replace(':', '-') + return s.replace(':', '-') .replace('_', '-') .replace('.', '-') .replace('/', '-') @@ -469,15 +468,19 @@ public final class StringHelper { return text; } - StringBuilder sb = new StringBuilder(); - String[] splittedString = text.split("\\-"); - for (int i = 0; i < splittedString.length; i++) { - String currentToken = splittedString[i]; - if (i == 0) { - sb.append(currentToken); - } else if (!currentToken.isEmpty()) { - sb.append(Character.toUpperCase(currentToken.charAt(0))); - sb.append(currentToken.substring(1)); + // there is at least 1 dash so the capacity can be shorter + StringBuilder sb = new StringBuilder(length - 1); + boolean upper = false; + for (int i = 0; i < length; i++) { + char c = text.charAt(i); + if (c == '-') { + upper = true; + } else { + if (upper) { + c = Character.toUpperCase(c); + } + sb.append(c); + upper = false; } } return sb.toString(); 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 ec122ec..3c89c24 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 @@ -56,45 +56,45 @@ public class StringHelperTest { assertEquals("available-phone-number-country", camelCaseToDash("availablePhoneNumberCountry")); assertEquals("available-phone-number-country", camelCaseToDash("AvailablePhoneNumberCountry")); } - + @Nested class DashToCamelCase { - + @Test void testDashToCamelCaseWithNull() throws Exception { assertThat(dashToCamelCase(null)).isNull(); } - + @Test void testDashToCamelCaseWithEmptyValue() throws Exception { assertThat(dashToCamelCase("")).isEmpty(); } - + @Test void testDashToCamelCaseWithNoDash() throws Exception { assertThat(dashToCamelCase("a")).isEqualTo("a"); } - + @Test void testDashToCamelCaseWithOneDash() throws Exception { assertThat(dashToCamelCase("a-b")).isEqualTo("aB"); } - + @Test void testDashToCamelCaseWithSeveralDashes() throws Exception { assertThat(dashToCamelCase("a-bb-cc-dd")).isEqualTo("aBbCcDd"); } - + @Test void testDashToCamelCaseWithEndDash() throws Exception { assertThat(dashToCamelCase("a-")).isEqualTo("a"); } - + @Test void testDashToCamelCaseWithEndDashes() throws Exception { assertThat(dashToCamelCase("a----")).isEqualTo("a"); } - + @Test void testDashToCamelCaseWithSeceralDashesGrouped() throws Exception { assertThat(dashToCamelCase("a--b")).isEqualTo("aB");