Skip to content
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
19 changes: 17 additions & 2 deletions dbus-java-core/src/main/java/org/freedesktop/dbus/utils/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,33 @@ public static String upperCaseFirstChar(String _str) {
* @return camel case string or input if nothing todo. Returns null if input was null.
*/
public static String snakeToCamelCase(String _input) {
return toCamelCase("_", _input);
}

/**
* Converts a kabab-case-string to camel case string.
* <br>
* Eg. this-is-kebab-case &rarr; thisIsSnakeCase
* @param _input string
* @return camel case string or input if nothing todo. Returns null if input was null.
*/
public static String kebabToCamelCase(String _input) {
return toCamelCase("-", _input);
}

private static String toCamelCase(String _delimiter, String _input) {
if (isBlank(_input)) {
return _input;
}

Pattern compile = Pattern.compile("_[a-zA-Z]");
Pattern compile = Pattern.compile(Pattern.quote(_delimiter) + "[a-zA-Z0-9]");
Matcher matcher = compile.matcher(_input);

String result = _input;

while (matcher.find()) {
String match = matcher.group();
String replacement = match.replace("_", "");
String replacement = match.replace(_delimiter, "");
replacement = replacement.toUpperCase();

result = result.replaceFirst(match, replacement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ void testRequireMinimum(String _name, int _minVal, int _testVal, boolean _throws
assertEquals(_testVal, result);
}
}

@ParameterizedTest(name = "{index}: {0}")
@CsvSource({
"No Snake,This is no snake,This is no snake",
"Snake,This_is_a_snake,ThisIsASnake",
"Partial Snake,This_is_partial snake,ThisIsPartial snake",
"Snake with numbers,This_is_0_8_15_snake,ThisIs0815Snake",
})
void testSnakeToCamelCase(String _name, String _input, String _expected) {
assertEquals(_expected, Util.snakeToCamelCase(_input));
}
}
Loading
Loading