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

Culture-Dependent parsing of feet/inches #817

Open
pgrawehr opened this issue Jul 26, 2020 · 3 comments
Open

Culture-Dependent parsing of feet/inches #817

pgrawehr opened this issue Jul 26, 2020 · 3 comments
Labels
bug pinned Issues that should not be auto-closed due to inactivity.

Comments

@pgrawehr
Copy link
Contributor

(Creating an issue on this, as the task is now spread over several PRs)

Problem
As already described in PRs #794 #799 and #803, parsing Feet/inches combinations (ie. "7'23") fails for some cultures. This is because the apostrophe may be part of a number (typically as thousands separator) which makes this string ambigous.

To Reproduce
Steps to reproduce the behavior (just an example):
Run the unit tests on a computer that has the locale set to "de-Ch" (sometimes, error depends on the actual char set as number separator)

How should we proceed on this? #799 and #803 are closed, because they produce other side-effects that are equaly undesirable.

cc: @angularsen , @tmilnthorp

@pgrawehr pgrawehr added the bug label Jul 26, 2020
@angularsen
Copy link
Owner

Thanks for creating a mother of issues.

There are two issues I can think of:

  1. Length.ParseFeetInches() does not support number grouping separators, effectively failing ToString/Parse round-trip for any number larger than 1000 (Fix feet inches #803 (comment))
  2. Length.ParseFeetInches("1' 2\"", myCulture) fails if the culture uses apostrophe ' as the number grouping separator (reportedly with de-CH culture on @pgrawehr's PC, but a different symbol is used on some other PCs) (Fix TryParseFeetInches when current locale uses ' as number separator #794)

As a start, maybe we can list the special strings we expect to be able to parse?

  • Number grouping: Length.ParseFeetInches("1,500 ft 1 in", enUsCulture)
  • Number grouping separator same as unit abbreviation: Length.ParseFeetInches(1'1", cultureWithApostropheNumberGroupingSeparator)
  • Scientific notation: 1.2345e-3 ft (debatable, not emitted by ToString())
  • Fractions: 1/2 ft or 3 ½ ft (debatable, not emitted by ToString(), double.Parse() does not support parsing these)

Implementation proposal

Example problem: Length.ParseFeetInches(1'000'000'2", cultureWithApostrophe) should return 1 million feet and 2 inches.

Algorithm:

  • Split on feet abbreviations ["'", "ft"] etc to parts ["1", "000", "000", "2\""]
  • double.TryParse() on consecutive parts until it fails, 1 ✔, 1'000 ✔, 1'000'000 ✔, 1'000'000'2
  • Repeat similar for inches on remaining substring, splitting on ["\"", "in"] etc to get the remaining parts [2]
  • Return the sum of parsed feet + inches (or just one of them, does not require both)
  • Fail if not all parts are parsed.

This should work for everything that double.TryParse can eat, like scientific notation, but fractions would need extra work.

Note that we previously supported a similar summing method, but we had to tighten it so we did not allow invalid ordering like 1 in 2 ft or 1" 2' or repeating the same abbreviation 1" 2".

@pgrawehr
Copy link
Contributor Author

Two points:

First, IIRC default ToString() does not emit number separators, so probably the default Length.ToString() shouldn't, either. That would solve the basic round-trip problem. Round-tripping is only a problem if using the "N" format specifier (or "C", but that is pointless for our case).

Second: 1'000'000'2 will not fail to parse, 1'000'000'2" will (probably a simple typo in your comment above). The parser doesn't care where number separators are (there are also cultures where grouping is not with 3 digits, but with 2).

@stale
Copy link

stale bot commented Sep 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 29, 2020
@tmilnthorp tmilnthorp added pinned Issues that should not be auto-closed due to inactivity. and removed wontfix labels Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pinned Issues that should not be auto-closed due to inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants