Skip to content

Commit

Permalink
refactor(fossid): Extract a regular expression to a constant
Browse files Browse the repository at this point in the history
While at it, also simplify the expression by using `\w`.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher committed Jan 13, 2025
1 parent 2171243 commit 2196bf4
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class FossIdNamingProvider(
private val namingConventionVariables: Map<String, String>
) {
companion object {
val ALLOWED_CHARACTERS_REGEX = Regex("[\\w-]")

@JvmStatic
val FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")

Expand Down Expand Up @@ -78,8 +80,8 @@ class FossIdNamingProvider(
}

/**
* Replace characters in [branch] not matching `[a-zA-Z0-9-_]` with underscores and trim its length so that the
* total length of the generated scan code does not exceed [MAX_SCAN_CODE_LEN].
* Replace characters in [branch] not matching [ALLOWED_CHARACTERS_REGEX] with underscores and trim its length so
* that the total length of the generated scan code does not exceed [MAX_SCAN_CODE_LEN].
*/
private fun normalizeBranchName(
branch: String,
Expand All @@ -101,7 +103,7 @@ class FossIdNamingProvider(
}

val maxBranchNameLength = MAX_SCAN_CODE_LEN - noBranchScanCode.length
return branch.replace(Regex("[^a-zA-Z0-9-_]"), "_").take(maxBranchNameLength)
return branch.replace(ALLOWED_CHARACTERS_REGEX, "_").take(maxBranchNameLength)
}

/**
Expand Down

0 comments on commit 2196bf4

Please sign in to comment.