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

Use minimum width for zero-padded integer format specifications? #131

Open
azjps opened this issue Jan 6, 2021 · 2 comments
Open

Use minimum width for zero-padded integer format specifications? #131

azjps opened this issue Jan 6, 2021 · 2 comments
Labels

Comments

@azjps
Copy link

azjps commented Jan 6, 2021

When both the width and zero specifiers are present, this should give enough information that the string to be matched has to have a minimum width of width, instead of a maximum (or in addition to a maximum):

import parse
parse.extract_format("03d", None)
{'type': 'd',
 'width': '3',
 'zero': True,
 'align': None,
 'fill': None,
 'extra_types': None,
 'format': 'd'}

parse.parse("{:d}{:03d}", "1012")
# yields <Result (101, 2) {}>, but this can't be a result of the format string; (1, 12) is the only possible match

I realize that there are some tricky cases here such as with the possibility of negative signs, but it seems like the width could be calculated a bit more precisely here. Thoughts?

@r1chardj0n3s
Copy link
Owner

I'm marking this one as a wishlist item because I do not believe the current implementation of parse() can be made to enforce widths as precisely as you need. Because parse() is based off some increasingly-complicated regexes, rather than a proper syntax parser, it those regexes start getting really complicated when you want to think about enforcing that "the width of text captured for a number is N characters" especially when the number can be in a variety of formats.

@r1chardj0n3s
Copy link
Owner

Noting that this is the same issue as reported in #99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants