Skip to content

Commit c2f3956

Browse files
authored
Merge pull request #305 from hypfvieh/issue-302-303-304
Issue 302, Issue 303, Issue 304:
2 parents 10fd67d + 4017b11 commit c2f3956

9 files changed

Lines changed: 885 additions & 56 deletions

File tree

dbus-java-core/src/main/java/org/freedesktop/dbus/utils/Util.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,33 @@ public static String upperCaseFirstChar(String _str) {
163163
* @return camel case string or input if nothing todo. Returns null if input was null.
164164
*/
165165
public static String snakeToCamelCase(String _input) {
166+
return toCamelCase("_", _input);
167+
}
168+
169+
/**
170+
* Converts a kabab-case-string to camel case string.
171+
* <br>
172+
* Eg. this-is-kebab-case &rarr; thisIsSnakeCase
173+
* @param _input string
174+
* @return camel case string or input if nothing todo. Returns null if input was null.
175+
*/
176+
public static String kebabToCamelCase(String _input) {
177+
return toCamelCase("-", _input);
178+
}
179+
180+
private static String toCamelCase(String _delimiter, String _input) {
166181
if (isBlank(_input)) {
167182
return _input;
168183
}
169184

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

173188
String result = _input;
174189

175190
while (matcher.find()) {
176191
String match = matcher.group();
177-
String replacement = match.replace("_", "");
192+
String replacement = match.replace(_delimiter, "");
178193
replacement = replacement.toUpperCase();
179194

180195
result = result.replaceFirst(match, replacement);

dbus-java-tests/src/test/java/org/freedesktop/dbus/utils/UtilTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ void testRequireMinimum(String _name, int _minVal, int _testVal, boolean _throws
2121
assertEquals(_testVal, result);
2222
}
2323
}
24+
25+
@ParameterizedTest(name = "{index}: {0}")
26+
@CsvSource({
27+
"No Snake,This is no snake,This is no snake",
28+
"Snake,This_is_a_snake,ThisIsASnake",
29+
"Partial Snake,This_is_partial snake,ThisIsPartial snake",
30+
"Snake with numbers,This_is_0_8_15_snake,ThisIs0815Snake",
31+
})
32+
void testSnakeToCamelCase(String _name, String _input, String _expected) {
33+
assertEquals(_expected, Util.snakeToCamelCase(_input));
34+
}
2435
}

0 commit comments

Comments
 (0)