serde-yaml icon indicating copy to clipboard operation
serde-yaml copied to clipboard

Allow parsing integers containing underscores

Open kootenpv opened this issue 2 years ago • 6 comments

The official yaml spec allows underscores in integers (for readability), would there be a way to achieve this in serde_yaml too?

kootenpv avatar May 01 '22 19:05 kootenpv

Could you point out where this is described in the official spec?

dtolnay avatar Jul 24 '22 23:07 dtolnay

https://yaml.org/type/int.html

kootenpv avatar Jul 25 '22 13:07 kootenpv

The spec is https://yaml.org/spec/1.2.2. Your link is an old draft proposal which has been abandoned since 17 years ago and has never made it into a spec. I don't think I would want to implement such drafts.

As far as I can tell, § 10.3.2 "Tag Resolution" is what defines what is a valid integer. It says:

  • [-+]? [0-9]+ (base 10)
  • 0o [0-7]+ (base 8)
  • 0x [0-9a-fA-F]+ (base 16)

where none of those mentions _.

dtolnay avatar Jul 25 '22 14:07 dtolnay

Oh my bad, I indeed thought I had read it somewhere but had not realized it was a draft.

All Python versions have implemented it which added extra confidence to it being official. I just checked, also js-yaml in nodejs reads integer numbers with underscore.

kootenpv avatar Jul 26 '22 01:07 kootenpv

Underscores are not part of yaml 1.2 !!int tag resolution.

My suggestion would be to not match them by default (if you are claiming to be a YAML 1.2 framework).

But also to support an option allowing them for compatibility (if you want to, your call).

ingydotnet avatar Sep 26 '22 17:09 ingydotnet

Would love to see this feature added. We tend to have large numbers in our config for configuring various limits, and visually parsing through a chain of 8 zeros without separators is rather error prone unless one is being careful. Although this isn't in the yaml spec, the feature is found in many yaml libraries.

If you're concerned about strict adherence to the spec, perhaps this could be implemented behind a feature flag?

jwilm avatar May 26 '23 16:05 jwilm