r1chardj0n3s / parse

Parse strings using a specification based on the Python format() syntax.
http://pypi.python.org/pypi/parse
MIT License
1.72k stars 101 forks source link

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

Open azjps opened 3 years ago

azjps commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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