Skip to content

Commit

Permalink
fix(workflow-server): CharacterReplacementStrategy Empty Result Handling
Browse files Browse the repository at this point in the history
While artificial if used "as intended", the replacement strategy should
be able not to provide unexpected results for unexpected successful
empty matches, like for `.*`.
  • Loading branch information
mmichaelis committed Jan 23, 2025
1 parent 98451e3 commit 2718440
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ public enum CharacterReplacementStrategy {
/**
* Replace with an underscore.
*/
UNDERSCORE(mr -> "_"),
UNDERSCORE(mr -> replaceIfNotEmpty(mr, "_")),
/**
* Replace with a question mark.
*/
QUESTION_MARK(mr -> "?"),
QUESTION_MARK(mr -> replaceIfNotEmpty(mr, "?")),
/**
* Replace with a Unicode code point.
*/
UNICODE_CODE_POINT(mr -> {
int codePoint = mr.group().codePointAt(0);
String group = mr.group();
if (mr.group().isEmpty()) {
return "";
}
int codePoint = group.codePointAt(0);
return String.format("U+%04X", codePoint);
}),
;
Expand Down Expand Up @@ -103,6 +107,11 @@ public static Optional<CharacterReplacementStrategy> fromString(@Nullable String
return Optional.empty();
}

@NonNull
private static String replaceIfNotEmpty(@NonNull MatchResult mr, @NonNull String replacement) {
return mr.group().isEmpty() ? "" : replacement;
}

/**
* Used to eventually support camel-case, snake-case, and kebab-case.
*
Expand Down

0 comments on commit 2718440

Please sign in to comment.