-
Notifications
You must be signed in to change notification settings - Fork 48
/
Util.kt
62 lines (50 loc) · 1.54 KB
/
Util.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.kover.gradle.plugin.util
import java.io.File
import java.util.regex.Pattern
internal fun Iterable<String>.asPatterns(): List<Pattern> = map { Pattern.compile(it.wildcardsToRegex()) }
/**
* Executes `block` of code only if boolean value is `true`.
*/
internal inline fun <T : Any> Boolean.ifTrue(block: () -> T): T? {
return if (this) {
block()
} else {
null
}
}
/**
* Executes `block` of code only if boolean value is `false`.
*/
internal inline fun <T : Any> Boolean.ifFalse(block: () -> T): T? {
return if (!this) {
block()
} else {
null
}
}
/**
* Replaces characters `*` or `.` to `.*` and `.` regexp characters.
*/
internal fun String.wildcardsToRegex(): String {
// in most cases, the characters `*` or `.` will be present therefore, we increase the capacity in advance
val builder = StringBuilder(length * 2)
forEach { char ->
when (char) {
in regexMetacharactersSet -> builder.append('\\').append(char)
'*' -> builder.append('.').append("*")
'?' -> builder.append('.')
else -> builder.append(char)
}
}
return builder.toString()
}
internal val ONE_HUNDRED = 100.toBigDecimal()
private val regexMetacharactersSet = "<([{\\^-=$!|]})+.>".toSet()
internal fun File.subdirs(): List<File> {
return listFiles { it ->
it.exists() && it.isDirectory
}?.toList() ?: emptyList()
}