parse icon indicating copy to clipboard operation
parse copied to clipboard

Use minimum width for zero-padded integer format specifications?

Open azjps opened this issue 3 years ago • 2 comments

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?

azjps avatar Jan 06 '21 06:01 azjps

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 avatar Jan 14 '21 01:01 r1chardj0n3s

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

r1chardj0n3s avatar Jan 14 '21 01:01 r1chardj0n3s