Skip to content

Parsing fails when mapped abbreviations contain numbers #264

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

Closed
bplubell opened this issue Jul 8, 2017 · 5 comments
Closed

Parsing fails when mapped abbreviations contain numbers #264

bplubell opened this issue Jul 8, 2017 · 5 comments

Comments

@bplubell
Copy link
Contributor

bplubell commented Jul 8, 2017

Setting up by mapping an abbreviation to an unit:
UnitSystem unitSystem = UnitSystem.GetCached("en-US"); unitSystem.MapUnitToAbbreviation(UnitsNet.Units.AccelerationUnit.MeterPerSecondSquared, "m2");

The then following works as expected, printing "MeterPerSecondSquared":
Console.WriteLine(unitSystem.Parse<UnitsNet.Units.AccelerationUnit>("m2").ToString());

But parsing from Acceleration , I expect "12 m/s²" but it throws exception

UnitsNetException: Error parsing string. The unit is not a recognized AccelerationUnit.

Console.WriteLine(Acceleration.Parse("12 m2").ToString());

It likely lies with the regular expression pattern in UnitParser.ParseUnit.

This is important for me because I am expecting to parse user-typed input. It is not reasonable for the user to enter the pretty input of m/s², but rather m/s^2 which has the same meaning.

I will fork and try to work it out.

@bplubell
Copy link
Contributor Author

bplubell commented Jul 8, 2017

So I am a bit confused now... I wrote a test for the above conditions and it is passing in xunit. However, it fails with symbols (like ^). Will get a sample repo up tomorrow to compare to test results.

@angularsen
Copy link
Owner

Huh, that is odd. Only thing I noticed was in your example you wrote Acceleration.Parse("12 m2") instead of m/s^2.

Also, UnitSystem is static and can cause problems if tests are run in parallel. I don't know how xunit is setup as I just recently switched to it.

Look forward to see what you find.

@bplubell
Copy link
Contributor Author

bplubell commented Jul 9, 2017

Yes, I removed the "^" while trying to troubleshoot and find the root cause. In my tests, I removed the UnitSystem.ClearCache(); and it seems to work as it does in this sample app.

Also, I found the issue is with this line in the regex parser:
@"(?<unit>[^\s\d,]+)", // capture Unit (non-whitespace) input.
It is not allowing any digits in the unit match. I am working on a solution now, but it will likely affect all generated unit classes (so extra attention during review would be appreciated).

@bplubell
Copy link
Contributor Author

Thanks for the follow up on the PR. This is a very useful project!

@angularsen
Copy link
Owner

Great! I'm glad you enjoy it.

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

No branches or pull requests

2 participants