Skip to content

Commit

Permalink
Use JS-based regexes. It fixes year 2015 (which contains a NBSP, whic…
Browse files Browse the repository at this point in the history
…h wasn't matched by \s regex). Additionally, it should decrease the output JS size
  • Loading branch information
v6ak committed Sep 22, 2023
1 parent 01ba7dd commit 54255c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions client/src/main/scala/com/v6ak/scalajs/regex/JsPattern.scala
Original file line number Diff line number Diff line change
@@ -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)

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions client/src/main/scala/com/v6ak/zbdb/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ 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{

import Assertions._

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){
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 54255c9

Please sign in to comment.