Skip to content

Commit 8e59be5

Browse files
authored
Improve performance of Version.startsWithDate (#2014)
1 parent 3e21692 commit 8e59be5

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

modules/benchmark/src/main/scala/org/scalasteward/benchmark/VersionBenchmark.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ class VersionBenchmark {
2929
Component.parse("1.1.2-1")
3030
Component.parse("8.0.192-R14")
3131
Component.parse("1.2.0+9-4a769501")
32+
Component.parse("1.0.0-20201119-091040")
3233
}
3334
}

modules/core/src/main/scala/org/scalasteward/core/data/Version.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ object Version {
113113
}
114114

115115
private def startsWithDate(s: String): Boolean =
116-
"""(\d{4})(\d{2})(\d{2})""".r.findPrefixMatchOf(s).exists { m =>
117-
val year = m.group(1).toInt
118-
val month = m.group(2).toInt
119-
val day = m.group(3).toInt
116+
s.length >= 8 && s.substring(0, 8).forall(_.isDigit) && {
117+
val year = s.substring(0, 4).toInt
118+
val month = s.substring(4, 6).toInt
119+
val day = s.substring(6, 8).toInt
120120
(year >= 1900 && year <= 2100) &&
121121
(month >= 1 && month <= 12) &&
122122
(day >= 1 && day <= 31)

0 commit comments

Comments
 (0)