Commit dcf6b827 authored by Stefan Behnel's avatar Stefan Behnel

Fix optimisation of aligned '%' formats ('%05s', '%-5s') into the correct...

Fix optimisation of aligned '%' formats ('%05s', '%-5s') into the correct f-string alignment format.
Closes https://github.com/cython/cython/issues/3476
parent 75f544fc
......@@ -4293,7 +4293,7 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
_parse_string_format_regex = (
u'(%(?:' # %...
u'(?:[0-9]+|[ ])?' # width (optional) or space prefix fill character (optional)
u'(?:[-0-9]+|[ ])?' # width (optional) or space prefix fill character (optional)
u'(?:[.][0-9]+)?' # precision (optional)
u')?.)' # format type (or something different for unsupported formats)
)
......@@ -4334,9 +4334,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
elif format_type in u'ars':
format_spec = format_spec[:-1]
conversion_char = format_type
if format_spec.startswith('0'):
format_spec = '>' + format_spec[1:] # right-alignment '%05s' spells '{:>5}'
elif format_type == u'd':
# '%d' formatting supports float, but '{obj:d}' does not => convert to int first.
conversion_char = 'd'
if format_spec.startswith('-'):
format_spec = '<' + format_spec[1:] # left-alignment '%-5s' spells '{:<5}'
substrings.append(ExprNodes.FormattedValueNode(
arg.pos, value=arg,
conversion_char=conversion_char,
......
......@@ -478,29 +478,29 @@ def generated_fstring(int i, float f, unicode u not None, o):
"""
>>> i, f, u, o = 11, 1.3125, u'xyz', [1]
>>> print(((
... u"(i) %s-%.3s-%r-%.3r-%d-%3d-%o-%04o-%x-%4x-%X-%03X-%.1f-%04.2f %% "
... u"(u) %s-%.2s-%r-%.7r %% "
... u"(i) %s-%.3s-%r-%.3r-%d-%3d-%-3d-%o-%04o-%x-%4x-%X-%03X-%.1f-%04.2f %% "
... u"(u) %s-%.2s-%r-%.7r-%05s-%-5s %% "
... u"(o) %s-%.2s-%r-%.2r %% "
... u"(f) %.2f-%d"
... ) % (
... i, i, i, i, i, i, i, i, i, i, i, i, i, i,
... u, u, u, u,
... i, i, i, i, i, i, i, i, i, i, i, i, i, i, i,
... u, u, u, u, u, u,
... o, o, o, o,
... f, f,
... )).replace("-u'xyz'", "-'xyz'"))
(i) 11-11-11-11-11- 11-13-0013-b- b-B-00B-11.0-11.00 % (u) xyz-xy-'xyz'-'xyz' % (o) [1]-[1-[1]-[1 % (f) 1.31-1
(i) 11-11-11-11-11- 11-11 -13-0013-b- b-B-00B-11.0-11.00 % (u) xyz-xy-'xyz'-'xyz'- xyz-xyz % (o) [1]-[1-[1]-[1 % (f) 1.31-1
>>> print(generated_fstring(i, f, u, o).replace("-u'xyz'", "-'xyz'"))
(i) 11-11-11-11-11- 11-13-0013-b- b-B-00B-11.0-11.00 % (u) xyz-xy-'xyz'-'xyz' % (o) [1]-[1-[1]-[1 % (f) 1.31-1
(i) 11-11-11-11-11- 11-11 -13-0013-b- b-B-00B-11.0-11.00 % (u) xyz-xy-'xyz'-'xyz'- xyz-xyz % (o) [1]-[1-[1]-[1 % (f) 1.31-1
"""
return (
u"(i) %s-%.3s-%r-%.3r-%d-%3d-%o-%04o-%x-%4x-%X-%03X-%.1f-%04.2f %% "
u"(u) %s-%.2s-%r-%.7r %% "
u"(i) %s-%.3s-%r-%.3r-%d-%3d-%-3d-%o-%04o-%x-%4x-%X-%03X-%.1f-%04.2f %% "
u"(u) %s-%.2s-%r-%.7r-%05s-%-5s %% "
u"(o) %s-%.2s-%r-%.2r %% "
u"(f) %.2f-%d"
) % (
i, i, i, i, i, i, i, i, i, i, i, i, i, i,
u, u, u, u,
i, i, i, i, i, i, i, i, i, i, i, i, i, i, i,
u, u, u, u, u, u,
o, o, o, o,
f, f,
)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment