2019-10-08 17:17:51 +00:00
|
|
|
package com.insanusmokrassar.krontab.internal
|
2019-10-05 10:40:51 +00:00
|
|
|
|
2019-10-08 17:17:51 +00:00
|
|
|
import com.insanusmokrassar.krontab.utils.clamp
|
2019-10-05 10:40:51 +00:00
|
|
|
|
2019-10-10 10:43:52 +00:00
|
|
|
private fun createSimpleScheduler(from: String, dataRange: IntRange): Array<Byte>? {
|
2019-10-05 10:40:51 +00:00
|
|
|
val things = from.split(",")
|
|
|
|
|
|
|
|
val results = things.flatMap {
|
|
|
|
when {
|
2020-07-24 07:16:56 +00:00
|
|
|
it.contains("-") -> {
|
|
|
|
val splitted = it.split("-")
|
|
|
|
(splitted.first().toInt().clamp(dataRange) .. splitted[1].toInt().clamp(dataRange)).toList()
|
|
|
|
}
|
2019-10-05 10:40:51 +00:00
|
|
|
it.contains("/") -> {
|
|
|
|
val (start, step) = it.split("/")
|
|
|
|
val startNum = (if (start.isEmpty() || start == "*") {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
start.toInt()
|
|
|
|
}).clamp(dataRange)
|
|
|
|
val stepNum = step.toInt().clamp(dataRange)
|
|
|
|
(startNum .. dataRange.last step stepNum).map { it }
|
|
|
|
}
|
|
|
|
it == "*" -> return null
|
|
|
|
else -> listOf(it.toInt().clamp(dataRange))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results.map { it.toByte() }.toTypedArray()
|
|
|
|
}
|
|
|
|
|
2019-10-10 10:43:52 +00:00
|
|
|
internal fun parseMonths(from: String) = createSimpleScheduler(from, monthRange)
|
|
|
|
internal fun parseDaysOfMonth(from: String) = createSimpleScheduler(from, dayOfMonthRange)
|
|
|
|
internal fun parseHours(from: String) = createSimpleScheduler(from, hoursRange)
|
|
|
|
internal fun parseMinutes(from: String) = createSimpleScheduler(from, minutesRange)
|
|
|
|
internal fun parseSeconds(from: String) = createSimpleScheduler(from, secondsRange)
|
2019-10-05 10:40:51 +00:00
|
|
|
|
|
|
|
internal fun Array<Byte>.fillWith(
|
|
|
|
whereToPut: MutableList<CronDateTime>,
|
|
|
|
createFactory: (CronDateTime, Byte) -> CronDateTime
|
|
|
|
) {
|
|
|
|
val previousValues = whereToPut.toList()
|
|
|
|
|
|
|
|
whereToPut.clear()
|
|
|
|
|
|
|
|
previousValues.forEach { previousValue ->
|
|
|
|
forEach {
|
|
|
|
whereToPut.add(createFactory(previousValue, it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|