Skip to content

Commit 3179ebf

Browse files
committed
perf(model-datastructure): HashUtil.isSha256 is now ~ 6 times faster
1 parent d32d31b commit 3179ebf

File tree

1 file changed

+12
-4
lines changed
  • model-datastructure/src/commonMain/kotlin/org/modelix/model/persistent

1 file changed

+12
-4
lines changed

model-datastructure/src/commonMain/kotlin/org/modelix/model/persistent/HashUtil.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ object HashUtil {
2626
}
2727

2828
fun isSha256(value: String?): Boolean {
29-
return if (value == null || value.length != 44) {
30-
false
31-
} else {
32-
value.matches(HASH_PATTERN)
29+
// this implementation is equivalent to matching against HASH_PATTERN, but ~ 6 times faster
30+
if (value == null) return false
31+
if (value.length != 44) return false
32+
if (value[5] != '*') return false
33+
for (i in 0..4) {
34+
val c = value[i]
35+
if (c !in 'a'..'z' && c !in 'A'..'Z' && c !in '0'..'9' && c != '-' && c != '_') return false
3336
}
37+
for (i in 6..43) {
38+
val c = value[i]
39+
if (c !in 'a'..'z' && c !in 'A'..'Z' && c !in '0'..'9' && c != '-' && c != '_') return false
40+
}
41+
return true
3442
}
3543

3644
fun extractSha256(input: String?): Iterable<String> {

0 commit comments

Comments
 (0)