Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't parse multiple degree symbols and be more flexible in whitespace when parsing temperature #190

Merged
merged 6 commits into from
Feb 8, 2017

Conversation

derekmorr
Copy link
Collaborator

This would fix #189. I adapted the regex slightly from the current pureconfig-squants version.

This disallows multiple degree symbols. Arguably this was a bug. If this PR is merged, we should add a note to the release notes that this is technically a breaking change (we're less liberal in what inputs we parse).

We should also look at the other string parsers to see if they're similarly affected.

@derekmorr
Copy link
Collaborator Author

A second commit removes duplicate temperature parsing code (and removed the extraneous degree matches, since the degree symbol isn't captured by the regex).

they should "be flexible in parsing strings with regard to degree symbol and whitespace" in {
Temperature("10.22 f").get should be (Fahrenheit(10.22))
Temperature("10.22 °f").get should be (Fahrenheit(10.22))
Temperature("10.22 °f").get should be (Fahrenheit(10.22))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is ok though I can imagine somebody arguing that we should only allow one format. How is this for other units? do we allow e.g. m / s?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already allow this syntax with the current Temperature parsing regex. I was just adding tests to make sure I didn't break anything while I was changing the regex.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent 👍

Copy link
Collaborator

@cquiroz cquiroz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

@@ -141,21 +141,15 @@ object Temperature extends Dimension[Temperature] with BaseDimension {
def apply[A](n: A, scale: TemperatureScale)(implicit num: Numeric[A]) = new Temperature(num.toDouble(n), scale)

def apply(s: String): Try[Temperature] = {
val regex = "([-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)[ °]*(f|F|c|C|k|K|r|R)".r
val regex = "([-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?)*[ ]*°? *(f|F|c|C|k|K|r|R)".r
Copy link

@leifwickland leifwickland Feb 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, I suggest one of the following:

  • enclose space in square brackets both times, i.e. [ ]*°?[ ]*
  • don't place space in square brackets, i.e. *°? *
  • take it to eleven and allow all the goofy varieties of horizontal whitespace including tab and unicode, i.e. [\\p{Zs}\\t]*°?[\\p{Zs}\\t]*

(See http://www.fileformat.info/info/unicode/category/Zs/list.htm for the meaning of \p{Zs})

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok this is addressed in the latest version.

@cquiroz
Copy link
Collaborator

cquiroz commented Feb 8, 2017

Is this ready to be merged?

@derekmorr
Copy link
Collaborator Author

Yes.

@cquiroz cquiroz merged commit e06ad93 into master Feb 8, 2017
@cquiroz cquiroz deleted the temperature-regex branch February 8, 2017 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Temperature Parsing Improvements
3 participants