Skip to content

strformat: the fill character is no longer uppercased by the uppercase float types - #26894

Merged
Araq merged 1 commit into
nim-lang:develfrom
farisbasha:fix/strformat-uppercase-fill
Dec 5, 2026
Merged

Araq merged 1 commit into
nim-lang:develfrom
farisbasha:fix/strformat-uppercase-fill

Conversation

@farisbasha

@farisbasha farisbasha commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

strformat documents E as "Exponent notation. Same as e except it converts
the number to uppercase", and F likewise for f. formatFloat instead applies
toUpperAscii to the aligned string, so the fill character is uppercased too
(G goes through the same code path):

import std/strformat

echo &"{2.5:x>10.1F}"   # XXXXXXX2.5  -- expected xxxxxxx2.5
echo &"{2.5:x>10.1f}"   # xxxxxxx2.5  -- the lowercase type is fine
echo &"{1.5:a>16E}"     # AAAA1.500000E+00  -- expected aaaa1.500000E+00
echo &"{1.5:a^16G}"     # AAAAAA1.5AAAAAAA  -- expected aaaaaa1.5aaaaaaa
echo &"{Inf:q>8F}"      # QQQQQINF  -- expected qqqqqINF
echo &"{255:z>8X}"      # zzzzzzFF  -- integers are already correct

Integers go through formatInt, which never touches the fill, so the two number
kinds disagree today. Python, whose f-strings this module's syntax is modelled on,
keeps the fill as written (format(2.5, 'x>10.1F') is 'xxxxxxx2.5').

The fix uppercases f (the formatted number) before aligning it, instead of
uppercasing the aligned result. The output is unchanged whenever the fill is not
a lowercase ASCII letter, so zero padding and space padding are unaffected.

Tests

A new block in tests/stdlib/tstrformat.nim; that file runs main() both in the
VM (static: main()) and at runtime, and the bug was present in both.

Run on Linux x86_64 with the compiler that ./build_all.sh produces from this
checkout. koch boot had not finished on that machine, so these runs used
bin/nim from csources_v4 (Nim 2.4.0) together with this checkout's lib/:

# before the fix: strformat.nim reverted to devel, new test block kept
$ ./bin/nim c -r --hints:off --skipUserCfg --skipParentCfg --mm:refc \
      --path:tests/stdlib tests/stdlib/tstrformat.nim
assertions.nim(38, 5) Error: unhandled exception: tstrformat.nim(600, 5)
  `&"{2.5:x>10.1F}" == "xxxxxxx2.5"`  [AssertionDefect]
exit status 1          # raised in the VM, at `static: main()`

# after the fix, both entries of the test file's matrix
$ ./bin/nim c -r --hints:off --skipUserCfg --skipParentCfg --mm:refc \
      --path:tests/stdlib tests/stdlib/tstrformat.nim
exit status 0
$ ./bin/nim c -r --hints:off --skipUserCfg --skipParentCfg --mm:orc \
      --path:tests/stdlib tests/stdlib/tstrformat.nim
exit status 0

git diff --check is clean. A regex scan of {...} format literals in lib/ and
tests/ turned up no other assertion that formats a float with an uppercase type
and an explicit fill character.

#26849 touches the same proc (the zero-padding sign check a few lines above) but
the two changes are independent and the hunks do not overlap.

This PR was prepared with AI assistance (Claude).

…t types

`E`, `F` and `G` are documented as converting the number to uppercase, but formatFloat applied toUpperAscii to the aligned string, so the fill character was uppercased as well: `&"{2.5:x>10.1F}"` gave `XXXXXXX2.5` instead of `xxxxxxx2.5`. Integers are not affected because formatInt never touches the fill.

Uppercase the formatted number before aligning it.
@Araq
Araq merged commit d2dd58c into nim-lang:devel Dec 5, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants