Skip to content

std.format string positions affect all further format specifiers #10713

Closed

Description

@bhaible

The specification https://dlang.org/library/std/format.html says:
"By default, the arguments are processed in the provided order. With the position parameter it is possible to address arguments directly."

Since every format specifier can have a position, the normal interpretation of these sentences by a programmer is that when one specifies a position, it has an effect on the current format specifier.

What happens at runtime, however, is that the same position is used for all further format specifiers as well!

How to reproduce:

import std.stdio;
import std.format;

void main (string[] args)
{
  // As expected: Each format specifier consumes the next argument.
  writeln (format ("%*d %d %d %s", 5, 12, 13, 14, 23));
  writeln (format ("%*1$d %d %d %s", 5, 12, 13, 14, 23));
  writeln (format ("%.*d %d %d %s", 5, 12, 13, 14, 23));
  writeln (format ("%.*1$d %d %d %s", 5, 12, 13, 14, 23));

  // BUG: The three format specifiers after %3$d consume the same argument.
  writeln (format ("%3$d %d %d %s %7$d", 5, 12, 23, 24, 25, 26, 99));
  writeln (format ("%d %3$d %d %d %s %7$d", 5, 12, 23, 24, 25, 26, 99));
  writeln (format ("%d %d %3$d %d %d %s %7$d", 5, 12, 23, 24, 25, 26, 99));
  writeln (format ("%d %d %d %3$d %d %d %s %7$d", 5, 12, 23, 24, 25, 26, 99));
}

Compile and run this program (with dmd 2.110).

dmd foo.d && ./foo

Expected output:

   12 13 14 23
   12 13 14 23
00012 13 14 23
00012 13 14 23
23 24 25 26 99
5 23 24 25 26 99
5 12 23 24 25 26 99
5 12 23 23 24 25 26 99

Actual output:

   12 13 14 23
   12 13 14 23
00012 13 14 23
00012 13 14 23
23 23 23 23 99
5 23 23 23 23 99
5 12 23 23 23 23 99
5 12 23 23 23 23 23 99

Activity

  1. added 2 commits that reference this issue on Mar 25, 2025
    e16745c
    5e8bb13
  2. added a commit that references this issue on Mar 25, 2025
    c64232d
  3. bhaible commented on Mar 25, 2025

    @bhaible
    Author

    I confirm that the patch fixes the issue. Tested by applying the patch to GCC and rebuilding GCC.

  4. added a commit that references this issue on Jun 5, 2025
    ebf725c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions