Skip to content

Commit

Permalink
Merge pull request #341 from IvanGrund/master
Browse files Browse the repository at this point in the history
feat(StringUtil): implement format placeholders closer to _posix parameter expansion_ - i.e. implement optional colon
  • Loading branch information
qoomon authored Oct 23, 2024
2 parents 699d147 + 5683728 commit 9d6ea0d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ You can configure the version and properties adjustments for specific branches a

ℹ Final `version` will be slugified automatically, so no need to use `${….slug}` placeholders in `<version>` format.

ℹ define placeholder default value (placeholder is not defined) like this `${name:-DEFAULT_VALUE}`<br>
e.g `${env.BUILD_NUMBER:-0}` or `${env.BUILD_NUMBER:-local}`
ℹ define placeholder default value (placeholder is not defined '-' or empty ':-') like this `${name:-DEFAULT_VALUE}`<br>
e.g `${env.BUILD_NUMBER-0}` or `${env.BUILD_NUMBER:-local}`

ℹ define placeholder overwrite value (placeholder is defined) like this `${name:+OVERWRITE_VALUE}`<br>
e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
ℹ define placeholder overwrite value (placeholder is defined '+' and non-empty ':+' ) like this `${name:+OVERWRITE_VALUE}`<br>
e.g `${dirty:+SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`

###### Placeholders

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public final class StringUtil {

public static String substituteText(String text, Map<String, Supplier<String>> replacements) {
StringBuffer result = new StringBuffer();
Pattern placeholderPattern = Pattern.compile("\\$\\{(?<key>[^}:]+)(?::(?<modifier>[-+])(?<value>[^}]*))?}");
Pattern placeholderPattern = Pattern.compile("\\$\\{(?<key>[^}:]+)(?<modifier>:?[-+])?(?<value>[^}]*)?}");
Matcher placeholderMatcher = placeholderPattern.matcher(text);
while (placeholderMatcher.find()) {
String placeholderKey = placeholderMatcher.group("key");
Expand All @@ -26,6 +26,12 @@ public static String substituteText(String text, Map<String, Supplier<String>> r
if (placeholderModifier.equals("+") && replacement != null) {
replacement = placeholderMatcher.group("value");
}
if (placeholderModifier.equals(":-") && (replacement == null || replacement.isEmpty())) {
replacement = placeholderMatcher.group("value");
}
if (placeholderModifier.equals(":+") && replacement != null && !replacement.isEmpty()) {
replacement = placeholderMatcher.group("value");
}
}
if (replacement != null) {
// avoid group name replacement behaviour of replacement parameter value
Expand Down

0 comments on commit 9d6ea0d

Please sign in to comment.