From 144fc8a4b3dd8915fd10461c9cdc6bda556d5645 Mon Sep 17 00:00:00 2001 From: "Frank S. Thomas" Date: Tue, 28 Jul 2020 07:56:44 +0200 Subject: [PATCH] Fix Version.preReleaseIndex (#1552) * Fix Version.preReleaseIndex ... and recognize hashes which are only 6 characters long. `preReleaseIndex` reported the wrong index for versions with components with a length greater than one. This is a follow-up #1549. * Fix off-by-one * Use foldMap --- .../org/scalasteward/core/data/Version.scala | 18 ++++++++++++++---- .../scalasteward/core/data/VersionTest.scala | 6 ++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala b/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala index 8136dffd05..e58130125c 100644 --- a/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala +++ b/modules/core/src/main/scala/org/scalasteward/core/data/Version.scala @@ -77,15 +77,17 @@ final case class Version(value: String) { .getOrElse(alnumComponents) private[this] val preReleaseIndex: Option[NonNegInt] = { - val preReleaseIdentIndex = components.indexWhere { + val preReleaseIdentIndex = NonNegInt.unapply(components.indexWhere { case a @ Version.Component.Alpha(_) => a.isPreReleaseIdent case _ => false - } - NonNegInt.unapply(preReleaseIdentIndex).orElse(hashIndex) + }) + preReleaseIdentIndex + .map(i => NonNegInt.unsafeFrom(components.take(i.value).foldMap(_.length))) + .orElse(hashIndex) } private[this] def hashIndex: Option[NonNegInt] = - """[-+]\p{XDigit}{7,}""".r.findFirstMatchIn(value).flatMap(m => NonNegInt.unapply(m.start)) + """[-+]\p{XDigit}{6,}""".r.findFirstMatchIn(value).flatMap(m => NonNegInt.unapply(m.start)) } object Version { @@ -110,6 +112,14 @@ object Version { case Component.Alpha(_) => true case _ => false } + + final def length: Int = + this match { + case Component.Numeric(value) => value.length + case Component.Alpha(value) => value.length + case Component.Separator(_) => 1 + case Component.Empty => 0 + } } object Component { final case class Numeric(value: String) extends Component { diff --git a/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala b/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala index 037b823655..861a66efe4 100644 --- a/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala +++ b/modules/core/src/test/scala/org/scalasteward/core/data/VersionTest.scala @@ -149,6 +149,7 @@ class VersionTest ("3.0-RC3", List("3.0-RC4", "3.0-RC5", "3.0", "3.2"), Some("3.2")), ("1.3.0-RC5", List("1.3.0", "1.3.1", "1.3.2"), Some("1.3.2")), ("2.5", List("2.6", "3.6"), Some("2.6")), + ("3.8", List("3.9.4"), Some("3.9.4")), ("1.3.0-RC5", List("1.3.0", "1.4.0"), Some("1.3.0")), ("1.1.2-1", List("2.0.0", "2.0.1-M3"), Some("2.0.0")), ("0.19.0-RC1", List("0.20.0-RC1", "0.20.0"), Some("0.20.0")), @@ -185,6 +186,11 @@ class VersionTest ("2.1.4-11-307f3d8", List("2.1.4-13-fb16e4e"), Some("2.1.4-13-fb16e4e")), ("2.1.4-13-fb16e4e", List("2.2.0", "2.2.0-0-fe5ed67"), Some("2.2.0")), ("2.2.0", List("2.2.0-0-fe5ed67", "2.2.0-4-4bd225e"), None), + ("0.116.0-alpha", List("0.118.1-alpha"), None), + ("0.8.0", List("0.8.0-1-d81662"), None), + ("0.6.3", List("289f9e3aa3f5014a5c64319da8e6ab993947ade2-0-289f9e"), None), + ("0.1-58d9629", List("0.8.0"), Some("0.8.0")), + ("0.9-a3bf234", List("0.14-9419610"), Some("0.14-9419610")), ("v2-rev374-1.23.0", List("v2-rev20190917-1.30.3"), Some("v2-rev20190917-1.30.3")) )