typelevel/squants

String input to constructor should be trimmed

jehrlich opened this issue · 0 comments

Quantity constructors taking String input are not tolerant of extra whitespace at beginning or end, such that Volume("3 ml") parses correctly but Volume("3 ml ") fails. It seems this could be fixed by changing parseString(s) to parseString(s.trim) in Dimension.parse(value: Any).

I haven't tested further but probably all the parseTuple((v, u)) should be parseTuple((v, u.trim)) (or maybe the regex takes care of that).

/**
  * Tries to map a string or tuple value to Quantity of this Dimension
  * @param value the source string (ie, "10 kW") or tuple (ie, (10, "kW"))
  * @return Try[A]
  */
 protected def parse(value: Any): Try[A] = value match {
   case s: String              ⇒ parseString(s)
   case (v: Byte, u: String)   ⇒ parseTuple((v, u))
   case (v: Short, u: String)  ⇒ parseTuple((v, u))
   case (v: Int, u: String)    ⇒ parseTuple((v, u))
   case (v: Long, u: String)   ⇒ parseTuple((v, u))
   case (v: Float, u: String)  ⇒ parseTuple((v, u))
   case (v: Double, u: String) ⇒ parseTuple((v, u))
   case _ ⇒ Failure(QuantityParseException(s"Unable to parse $name", value.toString))
 }