Merge pull request #7818 from xuwei-k/toIntOption-toLongOption-toDoubleOption

[2.x] use `to{Int, Long, Double}Option` methods
This commit is contained in:
eugene yokota 2024-10-22 23:52:41 -04:00 committed by GitHub
commit fc18a7e4fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 8 deletions

View File

@ -57,8 +57,5 @@ object History {
def apply(lines: Seq[String], path: Option[File]): History =
new History(lines.toIndexedSeq, path)
def number(s: String): Option[Int] =
try {
Some(s.toInt)
} catch { case _: NumberFormatException => None }
def number(s: String): Option[Int] = s.toIntOption
}

View File

@ -18,11 +18,15 @@ private[sbt] object SizeParser {
private case object MegaBytes extends SizeUnit
private case object GigaBytes extends SizeUnit
private def parseDouble(s: String): Parser[Either[Double, Long]] =
try Parser.success(Left(java.lang.Double.valueOf(s)))
catch { case _: NumberFormatException => Parser.failure(s"Couldn't parse $s as double.") }
s.toDoubleOption match {
case Some(x) => Parser.success(Left(x))
case _ => Parser.failure(s"Couldn't parse $s as double.")
}
private def parseLong(s: String): Parser[Either[Double, Long]] =
try Parser.success(Right(java.lang.Long.valueOf(s)))
catch { case _: NumberFormatException => Parser.failure(s"Couldn't parse $s as double.") }
s.toLongOption match {
case Some(x) => Parser.success(Right(x))
case _ => Parser.failure(s"Couldn't parse $s as double.")
}
private val digit = charClass(_.isDigit, "digit")
private val numberParser: Parser[Either[Double, Long]] =
(digit.+ ~ ('.'.examples() ~> digit.+).?).flatMap {