Skip to content

Commit

Permalink
Added StringUtils method to filter a string so that it becomes a vali…
Browse files Browse the repository at this point in the history
…d Java identifier
  • Loading branch information
gbevin committed Jul 5, 2024
1 parent d71c6f5 commit 86e335e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/rife/tools/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2481,6 +2481,46 @@ public static boolean filter(String name, Pattern[] included, Pattern[] excluded
return accepted;
}

/**
* 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
* @return the filtered string as a valid Java identifier; or
* <p>{@code null} if a valid Java identifier couldn't be generated
* @since 1.8.0
*/
public static String filterAsIdentifier(String value) {
if (null == value || value.isEmpty()) {
return null;
}

StringBuilder identifier = null;
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));
}
}
else if ((identifier.isEmpty() && Character.isJavaIdentifierStart(c)) ||
(!identifier.isEmpty() && Character.isJavaIdentifierPart(c))) {
identifier.append(c);
}
}

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

if (identifier.isEmpty()) {
return null;
}

return identifier.toString();
}

/**
* Ensure that the first character of the provided string is upper case.
*
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 @@ -637,6 +637,35 @@ void testFilterMultipleNonMatching() {
assertFalse(StringUtils.filter("test", new Pattern[]{Pattern.compile("x"), Pattern.compile("b")}, new Pattern[]{Pattern.compile("a"), Pattern.compile("t")}, false));
}

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

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

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

@Test void testFilterAsIdentifierWithNullValue() {
assertNull(StringUtils.filterAsIdentifier(null));
}

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

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

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

0 comments on commit 86e335e

Please sign in to comment.