parse
parse copied to clipboard
Numbers with a fixed field width/precision are not parsed properly
There are several issues with the parsing of numeric fields with a fixed width and/or precision. The common pattern for all of them is that the width/precision specification is applied inconsistently for different numeric representations (i.e. decimal, hexadecimal, etc):
>>> from parse import parse
>>> parse("#{:2.2x}{:2.2x}{:2.2x}", "#FFFFFF")
<Result (65535, 15, 15) {}>
>>> parse("#{:2.2x}{:2.2x}{:2.2x}", "#FFFF")
<Result (255, 15, 15) {}>
>>> parse("#{:2.2x}{:2.2x}{:2.2x}", "#FFF")
<Result (15, 15, 15) {}>
>>> parse("{:2.2d}{:2.2d}{:2.2d}", "9999")
<Result (99, 9, 9) {}>
>>> parse("{:2.2d}{:2.2d}{:2.2d}", "999")
<Result (9, 9, 9) {}>
As per the documentation: Width specifies a minimum size and precision specifies a maximum.
In all of the cases, we both have a width
and a precision
field. However, in the first case the parsing of the first number field consumes four hexadecimal digits despite the fact that the precision
should specify a maximum field length. What is more, we also have a width specifier which says that the numbers should be at least two digits long, and yet the latter two numbers are represented using only one digit.
The rest of the cases demonstrate the same issue with the width
specifier.
This is quite likely related to #99