Skip to content

Commit

Permalink
fix: optimize cpu; precompile semver regex; containsBooleans
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Jul 23, 2024
1 parent 73e8368 commit f50cd2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 9 additions & 10 deletions evaluation-core/src/commonMain/kotlin/EvaluationEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface EvaluationEngine {
): Map<String, EvaluationVariant>
}

class EvaluationEngineImpl(private val log: Logger? = DefaultLogger()) : EvaluationEngine {
class EvaluationEngineImpl(private val log: Logger? = null) : EvaluationEngine {

data class EvaluationTarget(
val context: EvaluationContext,
Expand Down Expand Up @@ -226,11 +226,8 @@ class EvaluationEngineImpl(private val log: Logger? = DefaultLogger()) : Evaluat
}

private fun matchesIs(propValue: String, filterValues: Set<String>): Boolean {
if (containsBooleans(filterValues)) {
val lower: String = propValue.lowercase()
if (lower == "true" || lower == "false") {
return filterValues.any { it.lowercase() == lower }
}
if (isBoolean(propValue) && containsBooleans(filterValues)) {
return filterValues.any { propValue.equals(it, ignoreCase = true) }
}
return filterValues.contains(propValue)
}
Expand Down Expand Up @@ -306,12 +303,14 @@ class EvaluationEngineImpl(private val log: Logger? = DefaultLogger()) : Evaluat
return filterValues.contains("(none)")
}

private fun isBoolean(value: String): Boolean {
return value.equals("true", ignoreCase = true) ||
value.equals("false", ignoreCase = true)
}

private fun containsBooleans(filterValues: Set<String>): Boolean {
return filterValues.any { filterValue ->
when (filterValue.lowercase()) {
"true", "false" -> true
else -> false
}
isBoolean(filterValue)
}
}

Expand Down
2 changes: 2 additions & 0 deletions evaluation-core/src/commonMain/kotlin/SemanticVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ private const val PRERELEASE_REGEX = "(-(([-\\w]+\\.?)*))?"
// version pattern should be major.minor(.patchAndPreRelease) where .patchAndPreRelease is optional
private const val VERSION_PATTERN = "$MAJOR_MINOR_REGEX(\\.$PATCH_REGEX$PRERELEASE_REGEX)?$"

private val regex = Regex(VERSION_PATTERN)

/**
* Implementation of Semantic version specification as per the spec in
* https://semver.org/#semantic-versioning-specification-semver
Expand Down

0 comments on commit f50cd2d

Please sign in to comment.