Skip to content

Commit d89e000

Browse files
authored
Merge pull request #2012 from scala-steward-org/topic/cats-parse
Implement Version.Component.parse with cats-parse
2 parents 6b50ebb + 30bc412 commit d89e000

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ lazy val core = myCrossProject("core")
8282
Dependencies.caseApp,
8383
Dependencies.catsCore,
8484
Dependencies.catsEffect,
85+
Dependencies.catsParse,
8586
Dependencies.circeConfig,
8687
Dependencies.circeGeneric,
8788
Dependencies.circeGenericExtras,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
package org.scalasteward.benchmark
1818

1919
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Mode, OutputTimeUnit}
20-
import org.scalasteward.core.data.Version
20+
import org.scalasteward.core.data.Version.Component
2121

2222
import java.util.concurrent.TimeUnit
2323

2424
@BenchmarkMode(Array(Mode.AverageTime))
2525
class VersionBenchmark {
2626
@Benchmark
2727
@OutputTimeUnit(TimeUnit.MICROSECONDS)
28-
def constructor: Any = {
29-
Version("1.1.2-1")
30-
Version("8.0.192-R14")
31-
Version("1.2.0+9-4a769501")
28+
def parseBench: Any = {
29+
Component.parse("1.1.2-1")
30+
Component.parse("8.0.192-R14")
31+
Component.parse("1.2.0+9-4a769501")
3232
}
3333
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.scalasteward.core.data
1818

1919
import cats.Order
2020
import cats.implicits._
21+
import cats.parse.{Numbers, Parser, Rfc5234}
2122
import io.circe.Codec
2223
import io.circe.generic.extras.semiauto.deriveUnwrappedCodec
2324

@@ -149,24 +150,23 @@ object Version {
149150
final case class Separator(c: Char) extends Component
150151
case object Empty extends Component
151152

152-
private val numeric = """^(\d+)(.*)$""".r
153-
private val separator = """^([.\-_+])(.*)$""".r
154-
private val alpha = """^([^.\-_+\d]+)(.*)$""".r
155-
private val hash = """^([-+])(g?\p{XDigit}{6,})(.*)$""".r
153+
private val componentsParser = {
154+
val digits = ('0' to '9').toSet
155+
val separators = Set('.', '-', '_', '+')
156+
157+
val numeric = Numbers.digits.map(s => List(Numeric(s)))
158+
val alpha = Parser.charsWhile(c => !digits(c) && !separators(c)).map(s => List(Alpha(s)))
159+
val separator = Parser.charIn(separators).map(c => List(Separator(c)))
160+
val hash = (Parser.charIn('-', '+') ~
161+
Parser.char('g').string.? ~
162+
Rfc5234.hexdig.rep(6).string.filterNot(startsWithDate)).backtrack
163+
.map { case ((s, g), h) => List(Separator(s), Hash(g.getOrElse("") + h)) }
164+
165+
(numeric | alpha | hash | separator).rep0.map(_.flatten)
166+
}
156167

157168
def parse(str: String): List[Component] =
158-
str match {
159-
case "" => List.empty
160-
case numeric(value, rest) =>
161-
Numeric(value) +: parse(rest)
162-
case alpha(value, rest) =>
163-
Alpha(value) +: parse(rest)
164-
case hash(sep, value, rest) if !startsWithDate(value) =>
165-
Separator(sep.head) +: Hash(value) +: parse(rest)
166-
case separator(value, rest) =>
167-
Separator(value.head) +: parse(rest)
168-
case _ => List.empty
169-
}
169+
componentsParser.parseAll(str).getOrElse(List.empty)
170170

171171
def render(components: List[Component]): String =
172172
components.map {

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ object Dependencies {
1111
val catsEffect = "org.typelevel" %% "cats-effect" % "2.3.3"
1212
val catsCore = "org.typelevel" %% "cats-core" % "2.4.2"
1313
val catsLaws = "org.typelevel" %% "cats-laws" % catsCore.revision
14+
val catsParse = "org.typelevel" %% "cats-parse" % "0.3.1"
1415
val circeConfig = "io.circe" %% "circe-config" % "0.8.0"
1516
val circeGeneric = "io.circe" %% "circe-generic" % "0.13.0"
1617
val circeGenericExtras = "io.circe" %% "circe-generic-extras" % "0.13.0"

0 commit comments

Comments
 (0)