Skip to content

Commit

Permalink
Added StringUtils filterAsIdentifier option to capitalize all segments
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Dec 26, 2024
1 parent 3b8ac2d commit 4f780fe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/main/java/rife/tools/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2492,25 +2492,54 @@ public static boolean filter(String name, Pattern[] included, Pattern[] excluded
* @since 1.8.0
*/
public static String filterAsIdentifier(String value) {
return filterAsIdentifier(value, false);
}

/**
* Filters the given string by removing any characters that are not valid in a Java identifier.
* <p>
* If a valid identifier can't be generated, {@code null} will be returned.
*
* @param value the string to be filtered
* @param capitalizeSegments {@code true} to capitalize each valid character segment; or
* {@code false} to leave the original characters unaffected
* @return the filtered string as a valid Java identifier; or
* <p>{@code null} if a valid Java identifier couldn't be generated
* @since 1.9.1
*/
public static String filterAsIdentifier(String value, boolean capitalizeSegments) {
if (null == value || value.isEmpty()) {
return null;
}

StringBuilder identifier = null;
int last_valid_position = -1;
for (int i = 0; i < value.length(); i++) {
var c = value.charAt(i);
if (identifier == null) {
if (!Character.isJavaIdentifierStart(c)) {
identifier = new StringBuilder(value.substring(0, i));
var segment = value.substring(0, i);
if (capitalizeSegments) {
segment = capitalize(segment);
}
identifier = new StringBuilder(segment);
last_valid_position = i - 1;
}
}
else if ((identifier.isEmpty() && Character.isJavaIdentifierStart(c)) ||
(!identifier.isEmpty() && Character.isJavaIdentifierPart(c))) {
if (capitalizeSegments && (last_valid_position == -1 || i - last_valid_position > 1)) {
c = Character.toUpperCase(c);
}
identifier.append(c);
last_valid_position = i;
}
}

if (identifier == null) {
if (capitalizeSegments) {
return capitalize(value);
}
return value;
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/rife/tools/TestStringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,35 @@ void testFilterMultipleNonMatching() {
assertNull(StringUtils.filterAsIdentifier(stringWithInvalidChars));
}

@Test void testFilterAsCapitalizedIdentifierWithValidString() {
var validIdentifier = "validIdentifier";
assertEquals("ValidIdentifier", StringUtils.filterAsIdentifier(validIdentifier, true));
}

@Test void testFilterAsCapitalizedIdentifierWithInvalidString() {
var invalidIdentifier = "1nvalidIdentifier";
assertEquals("NvalidIdentifier", StringUtils.filterAsIdentifier(invalidIdentifier, true));
}

@Test void testFilterAsCapitalizedIdentifierWithEmptyString() {
var emptyString = "";
assertNull(StringUtils.filterAsIdentifier(emptyString, true));
}

@Test void testFilterAsCapitalizedIdentifierWithNullValue() {
assertNull(StringUtils.filterAsIdentifier(null, true));
}

@Test void testFilterAsCapitalizedIdentifierWithInvalidCharacters() {
var stringWithInvalidChars = "some@string)with%invalid^chars";
assertEquals("SomeStringWithInvalidChars", StringUtils.filterAsIdentifier(stringWithInvalidChars, true));
}

@Test void testFilterAsCapitalizedIdentifierWithAllInvalidCharacters() {
var stringWithInvalidChars = "12987@)%^";
assertNull(StringUtils.filterAsIdentifier(stringWithInvalidChars, true));
}

@Test
void testCapitalize() {
assertNull(StringUtils.capitalize(null));
Expand Down

0 comments on commit 4f780fe

Please sign in to comment.