From 54255c9d535888c646871dc956cf45367f7c2dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20=C5=A0est=C3=A1k?= Date: Fri, 22 Sep 2023 21:49:36 +0200 Subject: [PATCH] Use JS-based regexes. It fixes year 2015 (which contains a NBSP, which wasn't matched by \s regex). Additionally, it should decrease the output JS size --- .../com/v6ak/scalajs/regex/JsPattern.scala | 22 +++++++++++++++++++ .../com/v6ak/scalajs/time/TimeInterval.scala | 4 +++- .../src/main/scala/com/v6ak/zbdb/Parser.scala | 7 +++--- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 client/src/main/scala/com/v6ak/scalajs/regex/JsPattern.scala diff --git a/client/src/main/scala/com/v6ak/scalajs/regex/JsPattern.scala b/client/src/main/scala/com/v6ak/scalajs/regex/JsPattern.scala new file mode 100644 index 0000000..8888a37 --- /dev/null +++ b/client/src/main/scala/com/v6ak/scalajs/regex/JsPattern.scala @@ -0,0 +1,22 @@ +package com.v6ak.scalajs.regex + +import scala.scalajs.js +import scala.language.implicitConversions + + +class JsPattern(val regex: js.RegExp) extends AnyVal { + def unapplySeq(s: String): Option[Seq[String]] = regex.exec(s) match { + case null => None + case parts => Some(parts.toSeq.drop(1).asInstanceOf[Seq[String]]) + } +} + +object JsPattern { + + class JsPatternFactory(val s: String) extends AnyVal { + @inline def jsr: JsPattern = new JsPattern(new js.RegExp(s)) + } + + @inline implicit def wrapString(s: String): JsPatternFactory = new JsPatternFactory(s) + +} diff --git a/client/src/main/scala/com/v6ak/scalajs/time/TimeInterval.scala b/client/src/main/scala/com/v6ak/scalajs/time/TimeInterval.scala index 9400f50..ed9e91f 100644 --- a/client/src/main/scala/com/v6ak/scalajs/time/TimeInterval.scala +++ b/client/src/main/scala/com/v6ak/scalajs/time/TimeInterval.scala @@ -1,5 +1,7 @@ package com.v6ak.scalajs.time +import com.v6ak.scalajs.regex.JsPattern.wrapString + case class TimeInterval(totalMinutes: Int) extends AnyVal{ def hours = totalMinutes/60 def minutes = totalMinutes%60 @@ -8,7 +10,7 @@ case class TimeInterval(totalMinutes: Int) extends AnyVal{ } object TimeInterval{ - private val TimeIntervalRegex = """^([0-9]+):([0-9]+)$""".r + private val TimeIntervalRegex = """^([0-9]+):([0-9]+)$""".jsr def parse(s: String) = s match { case TimeIntervalRegex(hs, ms) => TimeInterval(hs.toInt*60 + ms.toInt) diff --git a/client/src/main/scala/com/v6ak/zbdb/Parser.scala b/client/src/main/scala/com/v6ak/zbdb/Parser.scala index e66f05e..3e7293b 100644 --- a/client/src/main/scala/com/v6ak/zbdb/Parser.scala +++ b/client/src/main/scala/com/v6ak/zbdb/Parser.scala @@ -11,6 +11,7 @@ import scala.collection.immutable import scala.scalajs.js import scala.util.Try import scala.util.matching.Regex +import com.v6ak.scalajs.regex.JsPattern._ object Parser{ @@ -18,14 +19,14 @@ object Parser{ private val StrictMode = true - private val TrackLengthRegex = """^([0-9]+(?:,[0-9]+)?)\s?(?:km)?$""".r + private val TrackLengthRegex = """^([0-9]+(?:,[0-9]+)?)\s?(?:km)?$""".jsr private def parseTrackLength(s: String) = s match { case TrackLengthRegex(tl) => BigDecimal(tl.replace(',', '.')) case other => sys.error(s"Unknown track length: $s") } - private val TimeRegexp = """^([0-9]+):([0-9]+)$""".r + private val TimeRegexp = """^([0-9]+):([0-9]+)$""".jsr private def strictCheck(f: => Unit): Unit = { if(StrictMode){ @@ -72,7 +73,7 @@ object Parser{ case e: Throwable => throw CellsParsingException(data, e) } - private val Empty = """^(?:x|X|)$""".r + private val Empty = """^(?:x|X|)$""".jsr private def parseTimeInfo(data: Seq[String], prevTimeOption: Option[Moment], maxHourDelta: Int): Option[PartTimeInfo] = guard(data){ data match {